blob: 6a0978b8cc27b25b6b2f6559b153a0fa59055a8d [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.app.Activity;
20import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Dialog;
Mike LeBeau736cf282009-07-02 17:46:59 -070022import android.app.ISearchManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.SearchManager;
24import android.app.StatusBarManager;
Dianne Hackborn8355ae32009-09-07 21:47:51 -070025import android.app.WallpaperInfo;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
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;
Dianne Hackborn8355ae32009-09-07 21:47:51 -070037import android.content.pm.LabeledIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.pm.PackageManager;
39import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070041import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080042import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080044import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080045import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080047import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080049import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.os.Parcelable;
51import android.os.RemoteException;
52import android.os.ServiceManager;
Joe Onorato080d9b62009-11-02 12:01:11 -050053import android.os.SystemClock;
Karl Rosaen138a0412009-04-23 19:00:21 -070054import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.text.Selection;
56import android.text.SpannableStringBuilder;
57import android.text.TextUtils;
58import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070059import static android.util.Log.*;
Joe Onorato7c312c12009-08-13 21:36:53 -070060import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.view.KeyEvent;
63import android.view.LayoutInflater;
64import android.view.Menu;
65import android.view.MenuItem;
66import android.view.View;
67import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.view.View.OnLongClickListener;
69import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.widget.TextView;
72import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080073import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080074import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080075import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070076import android.appwidget.AppWidgetManager;
77import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040080import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070081import java.io.DataOutputStream;
82import java.io.FileNotFoundException;
83import java.io.IOException;
84import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085
86/**
87 * Default launcher application.
88 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040089public final class Launcher extends Activity
Romain Guya6abce82009-11-10 02:54:41 -080090 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 static final String LOG_TAG = "Launcher";
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 static final String TAG = LOG_TAG;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 static final boolean LOGD = false;
94
Joe Onorato9c1289c2009-08-17 11:03:03 -040095 static final boolean PROFILE_STARTUP = false;
96 static final boolean PROFILE_ROTATE = false;
97 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070098
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 private static final int WALLPAPER_SCREENS_SPAN = 2;
100
101 private static final int MENU_GROUP_ADD = 1;
102 private static final int MENU_ADD = Menu.FIRST + 1;
103 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
104 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
105 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700106 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107
108 private static final int REQUEST_CREATE_SHORTCUT = 1;
109 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700110 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 private static final int REQUEST_PICK_APPLICATION = 6;
112 private static final int REQUEST_PICK_SHORTCUT = 7;
113 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700114 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700115 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116
117 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
118
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700119 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
120 static final String SEARCH_WIDGET = "search_widget";
121
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800122 static final int SCREEN_COUNT = 5;
123 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700125 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Joe Onorato7c312c12009-08-13 21:36:53 -0700127 static final int DIALOG_CREATE_SHORTCUT = 1;
128 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
Romain Guy98d01652009-06-30 16:21:04 -0700130 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
132 // Type: int
133 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
134 // Type: boolean
135 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
136 // Type: long
137 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
146 // Type: int
147 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
148 // Type: int
149 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
150 // Type: int
151 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
152 // Type: int[]
153 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
154 // Type: boolean
155 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
156 // Type: long
157 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
158
Joe Onorato9c1289c2009-08-17 11:03:03 -0400159 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800162 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800164 private final BroadcastReceiver mCloseSystemDialogsReceiver
165 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800166 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private LayoutInflater mInflater;
169
Joe Onorato00acb122009-08-04 16:04:30 -0400170 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700172
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700173 private AppWidgetManager mAppWidgetManager;
174 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private CellLayout.CellInfo mAddItemCellInfo;
177 private CellLayout.CellInfo mMenuAddInfo;
178 private final int[] mCellCoordinates = new int[2];
179 private FolderInfo mFolderInfo;
180
Joe Onorato7c312c12009-08-13 21:36:53 -0700181 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700182 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700183 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private Bundle mSavedState;
186
187 private SpannableStringBuilder mDefaultKeySsb = null;
188
Mike LeBeau736cf282009-07-02 17:46:59 -0700189 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 private boolean mWorkspaceLoading = true;
192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private boolean mRestoring;
194 private boolean mWaitingForResult;
Joe Onoratoff3862d2009-10-07 10:35:51 -0700195 private boolean mExitingBecauseOfLaunch;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
197 private Bundle mSavedInstanceState;
198
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199 private LauncherModel mModel;
200
Romain Guy84f296c2009-11-04 15:00:44 -0800201 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
202 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700203
Joe Onorato080d9b62009-11-02 12:01:11 -0500204 public static long lastStartTime;
Romain Guy1fbc1c82009-11-09 20:43:08 -0800205 private ImageView mPreviousView;
206 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 @Override
209 protected void onCreate(Bundle savedInstanceState) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500210 lastStartTime = SystemClock.uptimeMillis();
211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700213
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400215 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700217
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800218 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
219 registerReceiver(mCloseSystemDialogsReceiver, filter);
220
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700221 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700222 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
223 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700224
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 if (PROFILE_STARTUP) {
226 android.os.Debug.startMethodTracing("/sdcard/launcher");
227 }
228
229 checkForLocaleChange();
230 setWallpaperDimension();
231
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 setContentView(R.layout.launcher);
233 setupViews();
234
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800235 registerContentObservers();
236
Joe Onorato7c312c12009-08-13 21:36:53 -0700237 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700238
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 mSavedState = savedInstanceState;
240 restoreState(mSavedState);
241
242 if (PROFILE_STARTUP) {
243 android.os.Debug.stopMethodTracing();
244 }
245
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700246 // We have a new AllAppsView, we need to re-bind everything, and it could have
247 // changed in our absence.
248 mModel.setAllAppsDirty();
249 mModel.setWorkspaceDirty();
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400252 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 }
254
255 // For handling default keys
256 mDefaultKeySsb = new SpannableStringBuilder();
257 Selection.setSelection(mDefaultKeySsb, 0);
258 }
Romain Guycbb89e42009-06-08 15:52:54 -0700259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700261 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
262 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 final Configuration configuration = getResources().getConfiguration();
265
Romain Guy98d01652009-06-30 16:21:04 -0700266 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 final String locale = configuration.locale.toString();
268
Romain Guy98d01652009-06-30 16:21:04 -0700269 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 final int mcc = configuration.mcc;
271
Romain Guy98d01652009-06-30 16:21:04 -0700272 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 final int mnc = configuration.mnc;
274
Romain Guy84f296c2009-11-04 15:00:44 -0800275 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
Romain Guy84f296c2009-11-04 15:00:44 -0800277 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700278 localeConfiguration.locale = locale;
279 localeConfiguration.mcc = mcc;
280 localeConfiguration.mnc = mnc;
281
282 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400283 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700284 }
285 }
286
287 private static class LocaleConfiguration {
288 public String locale;
289 public int mcc = -1;
290 public int mnc = -1;
291 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700292
Romain Guy98d01652009-06-30 16:21:04 -0700293 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
294 DataInputStream in = null;
295 try {
296 in = new DataInputStream(context.openFileInput(PREFERENCES));
297 configuration.locale = in.readUTF();
298 configuration.mcc = in.readInt();
299 configuration.mnc = in.readInt();
300 } catch (FileNotFoundException e) {
301 // Ignore
302 } catch (IOException e) {
303 // Ignore
304 } finally {
305 if (in != null) {
306 try {
307 in.close();
308 } catch (IOException e) {
309 // Ignore
310 }
311 }
312 }
313 }
314
315 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
316 DataOutputStream out = null;
317 try {
318 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
319 out.writeUTF(configuration.locale);
320 out.writeInt(configuration.mcc);
321 out.writeInt(configuration.mnc);
322 out.flush();
323 } catch (FileNotFoundException e) {
324 // Ignore
325 } catch (IOException e) {
326 //noinspection ResultOfMethodCallIgnored
327 context.getFileStreamPath(PREFERENCES).delete();
328 } finally {
329 if (out != null) {
330 try {
331 out.close();
332 } catch (IOException e) {
333 // Ignore
334 }
335 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 }
337 }
338
339 static int getScreen() {
340 synchronized (sLock) {
341 return sScreen;
342 }
343 }
344
345 static void setScreen(int screen) {
346 synchronized (sLock) {
347 sScreen = screen;
348 }
349 }
350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700352 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353
354 Display display = getWindowManager().getDefaultDisplay();
355 boolean isPortrait = display.getWidth() < display.getHeight();
356
357 final int width = isPortrait ? display.getWidth() : display.getHeight();
358 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700359 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 }
361
362 @Override
363 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700364 mWaitingForResult = false;
365
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 // The pattern used here is that a user PICKs a specific application,
367 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
370 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700371
Romain Guy94dabf12009-07-21 10:55:43 -0700372 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 switch (requestCode) {
374 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400375 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 break;
377 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700378 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 break;
380 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400381 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 break;
383 case REQUEST_PICK_LIVE_FOLDER:
384 addLiveFolder(data);
385 break;
386 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400387 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700389 case REQUEST_PICK_APPWIDGET:
390 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700392 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400393 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700395 case REQUEST_PICK_WALLPAPER:
396 // We just wanted the activity result here so we can clear mWaitingForResult
397 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 }
Romain Guy18042c82009-11-06 11:44:55 -0800399 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
400 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
401 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700402 // Clean up the appWidgetId if we canceled
403 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
404 if (appWidgetId != -1) {
405 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 }
407 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 }
409
410 @Override
411 protected void onResume() {
Joe Onorato080d9b62009-11-02 12:01:11 -0500412 if (lastStartTime == 0) {
413 lastStartTime = SystemClock.uptimeMillis();
414 }
415
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 super.onResume();
417
418 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400419 mWorkspaceLoading = true;
420 mModel.startLoader(this, true);
421 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700423
Mike LeBeau736cf282009-07-02 17:46:59 -0700424 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
425 // onNewIntent), then close the search dialog if needed, because it probably
426 // came from the user pressing 'home' (rather than, for example, pressing 'back').
427 if (mIsNewIntent) {
428 // Post to a handler so that this happens after the search dialog tries to open
429 // itself again.
430 mWorkspace.post(new Runnable() {
431 public void run() {
432 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
433 ServiceManager.getService(Context.SEARCH_SERVICE));
434 try {
435 searchManagerService.stopSearch();
436 } catch (RemoteException e) {
437 e(LOG_TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700438 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700439 }
440 });
441 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700442
Mike LeBeau736cf282009-07-02 17:46:59 -0700443 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 }
445
446 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700447 protected void onPause() {
448 super.onPause();
Joe Onoratoff3862d2009-10-07 10:35:51 -0700449 if (mExitingBecauseOfLaunch) {
450 mExitingBecauseOfLaunch = false;
451 closeAllApps(false);
452 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800453 dismissPreview(mPreviousView);
454 dismissPreview(mNextView);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700455 }
Romain Guycbb89e42009-06-08 15:52:54 -0700456
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700457 @Override
458 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400459 // Flag the loader to stop early before switching
460 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700461
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700462 if (PROFILE_ROTATE) {
463 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
464 }
465 return null;
466 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700467
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468 private boolean acceptFilter() {
469 final InputMethodManager inputManager = (InputMethodManager)
470 getSystemService(Context.INPUT_METHOD_SERVICE);
471 return !inputManager.isFullscreenMode();
472 }
473
474 @Override
475 public boolean onKeyDown(int keyCode, KeyEvent event) {
476 boolean handled = super.onKeyDown(keyCode, event);
477 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
478 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
479 keyCode, event);
480 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700481 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700482 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700483 // showSearchDialog()
484 // If there are multiple keystrokes before the search dialog takes focus,
485 // onSearchRequested() will be called for every keystroke,
486 // but it is idempotent, so it's fine.
487 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 }
489 }
490
491 return handled;
492 }
493
Karl Rosaen138a0412009-04-23 19:00:21 -0700494 private String getTypedText() {
495 return mDefaultKeySsb.toString();
496 }
497
498 private void clearTypedText() {
499 mDefaultKeySsb.clear();
500 mDefaultKeySsb.clearSpans();
501 Selection.setSelection(mDefaultKeySsb, 0);
502 }
503
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 /**
505 * Restores the previous state, if it exists.
506 *
507 * @param savedState The previous state.
508 */
509 private void restoreState(Bundle savedState) {
510 if (savedState == null) {
511 return;
512 }
513
514 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
515 if (currentScreen > -1) {
516 mWorkspace.setCurrentScreen(currentScreen);
517 }
518
519 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
520 if (addScreen > -1) {
521 mAddItemCellInfo = new CellLayout.CellInfo();
522 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
523 addItemCellInfo.valid = true;
524 addItemCellInfo.screen = addScreen;
525 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
526 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
527 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
528 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
529 addItemCellInfo.findVacantCellsFromOccupied(
530 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
531 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
532 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
533 mRestoring = true;
534 }
535
536 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
537 if (renameFolder) {
538 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400539 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 mRestoring = true;
541 }
542 }
543
544 /**
545 * Finds all the views we need and configure them properly.
546 */
547 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400548 DragController dragController = mDragController;
549
Romain Guyb1b69f52009-08-10 15:10:15 -0700550 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400551 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552
Joe Onorato7c312c12009-08-13 21:36:53 -0700553 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700554 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700555 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700556 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa2009-10-19 17:03:36 -0700557 // Manage focusability manually since this thing is always visible
558 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700559
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
561 final Workspace workspace = mWorkspace;
562
Joe Onorato7c312c12009-08-13 21:36:53 -0700563 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
564 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565
Joe Onorato7c312c12009-08-13 21:36:53 -0700566 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700567 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700568 mHandleView.setOnClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800569
570 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
571 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
572
573 Drawable previous = mPreviousView.getDrawable();
574 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800575 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576
Romain Guy1fbc1c82009-11-09 20:43:08 -0800577 mPreviousView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800578 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800579
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400581 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583
584 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400585 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700586 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587
Joe Onorato00acb122009-08-04 16:04:30 -0400588 dragController.setDragScoller(workspace);
589 dragController.setDragListener(deleteZone);
590 dragController.setScrollView(dragLayer);
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) {
599 mWorkspace.scrollLeft();
600 }
601
602 @SuppressWarnings({"UnusedDeclaration"})
603 public void nextScreen(View v) {
604 mWorkspace.scrollRight();
605 }
606
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 /**
608 * Creates a view representing a shortcut.
609 *
610 * @param info The data structure describing the shortcut.
611 *
612 * @return A View inflated from R.layout.application.
613 */
614 View createShortcut(ApplicationInfo info) {
615 return createShortcut(R.layout.application,
616 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
617 }
618
619 /**
620 * Creates a view representing a shortcut inflated from the specified resource.
621 *
622 * @param layoutResId The id of the XML layout used to create the shortcut.
623 * @param parent The group the shortcut belongs to.
624 * @param info The data structure describing the shortcut.
625 *
626 * @return A View inflated from layoutResId.
627 */
628 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
629 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
630
Joe Onorato5162ea92009-09-03 09:39:42 -0700631 if (info.icon == null) {
632 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
633 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700635 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 info.filtered = true;
637 }
638
639 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
640 favorite.setText(info.title);
641 favorite.setTag(info);
642 favorite.setOnClickListener(this);
643
644 return favorite;
645 }
646
647 /**
648 * Add an application shortcut to the workspace.
649 *
650 * @param data The intent describing the application.
651 * @param cellInfo The position on screen where to create the shortcut.
652 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400653 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 cellInfo.screen = mWorkspace.getCurrentScreen();
655 if (!findSingleSlot(cellInfo)) return;
656
Romain Guy73b979d2009-06-09 12:57:21 -0700657 final ApplicationInfo info = infoFromApplicationIntent(context, data);
658 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400659 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700660 }
661 }
662
663 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 ComponentName component = data.getComponent();
665 PackageManager packageManager = context.getPackageManager();
666 ActivityInfo activityInfo = null;
667 try {
668 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
669 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700670 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
Romain Guycbb89e42009-06-08 15:52:54 -0700672
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 if (activityInfo != null) {
674 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700675
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 itemInfo.title = activityInfo.loadLabel(packageManager);
677 if (itemInfo.title == null) {
678 itemInfo.title = activityInfo.name;
679 }
Romain Guycbb89e42009-06-08 15:52:54 -0700680
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
682 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
683 itemInfo.icon = activityInfo.loadIcon(packageManager);
684 itemInfo.container = ItemInfo.NO_ID;
685
Romain Guy73b979d2009-06-09 12:57:21 -0700686 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 }
Romain Guy73b979d2009-06-09 12:57:21 -0700688
689 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 }
Romain Guycbb89e42009-06-08 15:52:54 -0700691
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 /**
693 * Add a shortcut to the workspace.
694 *
695 * @param data The intent describing the shortcut.
696 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400698 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 cellInfo.screen = mWorkspace.getCurrentScreen();
700 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
703
704 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400706 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
707 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 }
709 }
710
Romain Guycbb89e42009-06-08 15:52:54 -0700711
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700713 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700715 * @param data The intent describing the appWidgetId.
716 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400718 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700720 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700721
Romain Guy18042c82009-11-06 11:44:55 -0800722 if (LOGD) d(LOG_TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700723
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700724 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700725
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700726 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700728 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700729
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 // Try finding open space on Launcher screen
731 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800732 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
733 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
734 return;
735 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700737 // Build Launcher-specific widget info and save to database
738 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 launcherInfo.spanX = spans[0];
740 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700741
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 LauncherModel.addItemToDatabase(this, launcherInfo,
743 LauncherSettings.Favorites.CONTAINER_DESKTOP,
744 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
745
746 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700748
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700750 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700751
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700752 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700754
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400756 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 }
758 }
Romain Guycbb89e42009-06-08 15:52:54 -0700759
Joe Onorato9c1289c2009-08-17 11:03:03 -0400760 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
761 mDesktopItems.remove(launcherInfo);
762 launcherInfo.hostView = null;
763 }
764
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700765 public LauncherAppWidgetHost getAppWidgetHost() {
766 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 }
Romain Guycbb89e42009-06-08 15:52:54 -0700768
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 static ApplicationInfo addShortcut(Context context, Intent data,
770 CellLayout.CellInfo cellInfo, boolean notify) {
771
Romain Guy73b979d2009-06-09 12:57:21 -0700772 final ApplicationInfo info = infoFromShortcutIntent(context, data);
773 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
774 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
775
776 return info;
777 }
778
779 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
781 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
782 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
783
784 Drawable icon = null;
785 boolean filtered = false;
786 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700787 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788
789 if (bitmap != null) {
790 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
791 filtered = true;
792 customIcon = true;
793 } else {
794 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700795 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700797 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 final PackageManager packageManager = context.getPackageManager();
799 Resources resources = packageManager.getResourcesForApplication(
800 iconResource.packageName);
801 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
802 icon = resources.getDrawable(id);
803 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700804 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 }
806 }
807 }
808
809 if (icon == null) {
810 icon = context.getPackageManager().getDefaultActivityIcon();
811 }
812
813 final ApplicationInfo info = new ApplicationInfo();
814 info.icon = icon;
815 info.filtered = filtered;
816 info.title = name;
817 info.intent = intent;
818 info.customIcon = customIcon;
819 info.iconResource = iconResource;
820
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 return info;
822 }
823
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800824 void closeSystemDialogs() {
825 closeAllApps(false);
826 getWindow().closeAllPanels();
827
828 try {
829 dismissDialog(DIALOG_CREATE_SHORTCUT);
830 // Unlock the workspace if the dialog was showing
831 } catch (Exception e) {
832 // An exception is thrown if the dialog is not visible, which is fine
833 }
834
835 try {
836 dismissDialog(DIALOG_RENAME_FOLDER);
837 // Unlock the workspace if the dialog was showing
838 } catch (Exception e) {
839 // An exception is thrown if the dialog is not visible, which is fine
840 }
841 }
842
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 @Override
844 protected void onNewIntent(Intent intent) {
845 super.onNewIntent(intent);
846
847 // Close the menu
848 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800849 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700850
Joe Onorato742966b2009-10-07 10:26:17 -0700851 // Whatever we were doing is hereby canceled.
852 mWaitingForResult = false;
853
Mike LeBeau736cf282009-07-02 17:46:59 -0700854 // Set this flag so that onResume knows to close the search dialog if it's open,
855 // because this was a new intent (thus a press of 'home' or some such) rather than
856 // for example onResume being called when the user pressed the 'back' button.
857 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
860 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700861
Romain Guy94dabf12009-07-21 10:55:43 -0700862 if (!mWorkspace.isDefaultScreenShowing()) {
863 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700865
Joe Onorato7bb17492009-09-24 17:51:01 -0700866 closeAllApps(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700867
868 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 if (v != null && v.getWindowToken() != null) {
870 InputMethodManager imm = (InputMethodManager)getSystemService(
871 INPUT_METHOD_SERVICE);
872 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
873 }
874 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700875 closeAllApps(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 }
877 }
878 }
879
880 @Override
881 protected void onRestoreInstanceState(Bundle savedInstanceState) {
882 // Do not call super here
883 mSavedInstanceState = savedInstanceState;
884 }
885
886 @Override
887 protected void onSaveInstanceState(Bundle outState) {
888 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
889
890 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
891 if (folders.size() > 0) {
892 final int count = folders.size();
893 long[] ids = new long[count];
894 for (int i = 0; i < count; i++) {
895 final FolderInfo info = folders.get(i).getInfo();
896 ids[i] = info.id;
897 }
898 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
899 } else {
900 super.onSaveInstanceState(outState);
901 }
902
Romain Guy3cf604f2009-06-16 13:12:53 -0700903 final boolean isConfigurationChange = getChangingConfigurations() != 0;
904
Romain Guy5a941392009-04-28 15:18:25 -0700905 // When the drawer is opened and we are saving the state because of a
906 // configuration change
Joe Onoratofb0ca672009-09-14 17:55:46 -0400907 // TODO should not do this if the drawer is currently closing.
908 if (isAllAppsVisible() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911
912 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
913 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
914 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
915
916 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
917 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
918 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
919 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
920 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
921 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
922 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
923 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
924 layout.getOccupiedCells());
925 }
926
927 if (mFolderInfo != null && mWaitingForResult) {
928 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
929 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
930 }
931 }
932
933 @Override
934 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700936
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700938 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700940 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 }
942
943 TextKeyListener.getInstance().release();
944
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947 unbindDesktopItems();
948 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800949
950 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800951
952 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800953 dismissPreview(mNextView);
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800954
955 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 }
957
958 @Override
959 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700960 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 super.startActivityForResult(intent, requestCode);
962 }
963
964 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700965 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700967
Joe Onorato7bb17492009-09-24 17:51:01 -0700968 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700969
Karl Rosaen138a0412009-04-23 19:00:21 -0700970 // Slide the search widget to the top, if it's on the current screen,
971 // otherwise show the search dialog immediately.
972 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
973 if (searchWidget == null) {
974 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
975 } else {
976 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
977 // show the currently typed text in the search widget while sliding
978 searchWidget.setQuery(getTypedText());
979 }
980 }
Romain Guycbb89e42009-06-08 15:52:54 -0700981
Karl Rosaen138a0412009-04-23 19:00:21 -0700982 /**
983 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700984 *
985 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700986 */
Romain Guycbb89e42009-06-08 15:52:54 -0700987 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700988 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700989
Karl Rosaen138a0412009-04-23 19:00:21 -0700990 if (initialQuery == null) {
991 // Use any text typed in the launcher as the initial query
992 initialQuery = getTypedText();
993 clearTypedText();
994 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 if (appSearchData == null) {
996 appSearchData = new Bundle();
997 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
998 }
Romain Guycbb89e42009-06-08 15:52:54 -0700999
Karl Rosaen138a0412009-04-23 19:00:21 -07001000 final SearchManager searchManager =
1001 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1002
1003 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1004 if (searchWidget != null) {
1005 // This gets called when the user leaves the search dialog to go back to
1006 // the Launcher.
1007 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1008 public void onCancel() {
1009 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001010 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001011 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001012 });
1013 }
Romain Guycbb89e42009-06-08 15:52:54 -07001014
Karl Rosaen138a0412009-04-23 19:00:21 -07001015 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001016 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 }
1018
Karl Rosaen138a0412009-04-23 19:00:21 -07001019 /**
1020 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001021 */
Romain Guy5a941392009-04-28 15:18:25 -07001022 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001023 // Close search dialog
1024 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001025 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001026 // Restore search widget to its normal position
1027 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1028 if (searchWidget != null) {
1029 searchWidget.stopSearch(false);
1030 }
1031 }
Romain Guycbb89e42009-06-08 15:52:54 -07001032
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 @Override
1034 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001035 if (isWorkspaceLocked()) {
1036 return false;
1037 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038
1039 super.onCreateOptionsMenu(menu);
1040 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1041 .setIcon(android.R.drawable.ic_menu_add)
1042 .setAlphabeticShortcut('A');
1043 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1044 .setIcon(android.R.drawable.ic_menu_gallery)
1045 .setAlphabeticShortcut('W');
1046 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1047 .setIcon(android.R.drawable.ic_search_category_default)
1048 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1049 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1050 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1051 .setAlphabeticShortcut('N');
1052
1053 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001054 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1055 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056
1057 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1058 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1059 .setIntent(settings);
1060
1061 return true;
1062 }
1063
1064 @Override
1065 public boolean onPrepareOptionsMenu(Menu menu) {
1066 super.onPrepareOptionsMenu(menu);
1067
1068 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1069 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1070
1071 return true;
1072 }
1073
1074 @Override
1075 public boolean onOptionsItemSelected(MenuItem item) {
1076 switch (item.getItemId()) {
1077 case MENU_ADD:
1078 addItems();
1079 return true;
1080 case MENU_WALLPAPER_SETTINGS:
1081 startWallpaper();
1082 return true;
1083 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001084 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 return true;
1086 case MENU_NOTIFICATIONS:
1087 showNotifications();
1088 return true;
1089 }
1090
1091 return super.onOptionsItemSelected(item);
1092 }
Romain Guycbb89e42009-06-08 15:52:54 -07001093
Karl Rosaen138a0412009-04-23 19:00:21 -07001094 /**
1095 * Indicates that we want global search for this activity by setting the globalSearch
1096 * argument for {@link #startSearch} to true.
1097 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001099 @Override
1100 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001101 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001102 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001103 }
1104
Joe Onorato9c1289c2009-08-17 11:03:03 -04001105 public boolean isWorkspaceLocked() {
1106 return mWorkspaceLoading || mWaitingForResult;
1107 }
1108
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001110 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 showAddDialog(mMenuAddInfo);
1112 }
1113
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001114 void addAppWidget(Intent data) {
1115 // TODO: catch bad widget exception when sent
1116 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001118 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1119 if (SEARCH_WIDGET.equals(customWidget)) {
1120 // We don't need this any more, since this isn't a real app widget.
1121 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1122 // add the search widget
1123 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001125 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1126
1127 if (appWidget.configure != null) {
1128 // Launch over to configure widget, if needed
1129 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1130 intent.setComponent(appWidget.configure);
1131 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1132
1133 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1134 } else {
1135 // Otherwise just add it
1136 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 }
1139 }
Romain Guycbb89e42009-06-08 15:52:54 -07001140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 void addSearch() {
1142 final Widget info = Widget.makeSearch();
1143 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001144
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 final int[] xy = mCellCoordinates;
1146 final int spanX = info.spanX;
1147 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001148
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1152 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001153
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 final View view = mInflater.inflate(info.layoutResource, null);
1155 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001156 Search search = (Search) view.findViewById(R.id.widget_search);
1157 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001158
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1160 }
1161
Romain Guy73b979d2009-06-09 12:57:21 -07001162 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001163 // Handle case where user selected "Applications"
1164 String applicationName = getResources().getString(R.string.group_applications);
1165 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001166
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001167 if (applicationName != null && applicationName.equals(shortcutName)) {
1168 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1169 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001170
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001171 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1172 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001173 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001174 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001175 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001176 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 }
1178
1179 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001180 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001181 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001182 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001183
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001184 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001185 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001186 } else {
1187 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1188 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 }
1190
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 UserFolderInfo folderInfo = new UserFolderInfo();
1193 folderInfo.title = getText(R.string.folder_name);
1194
1195 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1196 cellInfo.screen = mWorkspace.getCurrentScreen();
1197 if (!findSingleSlot(cellInfo)) return;
1198
1199 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 LauncherModel.addItemToDatabase(this, folderInfo,
1201 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001203 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204
1205 // Create the view
1206 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1207 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1208 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001209 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 }
Romain Guycbb89e42009-06-08 15:52:54 -07001211
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 void removeFolder(FolderInfo folder) {
1213 mFolders.remove(folder.id);
1214 }
1215
1216 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 cellInfo.screen = mWorkspace.getCurrentScreen();
1218 if (!findSingleSlot(cellInfo)) return;
1219
1220 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1221
1222 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1224 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001225 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1226 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
1228 }
1229
1230 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1231 CellLayout.CellInfo cellInfo, boolean notify) {
1232
1233 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1234 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1235
1236 Drawable icon = null;
1237 boolean filtered = false;
1238 Intent.ShortcutIconResource iconResource = null;
1239
1240 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1241 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1242 try {
1243 iconResource = (Intent.ShortcutIconResource) extra;
1244 final PackageManager packageManager = context.getPackageManager();
1245 Resources resources = packageManager.getResourcesForApplication(
1246 iconResource.packageName);
1247 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1248 icon = resources.getDrawable(id);
1249 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001250 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 }
1252 }
1253
1254 if (icon == null) {
1255 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1256 }
1257
1258 final LiveFolderInfo info = new LiveFolderInfo();
1259 info.icon = icon;
1260 info.filtered = filtered;
1261 info.title = name;
1262 info.iconResource = iconResource;
1263 info.uri = data.getData();
1264 info.baseIntent = baseIntent;
1265 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1266 LiveFolders.DISPLAY_MODE_GRID);
1267
1268 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1269 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001270 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271
1272 return info;
1273 }
1274
1275 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1276 final int[] xy = new int[2];
1277 if (findSlot(cellInfo, xy, 1, 1)) {
1278 cellInfo.cellX = xy[0];
1279 cellInfo.cellY = xy[1];
1280 return true;
1281 }
1282 return false;
1283 }
1284
1285 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1286 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1287 boolean[] occupied = mSavedState != null ?
1288 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1289 cellInfo = mWorkspace.findAllVacantCells(occupied);
1290 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1291 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1292 return false;
1293 }
1294 }
1295 return true;
1296 }
1297
1298 private void showNotifications() {
1299 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1300 if (statusBar != null) {
1301 statusBar.expand();
1302 }
1303 }
1304
1305 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001306 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001308 Intent chooser = Intent.createChooser(pickWallpaper,
1309 getText(R.string.chooser_wallpaper));
1310 WallpaperManager wm = (WallpaperManager)
1311 getSystemService(Context.WALLPAPER_SERVICE);
1312 WallpaperInfo wi = wm.getWallpaperInfo();
1313 if (wi != null && wi.getSettingsActivity() != null) {
1314 LabeledIntent li = new LabeledIntent(getPackageName(),
1315 R.string.configure_wallpaper, 0);
1316 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1317 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1318 }
Mike Clerona0618e42009-10-22 13:55:21 -07001319 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 }
1321
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001322 /**
1323 * Registers various content observers. The current implementation registers
1324 * only a favorites observer to keep track of the favorites applications.
1325 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001326 private void registerContentObservers() {
1327 ContentResolver resolver = getContentResolver();
1328 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1329 true, mWidgetObserver);
1330 }
1331
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 @Override
1333 public boolean dispatchKeyEvent(KeyEvent event) {
1334 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1335 switch (event.getKeyCode()) {
1336 case KeyEvent.KEYCODE_BACK:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 return true;
1338 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001339 return true;
1340 }
1341 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1342 switch (event.getKeyCode()) {
1343 case KeyEvent.KEYCODE_BACK:
1344 if (!event.isCanceled()) {
1345 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001346 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001347 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001348 } else {
1349 closeFolder();
1350 }
Romain Guya6abce82009-11-10 02:54:41 -08001351 dismissPreview(mPreviousView);
1352 dismissPreview(mNextView);
Dianne Hackborn67800862009-07-24 17:15:20 -07001353 }
Dianne Hackborn67800862009-07-24 17:15:20 -07001354 return true;
1355 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 return true;
1357 }
1358 }
1359
1360 return super.dispatchKeyEvent(event);
1361 }
1362
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 private void closeFolder() {
1364 Folder folder = mWorkspace.getOpenFolder();
1365 if (folder != null) {
1366 closeFolder(folder);
1367 }
1368 }
1369
1370 void closeFolder(Folder folder) {
1371 folder.getInfo().opened = false;
1372 ViewGroup parent = (ViewGroup) folder.getParent();
1373 if (parent != null) {
1374 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001375 if (folder instanceof DropTarget) {
1376 // Live folders aren't DropTargets.
1377 mDragController.removeDropTarget((DropTarget)folder);
1378 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 }
1380 folder.onClose();
1381 }
1382
1383 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001384 * Re-listen when widgets are reset.
1385 */
1386 private void onAppWidgetReset() {
1387 mAppWidgetHost.startListening();
1388 }
1389
1390 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001391 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1392 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001394 private void unbindDesktopItems() {
1395 for (ItemInfo item: mDesktopItems) {
1396 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 }
1398 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001399
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 /**
1401 * Launches the intent referred by the clicked shortcut.
1402 *
1403 * @param v The view representing the clicked shortcut.
1404 */
1405 public void onClick(View v) {
1406 Object tag = v.getTag();
1407 if (tag instanceof ApplicationInfo) {
1408 // Open shortcut
1409 final Intent intent = ((ApplicationInfo) tag).intent;
1410 startActivitySafely(intent);
Joe Onoratoff3862d2009-10-07 10:35:51 -07001411 mExitingBecauseOfLaunch = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 } else if (tag instanceof FolderInfo) {
1413 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001414 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001415 Log.d(TAG, "onClick");
1416 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001417 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001418 } else {
Jason Samsfd22dac2009-09-20 17:24:16 -07001419 showAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -07001420 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 }
1422 }
1423
1424 void startActivitySafely(Intent intent) {
1425 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1426 try {
1427 startActivity(intent);
1428 } catch (ActivityNotFoundException e) {
1429 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1430 } catch (SecurityException e) {
1431 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001432 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1434 "or use the exported attribute for this activity.", e);
1435 }
1436 }
1437
1438 private void handleFolderClick(FolderInfo folderInfo) {
1439 if (!folderInfo.opened) {
1440 // Close any open folder
1441 closeFolder();
1442 // Open the requested folder
1443 openFolder(folderInfo);
1444 } else {
1445 // Find the open folder...
1446 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1447 int folderScreen;
1448 if (openFolder != null) {
1449 folderScreen = mWorkspace.getScreenForView(openFolder);
1450 // .. and close it
1451 closeFolder(openFolder);
1452 if (folderScreen != mWorkspace.getCurrentScreen()) {
1453 // Close any folder open on the current screen
1454 closeFolder();
1455 // Pull the folder onto this screen
1456 openFolder(folderInfo);
1457 }
1458 }
1459 }
1460 }
1461
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 /**
1463 * Opens the user fodler described by the specified tag. The opening of the folder
1464 * is animated relative to the specified View. If the View is null, no animation
1465 * is played.
1466 *
1467 * @param folderInfo The FolderInfo describing the folder to open.
1468 */
1469 private void openFolder(FolderInfo folderInfo) {
1470 Folder openFolder;
1471
1472 if (folderInfo instanceof UserFolderInfo) {
1473 openFolder = UserFolder.fromXml(this);
1474 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001475 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 } else {
1477 return;
1478 }
1479
Joe Onorato00acb122009-08-04 16:04:30 -04001480 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 openFolder.setLauncher(this);
1482
1483 openFolder.bind(folderInfo);
1484 folderInfo.opened = true;
1485
1486 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1487 openFolder.onOpen();
1488 }
1489
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001491 switch (v.getId()) {
1492 case R.id.previous_screen:
1493 showPreviousPreview(v);
1494 return true;
1495 case R.id.next_screen:
1496 showNextPreview(v);
1497 return true;
1498 }
1499
Joe Onorato9c1289c2009-08-17 11:03:03 -04001500 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 return false;
1502 }
1503
1504 if (!(v instanceof CellLayout)) {
1505 v = (View) v.getParent();
1506 }
1507
1508 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1509
1510 // This happens when long clicking an item with the dpad/trackball
1511 if (cellInfo == null) {
1512 return true;
1513 }
1514
1515 if (mWorkspace.allowLongPress()) {
1516 if (cellInfo.cell == null) {
1517 if (cellInfo.valid) {
1518 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001519 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 showAddDialog(cellInfo);
1521 }
1522 } else {
1523 if (!(cellInfo.cell instanceof Folder)) {
1524 // User long pressed on an item
1525 mWorkspace.startDrag(cellInfo);
1526 }
1527 }
1528 }
1529 return true;
1530 }
1531
Romain Guye6b8e2f2009-11-10 11:56:55 -08001532 @SuppressWarnings({"unchecked"})
Romain Guy1fbc1c82009-11-09 20:43:08 -08001533 private void dismissPreview(View v) {
1534 PopupWindow window = (PopupWindow) v.getTag();
1535 if (window != null) {
Romain Guya6abce82009-11-10 02:54:41 -08001536 window.setOnDismissListener(null);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001537 window.dismiss();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001538
1539 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1540 int count = group.getChildCount();
1541 for (int i = 0; i < count; i++) {
1542 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1543 }
1544
1545 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1546 for (Bitmap bitmap : bitmaps) bitmap.recycle();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001547
1548 v.setTag(R.id.workspace, null);
1549 v.setTag(R.id.icon, null);
1550 }
1551 v.setTag(null);
1552 }
1553
1554 private void showPreviousPreview(View anchor) {
1555 int current = mWorkspace.getCurrentScreen();
1556 if (current <= 0) return;
1557
Romain Guya6abce82009-11-10 02:54:41 -08001558 showPreviews(anchor, 0, current);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001559 }
1560
1561 private void showNextPreview(View anchor) {
1562 int current = mWorkspace.getCurrentScreen();
1563 if (current >= mWorkspace.getChildCount() - 1) return;
1564
Romain Guya6abce82009-11-10 02:54:41 -08001565 showPreviews(anchor, current + 1, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001566 }
1567
Romain Guya6abce82009-11-10 02:54:41 -08001568 @Override
1569 public void onWindowFocusChanged(boolean hasFocus) {
1570 super.onWindowFocusChanged(hasFocus);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001571
Romain Guya6abce82009-11-10 02:54:41 -08001572 if (!hasFocus) {
1573 dismissPreview(mPreviousView);
1574 dismissPreview(mNextView);
1575 }
1576 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001577
Romain Guya6abce82009-11-10 02:54:41 -08001578 private void showPreviews(final View anchor, int start, int end) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001579 Drawable d = getResources().getDrawable(R.drawable.preview_popup);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001580
Romain Guya6abce82009-11-10 02:54:41 -08001581 Workspace workspace = mWorkspace;
1582 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
1583
1584 float max = workspace.getChildCount() - 1;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001585
1586 Rect r = new Rect();
1587 d.getPadding(r);
1588 int extraW = (int) ((r.left + r.right) * max);
1589 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001590
1591 int aW = cell.getWidth() - extraW;
1592 float w = aW / max;
1593
1594 int width = cell.getWidth();
1595 int height = cell.getHeight();
1596 int x = cell.getLeftPadding();
1597 int y = cell.getTopPadding();
1598 width -= (x + cell.getRightPadding());
1599 height -= (y + cell.getBottomPadding());
1600
1601 float scale = w / width;
1602
1603 int count = end - start;
1604
1605 final float sWidth = width * scale;
1606 float sHeight = height * scale;
1607
Romain Guye6b8e2f2009-11-10 11:56:55 -08001608 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001609
Romain Guye6b8e2f2009-11-10 11:56:55 -08001610 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1611 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001612
1613 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001614 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001615 cell = (CellLayout) workspace.getChildAt(i);
1616
Romain Guye6b8e2f2009-11-10 11:56:55 -08001617 Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
1618 Bitmap.Config.ARGB_8888);
1619
1620 Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001621 c.scale(scale, scale);
1622 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1623 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001624
Romain Guye6b8e2f2009-11-10 11:56:55 -08001625 image.setBackgroundDrawable(d);
1626 image.setImageBitmap(bitmap);
1627 image.setTag(i);
1628 image.setOnClickListener(handler);
1629 bitmaps.add(bitmap);
1630
1631 preview.addView(image,
1632 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Romain Guya6abce82009-11-10 02:54:41 -08001633 }
Romain Guye6b8e2f2009-11-10 11:56:55 -08001634
1635 PopupWindow p = new PopupWindow(this);
1636 p.setContentView(preview);
1637 p.setWidth((int) (sWidth * count + extraW));
1638 p.setHeight((int) (sHeight + extraH));
1639 p.setAnimationStyle(R.style.AnimationPreview);
1640 p.setOutsideTouchable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001641 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001642 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001643
Romain Guye6b8e2f2009-11-10 11:56:55 -08001644 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1645 public void onDismiss() {
1646 dismissPreview(anchor);
1647 }
1648 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001649
1650 anchor.setTag(p);
1651 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001652 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001653 }
1654
Romain Guye6b8e2f2009-11-10 11:56:55 -08001655 class PreviewTouchHandler implements View.OnClickListener {
Romain Guya6abce82009-11-10 02:54:41 -08001656 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001657
Romain Guye6b8e2f2009-11-10 11:56:55 -08001658 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001659 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001660 }
1661
1662 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001663 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guya6abce82009-11-10 02:54:41 -08001664 dismissPreview(mAnchor);
1665 }
1666 }
1667
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001668 View getDrawerHandle() {
1669 return mHandleView;
1670 }
1671
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 Workspace getWorkspace() {
1673 return mWorkspace;
1674 }
1675
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 @Override
1677 protected Dialog onCreateDialog(int id) {
1678 switch (id) {
1679 case DIALOG_CREATE_SHORTCUT:
1680 return new CreateShortcut().createDialog();
1681 case DIALOG_RENAME_FOLDER:
1682 return new RenameFolder().createDialog();
1683 }
1684
1685 return super.onCreateDialog(id);
1686 }
1687
1688 @Override
1689 protected void onPrepareDialog(int id, Dialog dialog) {
1690 switch (id) {
1691 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 break;
1693 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001694 if (mFolderInfo != null) {
1695 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1696 final CharSequence text = mFolderInfo.title;
1697 input.setText(text);
1698 input.setSelection(0, text.length());
1699 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 break;
1701 }
1702 }
1703
1704 void showRenameDialog(FolderInfo info) {
1705 mFolderInfo = info;
1706 mWaitingForResult = true;
1707 showDialog(DIALOG_RENAME_FOLDER);
1708 }
1709
1710 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1711 mAddItemCellInfo = cellInfo;
1712 mWaitingForResult = true;
1713 showDialog(DIALOG_CREATE_SHORTCUT);
1714 }
1715
Romain Guy73b979d2009-06-09 12:57:21 -07001716 private void pickShortcut(int requestCode, int title) {
1717 Bundle bundle = new Bundle();
1718
1719 ArrayList<String> shortcutNames = new ArrayList<String>();
1720 shortcutNames.add(getString(R.string.group_applications));
1721 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1722
1723 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1724 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1725 R.drawable.ic_launcher_application));
1726 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1727
1728 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1729 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1730 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1731 pickIntent.putExtras(bundle);
1732
1733 startActivityForResult(pickIntent, requestCode);
1734 }
1735
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 private class RenameFolder {
1737 private EditText mInput;
1738
1739 Dialog createDialog() {
1740 mWaitingForResult = true;
1741 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1742 mInput = (EditText) layout.findViewById(R.id.folder_name);
1743
1744 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1745 builder.setIcon(0);
1746 builder.setTitle(getString(R.string.rename_folder_title));
1747 builder.setCancelable(true);
1748 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1749 public void onCancel(DialogInterface dialog) {
1750 cleanup();
1751 }
1752 });
1753 builder.setNegativeButton(getString(R.string.cancel_action),
1754 new Dialog.OnClickListener() {
1755 public void onClick(DialogInterface dialog, int which) {
1756 cleanup();
1757 }
1758 }
1759 );
1760 builder.setPositiveButton(getString(R.string.rename_action),
1761 new Dialog.OnClickListener() {
1762 public void onClick(DialogInterface dialog, int which) {
1763 changeFolderName();
1764 }
1765 }
1766 );
1767 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001768
1769 final AlertDialog dialog = builder.create();
1770 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1771 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001772 mInput.requestFocus();
1773 InputMethodManager inputManager = (InputMethodManager)
1774 getSystemService(Context.INPUT_METHOD_SERVICE);
1775 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001776 }
1777 });
1778
1779 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 }
1781
1782 private void changeFolderName() {
1783 final String name = mInput.getText().toString();
1784 if (!TextUtils.isEmpty(name)) {
1785 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001786 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 mFolderInfo.title = name;
1788 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1789
Joe Onorato9c1289c2009-08-17 11:03:03 -04001790 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001791 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001792 mModel.setWorkspaceDirty();
1793 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 } else {
1795 final FolderIcon folderIcon = (FolderIcon)
1796 mWorkspace.getViewForTag(mFolderInfo);
1797 if (folderIcon != null) {
1798 folderIcon.setText(name);
1799 getWorkspace().requestLayout();
1800 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001801 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001802 mModel.setWorkspaceDirty();
1803 mWorkspaceLoading = true;
1804 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 }
1806 }
1807 }
1808 cleanup();
1809 }
1810
1811 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 dismissDialog(DIALOG_RENAME_FOLDER);
1813 mWaitingForResult = false;
1814 mFolderInfo = null;
1815 }
1816 }
1817
Joe Onoratofb0ca672009-09-14 17:55:46 -04001818 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001819 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001820 }
1821
Jason Samsfd22dac2009-09-20 17:24:16 -07001822 void showAllApps() {
Jason Sams12c14a82009-10-06 14:33:15 -07001823 mAllAppsGrid.zoom(1.0f);
Joe Onorato7bb17492009-09-24 17:51:01 -07001824 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001825
Mike Cleronb6082fa2009-10-19 17:03:36 -07001826 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001827 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa2009-10-19 17:03:36 -07001828
Joe Onorato7c312c12009-08-13 21:36:53 -07001829 // TODO: fade these two too
1830 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001831 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001832 }
1833
Joe Onorato7bb17492009-09-24 17:51:01 -07001834 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001835 if (mAllAppsGrid.isVisible()) {
Jason Sams12c14a82009-10-06 14:33:15 -07001836 mAllAppsGrid.zoom(0.0f);
Mike Cleronb6082fa2009-10-19 17:03:36 -07001837 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001838 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato7c312c12009-08-13 21:36:53 -07001839
Mike Cleronb6082fa2009-10-19 17:03:36 -07001840
Joe Onorato7c312c12009-08-13 21:36:53 -07001841 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001842 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001843 mDeleteZone.setVisibility(View.VISIBLE);
1844 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001845 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001846 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001847 }
1848
Joe Onorato7c312c12009-08-13 21:36:53 -07001849 void lockAllApps() {
1850 // TODO
1851 }
1852
1853 void unlockAllApps() {
1854 // TODO
1855 }
1856
Joe Onorato7404ee42009-07-31 11:54:44 -07001857 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 * Displays the shortcut creation dialog and launches, if necessary, the
1859 * appropriate activity.
1860 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001861 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001862 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1863 DialogInterface.OnShowListener {
1864
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001866
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 Dialog createDialog() {
1868 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001869
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001871
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1873 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001874 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001875
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 builder.setInverseBackgroundForced(true);
1877
1878 AlertDialog dialog = builder.create();
1879 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001880 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001881 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 return dialog;
1884 }
1885
1886 public void onCancel(DialogInterface dialog) {
1887 mWaitingForResult = false;
1888 cleanup();
1889 }
1890
Romain Guycbb89e42009-06-08 15:52:54 -07001891 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001892 }
1893
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 dismissDialog(DIALOG_CREATE_SHORTCUT);
1896 }
1897
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001898 /**
1899 * Handle the action clicked in the "Add to home" dialog.
1900 */
1901 public void onClick(DialogInterface dialog, int which) {
1902 Resources res = getResources();
1903 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001904
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001905 switch (which) {
1906 case AddAdapter.ITEM_SHORTCUT: {
1907 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001908 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001909 break;
1910 }
Romain Guycbb89e42009-06-08 15:52:54 -07001911
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001912 case AddAdapter.ITEM_APPWIDGET: {
1913 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001914
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001915 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1916 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1917 // add the search widget
1918 ArrayList<AppWidgetProviderInfo> customInfo =
1919 new ArrayList<AppWidgetProviderInfo>();
1920 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1921 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1922 info.label = getString(R.string.group_search);
1923 info.icon = R.drawable.ic_search_widget;
1924 customInfo.add(info);
1925 pickIntent.putParcelableArrayListExtra(
1926 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1927 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1928 Bundle b = new Bundle();
1929 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1930 customExtras.add(b);
1931 pickIntent.putParcelableArrayListExtra(
1932 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1933 // start the pick activity
1934 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1935 break;
1936 }
Romain Guycbb89e42009-06-08 15:52:54 -07001937
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001938 case AddAdapter.ITEM_LIVE_FOLDER: {
1939 // Insert extra item to handle inserting folder
1940 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001941
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001942 ArrayList<String> shortcutNames = new ArrayList<String>();
1943 shortcutNames.add(res.getString(R.string.group_folder));
1944 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001945
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001946 ArrayList<ShortcutIconResource> shortcutIcons =
1947 new ArrayList<ShortcutIconResource>();
1948 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1949 R.drawable.ic_launcher_folder));
1950 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1951
1952 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1953 pickIntent.putExtra(Intent.EXTRA_INTENT,
1954 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1955 pickIntent.putExtra(Intent.EXTRA_TITLE,
1956 getText(R.string.title_select_live_folder));
1957 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001958
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001959 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1960 break;
1961 }
1962
1963 case AddAdapter.ITEM_WALLPAPER: {
1964 startWallpaper();
1965 break;
1966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 }
1968 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001969
1970 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973
1974 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001975 * Receives notifications when applications are added/removed.
1976 */
1977 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1978 @Override
1979 public void onReceive(Context context, Intent intent) {
1980 Log.d(TAG, "CloseSystemDialogsIntentReceiver.onReceiver intent=" + intent);
1981 closeSystemDialogs();
1982 }
1983 }
1984
1985 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001986 * Receives notifications whenever the appwidgets are reset.
1987 */
1988 private class AppWidgetResetObserver extends ContentObserver {
1989 public AppWidgetResetObserver() {
1990 super(new Handler());
1991 }
1992
1993 @Override
1994 public void onChange(boolean selfChange) {
1995 onAppWidgetReset();
1996 }
1997 }
1998
1999 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002000 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002002 public int getCurrentWorkspaceScreen() {
2003 return mWorkspace.getCurrentScreen();
2004 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002005
Joe Onorato9c1289c2009-08-17 11:03:03 -04002006 /**
2007 * Refreshes the shortcuts shown on the workspace.
2008 *
2009 * Implementation of the method from LauncherModel.Callbacks.
2010 */
2011 public void startBinding() {
2012 final Workspace workspace = mWorkspace;
2013 int count = workspace.getChildCount();
2014 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002015 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002016 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2017 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002018
Joe Onorato9c1289c2009-08-17 11:03:03 -04002019 if (DEBUG_USER_INTERFACE) {
2020 android.widget.Button finishButton = new android.widget.Button(this);
2021 finishButton.setText("Finish");
2022 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2023
2024 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2025 public void onClick(View v) {
2026 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002027 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002028 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 }
2030 }
2031
2032 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002033 * Bind the items start-end from the list.
2034 *
2035 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002037 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2038
2039 final Workspace workspace = mWorkspace;
2040
2041 for (int i=start; i<end; i++) {
2042 final ItemInfo item = shortcuts.get(i);
2043 mDesktopItems.add(item);
2044 switch (item.itemType) {
2045 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2046 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2047 final View shortcut = createShortcut((ApplicationInfo) item);
2048 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2049 false);
2050 break;
2051 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2052 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2053 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2054 (UserFolderInfo) item);
2055 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2056 false);
2057 break;
2058 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2059 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2060 R.layout.live_folder_icon, this,
2061 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2062 (LiveFolderInfo) item);
2063 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2064 false);
2065 break;
2066 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
2067 final int screen = workspace.getCurrentScreen();
2068 final View view = mInflater.inflate(R.layout.widget_search,
2069 (ViewGroup) workspace.getChildAt(screen), false);
2070
2071 Search search = (Search) view.findViewById(R.id.widget_search);
2072 search.setLauncher(this);
2073
2074 final Widget widget = (Widget) item;
2075 view.setTag(widget);
2076
2077 workspace.addWidget(view, widget, false);
2078 break;
2079 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 }
2081
Joe Onorato9c1289c2009-08-17 11:03:03 -04002082 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 }
2084
Joe Onorato9c1289c2009-08-17 11:03:03 -04002085 /**
2086 * Implementation of the method from LauncherModel.Callbacks.
2087 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002088 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2089 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002090 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002092
2093 /**
2094 * Add the views for a widget to the workspace.
2095 *
2096 * Implementation of the method from LauncherModel.Callbacks.
2097 */
2098 public void bindAppWidget(LauncherAppWidgetInfo item) {
2099 final Workspace workspace = mWorkspace;
2100
2101 final int appWidgetId = item.appWidgetId;
2102 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2103 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2104
Joe Onorato9c1289c2009-08-17 11:03:03 -04002105 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2106 item.hostView.setTag(item);
2107
2108 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2109 item.cellY, item.spanX, item.spanY, false);
2110
2111 workspace.requestLayout();
2112
2113 mDesktopItems.add(item);
2114 }
2115
2116 /**
2117 * Callback saying that there aren't any more items to bind.
2118 *
2119 * Implementation of the method from LauncherModel.Callbacks.
2120 */
2121 public void finishBindingItems() {
2122 if (mSavedState != null) {
2123 if (!mWorkspace.hasFocus()) {
2124 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2125 }
2126
2127 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2128 if (userFolders != null) {
2129 for (long folderId : userFolders) {
2130 final FolderInfo info = mFolders.get(folderId);
2131 if (info != null) {
2132 openFolder(info);
2133 }
2134 }
2135 final Folder openFolder = mWorkspace.getOpenFolder();
2136 if (openFolder != null) {
2137 openFolder.requestFocus();
2138 }
2139 }
2140
2141 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
2142 if (allApps) {
Jason Samsfd22dac2009-09-20 17:24:16 -07002143 showAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002144 }
2145
2146 mSavedState = null;
2147 }
2148
2149 if (mSavedInstanceState != null) {
2150 super.onRestoreInstanceState(mSavedInstanceState);
2151 mSavedInstanceState = null;
2152 }
2153
Joe Onorato9c1289c2009-08-17 11:03:03 -04002154 Log.d(TAG, "finishBindingItems done");
2155 mWorkspaceLoading = false;
2156 }
2157
2158 /**
2159 * Add the icons for all apps.
2160 *
2161 * Implementation of the method from LauncherModel.Callbacks.
2162 */
2163 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002164 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002165 }
2166
2167 /**
2168 * A package was installed.
2169 *
2170 * Implementation of the method from LauncherModel.Callbacks.
2171 */
2172 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2173 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002174 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002175 }
2176
2177 /**
2178 * A package was updated.
2179 *
2180 * Implementation of the method from LauncherModel.Callbacks.
2181 */
2182 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2183 removeDialog(DIALOG_CREATE_SHORTCUT);
2184 mWorkspace.updateShortcutsForPackage(packageName);
2185 }
2186
2187 /**
2188 * A package was uninstalled.
2189 *
2190 * Implementation of the method from LauncherModel.Callbacks.
2191 */
2192 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2193 removeDialog(DIALOG_CREATE_SHORTCUT);
2194 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002195 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197}