blob: 646203c8c0c4771ffbb9303f63d439d1cb95eab1 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Bjorn Bringert3e244cf2010-02-10 14:17:35 +000019import com.android.common.Search;
20
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Activity;
22import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.Dialog;
24import android.app.SearchManager;
25import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080028import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080030import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070034import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.pm.ActivityInfo;
37import android.content.pm.PackageManager;
38import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070040import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080041import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080043import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080044import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080046import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080048import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.os.Parcelable;
Joe Onoratobe386092009-11-17 17:32:16 -080050import android.os.SystemProperties;
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;
Joe Onorato7c312c12009-08-13 21:36:53 -070056import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080058import android.view.HapticFeedbackConstants;
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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.view.View.OnLongClickListener;
Joe Onorato8a9625e2010-01-28 15:55:35 -080066import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.widget.TextView;
70import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080071import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080072import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080073import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070074import android.appwidget.AppWidgetManager;
75import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040078import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070079import java.io.DataOutputStream;
80import java.io.FileNotFoundException;
81import java.io.IOException;
82import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083
84/**
85 * Default launcher application.
86 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040087public final class Launcher extends Activity
Romain Guya6abce82009-11-10 02:54:41 -080088 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080089 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090 static final boolean LOGD = false;
91
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 static final boolean PROFILE_STARTUP = false;
93 static final boolean PROFILE_ROTATE = false;
94 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070095
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 private static final int WALLPAPER_SCREENS_SPAN = 2;
97
98 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -080099 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
100
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 private static final int MENU_ADD = Menu.FIRST + 1;
102 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
103 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
104 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700105 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106
107 private static final int REQUEST_CREATE_SHORTCUT = 1;
108 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700109 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 private static final int REQUEST_PICK_APPLICATION = 6;
111 private static final int REQUEST_PICK_SHORTCUT = 7;
112 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700113 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700114 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
116 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
117
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800118 static final int SCREEN_COUNT = 5;
119 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700121 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Joe Onorato7c312c12009-08-13 21:36:53 -0700123 static final int DIALOG_CREATE_SHORTCUT = 1;
124 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Romain Guy98d01652009-06-30 16:21:04 -0700126 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
128 // Type: int
129 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
130 // Type: boolean
131 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
132 // Type: long
133 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
146 // Type: int
147 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
148 // Type: int[]
149 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
150 // Type: boolean
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
152 // Type: long
153 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
154
Joe Onorato9c1289c2009-08-17 11:03:03 -0400155 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800158 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800160 private final BroadcastReceiver mCloseSystemDialogsReceiver
161 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800162 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 private LayoutInflater mInflater;
165
Joe Onorato00acb122009-08-04 16:04:30 -0400166 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700168
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700169 private AppWidgetManager mAppWidgetManager;
170 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private CellLayout.CellInfo mAddItemCellInfo;
173 private CellLayout.CellInfo mMenuAddInfo;
174 private final int[] mCellCoordinates = new int[2];
175 private FolderInfo mFolderInfo;
176
Joe Onorato7c312c12009-08-13 21:36:53 -0700177 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700178 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700179 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 private Bundle mSavedState;
182
183 private SpannableStringBuilder mDefaultKeySsb = null;
184
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 private boolean mWorkspaceLoading = true;
186
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800187 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private boolean mRestoring;
189 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
191 private Bundle mSavedInstanceState;
192
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 private LauncherModel mModel;
194
Romain Guy84f296c2009-11-04 15:00:44 -0800195 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
196 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700197
Romain Guy1fbc1c82009-11-09 20:43:08 -0800198 private ImageView mPreviousView;
199 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500200
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 @Override
202 protected void onCreate(Bundle savedInstanceState) {
203 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700204
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400206 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700208
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700209 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700210 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
211 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700212
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 if (PROFILE_STARTUP) {
214 android.os.Debug.startMethodTracing("/sdcard/launcher");
215 }
216
217 checkForLocaleChange();
218 setWallpaperDimension();
219
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 setContentView(R.layout.launcher);
221 setupViews();
222
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800223 registerContentObservers();
224
Joe Onorato7c312c12009-08-13 21:36:53 -0700225 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700226
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 mSavedState = savedInstanceState;
228 restoreState(mSavedState);
229
230 if (PROFILE_STARTUP) {
231 android.os.Debug.stopMethodTracing();
232 }
233
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700234 // We have a new AllAppsView, we need to re-bind everything, and it could have
235 // changed in our absence.
236 mModel.setAllAppsDirty();
237 mModel.setWorkspaceDirty();
238
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400240 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 }
242
243 // For handling default keys
244 mDefaultKeySsb = new SpannableStringBuilder();
245 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800246
247 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
248 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 }
Romain Guycbb89e42009-06-08 15:52:54 -0700250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700252 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
253 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700254
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 final Configuration configuration = getResources().getConfiguration();
256
Romain Guy98d01652009-06-30 16:21:04 -0700257 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 final String locale = configuration.locale.toString();
259
Romain Guy98d01652009-06-30 16:21:04 -0700260 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 final int mcc = configuration.mcc;
262
Romain Guy98d01652009-06-30 16:21:04 -0700263 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 final int mnc = configuration.mnc;
265
Romain Guy84f296c2009-11-04 15:00:44 -0800266 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267
Romain Guy84f296c2009-11-04 15:00:44 -0800268 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700269 localeConfiguration.locale = locale;
270 localeConfiguration.mcc = mcc;
271 localeConfiguration.mnc = mnc;
272
273 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700275 }
276 }
277
278 private static class LocaleConfiguration {
279 public String locale;
280 public int mcc = -1;
281 public int mnc = -1;
282 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700283
Romain Guy98d01652009-06-30 16:21:04 -0700284 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
285 DataInputStream in = null;
286 try {
287 in = new DataInputStream(context.openFileInput(PREFERENCES));
288 configuration.locale = in.readUTF();
289 configuration.mcc = in.readInt();
290 configuration.mnc = in.readInt();
291 } catch (FileNotFoundException e) {
292 // Ignore
293 } catch (IOException e) {
294 // Ignore
295 } finally {
296 if (in != null) {
297 try {
298 in.close();
299 } catch (IOException e) {
300 // Ignore
301 }
302 }
303 }
304 }
305
306 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
307 DataOutputStream out = null;
308 try {
309 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
310 out.writeUTF(configuration.locale);
311 out.writeInt(configuration.mcc);
312 out.writeInt(configuration.mnc);
313 out.flush();
314 } catch (FileNotFoundException e) {
315 // Ignore
316 } catch (IOException e) {
317 //noinspection ResultOfMethodCallIgnored
318 context.getFileStreamPath(PREFERENCES).delete();
319 } finally {
320 if (out != null) {
321 try {
322 out.close();
323 } catch (IOException e) {
324 // Ignore
325 }
326 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327 }
328 }
329
330 static int getScreen() {
331 synchronized (sLock) {
332 return sScreen;
333 }
334 }
335
336 static void setScreen(int screen) {
337 synchronized (sLock) {
338 sScreen = screen;
339 }
340 }
341
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700343 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344
345 Display display = getWindowManager().getDefaultDisplay();
346 boolean isPortrait = display.getWidth() < display.getHeight();
347
348 final int width = isPortrait ? display.getWidth() : display.getHeight();
349 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700350 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 }
352
353 @Override
354 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700355 mWaitingForResult = false;
356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 // The pattern used here is that a user PICKs a specific application,
358 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
361 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700362
Romain Guy94dabf12009-07-21 10:55:43 -0700363 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 switch (requestCode) {
365 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400366 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 break;
368 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700369 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 break;
371 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400372 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 break;
374 case REQUEST_PICK_LIVE_FOLDER:
375 addLiveFolder(data);
376 break;
377 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400378 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700380 case REQUEST_PICK_APPWIDGET:
381 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700383 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400384 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700386 case REQUEST_PICK_WALLPAPER:
387 // We just wanted the activity result here so we can clear mWaitingForResult
388 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 }
Romain Guy18042c82009-11-06 11:44:55 -0800390 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
391 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
392 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700393 // Clean up the appWidgetId if we canceled
394 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
395 if (appWidgetId != -1) {
396 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 }
398 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 }
400
401 @Override
402 protected void onResume() {
403 super.onResume();
404
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800405 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800406
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400408 mWorkspaceLoading = true;
409 mModel.startLoader(this, true);
410 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
412 }
413
414 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700415 protected void onPause() {
416 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800417 dismissPreview(mPreviousView);
418 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800419 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700420 }
Romain Guycbb89e42009-06-08 15:52:54 -0700421
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700422 @Override
423 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400424 // Flag the loader to stop early before switching
425 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700426
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700427 if (PROFILE_ROTATE) {
428 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
429 }
430 return null;
431 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700432
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800433 // We can't hide the IME if it was forced open. So don't bother
434 /*
435 @Override
436 public void onWindowFocusChanged(boolean hasFocus) {
437 super.onWindowFocusChanged(hasFocus);
438
439 if (hasFocus) {
440 final InputMethodManager inputManager = (InputMethodManager)
441 getSystemService(Context.INPUT_METHOD_SERVICE);
442 WindowManager.LayoutParams lp = getWindow().getAttributes();
443 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
444 android.os.Handler()) {
445 protected void onReceiveResult(int resultCode, Bundle resultData) {
446 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
447 }
448 });
449 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
450 }
451 }
452 */
453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 private boolean acceptFilter() {
455 final InputMethodManager inputManager = (InputMethodManager)
456 getSystemService(Context.INPUT_METHOD_SERVICE);
457 return !inputManager.isFullscreenMode();
458 }
459
460 @Override
461 public boolean onKeyDown(int keyCode, KeyEvent event) {
462 boolean handled = super.onKeyDown(keyCode, event);
463 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
464 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
465 keyCode, event);
466 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700467 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700468 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700469 // showSearchDialog()
470 // If there are multiple keystrokes before the search dialog takes focus,
471 // onSearchRequested() will be called for every keystroke,
472 // but it is idempotent, so it's fine.
473 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 }
475 }
476
Joe Onorato8a9625e2010-01-28 15:55:35 -0800477 // Eat the long press event so the keyboard doesn't come up.
478 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
479 return true;
480 }
481
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 return handled;
483 }
484
Karl Rosaen138a0412009-04-23 19:00:21 -0700485 private String getTypedText() {
486 return mDefaultKeySsb.toString();
487 }
488
489 private void clearTypedText() {
490 mDefaultKeySsb.clear();
491 mDefaultKeySsb.clearSpans();
492 Selection.setSelection(mDefaultKeySsb, 0);
493 }
494
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 /**
496 * Restores the previous state, if it exists.
497 *
498 * @param savedState The previous state.
499 */
500 private void restoreState(Bundle savedState) {
501 if (savedState == null) {
502 return;
503 }
504
Joe Onorato3a8820b2009-11-10 15:06:42 -0800505 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
506 if (allApps) {
507 showAllApps(false);
508 }
509
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800510 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
511 if (currentScreen > -1) {
512 mWorkspace.setCurrentScreen(currentScreen);
513 }
514
515 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
516 if (addScreen > -1) {
517 mAddItemCellInfo = new CellLayout.CellInfo();
518 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
519 addItemCellInfo.valid = true;
520 addItemCellInfo.screen = addScreen;
521 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
522 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
523 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
524 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
525 addItemCellInfo.findVacantCellsFromOccupied(
526 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
527 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
528 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
529 mRestoring = true;
530 }
531
532 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
533 if (renameFolder) {
534 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400535 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 mRestoring = true;
537 }
538 }
539
540 /**
541 * Finds all the views we need and configure them properly.
542 */
543 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400544 DragController dragController = mDragController;
545
Romain Guyb1b69f52009-08-10 15:10:15 -0700546 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400547 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548
Joe Onorato7c312c12009-08-13 21:36:53 -0700549 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700550 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700551 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700552 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700553 // Manage focusability manually since this thing is always visible
554 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700555
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
557 final Workspace workspace = mWorkspace;
558
Joe Onorato7c312c12009-08-13 21:36:53 -0700559 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
560 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561
Joe Onorato7c312c12009-08-13 21:36:53 -0700562 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700563 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700564 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800565 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800566
567 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
568 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
569
570 Drawable previous = mPreviousView.getDrawable();
571 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800572 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573
Joe Onorato0d44e942009-11-16 18:20:51 -0800574 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800575 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800576 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800577 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800578
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400580 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582
583 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400584 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700585 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586
Joe Onorato00acb122009-08-04 16:04:30 -0400587 dragController.setDragScoller(workspace);
588 dragController.setDragListener(deleteZone);
589 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800590 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700591
Joe Onorato00acb122009-08-04 16:04:30 -0400592 // The order here is bottom to top.
593 dragController.addDropTarget(workspace);
594 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 }
596
Romain Guy8a73c512009-11-09 19:19:59 -0800597 @SuppressWarnings({"UnusedDeclaration"})
598 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800599 if (!isAllAppsVisible()) {
600 mWorkspace.scrollLeft();
601 }
Romain Guy8a73c512009-11-09 19:19:59 -0800602 }
603
604 @SuppressWarnings({"UnusedDeclaration"})
605 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800606 if (!isAllAppsVisible()) {
607 mWorkspace.scrollRight();
608 }
Romain Guy8a73c512009-11-09 19:19:59 -0800609 }
610
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 /**
612 * Creates a view representing a shortcut.
613 *
614 * @param info The data structure describing the shortcut.
615 *
616 * @return A View inflated from R.layout.application.
617 */
618 View createShortcut(ApplicationInfo info) {
619 return createShortcut(R.layout.application,
620 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
621 }
622
623 /**
624 * Creates a view representing a shortcut inflated from the specified resource.
625 *
626 * @param layoutResId The id of the XML layout used to create the shortcut.
627 * @param parent The group the shortcut belongs to.
628 * @param info The data structure describing the shortcut.
629 *
630 * @return A View inflated from layoutResId.
631 */
632 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
633 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
634
Joe Onorato5162ea92009-09-03 09:39:42 -0700635 if (info.icon == null) {
636 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
637 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700639 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 info.filtered = true;
641 }
642
643 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
644 favorite.setText(info.title);
645 favorite.setTag(info);
646 favorite.setOnClickListener(this);
647
648 return favorite;
649 }
650
651 /**
652 * Add an application shortcut to the workspace.
653 *
654 * @param data The intent describing the application.
655 * @param cellInfo The position on screen where to create the shortcut.
656 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 cellInfo.screen = mWorkspace.getCurrentScreen();
659 if (!findSingleSlot(cellInfo)) return;
660
Romain Guy73b979d2009-06-09 12:57:21 -0700661 final ApplicationInfo info = infoFromApplicationIntent(context, data);
662 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400663 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700664 }
665 }
666
667 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 ComponentName component = data.getComponent();
669 PackageManager packageManager = context.getPackageManager();
670 ActivityInfo activityInfo = null;
671 try {
672 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
673 } catch (NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800674 Log.e(TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 }
Romain Guycbb89e42009-06-08 15:52:54 -0700676
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 if (activityInfo != null) {
678 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700679
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 itemInfo.title = activityInfo.loadLabel(packageManager);
681 if (itemInfo.title == null) {
682 itemInfo.title = activityInfo.name;
683 }
Romain Guycbb89e42009-06-08 15:52:54 -0700684
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
686 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
687 itemInfo.icon = activityInfo.loadIcon(packageManager);
688 itemInfo.container = ItemInfo.NO_ID;
689
Romain Guy73b979d2009-06-09 12:57:21 -0700690 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 }
Romain Guy73b979d2009-06-09 12:57:21 -0700692
693 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 }
Romain Guycbb89e42009-06-08 15:52:54 -0700695
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 /**
697 * Add a shortcut to the workspace.
698 *
699 * @param data The intent describing the shortcut.
700 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400702 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 cellInfo.screen = mWorkspace.getCurrentScreen();
704 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
707
708 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
711 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 }
713 }
714
Romain Guycbb89e42009-06-08 15:52:54 -0700715
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700717 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700719 * @param data The intent describing the appWidgetId.
720 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700724 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700725
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800726 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700727
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700728 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700729
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700730 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700732 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700733
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 // Try finding open space on Launcher screen
735 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800736 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
737 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
738 return;
739 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700741 // Build Launcher-specific widget info and save to database
742 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 launcherInfo.spanX = spans[0];
744 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700745
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 LauncherModel.addItemToDatabase(this, launcherInfo,
747 LauncherSettings.Favorites.CONTAINER_DESKTOP,
748 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
749
750 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400751 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700752
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700754 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700755
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700756 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700758
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400760 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 }
762 }
Romain Guycbb89e42009-06-08 15:52:54 -0700763
Joe Onorato9c1289c2009-08-17 11:03:03 -0400764 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
765 mDesktopItems.remove(launcherInfo);
766 launcherInfo.hostView = null;
767 }
768
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700769 public LauncherAppWidgetHost getAppWidgetHost() {
770 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 }
Romain Guycbb89e42009-06-08 15:52:54 -0700772
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 static ApplicationInfo addShortcut(Context context, Intent data,
774 CellLayout.CellInfo cellInfo, boolean notify) {
775
Romain Guy73b979d2009-06-09 12:57:21 -0700776 final ApplicationInfo info = infoFromShortcutIntent(context, data);
777 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
778 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
779
780 return info;
781 }
782
783 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
785 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guyb5de7752010-01-04 14:43:49 -0800786 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787
788 Drawable icon = null;
789 boolean filtered = false;
790 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700791 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792
Romain Guyb5de7752010-01-04 14:43:49 -0800793 if (bitmap != null && bitmap instanceof Bitmap) {
794 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail((Bitmap) bitmap, context));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 filtered = true;
796 customIcon = true;
797 } else {
798 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700799 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700801 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 final PackageManager packageManager = context.getPackageManager();
803 Resources resources = packageManager.getResourcesForApplication(
804 iconResource.packageName);
805 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
806 icon = resources.getDrawable(id);
807 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800808 Log.w(TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 }
810 }
811 }
812
813 if (icon == null) {
814 icon = context.getPackageManager().getDefaultActivityIcon();
815 }
816
817 final ApplicationInfo info = new ApplicationInfo();
818 info.icon = icon;
819 info.filtered = filtered;
820 info.title = name;
821 info.intent = intent;
822 info.customIcon = customIcon;
823 info.iconResource = iconResource;
824
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 return info;
826 }
827
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800828 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800829 getWindow().closeAllPanels();
830
831 try {
832 dismissDialog(DIALOG_CREATE_SHORTCUT);
833 // Unlock the workspace if the dialog was showing
834 } catch (Exception e) {
835 // An exception is thrown if the dialog is not visible, which is fine
836 }
837
838 try {
839 dismissDialog(DIALOG_RENAME_FOLDER);
840 // Unlock the workspace if the dialog was showing
841 } catch (Exception e) {
842 // An exception is thrown if the dialog is not visible, which is fine
843 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800844
845 // Whatever we were doing is hereby canceled.
846 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800847 }
848
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 @Override
850 protected void onNewIntent(Intent intent) {
851 super.onNewIntent(intent);
852
853 // Close the menu
854 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800855 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800856 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700857
Joe Onorato14f122b2009-11-19 14:06:36 -0800858 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
859 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
860 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800861 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800862 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800863 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800864 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700865
Joe Onorato3a8820b2009-11-10 15:06:42 -0800866 final View v = getWindow().peekDecorView();
867 if (v != null && v.getWindowToken() != null) {
868 InputMethodManager imm = (InputMethodManager)getSystemService(
869 INPUT_METHOD_SERVICE);
870 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 }
872 }
873 }
874
875 @Override
876 protected void onRestoreInstanceState(Bundle savedInstanceState) {
877 // Do not call super here
878 mSavedInstanceState = savedInstanceState;
879 }
880
881 @Override
882 protected void onSaveInstanceState(Bundle outState) {
883 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
884
885 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
886 if (folders.size() > 0) {
887 final int count = folders.size();
888 long[] ids = new long[count];
889 for (int i = 0; i < count; i++) {
890 final FolderInfo info = folders.get(i).getInfo();
891 ids[i] = info.id;
892 }
893 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
894 } else {
895 super.onSaveInstanceState(outState);
896 }
897
Joe Onoratofb0ca672009-09-14 17:55:46 -0400898 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800899 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700901 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902
903 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
904 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
905 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
906
907 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
908 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
909 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
910 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
911 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
912 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
913 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
914 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
915 layout.getOccupiedCells());
916 }
917
918 if (mFolderInfo != null && mWaitingForResult) {
919 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
920 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
921 }
922 }
923
924 @Override
925 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700927
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700929 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800931 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 }
933
934 TextKeyListener.getInstance().release();
935
Joe Onorato9c1289c2009-08-17 11:03:03 -0400936 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938 unbindDesktopItems();
939 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800940
941 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800942
943 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800944 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800945
946 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 }
948
949 @Override
950 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700951 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 super.startActivityForResult(intent, requestCode);
953 }
954
955 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700956 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700958
Joe Onorato7bb17492009-09-24 17:51:01 -0700959 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700960
Karl Rosaen138a0412009-04-23 19:00:21 -0700961 if (initialQuery == null) {
962 // Use any text typed in the launcher as the initial query
963 initialQuery = getTypedText();
964 clearTypedText();
965 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 if (appSearchData == null) {
967 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +0000968 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 }
Romain Guycbb89e42009-06-08 15:52:54 -0700970
Karl Rosaen138a0412009-04-23 19:00:21 -0700971 final SearchManager searchManager =
972 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -0700973 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700974 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 }
976
977 @Override
978 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 if (isWorkspaceLocked()) {
980 return false;
981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982
983 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800984
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
986 .setIcon(android.R.drawable.ic_menu_add)
987 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800988 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 .setIcon(android.R.drawable.ic_menu_gallery)
990 .setAlphabeticShortcut('W');
991 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
992 .setIcon(android.R.drawable.ic_search_category_default)
993 .setAlphabeticShortcut(SearchManager.MENU_KEY);
994 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
995 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
996 .setAlphabeticShortcut('N');
997
998 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700999 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1000 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001
1002 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1003 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1004 .setIntent(settings);
1005
1006 return true;
1007 }
1008
1009 @Override
1010 public boolean onPrepareOptionsMenu(Menu menu) {
1011 super.onPrepareOptionsMenu(menu);
1012
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001013 // If all apps is animating, don't show the menu, because we don't know
1014 // which one to show.
1015 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1016 return false;
1017 }
1018
1019 // Only show the add and wallpaper options when we're not in all apps.
1020 boolean visible = !mAllAppsGrid.isOpaque();
1021 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1022 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1023
1024 // Disable add if the workspace is full.
1025 if (visible) {
1026 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1027 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1028 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029
1030 return true;
1031 }
1032
1033 @Override
1034 public boolean onOptionsItemSelected(MenuItem item) {
1035 switch (item.getItemId()) {
1036 case MENU_ADD:
1037 addItems();
1038 return true;
1039 case MENU_WALLPAPER_SETTINGS:
1040 startWallpaper();
1041 return true;
1042 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001043 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 return true;
1045 case MENU_NOTIFICATIONS:
1046 showNotifications();
1047 return true;
1048 }
1049
1050 return super.onOptionsItemSelected(item);
1051 }
Romain Guycbb89e42009-06-08 15:52:54 -07001052
Karl Rosaen138a0412009-04-23 19:00:21 -07001053 /**
1054 * Indicates that we want global search for this activity by setting the globalSearch
1055 * argument for {@link #startSearch} to true.
1056 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001058 @Override
1059 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001060 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001061 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001062 }
1063
Joe Onorato9c1289c2009-08-17 11:03:03 -04001064 public boolean isWorkspaceLocked() {
1065 return mWorkspaceLoading || mWaitingForResult;
1066 }
1067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001069 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 showAddDialog(mMenuAddInfo);
1071 }
1072
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001073 void addAppWidget(Intent data) {
1074 // TODO: catch bad widget exception when sent
1075 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001076 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077
Bjorn Bringert7984c942009-12-09 15:38:25 +00001078 if (appWidget.configure != null) {
1079 // Launch over to configure widget, if needed
1080 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1081 intent.setComponent(appWidget.configure);
1082 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1083
1084 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001086 // Otherwise just add it
1087 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 }
1089 }
Romain Guycbb89e42009-06-08 15:52:54 -07001090
Romain Guy73b979d2009-06-09 12:57:21 -07001091 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001092 // Handle case where user selected "Applications"
1093 String applicationName = getResources().getString(R.string.group_applications);
1094 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001095
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001096 if (applicationName != null && applicationName.equals(shortcutName)) {
1097 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1098 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001099
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001100 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1101 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001102 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001103 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001104 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001105 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 }
1107
1108 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001109 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001110 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001111 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001112
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001113 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001114 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001115 } else {
1116 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1117 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 }
1119
Joe Onorato9c1289c2009-08-17 11:03:03 -04001120 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 UserFolderInfo folderInfo = new UserFolderInfo();
1122 folderInfo.title = getText(R.string.folder_name);
1123
1124 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1125 cellInfo.screen = mWorkspace.getCurrentScreen();
1126 if (!findSingleSlot(cellInfo)) return;
1127
1128 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001129 LauncherModel.addItemToDatabase(this, folderInfo,
1130 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133
1134 // Create the view
1135 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1136 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1137 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001138 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 }
Romain Guycbb89e42009-06-08 15:52:54 -07001140
Joe Onorato9c1289c2009-08-17 11:03:03 -04001141 void removeFolder(FolderInfo folder) {
1142 mFolders.remove(folder.id);
1143 }
1144
1145 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 cellInfo.screen = mWorkspace.getCurrentScreen();
1147 if (!findSingleSlot(cellInfo)) return;
1148
1149 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1150
1151 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1153 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001154 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1155 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 }
1157 }
1158
1159 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1160 CellLayout.CellInfo cellInfo, boolean notify) {
1161
1162 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1163 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1164
1165 Drawable icon = null;
1166 boolean filtered = false;
1167 Intent.ShortcutIconResource iconResource = null;
1168
1169 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1170 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1171 try {
1172 iconResource = (Intent.ShortcutIconResource) extra;
1173 final PackageManager packageManager = context.getPackageManager();
1174 Resources resources = packageManager.getResourcesForApplication(
1175 iconResource.packageName);
1176 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1177 icon = resources.getDrawable(id);
1178 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001179 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 }
1181 }
1182
1183 if (icon == null) {
1184 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1185 }
1186
1187 final LiveFolderInfo info = new LiveFolderInfo();
1188 info.icon = icon;
1189 info.filtered = filtered;
1190 info.title = name;
1191 info.iconResource = iconResource;
1192 info.uri = data.getData();
1193 info.baseIntent = baseIntent;
1194 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1195 LiveFolders.DISPLAY_MODE_GRID);
1196
1197 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1198 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001199 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200
1201 return info;
1202 }
1203
1204 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1205 final int[] xy = new int[2];
1206 if (findSlot(cellInfo, xy, 1, 1)) {
1207 cellInfo.cellX = xy[0];
1208 cellInfo.cellY = xy[1];
1209 return true;
1210 }
1211 return false;
1212 }
1213
1214 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1215 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1216 boolean[] occupied = mSavedState != null ?
1217 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1218 cellInfo = mWorkspace.findAllVacantCells(occupied);
1219 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1220 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1221 return false;
1222 }
1223 }
1224 return true;
1225 }
1226
1227 private void showNotifications() {
1228 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1229 if (statusBar != null) {
1230 statusBar.expand();
1231 }
1232 }
1233
1234 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001235 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001237 Intent chooser = Intent.createChooser(pickWallpaper,
1238 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001239 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1240 // Removed in Eclair MR1
1241// WallpaperManager wm = (WallpaperManager)
1242// getSystemService(Context.WALLPAPER_SERVICE);
1243// WallpaperInfo wi = wm.getWallpaperInfo();
1244// if (wi != null && wi.getSettingsActivity() != null) {
1245// LabeledIntent li = new LabeledIntent(getPackageName(),
1246// R.string.configure_wallpaper, 0);
1247// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1248// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1249// }
Mike Clerona0618e42009-10-22 13:55:21 -07001250 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 }
1252
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001253 /**
1254 * Registers various content observers. The current implementation registers
1255 * only a favorites observer to keep track of the favorites applications.
1256 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001257 private void registerContentObservers() {
1258 ContentResolver resolver = getContentResolver();
1259 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1260 true, mWidgetObserver);
1261 }
1262
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 @Override
1264 public boolean dispatchKeyEvent(KeyEvent event) {
1265 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1266 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001268 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001269 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001270 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001271 dumpState();
1272 return true;
1273 }
1274 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001275 }
1276 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1277 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001278 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 return true;
1280 }
1281 }
1282
1283 return super.dispatchKeyEvent(event);
1284 }
1285
Joe Onorato88ec0992009-11-19 13:16:06 -08001286 @Override
1287 public void onBackPressed() {
1288 if (isAllAppsVisible()) {
1289 closeAllApps(true);
1290 } else {
1291 closeFolder();
1292 }
1293 dismissPreview(mPreviousView);
1294 dismissPreview(mNextView);
1295 }
1296
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 private void closeFolder() {
1298 Folder folder = mWorkspace.getOpenFolder();
1299 if (folder != null) {
1300 closeFolder(folder);
1301 }
1302 }
1303
1304 void closeFolder(Folder folder) {
1305 folder.getInfo().opened = false;
1306 ViewGroup parent = (ViewGroup) folder.getParent();
1307 if (parent != null) {
1308 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001309 if (folder instanceof DropTarget) {
1310 // Live folders aren't DropTargets.
1311 mDragController.removeDropTarget((DropTarget)folder);
1312 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 }
1314 folder.onClose();
1315 }
1316
1317 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001318 * Re-listen when widgets are reset.
1319 */
1320 private void onAppWidgetReset() {
1321 mAppWidgetHost.startListening();
1322 }
1323
1324 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001325 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1326 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001328 private void unbindDesktopItems() {
1329 for (ItemInfo item: mDesktopItems) {
1330 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001333
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 /**
1335 * Launches the intent referred by the clicked shortcut.
1336 *
1337 * @param v The view representing the clicked shortcut.
1338 */
1339 public void onClick(View v) {
1340 Object tag = v.getTag();
1341 if (tag instanceof ApplicationInfo) {
1342 // Open shortcut
1343 final Intent intent = ((ApplicationInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001344 int[] pos = new int[2];
1345 v.getLocationOnScreen(pos);
1346 intent.setSourceBounds(
1347 new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 startActivitySafely(intent);
1349 } else if (tag instanceof FolderInfo) {
1350 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001351 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001352 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001353 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001354 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001355 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001356 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 }
1358 }
1359
1360 void startActivitySafely(Intent intent) {
1361 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1362 try {
1363 startActivity(intent);
1364 } catch (ActivityNotFoundException e) {
1365 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1366 } catch (SecurityException e) {
1367 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001368 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1370 "or use the exported attribute for this activity.", e);
1371 }
1372 }
1373
1374 private void handleFolderClick(FolderInfo folderInfo) {
1375 if (!folderInfo.opened) {
1376 // Close any open folder
1377 closeFolder();
1378 // Open the requested folder
1379 openFolder(folderInfo);
1380 } else {
1381 // Find the open folder...
1382 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1383 int folderScreen;
1384 if (openFolder != null) {
1385 folderScreen = mWorkspace.getScreenForView(openFolder);
1386 // .. and close it
1387 closeFolder(openFolder);
1388 if (folderScreen != mWorkspace.getCurrentScreen()) {
1389 // Close any folder open on the current screen
1390 closeFolder();
1391 // Pull the folder onto this screen
1392 openFolder(folderInfo);
1393 }
1394 }
1395 }
1396 }
1397
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 /**
1399 * Opens the user fodler described by the specified tag. The opening of the folder
1400 * is animated relative to the specified View. If the View is null, no animation
1401 * is played.
1402 *
1403 * @param folderInfo The FolderInfo describing the folder to open.
1404 */
1405 private void openFolder(FolderInfo folderInfo) {
1406 Folder openFolder;
1407
1408 if (folderInfo instanceof UserFolderInfo) {
1409 openFolder = UserFolder.fromXml(this);
1410 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001411 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 } else {
1413 return;
1414 }
1415
Joe Onorato00acb122009-08-04 16:04:30 -04001416 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 openFolder.setLauncher(this);
1418
1419 openFolder.bind(folderInfo);
1420 folderInfo.opened = true;
1421
1422 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1423 openFolder.onOpen();
1424 }
1425
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001427 switch (v.getId()) {
1428 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001429 if (!isAllAppsVisible()) {
1430 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1431 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001432 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001433 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001434 return true;
1435 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001436 if (!isAllAppsVisible()) {
1437 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1438 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001439 showPreviews(v);
1440 }
1441 return true;
1442 case R.id.all_apps_button:
1443 if (!isAllAppsVisible()) {
1444 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1445 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1446 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001447 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001448 return true;
1449 }
1450
Joe Onorato9c1289c2009-08-17 11:03:03 -04001451 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 return false;
1453 }
1454
1455 if (!(v instanceof CellLayout)) {
1456 v = (View) v.getParent();
1457 }
1458
1459 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1460
1461 // This happens when long clicking an item with the dpad/trackball
1462 if (cellInfo == null) {
1463 return true;
1464 }
1465
1466 if (mWorkspace.allowLongPress()) {
1467 if (cellInfo.cell == null) {
1468 if (cellInfo.valid) {
1469 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001470 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 showAddDialog(cellInfo);
1472 }
1473 } else {
1474 if (!(cellInfo.cell instanceof Folder)) {
1475 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001476 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1477 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 mWorkspace.startDrag(cellInfo);
1479 }
1480 }
1481 }
1482 return true;
1483 }
1484
Romain Guye6b8e2f2009-11-10 11:56:55 -08001485 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001486 private void dismissPreview(final View v) {
1487 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001488 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001489 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1490 public void onDismiss() {
1491 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1492 int count = group.getChildCount();
1493 for (int i = 0; i < count; i++) {
1494 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1495 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001496 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1497 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1498
1499 v.setTag(R.id.workspace, null);
1500 v.setTag(R.id.icon, null);
1501 window.setOnDismissListener(null);
1502 }
1503 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001504 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001505 }
1506 v.setTag(null);
1507 }
1508
Romain Guyf8e6a802009-12-07 17:48:02 -08001509 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001510 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001511 }
1512
Romain Guya6abce82009-11-10 02:54:41 -08001513 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001514 final Resources resources = getResources();
1515 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001516
Romain Guya6abce82009-11-10 02:54:41 -08001517 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001518
1519 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001520
Romain Guyf8e6a802009-12-07 17:48:02 -08001521 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001522 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001523 int extraW = (int) ((r.left + r.right) * max);
1524 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001525
1526 int aW = cell.getWidth() - extraW;
1527 float w = aW / max;
1528
1529 int width = cell.getWidth();
1530 int height = cell.getHeight();
1531 int x = cell.getLeftPadding();
1532 int y = cell.getTopPadding();
1533 width -= (x + cell.getRightPadding());
1534 height -= (y + cell.getBottomPadding());
1535
1536 float scale = w / width;
1537
1538 int count = end - start;
1539
1540 final float sWidth = width * scale;
1541 float sHeight = height * scale;
1542
Romain Guye6b8e2f2009-11-10 11:56:55 -08001543 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001544
Romain Guye6b8e2f2009-11-10 11:56:55 -08001545 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1546 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001547
1548 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001549 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001550 cell = (CellLayout) workspace.getChildAt(i);
1551
Romain Guyf8e6a802009-12-07 17:48:02 -08001552 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001553 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001554
1555 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001556 c.scale(scale, scale);
1557 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1558 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001559
Romain Guy9d31e9f2009-11-11 18:54:28 -08001560 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001561 image.setImageBitmap(bitmap);
1562 image.setTag(i);
1563 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001564 image.setOnFocusChangeListener(handler);
1565 image.setFocusable(true);
1566 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001567
1568 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001569 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1570
1571 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001572 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001573
1574 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001575 p.setContentView(preview);
1576 p.setWidth((int) (sWidth * count + extraW));
1577 p.setHeight((int) (sHeight + extraH));
1578 p.setAnimationStyle(R.style.AnimationPreview);
1579 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001580 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001581 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001582 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001583
Romain Guye6b8e2f2009-11-10 11:56:55 -08001584 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1585 public void onDismiss() {
1586 dismissPreview(anchor);
1587 }
1588 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001589
1590 anchor.setTag(p);
1591 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001592 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001593 }
1594
Romain Guy9d31e9f2009-11-11 18:54:28 -08001595 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001596 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001597
Romain Guye6b8e2f2009-11-10 11:56:55 -08001598 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001599 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001600 }
1601
1602 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001603 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001604 v.post(this);
1605 }
1606
1607 public void run() {
1608 dismissPreview(mAnchor);
1609 }
1610
1611 public void onFocusChange(View v, boolean hasFocus) {
1612 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001613 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001614 }
Romain Guya6abce82009-11-10 02:54:41 -08001615 }
1616 }
1617
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 Workspace getWorkspace() {
1619 return mWorkspace;
1620 }
1621
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 @Override
1623 protected Dialog onCreateDialog(int id) {
1624 switch (id) {
1625 case DIALOG_CREATE_SHORTCUT:
1626 return new CreateShortcut().createDialog();
1627 case DIALOG_RENAME_FOLDER:
1628 return new RenameFolder().createDialog();
1629 }
1630
1631 return super.onCreateDialog(id);
1632 }
1633
1634 @Override
1635 protected void onPrepareDialog(int id, Dialog dialog) {
1636 switch (id) {
1637 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 break;
1639 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001640 if (mFolderInfo != null) {
1641 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1642 final CharSequence text = mFolderInfo.title;
1643 input.setText(text);
1644 input.setSelection(0, text.length());
1645 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 break;
1647 }
1648 }
1649
1650 void showRenameDialog(FolderInfo info) {
1651 mFolderInfo = info;
1652 mWaitingForResult = true;
1653 showDialog(DIALOG_RENAME_FOLDER);
1654 }
1655
1656 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1657 mAddItemCellInfo = cellInfo;
1658 mWaitingForResult = true;
1659 showDialog(DIALOG_CREATE_SHORTCUT);
1660 }
1661
Romain Guy73b979d2009-06-09 12:57:21 -07001662 private void pickShortcut(int requestCode, int title) {
1663 Bundle bundle = new Bundle();
1664
1665 ArrayList<String> shortcutNames = new ArrayList<String>();
1666 shortcutNames.add(getString(R.string.group_applications));
1667 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1668
1669 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1670 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1671 R.drawable.ic_launcher_application));
1672 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1673
1674 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1675 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1676 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1677 pickIntent.putExtras(bundle);
1678
1679 startActivityForResult(pickIntent, requestCode);
1680 }
1681
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 private class RenameFolder {
1683 private EditText mInput;
1684
1685 Dialog createDialog() {
1686 mWaitingForResult = true;
1687 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1688 mInput = (EditText) layout.findViewById(R.id.folder_name);
1689
1690 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1691 builder.setIcon(0);
1692 builder.setTitle(getString(R.string.rename_folder_title));
1693 builder.setCancelable(true);
1694 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1695 public void onCancel(DialogInterface dialog) {
1696 cleanup();
1697 }
1698 });
1699 builder.setNegativeButton(getString(R.string.cancel_action),
1700 new Dialog.OnClickListener() {
1701 public void onClick(DialogInterface dialog, int which) {
1702 cleanup();
1703 }
1704 }
1705 );
1706 builder.setPositiveButton(getString(R.string.rename_action),
1707 new Dialog.OnClickListener() {
1708 public void onClick(DialogInterface dialog, int which) {
1709 changeFolderName();
1710 }
1711 }
1712 );
1713 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001714
1715 final AlertDialog dialog = builder.create();
1716 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1717 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001718 mInput.requestFocus();
1719 InputMethodManager inputManager = (InputMethodManager)
1720 getSystemService(Context.INPUT_METHOD_SERVICE);
1721 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001722 }
1723 });
1724
1725 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 }
1727
1728 private void changeFolderName() {
1729 final String name = mInput.getText().toString();
1730 if (!TextUtils.isEmpty(name)) {
1731 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001732 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 mFolderInfo.title = name;
1734 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1735
Joe Onorato9c1289c2009-08-17 11:03:03 -04001736 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001737 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001738 mModel.setWorkspaceDirty();
1739 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 } else {
1741 final FolderIcon folderIcon = (FolderIcon)
1742 mWorkspace.getViewForTag(mFolderInfo);
1743 if (folderIcon != null) {
1744 folderIcon.setText(name);
1745 getWorkspace().requestLayout();
1746 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001747 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001748 mModel.setWorkspaceDirty();
1749 mWorkspaceLoading = true;
1750 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 }
1752 }
1753 }
1754 cleanup();
1755 }
1756
1757 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 dismissDialog(DIALOG_RENAME_FOLDER);
1759 mWaitingForResult = false;
1760 mFolderInfo = null;
1761 }
1762 }
1763
Joe Onoratofb0ca672009-09-14 17:55:46 -04001764 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001765 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001766 }
1767
Joe Onorato3a8820b2009-11-10 15:06:42 -08001768 void showAllApps(boolean animated) {
1769 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001770
Mike Cleronb6082fa02009-10-19 17:03:36 -07001771 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001772 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001773
Joe Onorato7c312c12009-08-13 21:36:53 -07001774 // TODO: fade these two too
1775 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001776 }
1777
Joe Onoratob2061212009-11-24 16:13:54 -05001778 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001779 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001780 * - Home from workspace
1781 * - from center screen
1782 * - from other screens
1783 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001784 * - from center screen
1785 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001786 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001787 * - from center screen
1788 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001789 * - Launch app from workspace and quit
1790 * - with back
1791 * - with home
1792 * - Launch app from all apps and quit
1793 * - with back
1794 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001795 * - Go to a screen that's not the default, then all
1796 * apps, and launch and app, and go back
1797 * - with back
1798 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001799 * - On workspace, long press power and go back
1800 * - with back
1801 * - with home
1802 * - On all apps, long press power and go back
1803 * - with back
1804 * - with home
1805 * - On workspace, power off
1806 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001807 * - Launch an app and turn off the screen while in that app
1808 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001809 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001810 * - From all apps
1811 * - From workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001812 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001813 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001814 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001815 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001816 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001817 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001818 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001819 }
1820
Joe Onorato7c312c12009-08-13 21:36:53 -07001821 void lockAllApps() {
1822 // TODO
1823 }
1824
1825 void unlockAllApps() {
1826 // TODO
1827 }
1828
Joe Onorato7404ee42009-07-31 11:54:44 -07001829 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 * Displays the shortcut creation dialog and launches, if necessary, the
1831 * appropriate activity.
1832 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001833 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001834 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1835 DialogInterface.OnShowListener {
1836
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001838
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 Dialog createDialog() {
1840 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001841
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001843
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1845 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001846 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001847
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 builder.setInverseBackgroundForced(true);
1849
1850 AlertDialog dialog = builder.create();
1851 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001852 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001853 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 return dialog;
1856 }
1857
1858 public void onCancel(DialogInterface dialog) {
1859 mWaitingForResult = false;
1860 cleanup();
1861 }
1862
Romain Guycbb89e42009-06-08 15:52:54 -07001863 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001864 }
1865
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001867 try {
1868 dismissDialog(DIALOG_CREATE_SHORTCUT);
1869 } catch (Exception e) {
1870 // An exception is thrown if the dialog is not visible, which is fine
1871 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 }
1873
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001874 /**
1875 * Handle the action clicked in the "Add to home" dialog.
1876 */
1877 public void onClick(DialogInterface dialog, int which) {
1878 Resources res = getResources();
1879 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001880
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001881 switch (which) {
1882 case AddAdapter.ITEM_SHORTCUT: {
1883 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001884 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001885 break;
1886 }
Romain Guycbb89e42009-06-08 15:52:54 -07001887
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001888 case AddAdapter.ITEM_APPWIDGET: {
1889 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001890
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001891 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1892 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001893 // start the pick activity
1894 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1895 break;
1896 }
Romain Guycbb89e42009-06-08 15:52:54 -07001897
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001898 case AddAdapter.ITEM_LIVE_FOLDER: {
1899 // Insert extra item to handle inserting folder
1900 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001901
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001902 ArrayList<String> shortcutNames = new ArrayList<String>();
1903 shortcutNames.add(res.getString(R.string.group_folder));
1904 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001905
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001906 ArrayList<ShortcutIconResource> shortcutIcons =
1907 new ArrayList<ShortcutIconResource>();
1908 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1909 R.drawable.ic_launcher_folder));
1910 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1911
1912 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1913 pickIntent.putExtra(Intent.EXTRA_INTENT,
1914 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1915 pickIntent.putExtra(Intent.EXTRA_TITLE,
1916 getText(R.string.title_select_live_folder));
1917 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001918
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001919 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1920 break;
1921 }
1922
1923 case AddAdapter.ITEM_WALLPAPER: {
1924 startWallpaper();
1925 break;
1926 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 }
1928 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001929
1930 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 }
1933
1934 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001935 * Receives notifications when applications are added/removed.
1936 */
1937 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1938 @Override
1939 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001940 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05001941 String reason = intent.getStringExtra("reason");
1942 if (!"homekey".equals(reason)) {
1943 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001944 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05001945 animate = false;
1946 }
Joe Onoratob2061212009-11-24 16:13:54 -05001947 closeAllApps(animate);
1948 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001949 }
1950 }
1951
1952 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001953 * Receives notifications whenever the appwidgets are reset.
1954 */
1955 private class AppWidgetResetObserver extends ContentObserver {
1956 public AppWidgetResetObserver() {
1957 super(new Handler());
1958 }
1959
1960 @Override
1961 public void onChange(boolean selfChange) {
1962 onAppWidgetReset();
1963 }
1964 }
1965
1966 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001967 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001969 public int getCurrentWorkspaceScreen() {
1970 return mWorkspace.getCurrentScreen();
1971 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001972
Joe Onorato9c1289c2009-08-17 11:03:03 -04001973 /**
1974 * Refreshes the shortcuts shown on the workspace.
1975 *
1976 * Implementation of the method from LauncherModel.Callbacks.
1977 */
1978 public void startBinding() {
1979 final Workspace workspace = mWorkspace;
1980 int count = workspace.getChildCount();
1981 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001982 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001983 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1984 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001985
Joe Onorato9c1289c2009-08-17 11:03:03 -04001986 if (DEBUG_USER_INTERFACE) {
1987 android.widget.Button finishButton = new android.widget.Button(this);
1988 finishButton.setText("Finish");
1989 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1990
1991 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1992 public void onClick(View v) {
1993 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001994 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001995 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 }
1997 }
1998
1999 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002000 * Bind the items start-end from the list.
2001 *
2002 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002004 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2005
2006 final Workspace workspace = mWorkspace;
2007
2008 for (int i=start; i<end; i++) {
2009 final ItemInfo item = shortcuts.get(i);
2010 mDesktopItems.add(item);
2011 switch (item.itemType) {
2012 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2013 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2014 final View shortcut = createShortcut((ApplicationInfo) item);
2015 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2016 false);
2017 break;
2018 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2019 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2020 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2021 (UserFolderInfo) item);
2022 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2023 false);
2024 break;
2025 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2026 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2027 R.layout.live_folder_icon, this,
2028 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2029 (LiveFolderInfo) item);
2030 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2031 false);
2032 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 }
2035
Joe Onorato9c1289c2009-08-17 11:03:03 -04002036 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 }
2038
Joe Onorato9c1289c2009-08-17 11:03:03 -04002039 /**
2040 * Implementation of the method from LauncherModel.Callbacks.
2041 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002042 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2043 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002044 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002046
2047 /**
2048 * Add the views for a widget to the workspace.
2049 *
2050 * Implementation of the method from LauncherModel.Callbacks.
2051 */
2052 public void bindAppWidget(LauncherAppWidgetInfo item) {
2053 final Workspace workspace = mWorkspace;
2054
2055 final int appWidgetId = item.appWidgetId;
2056 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2057 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2058
Joe Onorato9c1289c2009-08-17 11:03:03 -04002059 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2060 item.hostView.setTag(item);
2061
2062 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2063 item.cellY, item.spanX, item.spanY, false);
2064
2065 workspace.requestLayout();
2066
2067 mDesktopItems.add(item);
2068 }
2069
2070 /**
2071 * Callback saying that there aren't any more items to bind.
2072 *
2073 * Implementation of the method from LauncherModel.Callbacks.
2074 */
2075 public void finishBindingItems() {
2076 if (mSavedState != null) {
2077 if (!mWorkspace.hasFocus()) {
2078 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2079 }
2080
2081 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2082 if (userFolders != null) {
2083 for (long folderId : userFolders) {
2084 final FolderInfo info = mFolders.get(folderId);
2085 if (info != null) {
2086 openFolder(info);
2087 }
2088 }
2089 final Folder openFolder = mWorkspace.getOpenFolder();
2090 if (openFolder != null) {
2091 openFolder.requestFocus();
2092 }
2093 }
2094
Joe Onorato9c1289c2009-08-17 11:03:03 -04002095 mSavedState = null;
2096 }
2097
2098 if (mSavedInstanceState != null) {
2099 super.onRestoreInstanceState(mSavedInstanceState);
2100 mSavedInstanceState = null;
2101 }
2102
Joe Onorato9c1289c2009-08-17 11:03:03 -04002103 mWorkspaceLoading = false;
2104 }
2105
2106 /**
2107 * Add the icons for all apps.
2108 *
2109 * Implementation of the method from LauncherModel.Callbacks.
2110 */
2111 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002112 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002113 }
2114
2115 /**
2116 * A package was installed.
2117 *
2118 * Implementation of the method from LauncherModel.Callbacks.
2119 */
2120 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2121 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002122 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002123 }
2124
2125 /**
2126 * A package was updated.
2127 *
2128 * Implementation of the method from LauncherModel.Callbacks.
2129 */
2130 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2131 removeDialog(DIALOG_CREATE_SHORTCUT);
2132 mWorkspace.updateShortcutsForPackage(packageName);
Joe Onoratof0be2132009-11-24 19:34:29 -05002133 mAllAppsGrid.updateApps(packageName, apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002134 }
2135
2136 /**
2137 * A package was uninstalled.
2138 *
2139 * Implementation of the method from LauncherModel.Callbacks.
2140 */
2141 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2142 removeDialog(DIALOG_CREATE_SHORTCUT);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002143 mWorkspace.removeItemsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002144 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002145 }
Joe Onoratobe386092009-11-17 17:32:16 -08002146
2147 /**
2148 * Prints out out state for debugging.
2149 */
2150 public void dumpState() {
2151 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002152 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002153 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2154 Log.d(TAG, "mRestoring=" + mRestoring);
2155 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2156 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2157 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2158 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002159 mModel.dumpState();
2160 mAllAppsGrid.dumpState();
2161 Log.d(TAG, "END launcher2 dump state");
2162 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163}