blob: e413a70871b7640e36944905f243f6dc529cec62 [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
Michael Jurka0e260592010-06-30 17:07:39 -070019import com.android.common.Search;
20import com.android.launcher.R;
Bjorn Bringert3e244cf2010-02-10 14:17:35 +000021
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.Dialog;
24import android.app.SearchManager;
25import android.app.StatusBarManager;
Michael Jurka0e260592010-06-30 17:07:39 -070026import android.app.TabActivity;
Dianne Hackborn107f8392009-08-05 21:32:16 -070027import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070028import android.appwidget.AppWidgetManager;
29import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080031import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040032import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080033import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.Context;
35import android.content.DialogInterface;
36import android.content.Intent;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.IntentFilter;
Michael Jurkaaf442092010-06-10 17:01:57 -070038import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070039import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040041import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070043import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040044import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080045import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080047import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070048import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080049import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070050import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040051import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080053import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040055import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080056import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070057import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.text.Selection;
59import android.text.SpannableStringBuilder;
60import android.text.TextUtils;
61import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070062import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080064import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.view.KeyEvent;
66import android.view.LayoutInflater;
67import android.view.Menu;
68import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070069import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.View;
71import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070072import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.view.View.OnLongClickListener;
Michael Jurka0e260592010-06-30 17:07:39 -070074import android.view.animation.Animation;
Michael Jurkaaf442092010-06-10 17:01:57 -070075import android.view.animation.AnimationUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.inputmethod.InputMethodManager;
Michael Jurka0e260592010-06-30 17:07:39 -070077import android.widget.AdapterView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070079import android.widget.ImageView;
80import android.widget.LinearLayout;
81import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070082import android.widget.TabHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.widget.TextView;
84import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085
Michael Jurka0e260592010-06-30 17:07:39 -070086import java.io.DataInputStream;
87import java.io.DataOutputStream;
88import java.io.FileNotFoundException;
89import java.io.IOException;
90import java.util.ArrayList;
91import java.util.HashMap;
92import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -080093
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094/**
95 * Default launcher application.
96 */
Michael Jurka0e260592010-06-30 17:07:39 -070097public final class Launcher extends TabActivity
98 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
99 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800100 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700101 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
Joe Onorato9c1289c2009-08-17 11:03:03 -0400103 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400104 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400105 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 private static final int WALLPAPER_SCREENS_SPAN = 2;
108
109 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800110 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
111
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 private static final int MENU_ADD = Menu.FIRST + 1;
113 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
114 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
115 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700116 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
118 private static final int REQUEST_CREATE_SHORTCUT = 1;
119 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700120 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 private static final int REQUEST_PICK_APPLICATION = 6;
122 private static final int REQUEST_PICK_SHORTCUT = 7;
123 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700124 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700125 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
127 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
128
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800129 static final int SCREEN_COUNT = 5;
130 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700132 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Joe Onorato7c312c12009-08-13 21:36:53 -0700134 static final int DIALOG_CREATE_SHORTCUT = 1;
135 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Romain Guy98d01652009-06-30 16:21:04 -0700137 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
139 // Type: int
140 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
141 // Type: boolean
142 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
143 // Type: long
144 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
147 // Type: int
148 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
149 // Type: int
150 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
151 // Type: int
152 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
153 // Type: int
154 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
159 // Type: int[]
160 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
161 // Type: boolean
162 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
163 // Type: long
164 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
165
Joe Onorato9c1289c2009-08-17 11:03:03 -0400166 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800169 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800171 private final BroadcastReceiver mCloseSystemDialogsReceiver
172 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800173 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private LayoutInflater mInflater;
176
Joe Onorato00acb122009-08-04 16:04:30 -0400177 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700179
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700180 private AppWidgetManager mAppWidgetManager;
181 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private CellLayout.CellInfo mAddItemCellInfo;
184 private CellLayout.CellInfo mMenuAddInfo;
185 private final int[] mCellCoordinates = new int[2];
186 private FolderInfo mFolderInfo;
187
Joe Onorato7c312c12009-08-13 21:36:53 -0700188 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700189 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700190 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700191 private TabHost mHomeCustomizationDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private Bundle mSavedState;
194
195 private SpannableStringBuilder mDefaultKeySsb = null;
196
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 private boolean mWorkspaceLoading = true;
198
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800199 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private boolean mRestoring;
201 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
203 private Bundle mSavedInstanceState;
204
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800206 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207
Romain Guy84f296c2009-11-04 15:00:44 -0800208 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
209 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700210
Romain Guy1fbc1c82009-11-09 20:43:08 -0800211 private ImageView mPreviousView;
212 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500213
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400214 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400215 private static final int NUM_HOTSEATS = 2;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400216 private String[] mHotseatConfig = null;
217 private Intent[] mHotseats = null;
218 private Drawable[] mHotseatIcons = null;
219 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400220
Winson Chungaafa03c2010-06-11 17:34:16 -0700221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 @Override
223 protected void onCreate(Bundle savedInstanceState) {
224 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700225
Winson Chungaafa03c2010-06-11 17:34:16 -0700226 if (LauncherApplication.isInPlaceRotationEnabled()) {
227 // hide the status bar (temporary until we get the status bar design figured out)
228 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
229 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
230 }
231
Joe Onorato0589f0f2010-02-08 13:44:00 -0800232 LauncherApplication app = ((LauncherApplication)getApplication());
233 mModel = app.setLauncher(this);
234 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400235 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700237
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700238 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700239 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
240 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 if (PROFILE_STARTUP) {
243 android.os.Debug.startMethodTracing("/sdcard/launcher");
244 }
245
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400246 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 checkForLocaleChange();
248 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 setContentView(R.layout.launcher);
Winson Chungaafa03c2010-06-11 17:34:16 -0700250
Michael Jurka0e260592010-06-30 17:07:39 -0700251 mHomeCustomizationDrawer = getTabHost();
252
253 String widgetsLabel = getString(R.string.widgets_tab_label);
254 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("widgets")
255 .setIndicator(widgetsLabel).setContent(R.id.widget_chooser));
256 String foldersLabel = getString(R.string.folders_tab_label);
257 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("folders")
258 .setIndicator(foldersLabel).setContent(R.id.folder_chooser));
259 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
260 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("shortcuts")
261 .setIndicator(shortcutsLabel).setContent(R.id.shortcut_chooser));
262 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
263 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("wallpapers")
264 .setIndicator(wallpapersLabel).setContent(R.id.wallpaperstab));
265
266 mHomeCustomizationDrawer.setCurrentTab(0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 setupViews();
268
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800269 registerContentObservers();
270
Joe Onorato7c312c12009-08-13 21:36:53 -0700271 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700272
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 mSavedState = savedInstanceState;
274 restoreState(mSavedState);
275
276 if (PROFILE_STARTUP) {
277 android.os.Debug.stopMethodTracing();
278 }
279
280 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400281 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 }
283
284 // For handling default keys
285 mDefaultKeySsb = new SpannableStringBuilder();
286 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800287
288 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
289 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 }
Romain Guycbb89e42009-06-08 15:52:54 -0700291
Winson Chungaafa03c2010-06-11 17:34:16 -0700292 @Override
293 public void onConfigurationChanged(Configuration newConfig) {
294 // TODO Auto-generated method stub
295 super.onConfigurationChanged(newConfig);
296
297 if (LauncherApplication.isInPlaceRotationEnabled()) {
298 mModel.updateOrientation();
299 mWorkspace.refreshWorkspaceChildren();
300 mWorkspace.rotateCurrentScreensChildren();
301 }
302 }
303
304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700306 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
307 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700308
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 final Configuration configuration = getResources().getConfiguration();
310
Romain Guy98d01652009-06-30 16:21:04 -0700311 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 final String locale = configuration.locale.toString();
313
Romain Guy98d01652009-06-30 16:21:04 -0700314 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 final int mcc = configuration.mcc;
316
Romain Guy98d01652009-06-30 16:21:04 -0700317 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 final int mnc = configuration.mnc;
319
Romain Guy84f296c2009-11-04 15:00:44 -0800320 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321
Romain Guy84f296c2009-11-04 15:00:44 -0800322 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700323 localeConfiguration.locale = locale;
324 localeConfiguration.mcc = mcc;
325 localeConfiguration.mnc = mnc;
326
327 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800328 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400329
330 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700331 }
332 }
333
334 private static class LocaleConfiguration {
335 public String locale;
336 public int mcc = -1;
337 public int mnc = -1;
338 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700339
Romain Guy98d01652009-06-30 16:21:04 -0700340 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
341 DataInputStream in = null;
342 try {
343 in = new DataInputStream(context.openFileInput(PREFERENCES));
344 configuration.locale = in.readUTF();
345 configuration.mcc = in.readInt();
346 configuration.mnc = in.readInt();
347 } catch (FileNotFoundException e) {
348 // Ignore
349 } catch (IOException e) {
350 // Ignore
351 } finally {
352 if (in != null) {
353 try {
354 in.close();
355 } catch (IOException e) {
356 // Ignore
357 }
358 }
359 }
360 }
361
362 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
363 DataOutputStream out = null;
364 try {
365 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
366 out.writeUTF(configuration.locale);
367 out.writeInt(configuration.mcc);
368 out.writeInt(configuration.mnc);
369 out.flush();
370 } catch (FileNotFoundException e) {
371 // Ignore
372 } catch (IOException e) {
373 //noinspection ResultOfMethodCallIgnored
374 context.getFileStreamPath(PREFERENCES).delete();
375 } finally {
376 if (out != null) {
377 try {
378 out.close();
379 } catch (IOException e) {
380 // Ignore
381 }
382 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 }
384 }
385
386 static int getScreen() {
387 synchronized (sLock) {
388 return sScreen;
389 }
390 }
391
392 static void setScreen(int screen) {
393 synchronized (sLock) {
394 sScreen = screen;
395 }
396 }
397
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700399 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400
401 Display display = getWindowManager().getDefaultDisplay();
402 boolean isPortrait = display.getWidth() < display.getHeight();
403
404 final int width = isPortrait ? display.getWidth() : display.getHeight();
405 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700406 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 }
408
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400409 // Note: This doesn't do all the client-id magic that BrowserProvider does
410 // in Browser. (http://b/2425179)
411 private Uri getDefaultBrowserUri() {
412 String url = getString(R.string.default_browser_url);
413 if (url.indexOf("{CID}") != -1) {
414 url = url.replace("{CID}", "android-google");
415 }
416 return Uri.parse(url);
417 }
418
419 // Load the Intent templates from arrays.xml to populate the hotseats. For
420 // each Intent, if it resolves to a single app, use that as the launch
421 // intent & use that app's label as the contentDescription. Otherwise,
422 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400423 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400424 if (mHotseatConfig == null) {
425 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
426 if (mHotseatConfig.length > 0) {
427 mHotseats = new Intent[mHotseatConfig.length];
428 mHotseatLabels = new CharSequence[mHotseatConfig.length];
429 mHotseatIcons = new Drawable[mHotseatConfig.length];
430 } else {
431 mHotseats = null;
432 mHotseatIcons = null;
433 mHotseatLabels = null;
434 }
435
436 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
437 for (int i=0; i<mHotseatConfig.length; i++) {
438 // load icon for this slot; currently unrelated to the actual activity
439 try {
440 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
441 } catch (ArrayIndexOutOfBoundsException ex) {
442 Log.w(TAG, "Missing hotseat_icons array item #" + i);
443 mHotseatIcons[i] = null;
444 }
445 }
446 hotseatIconDrawables.recycle();
447 }
448
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400449 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400450 for (int i=0; i<mHotseatConfig.length; i++) {
451 Intent intent = null;
452 if (mHotseatConfig[i].equals("*BROWSER*")) {
453 // magic value meaning "launch user's default web browser"
454 // replace it with a generic web request so we can see if there is indeed a default
455 String defaultUri = getString(R.string.default_browser_url);
456 intent = new Intent(
457 Intent.ACTION_VIEW,
458 ((defaultUri != null)
459 ? Uri.parse(defaultUri)
460 : getDefaultBrowserUri())
461 ).addCategory(Intent.CATEGORY_BROWSABLE);
462 // note: if the user launches this without a default set, she
463 // will always be taken to the default URL above; this is
464 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700465 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400466 // URL is unavoidably sent to the chosen app.
467 } else {
468 try {
469 intent = Intent.parseUri(mHotseatConfig[i], 0);
470 } catch (java.net.URISyntaxException ex) {
471 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
472 // bogus; leave intent=null
473 }
474 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700475
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400476 if (intent == null) {
477 mHotseats[i] = null;
478 mHotseatLabels[i] = getText(R.string.activity_not_found);
479 continue;
480 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400481
482 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700483 Log.d(TAG, "loadHotseats: hotseat " + i
484 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400485 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400486 + "]");
487 }
488
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400489 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
490 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700491 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400492 Log.d(TAG, "Best match for intent: " + bestMatch);
493 Log.d(TAG, "All matches: ");
494 for (ResolveInfo ri : allMatches) {
495 Log.d(TAG, " --> " + ri);
496 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400497 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400498 // did this resolve to a single app, or the resolver?
499 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700500 // can't find any activity to handle this. let's leave the
501 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400502 // to launch.
503 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400504
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400505 // set accessibility text to "Not installed"
506 mHotseatLabels[i] = getText(R.string.activity_not_found);
507 } else {
508 boolean found = false;
509 for (ResolveInfo ri : allMatches) {
510 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
511 && bestMatch.activityInfo.applicationInfo.packageName
512 .equals(ri.activityInfo.applicationInfo.packageName)) {
513 found = true;
514 break;
515 }
516 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700517
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400518 if (!found) {
519 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
520 // the bestMatch is probably the ResolveActivity, meaning the
521 // user has not yet selected a default
522 // so: we'll keep the original intent for now
523 mHotseats[i] = intent;
524
525 // set the accessibility text to "Select shortcut"
526 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
527 } else {
528 // we have an app!
529 // now reconstruct the intent to launch it through the front
530 // door
531 ComponentName com = new ComponentName(
532 bestMatch.activityInfo.applicationInfo.packageName,
533 bestMatch.activityInfo.name);
534 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
535
536 // load the app label for accessibility
537 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
538 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400539 }
540
541 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700542 Log.d(TAG, "loadHotseats: hotseat " + i
543 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400544 + ((mHotseats[i] == null)
545 ? "null"
546 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400547 + "] label=[" + mHotseatLabels[i]
548 + "]"
549 );
550 }
551 }
552 }
553
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 @Override
555 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700556 mWaitingForResult = false;
557
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 // The pattern used here is that a user PICKs a specific application,
559 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700560
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
562 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700563
Romain Guy94dabf12009-07-21 10:55:43 -0700564 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565 switch (requestCode) {
566 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400567 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 break;
569 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800570 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 break;
572 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400573 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 break;
575 case REQUEST_PICK_LIVE_FOLDER:
576 addLiveFolder(data);
577 break;
578 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400579 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700581 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700582 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700584 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700585 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700586 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700588 case REQUEST_PICK_WALLPAPER:
589 // We just wanted the activity result here so we can clear mWaitingForResult
590 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800591 }
Romain Guy18042c82009-11-06 11:44:55 -0800592 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
593 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
594 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700595 // Clean up the appWidgetId if we canceled
596 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
597 if (appWidgetId != -1) {
598 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 }
600 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601 }
602
603 @Override
604 protected void onResume() {
605 super.onResume();
606
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800607 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800608
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400610 mWorkspaceLoading = true;
611 mModel.startLoader(this, true);
612 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 }
614 }
615
616 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700617 protected void onPause() {
618 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700619 // Some launcher layouts don't have a previous and next view
620 if (mPreviousView != null) {
621 dismissPreview(mPreviousView);
622 dismissPreview(mNextView);
623 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800624 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700625 }
Romain Guycbb89e42009-06-08 15:52:54 -0700626
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700627 @Override
628 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400629 // Flag the loader to stop early before switching
630 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800631 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800632 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700633 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700634
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800635 // We can't hide the IME if it was forced open. So don't bother
636 /*
637 @Override
638 public void onWindowFocusChanged(boolean hasFocus) {
639 super.onWindowFocusChanged(hasFocus);
640
641 if (hasFocus) {
642 final InputMethodManager inputManager = (InputMethodManager)
643 getSystemService(Context.INPUT_METHOD_SERVICE);
644 WindowManager.LayoutParams lp = getWindow().getAttributes();
645 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
646 android.os.Handler()) {
647 protected void onReceiveResult(int resultCode, Bundle resultData) {
648 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
649 }
650 });
651 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
652 }
653 }
654 */
655
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 private boolean acceptFilter() {
657 final InputMethodManager inputManager = (InputMethodManager)
658 getSystemService(Context.INPUT_METHOD_SERVICE);
659 return !inputManager.isFullscreenMode();
660 }
661
662 @Override
663 public boolean onKeyDown(int keyCode, KeyEvent event) {
664 boolean handled = super.onKeyDown(keyCode, event);
665 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
666 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
667 keyCode, event);
668 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700669 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700670 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700671 // showSearchDialog()
672 // If there are multiple keystrokes before the search dialog takes focus,
673 // onSearchRequested() will be called for every keystroke,
674 // but it is idempotent, so it's fine.
675 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
677 }
678
Joe Onorato8a9625e2010-01-28 15:55:35 -0800679 // Eat the long press event so the keyboard doesn't come up.
680 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
681 return true;
682 }
683
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 return handled;
685 }
686
Karl Rosaen138a0412009-04-23 19:00:21 -0700687 private String getTypedText() {
688 return mDefaultKeySsb.toString();
689 }
690
691 private void clearTypedText() {
692 mDefaultKeySsb.clear();
693 mDefaultKeySsb.clearSpans();
694 Selection.setSelection(mDefaultKeySsb, 0);
695 }
696
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 /**
698 * Restores the previous state, if it exists.
699 *
700 * @param savedState The previous state.
701 */
702 private void restoreState(Bundle savedState) {
703 if (savedState == null) {
704 return;
705 }
706
Joe Onorato3a8820b2009-11-10 15:06:42 -0800707 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
708 if (allApps) {
709 showAllApps(false);
710 }
711
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
713 if (currentScreen > -1) {
714 mWorkspace.setCurrentScreen(currentScreen);
715 }
716
717 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
718 if (addScreen > -1) {
719 mAddItemCellInfo = new CellLayout.CellInfo();
720 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
721 addItemCellInfo.valid = true;
722 addItemCellInfo.screen = addScreen;
723 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
724 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
725 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
726 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
727 addItemCellInfo.findVacantCellsFromOccupied(
728 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
729 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
730 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
731 mRestoring = true;
732 }
733
734 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
735 if (renameFolder) {
736 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 mRestoring = true;
739 }
740 }
741
742 /**
743 * Finds all the views we need and configure them properly.
744 */
745 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400746 DragController dragController = mDragController;
747
Romain Guyb1b69f52009-08-10 15:10:15 -0700748 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400749 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750
Joe Onorato7c312c12009-08-13 21:36:53 -0700751 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700752 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700753 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800754 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa2009-10-19 17:03:36 -0700755 // Manage focusability manually since this thing is always visible
Winson Chungaafa03c2010-06-11 17:34:16 -0700756 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700757
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
759 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500760 workspace.setHapticFeedbackEnabled(false);
Michael Jurka0e260592010-06-30 17:07:39 -0700761 workspace.setOnInterceptTouchListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762
Joe Onorato7c312c12009-08-13 21:36:53 -0700763 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
764 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765
Joe Onorato7c312c12009-08-13 21:36:53 -0700766 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700767 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700768 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800769 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800770
Michael Jurka0e260592010-06-30 17:07:39 -0700771 WidgetChooser widgetChooser = (WidgetChooser) findViewById(R.id.widget_chooser);
772 if (widgetChooser != null) {
773 WidgetListAdapter widgetGalleryAdapter = new WidgetListAdapter(this);
774 widgetChooser.setAdapter(widgetGalleryAdapter);
775 widgetChooser.setDragController(dragController);
776 widgetChooser.setLauncher(this);
777
778 FolderChooser folderChooser = (FolderChooser) findViewById(R.id.folder_chooser);
779 IntentListAdapter folderTypes = new FolderListAdapter(this, LiveFolders.ACTION_CREATE_LIVE_FOLDER);
780 folderChooser.setAdapter(folderTypes);
781 folderChooser.setLauncher(this);
782
783 ShortcutChooser shortcutChooser = (ShortcutChooser) findViewById(R.id.shortcut_chooser);
784 IntentListAdapter shortcutTypes = new ShortcutListAdapter(this, Intent.ACTION_CREATE_SHORTCUT);
785 shortcutChooser.setAdapter(shortcutTypes);
786 shortcutChooser.setLauncher(this);
Michael Jurkaaf442092010-06-10 17:01:57 -0700787 } else {
788 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
789 hotseatLeft.setContentDescription(mHotseatLabels[0]);
790 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
791 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
792 hotseatRight.setContentDescription(mHotseatLabels[1]);
793 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400794
Michael Jurkaaf442092010-06-10 17:01:57 -0700795 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
796 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800797
Michael Jurkaaf442092010-06-10 17:01:57 -0700798 Drawable previous = mPreviousView.getDrawable();
799 Drawable next = mNextView.getDrawable();
800 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801
Michael Jurkaaf442092010-06-10 17:01:57 -0700802 mPreviousView.setHapticFeedbackEnabled(false);
803 mPreviousView.setOnLongClickListener(this);
804 mNextView.setHapticFeedbackEnabled(false);
805 mNextView.setOnLongClickListener(this);
806 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800807
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400809 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811
812 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400813 deleteZone.setDragController(dragController);
Winson Chungaafa03c2010-06-11 17:34:16 -0700814 int deleteZoneHandleId = LauncherApplication.isScreenXLarge() ? R.id.add_button : R.id.all_apps_button_cluster;
Michael Jurkaaf442092010-06-10 17:01:57 -0700815 deleteZone.setHandle(findViewById(deleteZoneHandleId));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816
Joe Onorato00acb122009-08-04 16:04:30 -0400817 dragController.setDragScoller(workspace);
818 dragController.setDragListener(deleteZone);
819 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800820 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700821
Joe Onorato00acb122009-08-04 16:04:30 -0400822 // The order here is bottom to top.
823 dragController.addDropTarget(workspace);
824 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 }
826
Romain Guy8a73c512009-11-09 19:19:59 -0800827 @SuppressWarnings({"UnusedDeclaration"})
828 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800829 if (!isAllAppsVisible()) {
830 mWorkspace.scrollLeft();
831 }
Romain Guy8a73c512009-11-09 19:19:59 -0800832 }
833
834 @SuppressWarnings({"UnusedDeclaration"})
835 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800836 if (!isAllAppsVisible()) {
837 mWorkspace.scrollRight();
838 }
Romain Guy8a73c512009-11-09 19:19:59 -0800839 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400840
841 @SuppressWarnings({"UnusedDeclaration"})
842 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400843 if (isAllAppsVisible()) return;
844
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400845 int index = -1;
846 if (v.getId() == R.id.hotseat_left) {
847 index = 0;
848 } else if (v.getId() == R.id.hotseat_right) {
849 index = 1;
850 }
851
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400852 // reload these every tap; you never know when they might change
853 loadHotseats();
854 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
855 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400856 startActivitySafely(
857 mHotseats[index],
858 "hotseat"
859 );
860 }
861 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700862
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 /**
864 * Creates a view representing a shortcut.
865 *
866 * @param info The data structure describing the shortcut.
867 *
868 * @return A View inflated from R.layout.application.
869 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800870 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 return createShortcut(R.layout.application,
872 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
873 }
874
875 /**
876 * Creates a view representing a shortcut inflated from the specified resource.
877 *
878 * @param layoutResId The id of the XML layout used to create the shortcut.
879 * @param parent The group the shortcut belongs to.
880 * @param info The data structure describing the shortcut.
881 *
882 * @return A View inflated from layoutResId.
883 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800884 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
886
Joe Onorato0589f0f2010-02-08 13:44:00 -0800887 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
888 new FastBitmapDrawable(info.getIcon(mIconCache)),
889 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 favorite.setText(info.title);
891 favorite.setTag(info);
892 favorite.setOnClickListener(this);
893
894 return favorite;
895 }
896
897 /**
898 * Add an application shortcut to the workspace.
899 *
900 * @param data The intent describing the application.
901 * @param cellInfo The position on screen where to create the shortcut.
902 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400903 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 cellInfo.screen = mWorkspace.getCurrentScreen();
905 if (!findSingleSlot(cellInfo)) return;
906
Joe Onorato0589f0f2010-02-08 13:44:00 -0800907 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
908 data, context);
909
Romain Guy73b979d2009-06-09 12:57:21 -0700910 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800911 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800913 info.container = ItemInfo.NO_ID;
914 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
915 } else {
916 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 }
918 }
Romain Guycbb89e42009-06-08 15:52:54 -0700919
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 /**
921 * Add a shortcut to the workspace.
922 *
923 * @param data The intent describing the shortcut.
924 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400926 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 cellInfo.screen = mWorkspace.getCurrentScreen();
928 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700929
Joe Onorato0589f0f2010-02-08 13:44:00 -0800930 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931
932 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400934 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
935 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 }
937 }
938
Romain Guycbb89e42009-06-08 15:52:54 -0700939
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700941 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700943 * @param data The intent describing the appWidgetId.
944 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 */
Michael Jurkaaf442092010-06-10 17:01:57 -0700946 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700947 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700948
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700949 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700951 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700952
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 // Try finding open space on Launcher screen
954 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800955 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
956 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
957 return;
958 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700960 // Build Launcher-specific widget info and save to database
961 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 launcherInfo.spanX = spans[0];
963 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700964
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 LauncherModel.addItemToDatabase(this, launcherInfo,
966 LauncherSettings.Favorites.CONTAINER_DESKTOP,
967 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
968
969 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400970 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700971
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700973 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700974
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700975 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700977
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 }
981 }
Romain Guycbb89e42009-06-08 15:52:54 -0700982
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
984 mDesktopItems.remove(launcherInfo);
985 launcherInfo.hostView = null;
986 }
987
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700988 public LauncherAppWidgetHost getAppWidgetHost() {
989 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 }
Romain Guycbb89e42009-06-08 15:52:54 -0700991
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800992 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800993 getWindow().closeAllPanels();
994
995 try {
996 dismissDialog(DIALOG_CREATE_SHORTCUT);
997 // Unlock the workspace if the dialog was showing
998 } catch (Exception e) {
999 // An exception is thrown if the dialog is not visible, which is fine
1000 }
1001
1002 try {
1003 dismissDialog(DIALOG_RENAME_FOLDER);
1004 // Unlock the workspace if the dialog was showing
1005 } catch (Exception e) {
1006 // An exception is thrown if the dialog is not visible, which is fine
1007 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001008
1009 // Whatever we were doing is hereby canceled.
1010 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001011 }
1012
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 @Override
1014 protected void onNewIntent(Intent intent) {
1015 super.onNewIntent(intent);
1016
1017 // Close the menu
1018 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001019 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001020 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001021
Joe Onorato14f122b2009-11-19 14:06:36 -08001022 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1023 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1024 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001025 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -08001026 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001027 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001028 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -07001029
Joe Onorato3a8820b2009-11-10 15:06:42 -08001030 final View v = getWindow().peekDecorView();
1031 if (v != null && v.getWindowToken() != null) {
1032 InputMethodManager imm = (InputMethodManager)getSystemService(
1033 INPUT_METHOD_SERVICE);
1034 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 }
1036 }
1037 }
1038
1039 @Override
1040 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1041 // Do not call super here
1042 mSavedInstanceState = savedInstanceState;
1043 }
1044
1045 @Override
1046 protected void onSaveInstanceState(Bundle outState) {
1047 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1048
1049 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1050 if (folders.size() > 0) {
1051 final int count = folders.size();
1052 long[] ids = new long[count];
1053 for (int i = 0; i < count; i++) {
1054 final FolderInfo info = folders.get(i).getInfo();
1055 ids[i] = info.id;
1056 }
1057 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1058 } else {
1059 super.onSaveInstanceState(outState);
1060 }
1061
Joe Onoratofb0ca672009-09-14 17:55:46 -04001062 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001063 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001065 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066
1067 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1068 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1069 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1070
1071 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1072 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1073 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1074 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1075 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1076 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1077 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1078 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1079 layout.getOccupiedCells());
1080 }
1081
1082 if (mFolderInfo != null && mWaitingForResult) {
1083 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1084 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1085 }
1086 }
1087
1088 @Override
1089 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001091
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001093 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001095 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 }
1097
1098 TextKeyListener.getInstance().release();
1099
Joe Onorato9c1289c2009-08-17 11:03:03 -04001100 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101
Joe Onorato9c1289c2009-08-17 11:03:03 -04001102 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001103
1104 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001105
Romain Guy1fbc1c82009-11-09 20:43:08 -08001106 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -08001107 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001108
1109 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 }
1111
1112 @Override
1113 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001114 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 super.startActivityForResult(intent, requestCode);
1116 }
1117
1118 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001119 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001121
Joe Onorato7bb17492009-09-24 17:51:01 -07001122 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001123
Karl Rosaen138a0412009-04-23 19:00:21 -07001124 if (initialQuery == null) {
1125 // Use any text typed in the launcher as the initial query
1126 initialQuery = getTypedText();
1127 clearTypedText();
1128 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 if (appSearchData == null) {
1130 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001131 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 }
Romain Guycbb89e42009-06-08 15:52:54 -07001133
Karl Rosaen138a0412009-04-23 19:00:21 -07001134 final SearchManager searchManager =
1135 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001136 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001137 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 }
1139
1140 @Override
1141 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001142 if (isWorkspaceLocked()) {
1143 return false;
1144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145
1146 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001147
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1149 .setIcon(android.R.drawable.ic_menu_add)
1150 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001151 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 .setIcon(android.R.drawable.ic_menu_gallery)
1153 .setAlphabeticShortcut('W');
1154 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1155 .setIcon(android.R.drawable.ic_search_category_default)
1156 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1157 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1158 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1159 .setAlphabeticShortcut('N');
1160
1161 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001162 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1163 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164
1165 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1166 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1167 .setIntent(settings);
1168
1169 return true;
1170 }
1171
1172 @Override
1173 public boolean onPrepareOptionsMenu(Menu menu) {
1174 super.onPrepareOptionsMenu(menu);
1175
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001176 // If all apps is animating, don't show the menu, because we don't know
1177 // which one to show.
1178 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1179 return false;
1180 }
1181
1182 // Only show the add and wallpaper options when we're not in all apps.
1183 boolean visible = !mAllAppsGrid.isOpaque();
1184 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1185 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1186
1187 // Disable add if the workspace is full.
1188 if (visible) {
1189 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1190 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1191 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192
1193 return true;
1194 }
1195
Michael Jurka0e260592010-06-30 17:07:39 -07001196 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1197 // using the settings menu to add an item, something similar happens in showAddDialog
1198 public void prepareAddItemFromHomeCustomizationDrawer() {
1199 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1200 mAddItemCellInfo = mMenuAddInfo;
1201 }
1202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 @Override
1204 public boolean onOptionsItemSelected(MenuItem item) {
1205 switch (item.getItemId()) {
1206 case MENU_ADD:
1207 addItems();
1208 return true;
1209 case MENU_WALLPAPER_SETTINGS:
1210 startWallpaper();
1211 return true;
1212 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001213 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 return true;
1215 case MENU_NOTIFICATIONS:
1216 showNotifications();
1217 return true;
1218 }
1219
1220 return super.onOptionsItemSelected(item);
1221 }
Romain Guycbb89e42009-06-08 15:52:54 -07001222
Karl Rosaen138a0412009-04-23 19:00:21 -07001223 /**
1224 * Indicates that we want global search for this activity by setting the globalSearch
1225 * argument for {@link #startSearch} to true.
1226 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001228 @Override
1229 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001230 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001231 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001232 }
1233
Joe Onorato9c1289c2009-08-17 11:03:03 -04001234 public boolean isWorkspaceLocked() {
1235 return mWorkspaceLoading || mWaitingForResult;
1236 }
1237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001239 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 showAddDialog(mMenuAddInfo);
1241 }
1242
Michael Jurkaaf442092010-06-10 17:01:57 -07001243 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo) {
1244 mAddItemCellInfo = cellInfo;
1245 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1246 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1247 addAppWidgetImpl(appWidgetId);
1248 }
1249
1250 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001251 // TODO: catch bad widget exception when sent
1252 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001253 // TODO: Is this log message meaningful?
1254 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1255 addAppWidgetImpl(appWidgetId);
1256 }
1257
1258 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001259 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260
Bjorn Bringert7984c942009-12-09 15:38:25 +00001261 if (appWidget.configure != null) {
1262 // Launch over to configure widget, if needed
1263 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1264 intent.setComponent(appWidget.configure);
1265 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1266
Romain Guy8e633c52010-03-04 12:51:36 -08001267 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001269 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001270 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
1272 }
Romain Guycbb89e42009-06-08 15:52:54 -07001273
Joe Onoratodeb98af2010-02-19 14:59:39 -08001274 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001275 // Handle case where user selected "Applications"
1276 String applicationName = getResources().getString(R.string.group_applications);
1277 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001278
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001279 if (applicationName != null && applicationName.equals(shortcutName)) {
1280 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1281 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001282
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001283 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1284 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001285 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001286 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001287 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001288 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 }
1290
1291 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001292 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001293 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001294 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001295
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001296 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001297 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001298 } else {
1299 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1300 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302
Joe Onorato9c1289c2009-08-17 11:03:03 -04001303 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 UserFolderInfo folderInfo = new UserFolderInfo();
1305 folderInfo.title = getText(R.string.folder_name);
1306
1307 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1308 cellInfo.screen = mWorkspace.getCurrentScreen();
1309 if (!findSingleSlot(cellInfo)) return;
1310
1311 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001312 LauncherModel.addItemToDatabase(this, folderInfo,
1313 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001315 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316
1317 // Create the view
1318 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1319 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1320 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001321 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 }
Romain Guycbb89e42009-06-08 15:52:54 -07001323
Joe Onorato9c1289c2009-08-17 11:03:03 -04001324 void removeFolder(FolderInfo folder) {
1325 mFolders.remove(folder.id);
1326 }
1327
1328 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 cellInfo.screen = mWorkspace.getCurrentScreen();
1330 if (!findSingleSlot(cellInfo)) return;
1331
1332 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1333
1334 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1336 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001337 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1338 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 }
1340 }
1341
1342 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1343 CellLayout.CellInfo cellInfo, boolean notify) {
1344
1345 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1346 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1347
1348 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 Intent.ShortcutIconResource iconResource = null;
1350
1351 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1352 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1353 try {
1354 iconResource = (Intent.ShortcutIconResource) extra;
1355 final PackageManager packageManager = context.getPackageManager();
1356 Resources resources = packageManager.getResourcesForApplication(
1357 iconResource.packageName);
1358 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1359 icon = resources.getDrawable(id);
1360 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001361 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 }
1363 }
1364
1365 if (icon == null) {
1366 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1367 }
1368
1369 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001370 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 info.title = name;
1372 info.iconResource = iconResource;
1373 info.uri = data.getData();
1374 info.baseIntent = baseIntent;
1375 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1376 LiveFolders.DISPLAY_MODE_GRID);
1377
1378 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1379 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001380 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381
1382 return info;
1383 }
1384
1385 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1386 final int[] xy = new int[2];
1387 if (findSlot(cellInfo, xy, 1, 1)) {
1388 cellInfo.cellX = xy[0];
1389 cellInfo.cellY = xy[1];
1390 return true;
1391 }
1392 return false;
1393 }
1394
1395 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1396 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1397 boolean[] occupied = mSavedState != null ?
1398 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1399 cellInfo = mWorkspace.findAllVacantCells(occupied);
1400 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1401 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1402 return false;
1403 }
1404 }
1405 return true;
1406 }
1407
1408 private void showNotifications() {
1409 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1410 if (statusBar != null) {
1411 statusBar.expand();
1412 }
1413 }
1414
1415 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001416 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001418 Intent chooser = Intent.createChooser(pickWallpaper,
1419 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001420 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1421 // Removed in Eclair MR1
1422// WallpaperManager wm = (WallpaperManager)
1423// getSystemService(Context.WALLPAPER_SERVICE);
1424// WallpaperInfo wi = wm.getWallpaperInfo();
1425// if (wi != null && wi.getSettingsActivity() != null) {
1426// LabeledIntent li = new LabeledIntent(getPackageName(),
1427// R.string.configure_wallpaper, 0);
1428// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1429// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1430// }
Mike Clerona0618e42009-10-22 13:55:21 -07001431 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 }
1433
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001434 /**
1435 * Registers various content observers. The current implementation registers
1436 * only a favorites observer to keep track of the favorites applications.
1437 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001438 private void registerContentObservers() {
1439 ContentResolver resolver = getContentResolver();
1440 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1441 true, mWidgetObserver);
1442 }
1443
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 @Override
1445 public boolean dispatchKeyEvent(KeyEvent event) {
1446 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1447 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001449 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001450 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001451 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001452 dumpState();
1453 return true;
1454 }
1455 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001456 }
1457 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1458 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001459 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 return true;
1461 }
1462 }
1463
1464 return super.dispatchKeyEvent(event);
1465 }
1466
Joe Onorato88ec0992009-11-19 13:16:06 -08001467 @Override
1468 public void onBackPressed() {
1469 if (isAllAppsVisible()) {
1470 closeAllApps(true);
1471 } else {
1472 closeFolder();
1473 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001474 // Some launcher layouts don't have a previous and next view
1475 if (mPreviousView != null) {
1476 dismissPreview(mPreviousView);
1477 dismissPreview(mNextView);
1478 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001479 }
1480
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 private void closeFolder() {
1482 Folder folder = mWorkspace.getOpenFolder();
1483 if (folder != null) {
1484 closeFolder(folder);
1485 }
1486 }
1487
1488 void closeFolder(Folder folder) {
1489 folder.getInfo().opened = false;
1490 ViewGroup parent = (ViewGroup) folder.getParent();
1491 if (parent != null) {
1492 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001493 if (folder instanceof DropTarget) {
1494 // Live folders aren't DropTargets.
1495 mDragController.removeDropTarget((DropTarget)folder);
1496 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 }
1498 folder.onClose();
1499 }
1500
1501 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001502 * Re-listen when widgets are reset.
1503 */
1504 private void onAppWidgetReset() {
1505 mAppWidgetHost.startListening();
1506 }
1507
1508 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001509 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1510 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001512 private void unbindDesktopItems() {
1513 for (ItemInfo item: mDesktopItems) {
1514 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 }
1516 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001517
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 /**
1519 * Launches the intent referred by the clicked shortcut.
1520 *
1521 * @param v The view representing the clicked shortcut.
1522 */
1523 public void onClick(View v) {
1524 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001525 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001527 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001528 int[] pos = new int[2];
1529 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001530 intent.setSourceBounds(new Rect(pos[0], pos[1],
1531 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001532 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 } else if (tag instanceof FolderInfo) {
1534 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001535 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001536 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001537 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001538 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001539 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001540 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 }
1542 }
1543
Michael Jurka0e260592010-06-30 17:07:39 -07001544 private final class SlideDownFinishedListener implements Animation.AnimationListener {
1545 TabHost mHomeCustomizationDrawer;
1546 SlideDownFinishedListener(TabHost homeCustomizationDrawer) {
1547 mHomeCustomizationDrawer = homeCustomizationDrawer;
1548 }
1549 public void onAnimationEnd(Animation animation) {
1550 mHomeCustomizationDrawer.setVisibility(View.GONE);
1551 }
1552 public void onAnimationRepeat(Animation animation) {}
1553 public void onAnimationStart(Animation animation) {}
1554 }
1555
1556 public boolean onTouch(View v, MotionEvent event) {
1557 // this is being forwarded from mWorkspace;
1558 // clicking anywhere on the workspace causes the drawer to slide down
1559 if (mHomeCustomizationDrawer.getVisibility() == View.VISIBLE) {
1560 Animation slideDownAnimation = AnimationUtils.loadAnimation(this, R.anim.home_customization_drawer_slide_down);
1561 slideDownAnimation.setAnimationListener(new SlideDownFinishedListener(mHomeCustomizationDrawer));
1562 mHomeCustomizationDrawer.startAnimation(slideDownAnimation);
1563 }
1564 return false;
1565 }
1566
Michael Jurkaaf442092010-06-10 17:01:57 -07001567 /**
1568 * Event handler for the "plus" button that appears on the home screen, which
1569 * enters home screen customization mode.
1570 *
1571 * @param v The view that was clicked.
1572 */
1573 public void onClickAddButton(View v) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001574
1575 // Animate the widget chooser up from the bottom of the screen
Michael Jurka0e260592010-06-30 17:07:39 -07001576 if (mHomeCustomizationDrawer.getVisibility() == View.GONE) {
1577 mHomeCustomizationDrawer.setVisibility(View.VISIBLE);
1578 mHomeCustomizationDrawer.startAnimation(AnimationUtils.loadAnimation(this, R.anim.home_customization_drawer_slide_up));
1579 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001580 }
1581
1582 public void onClickAllAppsButton(View w) {
1583 showAllApps(true);
1584 }
1585
Joe Onoratof984e852010-03-25 09:47:45 -07001586 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1588 try {
1589 startActivity(intent);
1590 } catch (ActivityNotFoundException e) {
1591 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001592 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 } catch (SecurityException e) {
1594 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001595 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001597 "or use the exported attribute for this activity. "
1598 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 }
1600 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001601
Romain Guy8e633c52010-03-04 12:51:36 -08001602 void startActivityForResultSafely(Intent intent, int requestCode) {
1603 try {
1604 startActivityForResult(intent, requestCode);
1605 } catch (ActivityNotFoundException e) {
1606 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1607 } catch (SecurityException e) {
1608 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1609 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1610 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1611 "or use the exported attribute for this activity.", e);
1612 }
1613 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614
1615 private void handleFolderClick(FolderInfo folderInfo) {
1616 if (!folderInfo.opened) {
1617 // Close any open folder
1618 closeFolder();
1619 // Open the requested folder
1620 openFolder(folderInfo);
1621 } else {
1622 // Find the open folder...
1623 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1624 int folderScreen;
1625 if (openFolder != null) {
1626 folderScreen = mWorkspace.getScreenForView(openFolder);
1627 // .. and close it
1628 closeFolder(openFolder);
1629 if (folderScreen != mWorkspace.getCurrentScreen()) {
1630 // Close any folder open on the current screen
1631 closeFolder();
1632 // Pull the folder onto this screen
1633 openFolder(folderInfo);
1634 }
1635 }
1636 }
1637 }
1638
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 /**
1640 * Opens the user fodler described by the specified tag. The opening of the folder
1641 * is animated relative to the specified View. If the View is null, no animation
1642 * is played.
1643 *
1644 * @param folderInfo The FolderInfo describing the folder to open.
1645 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001646 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 Folder openFolder;
1648
1649 if (folderInfo instanceof UserFolderInfo) {
1650 openFolder = UserFolder.fromXml(this);
1651 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001652 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 } else {
1654 return;
1655 }
1656
Joe Onorato00acb122009-08-04 16:04:30 -04001657 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 openFolder.setLauncher(this);
1659
1660 openFolder.bind(folderInfo);
1661 folderInfo.opened = true;
1662
Winson Chungaafa03c2010-06-11 17:34:16 -07001663 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1664
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 openFolder.onOpen();
1666 }
1667
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001669 switch (v.getId()) {
1670 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001671 if (!isAllAppsVisible()) {
1672 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1673 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001674 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001675 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001676 return true;
1677 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001678 if (!isAllAppsVisible()) {
1679 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1680 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001681 showPreviews(v);
1682 }
1683 return true;
1684 case R.id.all_apps_button:
1685 if (!isAllAppsVisible()) {
1686 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1687 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1688 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001689 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001690 return true;
1691 }
1692
Joe Onorato9c1289c2009-08-17 11:03:03 -04001693 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 return false;
1695 }
1696
1697 if (!(v instanceof CellLayout)) {
1698 v = (View) v.getParent();
1699 }
1700
1701 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1702
1703 // This happens when long clicking an item with the dpad/trackball
1704 if (cellInfo == null) {
1705 return true;
1706 }
1707
1708 if (mWorkspace.allowLongPress()) {
1709 if (cellInfo.cell == null) {
1710 if (cellInfo.valid) {
1711 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001712 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001713 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1714 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 showAddDialog(cellInfo);
1716 }
1717 } else {
1718 if (!(cellInfo.cell instanceof Folder)) {
1719 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001720 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1721 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 mWorkspace.startDrag(cellInfo);
1723 }
1724 }
1725 }
1726 return true;
1727 }
1728
Romain Guye6b8e2f2009-11-10 11:56:55 -08001729 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001730 private void dismissPreview(final View v) {
1731 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001732 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001733 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1734 public void onDismiss() {
1735 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1736 int count = group.getChildCount();
1737 for (int i = 0; i < count; i++) {
1738 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1739 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001740 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1741 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1742
1743 v.setTag(R.id.workspace, null);
1744 v.setTag(R.id.icon, null);
1745 window.setOnDismissListener(null);
1746 }
1747 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001748 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001749 }
1750 v.setTag(null);
1751 }
1752
Romain Guyf8e6a802009-12-07 17:48:02 -08001753 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001754 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001755 }
1756
Romain Guya6abce82009-11-10 02:54:41 -08001757 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001758 final Resources resources = getResources();
1759 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001760
Romain Guya6abce82009-11-10 02:54:41 -08001761 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001762
Romain Guy9d31e9f2009-11-11 18:54:28 -08001763 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001764
Romain Guyf8e6a802009-12-07 17:48:02 -08001765 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001766 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001767 int extraW = (int) ((r.left + r.right) * max);
1768 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001769
1770 int aW = cell.getWidth() - extraW;
1771 float w = aW / max;
1772
1773 int width = cell.getWidth();
1774 int height = cell.getHeight();
1775 int x = cell.getLeftPadding();
1776 int y = cell.getTopPadding();
1777 width -= (x + cell.getRightPadding());
1778 height -= (y + cell.getBottomPadding());
1779
1780 float scale = w / width;
1781
1782 int count = end - start;
1783
1784 final float sWidth = width * scale;
1785 float sHeight = height * scale;
1786
Romain Guye6b8e2f2009-11-10 11:56:55 -08001787 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001788
Romain Guye6b8e2f2009-11-10 11:56:55 -08001789 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1790 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001791
1792 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001793 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001794 cell = (CellLayout) workspace.getChildAt(i);
1795
Romain Guyf8e6a802009-12-07 17:48:02 -08001796 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001797 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001798
1799 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001800 c.scale(scale, scale);
1801 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1802 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001803
Romain Guy9d31e9f2009-11-11 18:54:28 -08001804 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001805 image.setImageBitmap(bitmap);
1806 image.setTag(i);
1807 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001808 image.setOnFocusChangeListener(handler);
1809 image.setFocusable(true);
1810 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001811
1812 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001813 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1814
Winson Chungaafa03c2010-06-11 17:34:16 -07001815 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001816 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001817
1818 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001819 p.setContentView(preview);
1820 p.setWidth((int) (sWidth * count + extraW));
1821 p.setHeight((int) (sHeight + extraH));
1822 p.setAnimationStyle(R.style.AnimationPreview);
1823 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001824 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001825 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001826 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001827
Romain Guye6b8e2f2009-11-10 11:56:55 -08001828 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1829 public void onDismiss() {
1830 dismissPreview(anchor);
1831 }
1832 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001833
1834 anchor.setTag(p);
1835 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001836 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001837 }
1838
Romain Guy9d31e9f2009-11-11 18:54:28 -08001839 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001840 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001841
Romain Guye6b8e2f2009-11-10 11:56:55 -08001842 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001843 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001844 }
1845
1846 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001847 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001848 v.post(this);
1849 }
1850
1851 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001852 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001853 }
1854
1855 public void onFocusChange(View v, boolean hasFocus) {
1856 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001857 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001858 }
Romain Guya6abce82009-11-10 02:54:41 -08001859 }
1860 }
1861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 Workspace getWorkspace() {
1863 return mWorkspace;
1864 }
1865
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 @Override
1867 protected Dialog onCreateDialog(int id) {
1868 switch (id) {
1869 case DIALOG_CREATE_SHORTCUT:
1870 return new CreateShortcut().createDialog();
1871 case DIALOG_RENAME_FOLDER:
1872 return new RenameFolder().createDialog();
1873 }
1874
1875 return super.onCreateDialog(id);
1876 }
1877
1878 @Override
1879 protected void onPrepareDialog(int id, Dialog dialog) {
1880 switch (id) {
1881 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 break;
1883 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001884 if (mFolderInfo != null) {
1885 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1886 final CharSequence text = mFolderInfo.title;
1887 input.setText(text);
1888 input.setSelection(0, text.length());
1889 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 break;
1891 }
1892 }
1893
1894 void showRenameDialog(FolderInfo info) {
1895 mFolderInfo = info;
1896 mWaitingForResult = true;
1897 showDialog(DIALOG_RENAME_FOLDER);
1898 }
1899
1900 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1901 mAddItemCellInfo = cellInfo;
1902 mWaitingForResult = true;
1903 showDialog(DIALOG_CREATE_SHORTCUT);
1904 }
1905
Joe Onoratodeb98af2010-02-19 14:59:39 -08001906 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07001907 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001908 Bundle bundle = new Bundle();
1909
1910 ArrayList<String> shortcutNames = new ArrayList<String>();
1911 shortcutNames.add(getString(R.string.group_applications));
1912 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1913
1914 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1915 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1916 R.drawable.ic_launcher_application));
1917 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1918
1919 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1920 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001921 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001922 pickIntent.putExtras(bundle);
1923
Joe Onoratodeb98af2010-02-19 14:59:39 -08001924 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001925 }
1926
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 private class RenameFolder {
1928 private EditText mInput;
1929
1930 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1932 mInput = (EditText) layout.findViewById(R.id.folder_name);
1933
1934 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1935 builder.setIcon(0);
1936 builder.setTitle(getString(R.string.rename_folder_title));
1937 builder.setCancelable(true);
1938 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1939 public void onCancel(DialogInterface dialog) {
1940 cleanup();
1941 }
1942 });
1943 builder.setNegativeButton(getString(R.string.cancel_action),
1944 new Dialog.OnClickListener() {
1945 public void onClick(DialogInterface dialog, int which) {
1946 cleanup();
1947 }
1948 }
1949 );
1950 builder.setPositiveButton(getString(R.string.rename_action),
1951 new Dialog.OnClickListener() {
1952 public void onClick(DialogInterface dialog, int which) {
1953 changeFolderName();
1954 }
1955 }
1956 );
1957 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001958
1959 final AlertDialog dialog = builder.create();
1960 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1961 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001962 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001963 mInput.requestFocus();
1964 InputMethodManager inputManager = (InputMethodManager)
1965 getSystemService(Context.INPUT_METHOD_SERVICE);
1966 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001967 }
1968 });
1969
1970 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 }
1972
1973 private void changeFolderName() {
1974 final String name = mInput.getText().toString();
1975 if (!TextUtils.isEmpty(name)) {
1976 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001977 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 mFolderInfo.title = name;
1979 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1980
Joe Onorato9c1289c2009-08-17 11:03:03 -04001981 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001982 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001983 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 } else {
1985 final FolderIcon folderIcon = (FolderIcon)
1986 mWorkspace.getViewForTag(mFolderInfo);
1987 if (folderIcon != null) {
1988 folderIcon.setText(name);
1989 getWorkspace().requestLayout();
1990 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001991 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001992 mWorkspaceLoading = true;
1993 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 }
1995 }
1996 }
1997 cleanup();
1998 }
1999
2000 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 dismissDialog(DIALOG_RENAME_FOLDER);
2002 mWaitingForResult = false;
2003 mFolderInfo = null;
2004 }
2005 }
2006
Daniel Sandler843e8602010-06-07 14:59:01 -04002007 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2008 public boolean isAllAppsVisible() {
2009 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002010 }
2011
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002012 // AllAppsView.Watcher
2013 public void zoomed(float zoom) {
2014 if (zoom == 1.0f) {
2015 mWorkspace.setVisibility(View.GONE);
2016 }
2017 }
2018
Joe Onorato3a8820b2009-11-10 15:06:42 -08002019 void showAllApps(boolean animated) {
2020 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08002021
Romain Guyc16fea72010-03-12 17:17:56 -08002022 ((View) mAllAppsGrid).setFocusable(true);
2023 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002024
Joe Onorato7c312c12009-08-13 21:36:53 -07002025 // TODO: fade these two too
2026 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002027 }
2028
Joe Onoratob2061212009-11-24 16:13:54 -05002029 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002030 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002031 * - Home from workspace
2032 * - from center screen
2033 * - from other screens
2034 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002035 * - from center screen
2036 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002037 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002038 * - from center screen
2039 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002040 * - Launch app from workspace and quit
2041 * - with back
2042 * - with home
2043 * - Launch app from all apps and quit
2044 * - with back
2045 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002046 * - Go to a screen that's not the default, then all
2047 * apps, and launch and app, and go back
2048 * - with back
2049 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002050 * - On workspace, long press power and go back
2051 * - with back
2052 * - with home
2053 * - On all apps, long press power and go back
2054 * - with back
2055 * - with home
2056 * - On workspace, power off
2057 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002058 * - Launch an app and turn off the screen while in that app
2059 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002060 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002061 * - From all apps
2062 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002063 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2064 * - From all apps
2065 * - From the center workspace
2066 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002067 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002068 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002069 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002070 mWorkspace.setVisibility(View.VISIBLE);
Joe Onorato3a8820b2009-11-10 15:06:42 -08002071 mAllAppsGrid.zoom(0.0f, animated);
Daniel Sandler388f6792010-03-02 14:08:08 -05002072 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002073 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002074 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002075 }
2076
Joe Onorato7c312c12009-08-13 21:36:53 -07002077 void lockAllApps() {
2078 // TODO
2079 }
2080
2081 void unlockAllApps() {
2082 // TODO
2083 }
2084
Joe Onorato7404ee42009-07-31 11:54:44 -07002085 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 * Displays the shortcut creation dialog and launches, if necessary, the
2087 * appropriate activity.
2088 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002089 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002090 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2091 DialogInterface.OnShowListener {
2092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2099 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002100 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 builder.setInverseBackgroundForced(true);
2103
2104 AlertDialog dialog = builder.create();
2105 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002106 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002107 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 return dialog;
2110 }
2111
2112 public void onCancel(DialogInterface dialog) {
2113 mWaitingForResult = false;
2114 cleanup();
2115 }
2116
Romain Guycbb89e42009-06-08 15:52:54 -07002117 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002118 }
2119
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002121 try {
2122 dismissDialog(DIALOG_CREATE_SHORTCUT);
2123 } catch (Exception e) {
2124 // An exception is thrown if the dialog is not visible, which is fine
2125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 }
2127
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002128 /**
2129 * Handle the action clicked in the "Add to home" dialog.
2130 */
2131 public void onClick(DialogInterface dialog, int which) {
2132 Resources res = getResources();
2133 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002134
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002135 switch (which) {
2136 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002137 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002138 break;
2139 }
Romain Guycbb89e42009-06-08 15:52:54 -07002140
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002141 case AddAdapter.ITEM_APPWIDGET: {
2142 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002143
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002144 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2145 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002146 // start the pick activity
2147 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2148 break;
2149 }
Romain Guycbb89e42009-06-08 15:52:54 -07002150
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002151 case AddAdapter.ITEM_LIVE_FOLDER: {
2152 // Insert extra item to handle inserting folder
2153 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002154
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002155 ArrayList<String> shortcutNames = new ArrayList<String>();
2156 shortcutNames.add(res.getString(R.string.group_folder));
2157 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002158
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002159 ArrayList<ShortcutIconResource> shortcutIcons =
2160 new ArrayList<ShortcutIconResource>();
2161 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2162 R.drawable.ic_launcher_folder));
2163 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2164
2165 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2166 pickIntent.putExtra(Intent.EXTRA_INTENT,
2167 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2168 pickIntent.putExtra(Intent.EXTRA_TITLE,
2169 getText(R.string.title_select_live_folder));
2170 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002171
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002172 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2173 break;
2174 }
2175
2176 case AddAdapter.ITEM_WALLPAPER: {
2177 startWallpaper();
2178 break;
2179 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 }
2181 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002182
2183 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002184 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002185 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 }
2187
2188 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002189 * Receives notifications when applications are added/removed.
2190 */
2191 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2192 @Override
2193 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002194 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002195 String reason = intent.getStringExtra("reason");
2196 if (!"homekey".equals(reason)) {
2197 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002198 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002199 animate = false;
2200 }
Joe Onoratob2061212009-11-24 16:13:54 -05002201 closeAllApps(animate);
2202 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002203 }
2204 }
2205
2206 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002207 * Receives notifications whenever the appwidgets are reset.
2208 */
2209 private class AppWidgetResetObserver extends ContentObserver {
2210 public AppWidgetResetObserver() {
2211 super(new Handler());
2212 }
2213
2214 @Override
2215 public void onChange(boolean selfChange) {
2216 onAppWidgetReset();
2217 }
2218 }
2219
2220 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002221 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002223 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002224 if (mWorkspace != null) {
2225 return mWorkspace.getCurrentScreen();
2226 } else {
2227 return SCREEN_COUNT / 2;
2228 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002229 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002230
Joe Onorato9c1289c2009-08-17 11:03:03 -04002231 /**
2232 * Refreshes the shortcuts shown on the workspace.
2233 *
2234 * Implementation of the method from LauncherModel.Callbacks.
2235 */
2236 public void startBinding() {
2237 final Workspace workspace = mWorkspace;
2238 int count = workspace.getChildCount();
2239 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002240 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002241 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2242 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002243
Joe Onorato9c1289c2009-08-17 11:03:03 -04002244 if (DEBUG_USER_INTERFACE) {
2245 android.widget.Button finishButton = new android.widget.Button(this);
2246 finishButton.setText("Finish");
2247 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2248
2249 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2250 public void onClick(View v) {
2251 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002252 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002253 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002254 }
2255 }
2256
2257 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002258 * Bind the items start-end from the list.
2259 *
2260 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002261 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002262 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2263
2264 final Workspace workspace = mWorkspace;
2265
2266 for (int i=start; i<end; i++) {
2267 final ItemInfo item = shortcuts.get(i);
2268 mDesktopItems.add(item);
2269 switch (item.itemType) {
2270 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2271 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002272 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002273 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2274 false);
2275 break;
2276 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2277 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2278 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2279 (UserFolderInfo) item);
2280 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2281 false);
2282 break;
2283 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2284 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2285 R.layout.live_folder_icon, this,
2286 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2287 (LiveFolderInfo) item);
2288 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2289 false);
2290 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002291 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002292 }
2293
Joe Onorato9c1289c2009-08-17 11:03:03 -04002294 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002295 }
2296
Joe Onorato9c1289c2009-08-17 11:03:03 -04002297 /**
2298 * Implementation of the method from LauncherModel.Callbacks.
2299 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002300 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2301 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002302 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002304
2305 /**
2306 * Add the views for a widget to the workspace.
2307 *
2308 * Implementation of the method from LauncherModel.Callbacks.
2309 */
2310 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002311 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2312 if (DEBUG_WIDGETS) {
2313 Log.d(TAG, "bindAppWidget: " + item);
2314 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002315 final Workspace workspace = mWorkspace;
2316
2317 final int appWidgetId = item.appWidgetId;
2318 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002319 if (DEBUG_WIDGETS) {
2320 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2321 }
2322
Joe Onorato9c1289c2009-08-17 11:03:03 -04002323 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2324
Joe Onorato9c1289c2009-08-17 11:03:03 -04002325 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2326 item.hostView.setTag(item);
2327
2328 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2329 item.cellY, item.spanX, item.spanY, false);
2330
2331 workspace.requestLayout();
2332
2333 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002334
2335 if (DEBUG_WIDGETS) {
2336 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2337 + (SystemClock.uptimeMillis()-start) + "ms");
2338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002339 }
2340
2341 /**
2342 * Callback saying that there aren't any more items to bind.
2343 *
2344 * Implementation of the method from LauncherModel.Callbacks.
2345 */
2346 public void finishBindingItems() {
2347 if (mSavedState != null) {
2348 if (!mWorkspace.hasFocus()) {
2349 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2350 }
2351
2352 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2353 if (userFolders != null) {
2354 for (long folderId : userFolders) {
2355 final FolderInfo info = mFolders.get(folderId);
2356 if (info != null) {
2357 openFolder(info);
2358 }
2359 }
2360 final Folder openFolder = mWorkspace.getOpenFolder();
2361 if (openFolder != null) {
2362 openFolder.requestFocus();
2363 }
2364 }
2365
Joe Onorato9c1289c2009-08-17 11:03:03 -04002366 mSavedState = null;
2367 }
2368
2369 if (mSavedInstanceState != null) {
2370 super.onRestoreInstanceState(mSavedInstanceState);
2371 mSavedInstanceState = null;
2372 }
2373
Joe Onorato9c1289c2009-08-17 11:03:03 -04002374 mWorkspaceLoading = false;
2375 }
2376
2377 /**
2378 * Add the icons for all apps.
2379 *
2380 * Implementation of the method from LauncherModel.Callbacks.
2381 */
2382 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002383 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002384 }
2385
2386 /**
2387 * A package was installed.
2388 *
2389 * Implementation of the method from LauncherModel.Callbacks.
2390 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002391 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002392 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002393 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002394 }
2395
2396 /**
2397 * A package was updated.
2398 *
2399 * Implementation of the method from LauncherModel.Callbacks.
2400 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002401 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002402 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002403 mWorkspace.updateShortcuts(apps);
2404 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002405 }
2406
2407 /**
2408 * A package was uninstalled.
2409 *
2410 * Implementation of the method from LauncherModel.Callbacks.
2411 */
Joe Onorato36115782010-06-17 13:28:48 -04002412 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002413 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002414 if (permanent) {
2415 mWorkspace.removeItems(apps);
2416 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002417 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002418 }
Joe Onoratobe386092009-11-17 17:32:16 -08002419
2420 /**
2421 * Prints out out state for debugging.
2422 */
2423 public void dumpState() {
2424 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002425 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002426 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2427 Log.d(TAG, "mRestoring=" + mRestoring);
2428 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2429 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2430 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2431 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002432 mModel.dumpState();
2433 mAllAppsGrid.dumpState();
2434 Log.d(TAG, "END launcher2 dump state");
2435 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436}