blob: ce3d5a71a24ee8a8a85eb461f8027da5280c5d88 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
196 private Bundle mSavedInstanceState;
197
Joe Onorato9c1289c2009-08-17 11:03:03 -0400198 private LauncherModel mModel;
199
Romain Guy84f296c2009-11-04 15:00:44 -0800200 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
201 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700202
Joe Onorato080d9b62009-11-02 12:01:11 -0500203 public static long lastStartTime;
Romain Guy1fbc1c82009-11-09 20:43:08 -0800204 private ImageView mPreviousView;
205 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 @Override
208 protected void onCreate(Bundle savedInstanceState) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500209 lastStartTime = SystemClock.uptimeMillis();
210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700212
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400214 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700216
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800217 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
218 registerReceiver(mCloseSystemDialogsReceiver, filter);
219
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700220 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700221 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
222 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700223
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 if (PROFILE_STARTUP) {
225 android.os.Debug.startMethodTracing("/sdcard/launcher");
226 }
227
228 checkForLocaleChange();
229 setWallpaperDimension();
230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 setContentView(R.layout.launcher);
232 setupViews();
233
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800234 registerContentObservers();
235
Joe Onorato7c312c12009-08-13 21:36:53 -0700236 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700237
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238 mSavedState = savedInstanceState;
239 restoreState(mSavedState);
240
241 if (PROFILE_STARTUP) {
242 android.os.Debug.stopMethodTracing();
243 }
244
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700245 // We have a new AllAppsView, we need to re-bind everything, and it could have
246 // changed in our absence.
247 mModel.setAllAppsDirty();
248 mModel.setWorkspaceDirty();
249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 }
253
254 // For handling default keys
255 mDefaultKeySsb = new SpannableStringBuilder();
256 Selection.setSelection(mDefaultKeySsb, 0);
257 }
Romain Guycbb89e42009-06-08 15:52:54 -0700258
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700260 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
261 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 final Configuration configuration = getResources().getConfiguration();
264
Romain Guy98d01652009-06-30 16:21:04 -0700265 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 final String locale = configuration.locale.toString();
267
Romain Guy98d01652009-06-30 16:21:04 -0700268 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 final int mcc = configuration.mcc;
270
Romain Guy98d01652009-06-30 16:21:04 -0700271 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 final int mnc = configuration.mnc;
273
Romain Guy84f296c2009-11-04 15:00:44 -0800274 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275
Romain Guy84f296c2009-11-04 15:00:44 -0800276 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700277 localeConfiguration.locale = locale;
278 localeConfiguration.mcc = mcc;
279 localeConfiguration.mnc = mnc;
280
281 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400282 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700283 }
284 }
285
286 private static class LocaleConfiguration {
287 public String locale;
288 public int mcc = -1;
289 public int mnc = -1;
290 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700291
Romain Guy98d01652009-06-30 16:21:04 -0700292 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
293 DataInputStream in = null;
294 try {
295 in = new DataInputStream(context.openFileInput(PREFERENCES));
296 configuration.locale = in.readUTF();
297 configuration.mcc = in.readInt();
298 configuration.mnc = in.readInt();
299 } catch (FileNotFoundException e) {
300 // Ignore
301 } catch (IOException e) {
302 // Ignore
303 } finally {
304 if (in != null) {
305 try {
306 in.close();
307 } catch (IOException e) {
308 // Ignore
309 }
310 }
311 }
312 }
313
314 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
315 DataOutputStream out = null;
316 try {
317 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
318 out.writeUTF(configuration.locale);
319 out.writeInt(configuration.mcc);
320 out.writeInt(configuration.mnc);
321 out.flush();
322 } catch (FileNotFoundException e) {
323 // Ignore
324 } catch (IOException e) {
325 //noinspection ResultOfMethodCallIgnored
326 context.getFileStreamPath(PREFERENCES).delete();
327 } finally {
328 if (out != null) {
329 try {
330 out.close();
331 } catch (IOException e) {
332 // Ignore
333 }
334 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 }
336 }
337
338 static int getScreen() {
339 synchronized (sLock) {
340 return sScreen;
341 }
342 }
343
344 static void setScreen(int screen) {
345 synchronized (sLock) {
346 sScreen = screen;
347 }
348 }
349
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700351 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352
353 Display display = getWindowManager().getDefaultDisplay();
354 boolean isPortrait = display.getWidth() < display.getHeight();
355
356 final int width = isPortrait ? display.getWidth() : display.getHeight();
357 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700358 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 }
360
361 @Override
362 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700363 mWaitingForResult = false;
364
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 // The pattern used here is that a user PICKs a specific application,
366 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700367
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
369 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700370
Romain Guy94dabf12009-07-21 10:55:43 -0700371 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 switch (requestCode) {
373 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400374 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 break;
376 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700377 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 break;
379 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 break;
382 case REQUEST_PICK_LIVE_FOLDER:
383 addLiveFolder(data);
384 break;
385 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400386 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700388 case REQUEST_PICK_APPWIDGET:
389 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700391 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400392 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700394 case REQUEST_PICK_WALLPAPER:
395 // We just wanted the activity result here so we can clear mWaitingForResult
396 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 }
Romain Guy18042c82009-11-06 11:44:55 -0800398 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
399 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
400 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700401 // Clean up the appWidgetId if we canceled
402 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
403 if (appWidgetId != -1) {
404 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 }
406 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 }
408
409 @Override
410 protected void onResume() {
Joe Onorato080d9b62009-11-02 12:01:11 -0500411 if (lastStartTime == 0) {
412 lastStartTime = SystemClock.uptimeMillis();
413 }
414
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 super.onResume();
416
417 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400418 mWorkspaceLoading = true;
419 mModel.startLoader(this, true);
420 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700422
Mike LeBeau736cf282009-07-02 17:46:59 -0700423 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
424 // onNewIntent), then close the search dialog if needed, because it probably
425 // came from the user pressing 'home' (rather than, for example, pressing 'back').
426 if (mIsNewIntent) {
427 // Post to a handler so that this happens after the search dialog tries to open
428 // itself again.
429 mWorkspace.post(new Runnable() {
430 public void run() {
431 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
432 ServiceManager.getService(Context.SEARCH_SERVICE));
433 try {
434 searchManagerService.stopSearch();
435 } catch (RemoteException e) {
436 e(LOG_TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700437 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700438 }
439 });
440 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700441
Mike LeBeau736cf282009-07-02 17:46:59 -0700442 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 }
444
445 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700446 protected void onPause() {
447 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800448 dismissPreview(mPreviousView);
449 dismissPreview(mNextView);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700450 }
Romain Guycbb89e42009-06-08 15:52:54 -0700451
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700452 @Override
453 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400454 // Flag the loader to stop early before switching
455 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700456
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700457 if (PROFILE_ROTATE) {
458 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
459 }
460 return null;
461 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700462
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 private boolean acceptFilter() {
464 final InputMethodManager inputManager = (InputMethodManager)
465 getSystemService(Context.INPUT_METHOD_SERVICE);
466 return !inputManager.isFullscreenMode();
467 }
468
469 @Override
470 public boolean onKeyDown(int keyCode, KeyEvent event) {
471 boolean handled = super.onKeyDown(keyCode, event);
472 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
473 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
474 keyCode, event);
475 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700476 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700477 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700478 // showSearchDialog()
479 // If there are multiple keystrokes before the search dialog takes focus,
480 // onSearchRequested() will be called for every keystroke,
481 // but it is idempotent, so it's fine.
482 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 }
484 }
485
486 return handled;
487 }
488
Karl Rosaen138a0412009-04-23 19:00:21 -0700489 private String getTypedText() {
490 return mDefaultKeySsb.toString();
491 }
492
493 private void clearTypedText() {
494 mDefaultKeySsb.clear();
495 mDefaultKeySsb.clearSpans();
496 Selection.setSelection(mDefaultKeySsb, 0);
497 }
498
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 /**
500 * Restores the previous state, if it exists.
501 *
502 * @param savedState The previous state.
503 */
504 private void restoreState(Bundle savedState) {
505 if (savedState == null) {
506 return;
507 }
508
Joe Onorato3a8820b2009-11-10 15:06:42 -0800509 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
510 if (allApps) {
511 showAllApps(false);
512 }
513
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800514 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
Joe Onorato3a8820b2009-11-10 15:06:42 -0800859 if (!mWorkspace.isDefaultScreenShowing()) {
860 mWorkspace.moveToDefaultScreen();
861 }
Romain Guy73b979d2009-06-09 12:57:21 -0700862
Joe Onorato3a8820b2009-11-10 15:06:42 -0800863 closeAllApps(false);
Romain Guy1dd3a072009-07-16 13:21:01 -0700864
Joe Onorato3a8820b2009-11-10 15:06:42 -0800865 final View v = getWindow().peekDecorView();
866 if (v != null && v.getWindowToken() != null) {
867 InputMethodManager imm = (InputMethodManager)getSystemService(
868 INPUT_METHOD_SERVICE);
869 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 }
871 }
872 }
873
874 @Override
875 protected void onRestoreInstanceState(Bundle savedInstanceState) {
876 // Do not call super here
877 mSavedInstanceState = savedInstanceState;
878 }
879
880 @Override
881 protected void onSaveInstanceState(Bundle outState) {
882 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
883
884 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
885 if (folders.size() > 0) {
886 final int count = folders.size();
887 long[] ids = new long[count];
888 for (int i = 0; i < count; i++) {
889 final FolderInfo info = folders.get(i).getInfo();
890 ids[i] = info.id;
891 }
892 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
893 } else {
894 super.onSaveInstanceState(outState);
895 }
896
Romain Guy3cf604f2009-06-16 13:12:53 -0700897 final boolean isConfigurationChange = getChangingConfigurations() != 0;
898
Joe Onoratofb0ca672009-09-14 17:55:46 -0400899 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800900 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903
904 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
905 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
906 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
907
908 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
909 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
910 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
911 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
912 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
913 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
914 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
915 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
916 layout.getOccupiedCells());
917 }
918
919 if (mFolderInfo != null && mWaitingForResult) {
920 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
921 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
922 }
923 }
924
925 @Override
926 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700928
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700930 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700932 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 }
934
935 TextKeyListener.getInstance().release();
936
Joe Onorato9c1289c2009-08-17 11:03:03 -0400937 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939 unbindDesktopItems();
940 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800941
942 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800943
944 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800945 dismissPreview(mNextView);
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800946
947 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 }
949
950 @Override
951 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700952 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 super.startActivityForResult(intent, requestCode);
954 }
955
956 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700957 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700959
Joe Onorato7bb17492009-09-24 17:51:01 -0700960 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700961
Karl Rosaen138a0412009-04-23 19:00:21 -0700962 // Slide the search widget to the top, if it's on the current screen,
963 // otherwise show the search dialog immediately.
964 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
965 if (searchWidget == null) {
966 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
967 } else {
968 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
969 // show the currently typed text in the search widget while sliding
970 searchWidget.setQuery(getTypedText());
971 }
972 }
Romain Guycbb89e42009-06-08 15:52:54 -0700973
Karl Rosaen138a0412009-04-23 19:00:21 -0700974 /**
975 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700976 *
977 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700978 */
Romain Guycbb89e42009-06-08 15:52:54 -0700979 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700980 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700981
Karl Rosaen138a0412009-04-23 19:00:21 -0700982 if (initialQuery == null) {
983 // Use any text typed in the launcher as the initial query
984 initialQuery = getTypedText();
985 clearTypedText();
986 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 if (appSearchData == null) {
988 appSearchData = new Bundle();
989 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
990 }
Romain Guycbb89e42009-06-08 15:52:54 -0700991
Karl Rosaen138a0412009-04-23 19:00:21 -0700992 final SearchManager searchManager =
993 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
994
995 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
996 if (searchWidget != null) {
997 // This gets called when the user leaves the search dialog to go back to
998 // the Launcher.
999 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
1000 public void onCancel() {
1001 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -07001002 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -07001003 }
Karl Rosaen138a0412009-04-23 19:00:21 -07001004 });
1005 }
Romain Guycbb89e42009-06-08 15:52:54 -07001006
Karl Rosaen138a0412009-04-23 19:00:21 -07001007 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001008 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 }
1010
Karl Rosaen138a0412009-04-23 19:00:21 -07001011 /**
1012 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001013 */
Romain Guy5a941392009-04-28 15:18:25 -07001014 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001015 // Close search dialog
1016 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001017 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001018 // Restore search widget to its normal position
1019 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1020 if (searchWidget != null) {
1021 searchWidget.stopSearch(false);
1022 }
1023 }
Romain Guycbb89e42009-06-08 15:52:54 -07001024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 @Override
1026 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001027 if (isWorkspaceLocked()) {
1028 return false;
1029 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030
1031 super.onCreateOptionsMenu(menu);
1032 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1033 .setIcon(android.R.drawable.ic_menu_add)
1034 .setAlphabeticShortcut('A');
1035 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1036 .setIcon(android.R.drawable.ic_menu_gallery)
1037 .setAlphabeticShortcut('W');
1038 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1039 .setIcon(android.R.drawable.ic_search_category_default)
1040 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1041 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1042 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1043 .setAlphabeticShortcut('N');
1044
1045 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001046 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1047 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048
1049 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1050 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1051 .setIntent(settings);
1052
1053 return true;
1054 }
1055
1056 @Override
1057 public boolean onPrepareOptionsMenu(Menu menu) {
1058 super.onPrepareOptionsMenu(menu);
1059
1060 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1061 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1062
1063 return true;
1064 }
1065
1066 @Override
1067 public boolean onOptionsItemSelected(MenuItem item) {
1068 switch (item.getItemId()) {
1069 case MENU_ADD:
1070 addItems();
1071 return true;
1072 case MENU_WALLPAPER_SETTINGS:
1073 startWallpaper();
1074 return true;
1075 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001076 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 return true;
1078 case MENU_NOTIFICATIONS:
1079 showNotifications();
1080 return true;
1081 }
1082
1083 return super.onOptionsItemSelected(item);
1084 }
Romain Guycbb89e42009-06-08 15:52:54 -07001085
Karl Rosaen138a0412009-04-23 19:00:21 -07001086 /**
1087 * Indicates that we want global search for this activity by setting the globalSearch
1088 * argument for {@link #startSearch} to true.
1089 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001091 @Override
1092 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001093 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001094 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001095 }
1096
Joe Onorato9c1289c2009-08-17 11:03:03 -04001097 public boolean isWorkspaceLocked() {
1098 return mWorkspaceLoading || mWaitingForResult;
1099 }
1100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001102 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 showAddDialog(mMenuAddInfo);
1104 }
1105
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001106 void addAppWidget(Intent data) {
1107 // TODO: catch bad widget exception when sent
1108 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001110 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1111 if (SEARCH_WIDGET.equals(customWidget)) {
1112 // We don't need this any more, since this isn't a real app widget.
1113 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1114 // add the search widget
1115 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001117 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1118
1119 if (appWidget.configure != null) {
1120 // Launch over to configure widget, if needed
1121 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1122 intent.setComponent(appWidget.configure);
1123 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1124
1125 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1126 } else {
1127 // Otherwise just add it
1128 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 }
1131 }
Romain Guycbb89e42009-06-08 15:52:54 -07001132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 void addSearch() {
1134 final Widget info = Widget.makeSearch();
1135 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001136
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 final int[] xy = mCellCoordinates;
1138 final int spanX = info.spanX;
1139 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001142
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1144 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001145
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 final View view = mInflater.inflate(info.layoutResource, null);
1147 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001148 Search search = (Search) view.findViewById(R.id.widget_search);
1149 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1152 }
1153
Romain Guy73b979d2009-06-09 12:57:21 -07001154 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001155 // Handle case where user selected "Applications"
1156 String applicationName = getResources().getString(R.string.group_applications);
1157 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001158
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001159 if (applicationName != null && applicationName.equals(shortcutName)) {
1160 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1161 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001162
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001163 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1164 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001165 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001166 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001167 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 }
1170
1171 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001172 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001173 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001174 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001175
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001176 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001177 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001178 } else {
1179 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1180 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 }
1182
Joe Onorato9c1289c2009-08-17 11:03:03 -04001183 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 UserFolderInfo folderInfo = new UserFolderInfo();
1185 folderInfo.title = getText(R.string.folder_name);
1186
1187 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1188 cellInfo.screen = mWorkspace.getCurrentScreen();
1189 if (!findSingleSlot(cellInfo)) return;
1190
1191 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001192 LauncherModel.addItemToDatabase(this, folderInfo,
1193 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001195 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196
1197 // Create the view
1198 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1199 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1200 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 }
Romain Guycbb89e42009-06-08 15:52:54 -07001203
Joe Onorato9c1289c2009-08-17 11:03:03 -04001204 void removeFolder(FolderInfo folder) {
1205 mFolders.remove(folder.id);
1206 }
1207
1208 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 cellInfo.screen = mWorkspace.getCurrentScreen();
1210 if (!findSingleSlot(cellInfo)) return;
1211
1212 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1213
1214 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1216 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001217 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1218 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 }
1220 }
1221
1222 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1223 CellLayout.CellInfo cellInfo, boolean notify) {
1224
1225 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1226 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1227
1228 Drawable icon = null;
1229 boolean filtered = false;
1230 Intent.ShortcutIconResource iconResource = null;
1231
1232 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1233 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1234 try {
1235 iconResource = (Intent.ShortcutIconResource) extra;
1236 final PackageManager packageManager = context.getPackageManager();
1237 Resources resources = packageManager.getResourcesForApplication(
1238 iconResource.packageName);
1239 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1240 icon = resources.getDrawable(id);
1241 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001242 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 }
1244 }
1245
1246 if (icon == null) {
1247 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1248 }
1249
1250 final LiveFolderInfo info = new LiveFolderInfo();
1251 info.icon = icon;
1252 info.filtered = filtered;
1253 info.title = name;
1254 info.iconResource = iconResource;
1255 info.uri = data.getData();
1256 info.baseIntent = baseIntent;
1257 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1258 LiveFolders.DISPLAY_MODE_GRID);
1259
1260 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1261 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001262 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263
1264 return info;
1265 }
1266
1267 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1268 final int[] xy = new int[2];
1269 if (findSlot(cellInfo, xy, 1, 1)) {
1270 cellInfo.cellX = xy[0];
1271 cellInfo.cellY = xy[1];
1272 return true;
1273 }
1274 return false;
1275 }
1276
1277 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1278 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1279 boolean[] occupied = mSavedState != null ?
1280 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1281 cellInfo = mWorkspace.findAllVacantCells(occupied);
1282 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1283 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1284 return false;
1285 }
1286 }
1287 return true;
1288 }
1289
1290 private void showNotifications() {
1291 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1292 if (statusBar != null) {
1293 statusBar.expand();
1294 }
1295 }
1296
1297 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001298 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001300 Intent chooser = Intent.createChooser(pickWallpaper,
1301 getText(R.string.chooser_wallpaper));
1302 WallpaperManager wm = (WallpaperManager)
1303 getSystemService(Context.WALLPAPER_SERVICE);
1304 WallpaperInfo wi = wm.getWallpaperInfo();
1305 if (wi != null && wi.getSettingsActivity() != null) {
1306 LabeledIntent li = new LabeledIntent(getPackageName(),
1307 R.string.configure_wallpaper, 0);
1308 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1309 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1310 }
Mike Clerona0618e42009-10-22 13:55:21 -07001311 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 }
1313
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001314 /**
1315 * Registers various content observers. The current implementation registers
1316 * only a favorites observer to keep track of the favorites applications.
1317 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001318 private void registerContentObservers() {
1319 ContentResolver resolver = getContentResolver();
1320 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1321 true, mWidgetObserver);
1322 }
1323
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 @Override
1325 public boolean dispatchKeyEvent(KeyEvent event) {
1326 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1327 switch (event.getKeyCode()) {
1328 case KeyEvent.KEYCODE_BACK:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 return true;
1330 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001331 return true;
1332 }
1333 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1334 switch (event.getKeyCode()) {
1335 case KeyEvent.KEYCODE_BACK:
1336 if (!event.isCanceled()) {
1337 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001338 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001339 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001340 } else {
1341 closeFolder();
1342 }
Romain Guya6abce82009-11-10 02:54:41 -08001343 dismissPreview(mPreviousView);
1344 dismissPreview(mNextView);
Dianne Hackborn67800862009-07-24 17:15:20 -07001345 }
Dianne Hackborn67800862009-07-24 17:15:20 -07001346 return true;
1347 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 return true;
1349 }
1350 }
1351
1352 return super.dispatchKeyEvent(event);
1353 }
1354
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 private void closeFolder() {
1356 Folder folder = mWorkspace.getOpenFolder();
1357 if (folder != null) {
1358 closeFolder(folder);
1359 }
1360 }
1361
1362 void closeFolder(Folder folder) {
1363 folder.getInfo().opened = false;
1364 ViewGroup parent = (ViewGroup) folder.getParent();
1365 if (parent != null) {
1366 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001367 if (folder instanceof DropTarget) {
1368 // Live folders aren't DropTargets.
1369 mDragController.removeDropTarget((DropTarget)folder);
1370 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 }
1372 folder.onClose();
1373 }
1374
1375 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001376 * Re-listen when widgets are reset.
1377 */
1378 private void onAppWidgetReset() {
1379 mAppWidgetHost.startListening();
1380 }
1381
1382 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001383 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1384 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001386 private void unbindDesktopItems() {
1387 for (ItemInfo item: mDesktopItems) {
1388 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 }
1390 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 /**
1393 * Launches the intent referred by the clicked shortcut.
1394 *
1395 * @param v The view representing the clicked shortcut.
1396 */
1397 public void onClick(View v) {
1398 Object tag = v.getTag();
1399 if (tag instanceof ApplicationInfo) {
1400 // Open shortcut
1401 final Intent intent = ((ApplicationInfo) tag).intent;
1402 startActivitySafely(intent);
1403 } else if (tag instanceof FolderInfo) {
1404 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001405 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001406 Log.d(TAG, "onClick");
1407 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001408 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001409 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001410 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001411 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 }
1413 }
1414
1415 void startActivitySafely(Intent intent) {
1416 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1417 try {
1418 startActivity(intent);
1419 } catch (ActivityNotFoundException e) {
1420 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1421 } catch (SecurityException e) {
1422 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001423 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1425 "or use the exported attribute for this activity.", e);
1426 }
1427 }
1428
1429 private void handleFolderClick(FolderInfo folderInfo) {
1430 if (!folderInfo.opened) {
1431 // Close any open folder
1432 closeFolder();
1433 // Open the requested folder
1434 openFolder(folderInfo);
1435 } else {
1436 // Find the open folder...
1437 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1438 int folderScreen;
1439 if (openFolder != null) {
1440 folderScreen = mWorkspace.getScreenForView(openFolder);
1441 // .. and close it
1442 closeFolder(openFolder);
1443 if (folderScreen != mWorkspace.getCurrentScreen()) {
1444 // Close any folder open on the current screen
1445 closeFolder();
1446 // Pull the folder onto this screen
1447 openFolder(folderInfo);
1448 }
1449 }
1450 }
1451 }
1452
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 /**
1454 * Opens the user fodler described by the specified tag. The opening of the folder
1455 * is animated relative to the specified View. If the View is null, no animation
1456 * is played.
1457 *
1458 * @param folderInfo The FolderInfo describing the folder to open.
1459 */
1460 private void openFolder(FolderInfo folderInfo) {
1461 Folder openFolder;
1462
1463 if (folderInfo instanceof UserFolderInfo) {
1464 openFolder = UserFolder.fromXml(this);
1465 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001466 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 } else {
1468 return;
1469 }
1470
Joe Onorato00acb122009-08-04 16:04:30 -04001471 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 openFolder.setLauncher(this);
1473
1474 openFolder.bind(folderInfo);
1475 folderInfo.opened = true;
1476
1477 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1478 openFolder.onOpen();
1479 }
1480
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001482 switch (v.getId()) {
1483 case R.id.previous_screen:
1484 showPreviousPreview(v);
1485 return true;
1486 case R.id.next_screen:
1487 showNextPreview(v);
1488 return true;
1489 }
1490
Joe Onorato9c1289c2009-08-17 11:03:03 -04001491 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 return false;
1493 }
1494
1495 if (!(v instanceof CellLayout)) {
1496 v = (View) v.getParent();
1497 }
1498
1499 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1500
1501 // This happens when long clicking an item with the dpad/trackball
1502 if (cellInfo == null) {
1503 return true;
1504 }
1505
1506 if (mWorkspace.allowLongPress()) {
1507 if (cellInfo.cell == null) {
1508 if (cellInfo.valid) {
1509 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001510 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 showAddDialog(cellInfo);
1512 }
1513 } else {
1514 if (!(cellInfo.cell instanceof Folder)) {
1515 // User long pressed on an item
1516 mWorkspace.startDrag(cellInfo);
1517 }
1518 }
1519 }
1520 return true;
1521 }
1522
Romain Guye6b8e2f2009-11-10 11:56:55 -08001523 @SuppressWarnings({"unchecked"})
Romain Guy1fbc1c82009-11-09 20:43:08 -08001524 private void dismissPreview(View v) {
1525 PopupWindow window = (PopupWindow) v.getTag();
1526 if (window != null) {
Romain Guya6abce82009-11-10 02:54:41 -08001527 window.setOnDismissListener(null);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001528 window.dismiss();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001529
1530 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1531 int count = group.getChildCount();
1532 for (int i = 0; i < count; i++) {
1533 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1534 }
1535
1536 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1537 for (Bitmap bitmap : bitmaps) bitmap.recycle();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001538
1539 v.setTag(R.id.workspace, null);
1540 v.setTag(R.id.icon, null);
1541 }
1542 v.setTag(null);
1543 }
1544
1545 private void showPreviousPreview(View anchor) {
1546 int current = mWorkspace.getCurrentScreen();
1547 if (current <= 0) return;
1548
Romain Guya6abce82009-11-10 02:54:41 -08001549 showPreviews(anchor, 0, current);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001550 }
1551
1552 private void showNextPreview(View anchor) {
1553 int current = mWorkspace.getCurrentScreen();
1554 if (current >= mWorkspace.getChildCount() - 1) return;
1555
Romain Guya6abce82009-11-10 02:54:41 -08001556 showPreviews(anchor, current + 1, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001557 }
1558
Romain Guya6abce82009-11-10 02:54:41 -08001559 @Override
1560 public void onWindowFocusChanged(boolean hasFocus) {
1561 super.onWindowFocusChanged(hasFocus);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001562
Romain Guya6abce82009-11-10 02:54:41 -08001563 if (!hasFocus) {
1564 dismissPreview(mPreviousView);
1565 dismissPreview(mNextView);
1566 }
1567 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001568
Romain Guya6abce82009-11-10 02:54:41 -08001569 private void showPreviews(final View anchor, int start, int end) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001570 Drawable d = getResources().getDrawable(R.drawable.preview_popup);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001571
Romain Guya6abce82009-11-10 02:54:41 -08001572 Workspace workspace = mWorkspace;
1573 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
1574
1575 float max = workspace.getChildCount() - 1;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001576
1577 Rect r = new Rect();
1578 d.getPadding(r);
1579 int extraW = (int) ((r.left + r.right) * max);
1580 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001581
1582 int aW = cell.getWidth() - extraW;
1583 float w = aW / max;
1584
1585 int width = cell.getWidth();
1586 int height = cell.getHeight();
1587 int x = cell.getLeftPadding();
1588 int y = cell.getTopPadding();
1589 width -= (x + cell.getRightPadding());
1590 height -= (y + cell.getBottomPadding());
1591
1592 float scale = w / width;
1593
1594 int count = end - start;
1595
1596 final float sWidth = width * scale;
1597 float sHeight = height * scale;
1598
Romain Guye6b8e2f2009-11-10 11:56:55 -08001599 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001600
Romain Guye6b8e2f2009-11-10 11:56:55 -08001601 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1602 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001603
1604 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001605 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001606 cell = (CellLayout) workspace.getChildAt(i);
1607
Romain Guye6b8e2f2009-11-10 11:56:55 -08001608 Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
1609 Bitmap.Config.ARGB_8888);
1610
1611 Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001612 c.scale(scale, scale);
1613 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1614 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001615
Romain Guye6b8e2f2009-11-10 11:56:55 -08001616 image.setBackgroundDrawable(d);
1617 image.setImageBitmap(bitmap);
1618 image.setTag(i);
1619 image.setOnClickListener(handler);
1620 bitmaps.add(bitmap);
1621
1622 preview.addView(image,
1623 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Romain Guya6abce82009-11-10 02:54:41 -08001624 }
Romain Guye6b8e2f2009-11-10 11:56:55 -08001625
1626 PopupWindow p = new PopupWindow(this);
1627 p.setContentView(preview);
1628 p.setWidth((int) (sWidth * count + extraW));
1629 p.setHeight((int) (sHeight + extraH));
1630 p.setAnimationStyle(R.style.AnimationPreview);
1631 p.setOutsideTouchable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001632 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001633 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001634
Romain Guye6b8e2f2009-11-10 11:56:55 -08001635 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1636 public void onDismiss() {
1637 dismissPreview(anchor);
1638 }
1639 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001640
1641 anchor.setTag(p);
1642 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001643 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001644 }
1645
Romain Guye6b8e2f2009-11-10 11:56:55 -08001646 class PreviewTouchHandler implements View.OnClickListener {
Romain Guya6abce82009-11-10 02:54:41 -08001647 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001648
Romain Guye6b8e2f2009-11-10 11:56:55 -08001649 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001650 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001651 }
1652
1653 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001654 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guya6abce82009-11-10 02:54:41 -08001655 dismissPreview(mAnchor);
1656 }
1657 }
1658
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001659 View getDrawerHandle() {
1660 return mHandleView;
1661 }
1662
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 Workspace getWorkspace() {
1664 return mWorkspace;
1665 }
1666
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 @Override
1668 protected Dialog onCreateDialog(int id) {
1669 switch (id) {
1670 case DIALOG_CREATE_SHORTCUT:
1671 return new CreateShortcut().createDialog();
1672 case DIALOG_RENAME_FOLDER:
1673 return new RenameFolder().createDialog();
1674 }
1675
1676 return super.onCreateDialog(id);
1677 }
1678
1679 @Override
1680 protected void onPrepareDialog(int id, Dialog dialog) {
1681 switch (id) {
1682 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 break;
1684 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001685 if (mFolderInfo != null) {
1686 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1687 final CharSequence text = mFolderInfo.title;
1688 input.setText(text);
1689 input.setSelection(0, text.length());
1690 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 break;
1692 }
1693 }
1694
1695 void showRenameDialog(FolderInfo info) {
1696 mFolderInfo = info;
1697 mWaitingForResult = true;
1698 showDialog(DIALOG_RENAME_FOLDER);
1699 }
1700
1701 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1702 mAddItemCellInfo = cellInfo;
1703 mWaitingForResult = true;
1704 showDialog(DIALOG_CREATE_SHORTCUT);
1705 }
1706
Romain Guy73b979d2009-06-09 12:57:21 -07001707 private void pickShortcut(int requestCode, int title) {
1708 Bundle bundle = new Bundle();
1709
1710 ArrayList<String> shortcutNames = new ArrayList<String>();
1711 shortcutNames.add(getString(R.string.group_applications));
1712 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1713
1714 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1715 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1716 R.drawable.ic_launcher_application));
1717 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1718
1719 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1720 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1721 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1722 pickIntent.putExtras(bundle);
1723
1724 startActivityForResult(pickIntent, requestCode);
1725 }
1726
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 private class RenameFolder {
1728 private EditText mInput;
1729
1730 Dialog createDialog() {
1731 mWaitingForResult = true;
1732 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1733 mInput = (EditText) layout.findViewById(R.id.folder_name);
1734
1735 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1736 builder.setIcon(0);
1737 builder.setTitle(getString(R.string.rename_folder_title));
1738 builder.setCancelable(true);
1739 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1740 public void onCancel(DialogInterface dialog) {
1741 cleanup();
1742 }
1743 });
1744 builder.setNegativeButton(getString(R.string.cancel_action),
1745 new Dialog.OnClickListener() {
1746 public void onClick(DialogInterface dialog, int which) {
1747 cleanup();
1748 }
1749 }
1750 );
1751 builder.setPositiveButton(getString(R.string.rename_action),
1752 new Dialog.OnClickListener() {
1753 public void onClick(DialogInterface dialog, int which) {
1754 changeFolderName();
1755 }
1756 }
1757 );
1758 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001759
1760 final AlertDialog dialog = builder.create();
1761 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1762 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001763 mInput.requestFocus();
1764 InputMethodManager inputManager = (InputMethodManager)
1765 getSystemService(Context.INPUT_METHOD_SERVICE);
1766 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001767 }
1768 });
1769
1770 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 }
1772
1773 private void changeFolderName() {
1774 final String name = mInput.getText().toString();
1775 if (!TextUtils.isEmpty(name)) {
1776 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001777 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 mFolderInfo.title = name;
1779 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1780
Joe Onorato9c1289c2009-08-17 11:03:03 -04001781 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001782 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001783 mModel.setWorkspaceDirty();
1784 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 } else {
1786 final FolderIcon folderIcon = (FolderIcon)
1787 mWorkspace.getViewForTag(mFolderInfo);
1788 if (folderIcon != null) {
1789 folderIcon.setText(name);
1790 getWorkspace().requestLayout();
1791 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001792 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001793 mModel.setWorkspaceDirty();
1794 mWorkspaceLoading = true;
1795 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 }
1797 }
1798 }
1799 cleanup();
1800 }
1801
1802 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 dismissDialog(DIALOG_RENAME_FOLDER);
1804 mWaitingForResult = false;
1805 mFolderInfo = null;
1806 }
1807 }
1808
Joe Onoratofb0ca672009-09-14 17:55:46 -04001809 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001810 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001811 }
1812
Joe Onorato3a8820b2009-11-10 15:06:42 -08001813 boolean isAllAppsOpaque() {
1814 return mAllAppsGrid.isOpaque();
1815 }
1816
1817 void showAllApps(boolean animated) {
1818 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato7bb17492009-09-24 17:51:01 -07001819 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001820
Joe Onorato3a8820b2009-11-10 15:06:42 -08001821 mWorkspace.startFading(false);
1822
Mike Cleronb6082fa2009-10-19 17:03:36 -07001823 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001824 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa2009-10-19 17:03:36 -07001825
Joe Onorato7c312c12009-08-13 21:36:53 -07001826 // TODO: fade these two too
1827 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001828 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001829 }
1830
Joe Onorato7bb17492009-09-24 17:51:01 -07001831 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001832 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001833 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa2009-10-19 17:03:36 -07001834 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001835 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001836 mWorkspace.startFading(true);
Mike Cleronb6082fa2009-10-19 17:03:36 -07001837
Joe Onorato7c312c12009-08-13 21:36:53 -07001838 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001839 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001840 mDeleteZone.setVisibility(View.VISIBLE);
1841 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001842 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001843 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001844 }
1845
Joe Onorato7c312c12009-08-13 21:36:53 -07001846 void lockAllApps() {
1847 // TODO
1848 }
1849
1850 void unlockAllApps() {
1851 // TODO
1852 }
1853
Joe Onorato7404ee42009-07-31 11:54:44 -07001854 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 * Displays the shortcut creation dialog and launches, if necessary, the
1856 * appropriate activity.
1857 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001858 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001859 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1860 DialogInterface.OnShowListener {
1861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001863
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 Dialog createDialog() {
1865 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001866
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001868
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1870 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001871 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001872
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 builder.setInverseBackgroundForced(true);
1874
1875 AlertDialog dialog = builder.create();
1876 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001877 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001878 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 return dialog;
1881 }
1882
1883 public void onCancel(DialogInterface dialog) {
1884 mWaitingForResult = false;
1885 cleanup();
1886 }
1887
Romain Guycbb89e42009-06-08 15:52:54 -07001888 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001889 }
1890
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 dismissDialog(DIALOG_CREATE_SHORTCUT);
1893 }
1894
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001895 /**
1896 * Handle the action clicked in the "Add to home" dialog.
1897 */
1898 public void onClick(DialogInterface dialog, int which) {
1899 Resources res = getResources();
1900 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001901
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001902 switch (which) {
1903 case AddAdapter.ITEM_SHORTCUT: {
1904 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001905 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001906 break;
1907 }
Romain Guycbb89e42009-06-08 15:52:54 -07001908
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001909 case AddAdapter.ITEM_APPWIDGET: {
1910 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001911
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001912 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1913 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1914 // add the search widget
1915 ArrayList<AppWidgetProviderInfo> customInfo =
1916 new ArrayList<AppWidgetProviderInfo>();
1917 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1918 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1919 info.label = getString(R.string.group_search);
1920 info.icon = R.drawable.ic_search_widget;
1921 customInfo.add(info);
1922 pickIntent.putParcelableArrayListExtra(
1923 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1924 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1925 Bundle b = new Bundle();
1926 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1927 customExtras.add(b);
1928 pickIntent.putParcelableArrayListExtra(
1929 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1930 // start the pick activity
1931 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1932 break;
1933 }
Romain Guycbb89e42009-06-08 15:52:54 -07001934
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001935 case AddAdapter.ITEM_LIVE_FOLDER: {
1936 // Insert extra item to handle inserting folder
1937 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001938
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001939 ArrayList<String> shortcutNames = new ArrayList<String>();
1940 shortcutNames.add(res.getString(R.string.group_folder));
1941 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001942
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001943 ArrayList<ShortcutIconResource> shortcutIcons =
1944 new ArrayList<ShortcutIconResource>();
1945 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1946 R.drawable.ic_launcher_folder));
1947 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1948
1949 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1950 pickIntent.putExtra(Intent.EXTRA_INTENT,
1951 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1952 pickIntent.putExtra(Intent.EXTRA_TITLE,
1953 getText(R.string.title_select_live_folder));
1954 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001955
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001956 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1957 break;
1958 }
1959
1960 case AddAdapter.ITEM_WALLPAPER: {
1961 startWallpaper();
1962 break;
1963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 }
1965 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001966
1967 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001968 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 }
1970
1971 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001972 * Receives notifications when applications are added/removed.
1973 */
1974 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1975 @Override
1976 public void onReceive(Context context, Intent intent) {
1977 Log.d(TAG, "CloseSystemDialogsIntentReceiver.onReceiver intent=" + intent);
1978 closeSystemDialogs();
1979 }
1980 }
1981
1982 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001983 * Receives notifications whenever the appwidgets are reset.
1984 */
1985 private class AppWidgetResetObserver extends ContentObserver {
1986 public AppWidgetResetObserver() {
1987 super(new Handler());
1988 }
1989
1990 @Override
1991 public void onChange(boolean selfChange) {
1992 onAppWidgetReset();
1993 }
1994 }
1995
1996 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001997 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001999 public int getCurrentWorkspaceScreen() {
2000 return mWorkspace.getCurrentScreen();
2001 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002002
Joe Onorato9c1289c2009-08-17 11:03:03 -04002003 /**
2004 * Refreshes the shortcuts shown on the workspace.
2005 *
2006 * Implementation of the method from LauncherModel.Callbacks.
2007 */
2008 public void startBinding() {
2009 final Workspace workspace = mWorkspace;
2010 int count = workspace.getChildCount();
2011 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002012 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002013 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2014 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002015
Joe Onorato9c1289c2009-08-17 11:03:03 -04002016 if (DEBUG_USER_INTERFACE) {
2017 android.widget.Button finishButton = new android.widget.Button(this);
2018 finishButton.setText("Finish");
2019 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2020
2021 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2022 public void onClick(View v) {
2023 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002024 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002025 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 }
2027 }
2028
2029 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002030 * Bind the items start-end from the list.
2031 *
2032 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002034 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2035
2036 final Workspace workspace = mWorkspace;
2037
2038 for (int i=start; i<end; i++) {
2039 final ItemInfo item = shortcuts.get(i);
2040 mDesktopItems.add(item);
2041 switch (item.itemType) {
2042 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2043 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2044 final View shortcut = createShortcut((ApplicationInfo) item);
2045 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2046 false);
2047 break;
2048 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2049 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2050 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2051 (UserFolderInfo) item);
2052 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2053 false);
2054 break;
2055 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2056 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2057 R.layout.live_folder_icon, this,
2058 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2059 (LiveFolderInfo) item);
2060 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2061 false);
2062 break;
2063 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
2064 final int screen = workspace.getCurrentScreen();
2065 final View view = mInflater.inflate(R.layout.widget_search,
2066 (ViewGroup) workspace.getChildAt(screen), false);
2067
2068 Search search = (Search) view.findViewById(R.id.widget_search);
2069 search.setLauncher(this);
2070
2071 final Widget widget = (Widget) item;
2072 view.setTag(widget);
2073
2074 workspace.addWidget(view, widget, false);
2075 break;
2076 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 }
2078
Joe Onorato9c1289c2009-08-17 11:03:03 -04002079 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 }
2081
Joe Onorato9c1289c2009-08-17 11:03:03 -04002082 /**
2083 * Implementation of the method from LauncherModel.Callbacks.
2084 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002085 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2086 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002087 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002089
2090 /**
2091 * Add the views for a widget to the workspace.
2092 *
2093 * Implementation of the method from LauncherModel.Callbacks.
2094 */
2095 public void bindAppWidget(LauncherAppWidgetInfo item) {
2096 final Workspace workspace = mWorkspace;
2097
2098 final int appWidgetId = item.appWidgetId;
2099 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2100 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2101
Joe Onorato9c1289c2009-08-17 11:03:03 -04002102 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2103 item.hostView.setTag(item);
2104
2105 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2106 item.cellY, item.spanX, item.spanY, false);
2107
2108 workspace.requestLayout();
2109
2110 mDesktopItems.add(item);
2111 }
2112
2113 /**
2114 * Callback saying that there aren't any more items to bind.
2115 *
2116 * Implementation of the method from LauncherModel.Callbacks.
2117 */
2118 public void finishBindingItems() {
2119 if (mSavedState != null) {
2120 if (!mWorkspace.hasFocus()) {
2121 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2122 }
2123
2124 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2125 if (userFolders != null) {
2126 for (long folderId : userFolders) {
2127 final FolderInfo info = mFolders.get(folderId);
2128 if (info != null) {
2129 openFolder(info);
2130 }
2131 }
2132 final Folder openFolder = mWorkspace.getOpenFolder();
2133 if (openFolder != null) {
2134 openFolder.requestFocus();
2135 }
2136 }
2137
Joe Onorato9c1289c2009-08-17 11:03:03 -04002138 mSavedState = null;
2139 }
2140
2141 if (mSavedInstanceState != null) {
2142 super.onRestoreInstanceState(mSavedInstanceState);
2143 mSavedInstanceState = null;
2144 }
2145
Joe Onorato9c1289c2009-08-17 11:03:03 -04002146 Log.d(TAG, "finishBindingItems done");
2147 mWorkspaceLoading = false;
2148 }
2149
2150 /**
2151 * Add the icons for all apps.
2152 *
2153 * Implementation of the method from LauncherModel.Callbacks.
2154 */
2155 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002156 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002157 }
2158
2159 /**
2160 * A package was installed.
2161 *
2162 * Implementation of the method from LauncherModel.Callbacks.
2163 */
2164 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2165 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002166 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002167 }
2168
2169 /**
2170 * A package was updated.
2171 *
2172 * Implementation of the method from LauncherModel.Callbacks.
2173 */
2174 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2175 removeDialog(DIALOG_CREATE_SHORTCUT);
2176 mWorkspace.updateShortcutsForPackage(packageName);
2177 }
2178
2179 /**
2180 * A package was uninstalled.
2181 *
2182 * Implementation of the method from LauncherModel.Callbacks.
2183 */
2184 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2185 removeDialog(DIALOG_CREATE_SHORTCUT);
2186 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002187 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002188 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189}