blob: 41f9e7dbbce276dc3c5243a1ffd376121bb7334e [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
Winson Chung80baf5a2010-08-09 16:03:15 -070019import java.io.DataInputStream;
20import java.io.DataOutputStream;
21import java.io.FileNotFoundException;
22import java.io.IOException;
23import java.util.ArrayList;
24import java.util.HashMap;
25import java.util.List;
Bjorn Bringert3e244cf2010-02-10 14:17:35 +000026
Patrick Dubroy7247f632010-08-04 16:02:59 -070027import android.animation.Animatable;
28import android.animation.AnimatableListenerAdapter;
29import android.animation.Animator;
30import android.animation.PropertyAnimator;
31import android.animation.Sequencer;
Michael Jurka946ad472010-07-09 18:05:18 -070032import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.app.Dialog;
35import android.app.SearchManager;
36import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070037import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070038import android.appwidget.AppWidgetManager;
39import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080041import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040042import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080043import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.Context;
45import android.content.DialogInterface;
46import android.content.Intent;
Patrick Dubroy7247f632010-08-04 16:02:59 -070047import android.content.IntentFilter;
Winson Chung80baf5a2010-08-09 16:03:15 -070048import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040051import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070053import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040054import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080055import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080057import android.graphics.Canvas;
Winson Chung80baf5a2010-08-09 16:03:15 -070058import android.graphics.Color;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080060import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070061import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040062import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040066import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080067import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070068import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
71import android.text.TextUtils;
72import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080075import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
79import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070080import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.View;
82import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070083import android.view.WindowManager;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070084import android.view.animation.AccelerateInterpolator;
Winson Chung80baf5a2010-08-09 16:03:15 -070085import android.view.View.OnLongClickListener;
86import android.view.animation.Animation;
87import android.view.animation.AnimationUtils;
Patrick Dubroy7247f632010-08-04 16:02:59 -070088import android.view.animation.DecelerateInterpolator;
89import android.view.animation.Interpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070092import android.widget.ImageView;
93import android.widget.LinearLayout;
94import android.widget.PopupWindow;
Winson Chung80baf5a2010-08-09 16:03:15 -070095import android.widget.RelativeLayout;
Michael Jurka0e260592010-06-30 17:07:39 -070096import android.widget.TabHost;
Winson Chung80baf5a2010-08-09 16:03:15 -070097import android.widget.TabWidget;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098import android.widget.TextView;
99import android.widget.Toast;
Winson Chung80baf5a2010-08-09 16:03:15 -0700100import android.widget.TabHost.OnTabChangeListener;
101import android.widget.TabHost.TabContentFactory;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
Winson Chung80baf5a2010-08-09 16:03:15 -0700103import com.android.common.Search;
104import com.android.launcher.R;
Romain Guyedcce092010-03-04 13:03:17 -0800105
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106/**
107 * Default launcher application.
108 */
Michael Jurka946ad472010-07-09 18:05:18 -0700109public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700110 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
111 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800112 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700113 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114
Joe Onorato9c1289c2009-08-17 11:03:03 -0400115 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400116 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400117 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700118
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 private static final int WALLPAPER_SCREENS_SPAN = 2;
120
121 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800122 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
123
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 private static final int MENU_ADD = Menu.FIRST + 1;
125 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
126 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
127 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700128 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
130 private static final int REQUEST_CREATE_SHORTCUT = 1;
131 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700132 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 private static final int REQUEST_PICK_APPLICATION = 6;
134 private static final int REQUEST_PICK_SHORTCUT = 7;
135 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700137 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
139 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
140
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800141 static final int SCREEN_COUNT = 5;
142 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Joe Onorato7c312c12009-08-13 21:36:53 -0700144 static final int DIALOG_CREATE_SHORTCUT = 1;
145 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Romain Guy98d01652009-06-30 16:21:04 -0700147 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
149 // Type: int
150 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
151 // Type: boolean
152 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
153 // Type: long
154 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
169 // Type: int[]
170 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
171 // Type: boolean
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
173 // Type: long
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
175
Winson Chung80baf5a2010-08-09 16:03:15 -0700176 // tags for the customization tabs
177 private static final String WIDGETS_TAG = "widgets";
178 private static final String FOLDERS_TAG = "folders";
179 private static final String SHORTCUTS_TAG = "shortcuts";
180 private static final String WALLPAPERS_TAG = "wallpapers";
181
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800185 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800187 private final BroadcastReceiver mCloseSystemDialogsReceiver
188 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800189 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private LayoutInflater mInflater;
192
Joe Onorato00acb122009-08-04 16:04:30 -0400193 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700195
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700196 private AppWidgetManager mAppWidgetManager;
197 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 private CellLayout.CellInfo mAddItemCellInfo;
200 private CellLayout.CellInfo mMenuAddInfo;
201 private final int[] mCellCoordinates = new int[2];
202 private FolderInfo mFolderInfo;
203
Joe Onorato7c312c12009-08-13 21:36:53 -0700204 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700205 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700206 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700207 private TabHost mHomeCustomizationDrawer;
Winson Chung80baf5a2010-08-09 16:03:15 -0700208 private CustomizePagedView mCustomizePagedView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 private Bundle mSavedState;
211
212 private SpannableStringBuilder mDefaultKeySsb = null;
213
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 private boolean mWorkspaceLoading = true;
215
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800216 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private boolean mRestoring;
218 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219
220 private Bundle mSavedInstanceState;
221
Joe Onorato9c1289c2009-08-17 11:03:03 -0400222 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800223 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400224
Romain Guy84f296c2009-11-04 15:00:44 -0800225 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
226 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Romain Guy1fbc1c82009-11-09 20:43:08 -0800228 private ImageView mPreviousView;
229 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500230
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400231 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400232 private String[] mHotseatConfig = null;
233 private Intent[] mHotseats = null;
234 private Drawable[] mHotseatIcons = null;
235 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400236
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 @Override
238 protected void onCreate(Bundle savedInstanceState) {
239 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700240
Winson Chungaafa03c2010-06-11 17:34:16 -0700241 if (LauncherApplication.isInPlaceRotationEnabled()) {
242 // hide the status bar (temporary until we get the status bar design figured out)
243 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
244 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
245 }
246
Joe Onorato0589f0f2010-02-08 13:44:00 -0800247 LauncherApplication app = ((LauncherApplication)getApplication());
248 mModel = app.setLauncher(this);
249 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400250 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700252
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700253 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700254 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
255 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 if (PROFILE_STARTUP) {
258 android.os.Debug.startMethodTracing("/sdcard/launcher");
259 }
260
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400261 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 checkForLocaleChange();
263 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700265 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
266 if (mHomeCustomizationDrawer != null) {
267 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700268
Winson Chung80baf5a2010-08-09 16:03:15 -0700269 // share the same customization workspace across all the tabs
270 mCustomizePagedView = new CustomizePagedView(this);
271 TabContentFactory contentFactory = new TabContentFactory() {
272 public View createTabContent(String tag) {
273 return mCustomizePagedView;
274 }
275 };
276
Michael Jurka946ad472010-07-09 18:05:18 -0700277 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700278 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
279 .setIndicator(widgetsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700280 String foldersLabel = getString(R.string.folders_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700281 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(FOLDERS_TAG)
282 .setIndicator(foldersLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700283 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700284 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
285 .setIndicator(shortcutsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700286 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700287 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
288 .setIndicator(wallpapersLabel).setContent(contentFactory));
289
290 // TEMP: just styling the tab widget to be a bit nicer until we get the actual
291 // new assets
292 TabWidget tabWidget = mHomeCustomizationDrawer.getTabWidget();
293 for (int i = 0; i < tabWidget.getChildCount(); ++i) {
294 RelativeLayout tab = (RelativeLayout) tabWidget.getChildTabViewAt(i);
295 TextView text = (TextView) tab.getChildAt(1);
296 text.setTextSize(20.0f);
297 text.setPadding(20, 0, 20, 0);
298 text.setShadowLayer(1.0f, 0.0f, 1.0f, Color.BLACK);
299 tab.setBackgroundDrawable(null);
300 }
301
302 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
303 public void onTabChanged(String tabId) {
304 // animate the changing of the tab content by fading pages in and out
305 final int duration = 150;
306 final float alpha = mCustomizePagedView.getAlpha();
307 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
308 "alpha", alpha, 0.0f);
309 alphaAnim.addListener(new AnimatableListenerAdapter() {
310 public void onAnimationEnd(Animatable animation) {
311 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
312 if (tag == WIDGETS_TAG) {
313 mCustomizePagedView.setCustomizationFilter(
314 CustomizePagedView.CustomizationType.WidgetCustomization);
315 } else if (tag == FOLDERS_TAG) {
316 mCustomizePagedView.setCustomizationFilter(
317 CustomizePagedView.CustomizationType.FolderCustomization);
318 } else if (tag == SHORTCUTS_TAG) {
319 mCustomizePagedView.setCustomizationFilter(
320 CustomizePagedView.CustomizationType.ShortcutCustomization);
321 } else if (tag == WALLPAPERS_TAG) {
322 mCustomizePagedView.setCustomizationFilter(
323 CustomizePagedView.CustomizationType.WallpaperCustomization);
324 }
325
326 final float alpha = mCustomizePagedView.getAlpha();
327 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
328 "alpha", alpha, 1.0f);
329 alphaAnim.start();
330 }
331 });
332 alphaAnim.start();
333 }
334 });
Michael Jurka946ad472010-07-09 18:05:18 -0700335
336 mHomeCustomizationDrawer.setCurrentTab(0);
337 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 setupViews();
339
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800340 registerContentObservers();
341
Joe Onorato7c312c12009-08-13 21:36:53 -0700342 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700343
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 mSavedState = savedInstanceState;
345 restoreState(mSavedState);
346
347 if (PROFILE_STARTUP) {
348 android.os.Debug.stopMethodTracing();
349 }
350
351 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400352 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 }
354
355 // For handling default keys
356 mDefaultKeySsb = new SpannableStringBuilder();
357 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800358
359 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
360 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 }
Romain Guycbb89e42009-06-08 15:52:54 -0700362
Winson Chungaafa03c2010-06-11 17:34:16 -0700363 @Override
364 public void onConfigurationChanged(Configuration newConfig) {
365 // TODO Auto-generated method stub
366 super.onConfigurationChanged(newConfig);
Winson Chungaafa03c2010-06-11 17:34:16 -0700367 }
368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700370 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
371 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700372
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 final Configuration configuration = getResources().getConfiguration();
374
Romain Guy98d01652009-06-30 16:21:04 -0700375 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 final String locale = configuration.locale.toString();
377
Romain Guy98d01652009-06-30 16:21:04 -0700378 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 final int mcc = configuration.mcc;
380
Romain Guy98d01652009-06-30 16:21:04 -0700381 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 final int mnc = configuration.mnc;
383
Romain Guy84f296c2009-11-04 15:00:44 -0800384 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385
Romain Guy84f296c2009-11-04 15:00:44 -0800386 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700387 localeConfiguration.locale = locale;
388 localeConfiguration.mcc = mcc;
389 localeConfiguration.mnc = mnc;
390
391 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800392 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400393
394 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700395 }
396 }
397
398 private static class LocaleConfiguration {
399 public String locale;
400 public int mcc = -1;
401 public int mnc = -1;
402 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700403
Romain Guy98d01652009-06-30 16:21:04 -0700404 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
405 DataInputStream in = null;
406 try {
407 in = new DataInputStream(context.openFileInput(PREFERENCES));
408 configuration.locale = in.readUTF();
409 configuration.mcc = in.readInt();
410 configuration.mnc = in.readInt();
411 } catch (FileNotFoundException e) {
412 // Ignore
413 } catch (IOException e) {
414 // Ignore
415 } finally {
416 if (in != null) {
417 try {
418 in.close();
419 } catch (IOException e) {
420 // Ignore
421 }
422 }
423 }
424 }
425
426 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
427 DataOutputStream out = null;
428 try {
429 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
430 out.writeUTF(configuration.locale);
431 out.writeInt(configuration.mcc);
432 out.writeInt(configuration.mnc);
433 out.flush();
434 } catch (FileNotFoundException e) {
435 // Ignore
436 } catch (IOException e) {
437 //noinspection ResultOfMethodCallIgnored
438 context.getFileStreamPath(PREFERENCES).delete();
439 } finally {
440 if (out != null) {
441 try {
442 out.close();
443 } catch (IOException e) {
444 // Ignore
445 }
446 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 }
448 }
449
450 static int getScreen() {
451 synchronized (sLock) {
452 return sScreen;
453 }
454 }
455
456 static void setScreen(int screen) {
457 synchronized (sLock) {
458 sScreen = screen;
459 }
460 }
461
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700463 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464
465 Display display = getWindowManager().getDefaultDisplay();
466 boolean isPortrait = display.getWidth() < display.getHeight();
467
468 final int width = isPortrait ? display.getWidth() : display.getHeight();
469 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700470 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 }
472
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400473 // Note: This doesn't do all the client-id magic that BrowserProvider does
474 // in Browser. (http://b/2425179)
475 private Uri getDefaultBrowserUri() {
476 String url = getString(R.string.default_browser_url);
477 if (url.indexOf("{CID}") != -1) {
478 url = url.replace("{CID}", "android-google");
479 }
480 return Uri.parse(url);
481 }
482
483 // Load the Intent templates from arrays.xml to populate the hotseats. For
484 // each Intent, if it resolves to a single app, use that as the launch
485 // intent & use that app's label as the contentDescription. Otherwise,
486 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400487 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400488 if (mHotseatConfig == null) {
489 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
490 if (mHotseatConfig.length > 0) {
491 mHotseats = new Intent[mHotseatConfig.length];
492 mHotseatLabels = new CharSequence[mHotseatConfig.length];
493 mHotseatIcons = new Drawable[mHotseatConfig.length];
494 } else {
495 mHotseats = null;
496 mHotseatIcons = null;
497 mHotseatLabels = null;
498 }
499
500 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
501 for (int i=0; i<mHotseatConfig.length; i++) {
502 // load icon for this slot; currently unrelated to the actual activity
503 try {
504 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
505 } catch (ArrayIndexOutOfBoundsException ex) {
506 Log.w(TAG, "Missing hotseat_icons array item #" + i);
507 mHotseatIcons[i] = null;
508 }
509 }
510 hotseatIconDrawables.recycle();
511 }
512
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400513 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400514 for (int i=0; i<mHotseatConfig.length; i++) {
515 Intent intent = null;
516 if (mHotseatConfig[i].equals("*BROWSER*")) {
517 // magic value meaning "launch user's default web browser"
518 // replace it with a generic web request so we can see if there is indeed a default
519 String defaultUri = getString(R.string.default_browser_url);
520 intent = new Intent(
521 Intent.ACTION_VIEW,
522 ((defaultUri != null)
523 ? Uri.parse(defaultUri)
524 : getDefaultBrowserUri())
525 ).addCategory(Intent.CATEGORY_BROWSABLE);
526 // note: if the user launches this without a default set, she
527 // will always be taken to the default URL above; this is
528 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700529 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400530 // URL is unavoidably sent to the chosen app.
531 } else {
532 try {
533 intent = Intent.parseUri(mHotseatConfig[i], 0);
534 } catch (java.net.URISyntaxException ex) {
535 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
536 // bogus; leave intent=null
537 }
538 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700539
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400540 if (intent == null) {
541 mHotseats[i] = null;
542 mHotseatLabels[i] = getText(R.string.activity_not_found);
543 continue;
544 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400545
546 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700547 Log.d(TAG, "loadHotseats: hotseat " + i
548 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400549 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400550 + "]");
551 }
552
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400553 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
554 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700555 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400556 Log.d(TAG, "Best match for intent: " + bestMatch);
557 Log.d(TAG, "All matches: ");
558 for (ResolveInfo ri : allMatches) {
559 Log.d(TAG, " --> " + ri);
560 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400561 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400562 // did this resolve to a single app, or the resolver?
563 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700564 // can't find any activity to handle this. let's leave the
565 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400566 // to launch.
567 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400568
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400569 // set accessibility text to "Not installed"
570 mHotseatLabels[i] = getText(R.string.activity_not_found);
571 } else {
572 boolean found = false;
573 for (ResolveInfo ri : allMatches) {
574 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
575 && bestMatch.activityInfo.applicationInfo.packageName
576 .equals(ri.activityInfo.applicationInfo.packageName)) {
577 found = true;
578 break;
579 }
580 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700581
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400582 if (!found) {
583 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
584 // the bestMatch is probably the ResolveActivity, meaning the
585 // user has not yet selected a default
586 // so: we'll keep the original intent for now
587 mHotseats[i] = intent;
588
589 // set the accessibility text to "Select shortcut"
590 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
591 } else {
592 // we have an app!
593 // now reconstruct the intent to launch it through the front
594 // door
595 ComponentName com = new ComponentName(
596 bestMatch.activityInfo.applicationInfo.packageName,
597 bestMatch.activityInfo.name);
598 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
599
600 // load the app label for accessibility
601 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
602 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400603 }
604
605 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700606 Log.d(TAG, "loadHotseats: hotseat " + i
607 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400608 + ((mHotseats[i] == null)
609 ? "null"
610 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400611 + "] label=[" + mHotseatLabels[i]
612 + "]"
613 );
614 }
615 }
616 }
617
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 @Override
619 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700620 mWaitingForResult = false;
621
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 // The pattern used here is that a user PICKs a specific application,
623 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700624
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
626 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700627
Romain Guy94dabf12009-07-21 10:55:43 -0700628 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 switch (requestCode) {
630 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 break;
633 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800634 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 break;
636 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 break;
639 case REQUEST_PICK_LIVE_FOLDER:
640 addLiveFolder(data);
641 break;
642 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400643 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700645 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700646 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700648 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700649 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700650 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700652 case REQUEST_PICK_WALLPAPER:
653 // We just wanted the activity result here so we can clear mWaitingForResult
654 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 }
Romain Guy18042c82009-11-06 11:44:55 -0800656 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
657 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
658 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700659 // Clean up the appWidgetId if we canceled
660 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
661 if (appWidgetId != -1) {
662 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
664 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
666
667 @Override
668 protected void onResume() {
669 super.onResume();
670
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800671 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800672
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400674 mWorkspaceLoading = true;
675 mModel.startLoader(this, true);
676 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 }
678 }
679
680 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700681 protected void onPause() {
682 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700683 // Some launcher layouts don't have a previous and next view
684 if (mPreviousView != null) {
685 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700686 }
687 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700688 dismissPreview(mNextView);
689 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800690 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700691 }
Romain Guycbb89e42009-06-08 15:52:54 -0700692
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700693 @Override
694 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400695 // Flag the loader to stop early before switching
696 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800697 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800698 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700699 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700700
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800701 // We can't hide the IME if it was forced open. So don't bother
702 /*
703 @Override
704 public void onWindowFocusChanged(boolean hasFocus) {
705 super.onWindowFocusChanged(hasFocus);
706
707 if (hasFocus) {
708 final InputMethodManager inputManager = (InputMethodManager)
709 getSystemService(Context.INPUT_METHOD_SERVICE);
710 WindowManager.LayoutParams lp = getWindow().getAttributes();
711 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
712 android.os.Handler()) {
713 protected void onReceiveResult(int resultCode, Bundle resultData) {
714 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
715 }
716 });
717 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
718 }
719 }
720 */
721
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 private boolean acceptFilter() {
723 final InputMethodManager inputManager = (InputMethodManager)
724 getSystemService(Context.INPUT_METHOD_SERVICE);
725 return !inputManager.isFullscreenMode();
726 }
727
728 @Override
729 public boolean onKeyDown(int keyCode, KeyEvent event) {
730 boolean handled = super.onKeyDown(keyCode, event);
731 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
732 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
733 keyCode, event);
734 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700735 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700736 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700737 // showSearchDialog()
738 // If there are multiple keystrokes before the search dialog takes focus,
739 // onSearchRequested() will be called for every keystroke,
740 // but it is idempotent, so it's fine.
741 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 }
743 }
744
Joe Onorato8a9625e2010-01-28 15:55:35 -0800745 // Eat the long press event so the keyboard doesn't come up.
746 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
747 return true;
748 }
749
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 return handled;
751 }
752
Karl Rosaen138a0412009-04-23 19:00:21 -0700753 private String getTypedText() {
754 return mDefaultKeySsb.toString();
755 }
756
757 private void clearTypedText() {
758 mDefaultKeySsb.clear();
759 mDefaultKeySsb.clearSpans();
760 Selection.setSelection(mDefaultKeySsb, 0);
761 }
762
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 /**
764 * Restores the previous state, if it exists.
765 *
766 * @param savedState The previous state.
767 */
768 private void restoreState(Bundle savedState) {
769 if (savedState == null) {
770 return;
771 }
772
Joe Onorato3a8820b2009-11-10 15:06:42 -0800773 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
774 if (allApps) {
775 showAllApps(false);
776 }
777
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
779 if (currentScreen > -1) {
780 mWorkspace.setCurrentScreen(currentScreen);
781 }
782
783 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
784 if (addScreen > -1) {
785 mAddItemCellInfo = new CellLayout.CellInfo();
786 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
787 addItemCellInfo.valid = true;
788 addItemCellInfo.screen = addScreen;
789 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
790 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
791 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
792 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Michael Jurkac28de512010-08-13 11:27:44 -0700793 addItemCellInfo.updateOccupiedCells(
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
795 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
796 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
797 mRestoring = true;
798 }
799
800 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
801 if (renameFolder) {
802 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400803 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 mRestoring = true;
805 }
806 }
807
808 /**
809 * Finds all the views we need and configure them properly.
810 */
811 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400812 DragController dragController = mDragController;
813
Romain Guyb1b69f52009-08-10 15:10:15 -0700814 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400815 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816
Joe Onorato7c312c12009-08-13 21:36:53 -0700817 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700818 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700819 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800820 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700821 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700822 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700823
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700824 if (LauncherApplication.isScreenXLarge()) {
825 // They need to be INVISIBLE initially so that they will be measured in the layout.
826 // Otherwise the animations are messed up when we show them for the first time.
827 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
828 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
829 }
830
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
832 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500833 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700834
Joe Onorato7c312c12009-08-13 21:36:53 -0700835 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
836 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700838 View handleView = findViewById(R.id.all_apps_button);
839 if (handleView != null && handleView instanceof HandleView) {
840 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700841 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700842 mHandleView.setLauncher(this);
843 mHandleView.setOnClickListener(this);
844 mHandleView.setOnLongClickListener(this);
845 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800846
Winson Chung80baf5a2010-08-09 16:03:15 -0700847 if (mCustomizePagedView != null) {
848 mCustomizePagedView.setLauncher(this);
849 mCustomizePagedView.setDragController(dragController);
850 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700851 } else {
852 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
853 hotseatLeft.setContentDescription(mHotseatLabels[0]);
854 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
855 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
856 hotseatRight.setContentDescription(mHotseatLabels[1]);
857 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400858
Michael Jurkaaf442092010-06-10 17:01:57 -0700859 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
860 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800861
Michael Jurkaaf442092010-06-10 17:01:57 -0700862 Drawable previous = mPreviousView.getDrawable();
863 Drawable next = mNextView.getDrawable();
864 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865
Michael Jurkaaf442092010-06-10 17:01:57 -0700866 mPreviousView.setHapticFeedbackEnabled(false);
867 mPreviousView.setOnLongClickListener(this);
868 mNextView.setHapticFeedbackEnabled(false);
869 mNextView.setOnLongClickListener(this);
870 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800871
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400873 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875
876 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400877 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700878 int deleteZoneHandleId;
879 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700880 deleteZoneHandleId = R.id.configure_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700881 } else {
882 deleteZoneHandleId = R.id.all_apps_button_cluster;
883 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700884 deleteZone.setHandle(findViewById(deleteZoneHandleId));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885
Joe Onorato00acb122009-08-04 16:04:30 -0400886 dragController.setDragScoller(workspace);
887 dragController.setDragListener(deleteZone);
888 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800889 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700890
Joe Onorato00acb122009-08-04 16:04:30 -0400891 // The order here is bottom to top.
892 dragController.addDropTarget(workspace);
893 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 }
895
Romain Guy8a73c512009-11-09 19:19:59 -0800896 @SuppressWarnings({"UnusedDeclaration"})
897 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800898 if (!isAllAppsVisible()) {
899 mWorkspace.scrollLeft();
900 }
Romain Guy8a73c512009-11-09 19:19:59 -0800901 }
902
903 @SuppressWarnings({"UnusedDeclaration"})
904 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800905 if (!isAllAppsVisible()) {
906 mWorkspace.scrollRight();
907 }
Romain Guy8a73c512009-11-09 19:19:59 -0800908 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400909
910 @SuppressWarnings({"UnusedDeclaration"})
911 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400912 if (isAllAppsVisible()) return;
913
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400914 int index = -1;
915 if (v.getId() == R.id.hotseat_left) {
916 index = 0;
917 } else if (v.getId() == R.id.hotseat_right) {
918 index = 1;
919 }
920
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400921 // reload these every tap; you never know when they might change
922 loadHotseats();
923 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
924 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400925 startActivitySafely(
926 mHotseats[index],
927 "hotseat"
928 );
929 }
930 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700931
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 /**
933 * Creates a view representing a shortcut.
934 *
935 * @param info The data structure describing the shortcut.
936 *
937 * @return A View inflated from R.layout.application.
938 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800939 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 return createShortcut(R.layout.application,
941 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
942 }
943
944 /**
945 * Creates a view representing a shortcut inflated from the specified resource.
946 *
947 * @param layoutResId The id of the XML layout used to create the shortcut.
948 * @param parent The group the shortcut belongs to.
949 * @param info The data structure describing the shortcut.
950 *
951 * @return A View inflated from layoutResId.
952 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800953 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
955
Joe Onorato0589f0f2010-02-08 13:44:00 -0800956 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
957 new FastBitmapDrawable(info.getIcon(mIconCache)),
958 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 favorite.setText(info.title);
960 favorite.setTag(info);
961 favorite.setOnClickListener(this);
962
963 return favorite;
964 }
965
966 /**
967 * Add an application shortcut to the workspace.
968 *
969 * @param data The intent describing the application.
970 * @param cellInfo The position on screen where to create the shortcut.
971 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400972 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 cellInfo.screen = mWorkspace.getCurrentScreen();
974 if (!findSingleSlot(cellInfo)) return;
975
Joe Onorato0589f0f2010-02-08 13:44:00 -0800976 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
977 data, context);
978
Romain Guy73b979d2009-06-09 12:57:21 -0700979 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800980 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800982 info.container = ItemInfo.NO_ID;
983 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
984 } else {
985 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 }
987 }
Romain Guycbb89e42009-06-08 15:52:54 -0700988
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 /**
990 * Add a shortcut to the workspace.
991 *
992 * @param data The intent describing the shortcut.
993 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400995 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 cellInfo.screen = mWorkspace.getCurrentScreen();
997 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700998
Joe Onorato0589f0f2010-02-08 13:44:00 -0800999 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000
1001 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1004 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 }
1006 }
1007
Romain Guycbb89e42009-06-08 15:52:54 -07001008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001010 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001012 * @param data The intent describing the appWidgetId.
1013 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 */
Michael Jurkaaf442092010-06-10 17:01:57 -07001015 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001016 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001017
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001018 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001020 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001021
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 // Try finding open space on Launcher screen
1023 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -08001024 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
1025 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1026 return;
1027 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001029 // Build Launcher-specific widget info and save to database
1030 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 launcherInfo.spanX = spans[0];
1032 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001033
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 LauncherModel.addItemToDatabase(this, launcherInfo,
1035 LauncherSettings.Favorites.CONTAINER_DESKTOP,
1036 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
1037
1038 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001039 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001042 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001043
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001044 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001046
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 }
1050 }
Romain Guycbb89e42009-06-08 15:52:54 -07001051
Joe Onorato9c1289c2009-08-17 11:03:03 -04001052 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1053 mDesktopItems.remove(launcherInfo);
1054 launcherInfo.hostView = null;
1055 }
1056
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001057 public LauncherAppWidgetHost getAppWidgetHost() {
1058 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 }
Romain Guycbb89e42009-06-08 15:52:54 -07001060
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001061 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001062 getWindow().closeAllPanels();
1063
1064 try {
1065 dismissDialog(DIALOG_CREATE_SHORTCUT);
1066 // Unlock the workspace if the dialog was showing
1067 } catch (Exception e) {
1068 // An exception is thrown if the dialog is not visible, which is fine
1069 }
1070
1071 try {
1072 dismissDialog(DIALOG_RENAME_FOLDER);
1073 // Unlock the workspace if the dialog was showing
1074 } catch (Exception e) {
1075 // An exception is thrown if the dialog is not visible, which is fine
1076 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001077
1078 // Whatever we were doing is hereby canceled.
1079 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001080 }
1081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 @Override
1083 protected void onNewIntent(Intent intent) {
1084 super.onNewIntent(intent);
1085
1086 // Close the menu
1087 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001088 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001089 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001090
Joe Onorato14f122b2009-11-19 14:06:36 -08001091 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1092 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1093 boolean allAppsVisible = isAllAppsVisible();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001094
Michael Jurka4cb37242010-08-09 21:05:32 -07001095 // in all these cases, only animate if we're already on home
1096 if (LauncherApplication.isScreenXLarge()) {
1097 mWorkspace.unshrink(alreadyOnHome);
1098 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08001099 if (!mWorkspace.isDefaultScreenShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001100 // on the phone, we don't animate the change to the workspace if all apps is visible
1101 // on xlarge screens, however, we want an animated transition
1102 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1103 (LauncherApplication.isScreenXLarge() || !allAppsVisible));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001104 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001105 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001106 hideCustomizationDrawer();
Romain Guy1dd3a072009-07-16 13:21:01 -07001107
Joe Onorato3a8820b2009-11-10 15:06:42 -08001108 final View v = getWindow().peekDecorView();
1109 if (v != null && v.getWindowToken() != null) {
1110 InputMethodManager imm = (InputMethodManager)getSystemService(
1111 INPUT_METHOD_SERVICE);
1112 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 }
1114 }
1115 }
1116
1117 @Override
1118 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1119 // Do not call super here
1120 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001121
1122 if (mHomeCustomizationDrawer != null) {
1123 String cur = savedInstanceState.getString("currentTab");
1124 if (cur != null) {
1125 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1126 }
1127 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 }
1129
1130 @Override
1131 protected void onSaveInstanceState(Bundle outState) {
1132 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1133
1134 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1135 if (folders.size() > 0) {
1136 final int count = folders.size();
1137 long[] ids = new long[count];
1138 for (int i = 0; i < count; i++) {
1139 final FolderInfo info = folders.get(i).getInfo();
1140 ids[i] = info.id;
1141 }
1142 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1143 } else {
1144 super.onSaveInstanceState(outState);
1145 }
1146
Joe Onoratofb0ca672009-09-14 17:55:46 -04001147 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001148 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001150 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151
1152 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1153 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1154 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1155
1156 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1157 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1158 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1159 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1160 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1161 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1162 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1163 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001164 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 }
1166
1167 if (mFolderInfo != null && mWaitingForResult) {
1168 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1169 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1170 }
Michael Jurka946ad472010-07-09 18:05:18 -07001171
1172 if (mHomeCustomizationDrawer != null) {
1173 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1174 if (currentTabTag != null) {
1175 outState.putString("currentTab", currentTabTag);
1176 }
1177 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 }
1179
1180 @Override
1181 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001185 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001187 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 }
1189
1190 TextKeyListener.getInstance().release();
1191
Joe Onorato9c1289c2009-08-17 11:03:03 -04001192 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001195
1196 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001197
Patrick Dubroyab962b72010-07-26 12:10:10 -07001198 // Some launcher layouts don't have a previous and next view
1199 if (mPreviousView != null) {
1200 dismissPreview(mPreviousView);
1201 }
1202 if (mNextView != null) {
1203 dismissPreview(mNextView);
1204 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001205
1206 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 }
1208
1209 @Override
1210 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001211 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 super.startActivityForResult(intent, requestCode);
1213 }
1214
1215 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001216 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001218
Joe Onorato7bb17492009-09-24 17:51:01 -07001219 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001220
Karl Rosaen138a0412009-04-23 19:00:21 -07001221 if (initialQuery == null) {
1222 // Use any text typed in the launcher as the initial query
1223 initialQuery = getTypedText();
1224 clearTypedText();
1225 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 if (appSearchData == null) {
1227 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001228 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 }
Romain Guycbb89e42009-06-08 15:52:54 -07001230
Karl Rosaen138a0412009-04-23 19:00:21 -07001231 final SearchManager searchManager =
1232 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001233 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001234 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 }
1236
1237 @Override
1238 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001239 if (isWorkspaceLocked()) {
1240 return false;
1241 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242
1243 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001244
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1246 .setIcon(android.R.drawable.ic_menu_add)
1247 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001248 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 .setIcon(android.R.drawable.ic_menu_gallery)
1250 .setAlphabeticShortcut('W');
1251 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1252 .setIcon(android.R.drawable.ic_search_category_default)
1253 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1254 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1255 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1256 .setAlphabeticShortcut('N');
1257
1258 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001259 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1260 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261
1262 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1263 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1264 .setIntent(settings);
1265
1266 return true;
1267 }
1268
1269 @Override
1270 public boolean onPrepareOptionsMenu(Menu menu) {
1271 super.onPrepareOptionsMenu(menu);
1272
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001273 // If all apps is animating, don't show the menu, because we don't know
1274 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001275 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001276 return false;
1277 }
1278
1279 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001280 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001281 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1282 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1283
1284 // Disable add if the workspace is full.
1285 if (visible) {
Michael Jurkac28de512010-08-13 11:27:44 -07001286 mMenuAddInfo = mWorkspace.updateOccupiedCells(null);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001287 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1288 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289
1290 return true;
1291 }
1292
Michael Jurka0e260592010-06-30 17:07:39 -07001293 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1294 // using the settings menu to add an item, something similar happens in showAddDialog
1295 public void prepareAddItemFromHomeCustomizationDrawer() {
Michael Jurkac28de512010-08-13 11:27:44 -07001296 mMenuAddInfo = mWorkspace.updateOccupiedCells(null);
Michael Jurka0e260592010-06-30 17:07:39 -07001297 mAddItemCellInfo = mMenuAddInfo;
1298 }
1299
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 @Override
1301 public boolean onOptionsItemSelected(MenuItem item) {
1302 switch (item.getItemId()) {
1303 case MENU_ADD:
1304 addItems();
1305 return true;
1306 case MENU_WALLPAPER_SETTINGS:
1307 startWallpaper();
1308 return true;
1309 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001310 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 return true;
1312 case MENU_NOTIFICATIONS:
1313 showNotifications();
1314 return true;
1315 }
1316
1317 return super.onOptionsItemSelected(item);
1318 }
Romain Guycbb89e42009-06-08 15:52:54 -07001319
Karl Rosaen138a0412009-04-23 19:00:21 -07001320 /**
1321 * Indicates that we want global search for this activity by setting the globalSearch
1322 * argument for {@link #startSearch} to true.
1323 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001325 @Override
1326 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001327 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001328 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001329 }
1330
Joe Onorato9c1289c2009-08-17 11:03:03 -04001331 public boolean isWorkspaceLocked() {
1332 return mWorkspaceLoading || mWaitingForResult;
1333 }
1334
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001336 if (LauncherApplication.isScreenXLarge()) {
1337 // Animate the widget chooser up from the bottom of the screen
1338 if (!isCustomizationDrawerVisible()) {
1339 showCustomizationDrawer();
Michael Jurka213d9632010-07-28 11:29:25 -07001340 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001341 }
1342 } else {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001343 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001344 showAddDialog(mMenuAddInfo);
1345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
1347
Michael Jurkaaf442092010-06-10 17:01:57 -07001348 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo) {
1349 mAddItemCellInfo = cellInfo;
1350 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1351 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1352 addAppWidgetImpl(appWidgetId);
1353 }
1354
1355 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001356 // TODO: catch bad widget exception when sent
1357 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001358 // TODO: Is this log message meaningful?
1359 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1360 addAppWidgetImpl(appWidgetId);
1361 }
1362
1363 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001364 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365
Bjorn Bringert7984c942009-12-09 15:38:25 +00001366 if (appWidget.configure != null) {
1367 // Launch over to configure widget, if needed
1368 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1369 intent.setComponent(appWidget.configure);
1370 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1371
Romain Guy8e633c52010-03-04 12:51:36 -08001372 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001374 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001375 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 }
1377 }
Romain Guycbb89e42009-06-08 15:52:54 -07001378
Joe Onoratodeb98af2010-02-19 14:59:39 -08001379 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001380 // Handle case where user selected "Applications"
1381 String applicationName = getResources().getString(R.string.group_applications);
1382 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001383
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001384 if (applicationName != null && applicationName.equals(shortcutName)) {
1385 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1386 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001387
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001388 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1389 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001390 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001391 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001392 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001393 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 }
1395
1396 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001397 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001398 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001399 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001400
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001401 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001402 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001403 } else {
1404 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1405 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 }
1407
Joe Onorato9c1289c2009-08-17 11:03:03 -04001408 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 UserFolderInfo folderInfo = new UserFolderInfo();
1410 folderInfo.title = getText(R.string.folder_name);
1411
1412 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1413 cellInfo.screen = mWorkspace.getCurrentScreen();
1414 if (!findSingleSlot(cellInfo)) return;
1415
1416 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001417 LauncherModel.addItemToDatabase(this, folderInfo,
1418 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001420 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421
1422 // Create the view
1423 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1424 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1425 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001426 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 }
Romain Guycbb89e42009-06-08 15:52:54 -07001428
Joe Onorato9c1289c2009-08-17 11:03:03 -04001429 void removeFolder(FolderInfo folder) {
1430 mFolders.remove(folder.id);
1431 }
1432
1433 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 cellInfo.screen = mWorkspace.getCurrentScreen();
1435 if (!findSingleSlot(cellInfo)) return;
1436
1437 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1438
1439 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1441 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001442 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1443 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 }
1445 }
1446
1447 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1448 CellLayout.CellInfo cellInfo, boolean notify) {
1449
1450 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1451 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1452
1453 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 Intent.ShortcutIconResource iconResource = null;
1455
1456 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1457 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1458 try {
1459 iconResource = (Intent.ShortcutIconResource) extra;
1460 final PackageManager packageManager = context.getPackageManager();
1461 Resources resources = packageManager.getResourcesForApplication(
1462 iconResource.packageName);
1463 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1464 icon = resources.getDrawable(id);
1465 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001466 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
1468 }
1469
1470 if (icon == null) {
1471 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1472 }
1473
1474 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001475 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 info.title = name;
1477 info.iconResource = iconResource;
1478 info.uri = data.getData();
1479 info.baseIntent = baseIntent;
1480 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1481 LiveFolders.DISPLAY_MODE_GRID);
1482
1483 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1484 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001485 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486
1487 return info;
1488 }
1489
1490 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1491 final int[] xy = new int[2];
1492 if (findSlot(cellInfo, xy, 1, 1)) {
1493 cellInfo.cellX = xy[0];
1494 cellInfo.cellY = xy[1];
1495 return true;
1496 }
1497 return false;
1498 }
1499
1500 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1501 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1502 boolean[] occupied = mSavedState != null ?
1503 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
Michael Jurkac28de512010-08-13 11:27:44 -07001504 cellInfo = mWorkspace.updateOccupiedCells(occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1506 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1507 return false;
1508 }
1509 }
1510 return true;
1511 }
1512
1513 private void showNotifications() {
1514 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1515 if (statusBar != null) {
1516 statusBar.expand();
1517 }
1518 }
1519
1520 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001521 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001523 Intent chooser = Intent.createChooser(pickWallpaper,
1524 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001525 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1526 // Removed in Eclair MR1
1527// WallpaperManager wm = (WallpaperManager)
1528// getSystemService(Context.WALLPAPER_SERVICE);
1529// WallpaperInfo wi = wm.getWallpaperInfo();
1530// if (wi != null && wi.getSettingsActivity() != null) {
1531// LabeledIntent li = new LabeledIntent(getPackageName(),
1532// R.string.configure_wallpaper, 0);
1533// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1534// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1535// }
Mike Clerona0618e42009-10-22 13:55:21 -07001536 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 }
1538
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001539 /**
1540 * Registers various content observers. The current implementation registers
1541 * only a favorites observer to keep track of the favorites applications.
1542 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001543 private void registerContentObservers() {
1544 ContentResolver resolver = getContentResolver();
1545 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1546 true, mWidgetObserver);
1547 }
1548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 @Override
1550 public boolean dispatchKeyEvent(KeyEvent event) {
1551 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1552 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001554 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001555 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001556 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001557 dumpState();
1558 return true;
1559 }
1560 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001561 }
1562 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1563 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001564 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 return true;
1566 }
1567 }
1568
1569 return super.dispatchKeyEvent(event);
1570 }
1571
Joe Onorato88ec0992009-11-19 13:16:06 -08001572 @Override
1573 public void onBackPressed() {
1574 if (isAllAppsVisible()) {
1575 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001576 } else if (isCustomizationDrawerVisible()) {
1577 hideCustomizationDrawer();
Joe Onorato88ec0992009-11-19 13:16:06 -08001578 } else {
1579 closeFolder();
1580 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001581 // Some launcher layouts don't have a previous and next view
1582 if (mPreviousView != null) {
1583 dismissPreview(mPreviousView);
1584 dismissPreview(mNextView);
1585 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001586 }
1587
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 private void closeFolder() {
1589 Folder folder = mWorkspace.getOpenFolder();
1590 if (folder != null) {
1591 closeFolder(folder);
1592 }
1593 }
1594
1595 void closeFolder(Folder folder) {
1596 folder.getInfo().opened = false;
1597 ViewGroup parent = (ViewGroup) folder.getParent();
1598 if (parent != null) {
1599 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001600 if (folder instanceof DropTarget) {
1601 // Live folders aren't DropTargets.
1602 mDragController.removeDropTarget((DropTarget)folder);
1603 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 }
1605 folder.onClose();
1606 }
1607
1608 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001609 * Re-listen when widgets are reset.
1610 */
1611 private void onAppWidgetReset() {
1612 mAppWidgetHost.startListening();
1613 }
1614
1615 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001616 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1617 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001619 private void unbindDesktopItems() {
1620 for (ItemInfo item: mDesktopItems) {
1621 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 }
1623 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001624
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 /**
1626 * Launches the intent referred by the clicked shortcut.
1627 *
1628 * @param v The view representing the clicked shortcut.
1629 */
1630 public void onClick(View v) {
1631 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001632 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001634 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001635 int[] pos = new int[2];
1636 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001637 intent.setSourceBounds(new Rect(pos[0], pos[1],
1638 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001639 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 } else if (tag instanceof FolderInfo) {
1641 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001642 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001643 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001644 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001645 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001646 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001647 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 }
1649 }
1650
Michael Jurka0e260592010-06-30 17:07:39 -07001651 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001652 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001653 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy558654c2010-07-23 16:48:11 -07001654 hideCustomizationDrawer();
Michael Jurka0e260592010-06-30 17:07:39 -07001655 return false;
1656 }
1657
Michael Jurkaaf442092010-06-10 17:01:57 -07001658 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001659 * Event handler for the search button
1660 *
1661 * @param v The view that was clicked.
1662 */
1663 public void onClickSearchButton(View v) {
1664 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1665 View button = findViewById(R.id.search_button);
1666 i.setSourceBounds(
1667 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1668 startActivity(i);
1669 }
1670
1671 /**
1672 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001673 * enters home screen customization mode.
1674 *
1675 * @param v The view that was clicked.
1676 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001677 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001678 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001679 }
1680
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001681 /**
Adam Cohenda4dc5e2010-08-10 22:11:35 -07001682 * Temporary: to allow us to test rotation in the launcher.
1683 *
1684 * @param v The view that was clicked.
1685 */
1686 public void onClickRotateButton(View v) {
1687 // TODO: this method is temporary. Remove it.
1688 setRequestedOrientation(
1689 (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
1690 ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
1691 : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
1692 }
1693
1694 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001695 * Event handler for the "grid" button that appears on the home screen, which
1696 * enters all apps mode.
1697 *
1698 * @param v The view that was clicked.
1699 */
1700 public void onClickAllAppsButton(View v) {
1701 showAllApps(true);
1702 }
1703
Joe Onoratof984e852010-03-25 09:47:45 -07001704 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1706 try {
1707 startActivity(intent);
1708 } catch (ActivityNotFoundException e) {
1709 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001710 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 } catch (SecurityException e) {
1712 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001713 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001715 "or use the exported attribute for this activity. "
1716 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 }
1718 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001719
Romain Guy8e633c52010-03-04 12:51:36 -08001720 void startActivityForResultSafely(Intent intent, int requestCode) {
1721 try {
1722 startActivityForResult(intent, requestCode);
1723 } catch (ActivityNotFoundException e) {
1724 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1725 } catch (SecurityException e) {
1726 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1727 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1728 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1729 "or use the exported attribute for this activity.", e);
1730 }
1731 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001732
1733 private void handleFolderClick(FolderInfo folderInfo) {
1734 if (!folderInfo.opened) {
1735 // Close any open folder
1736 closeFolder();
1737 // Open the requested folder
1738 openFolder(folderInfo);
1739 } else {
1740 // Find the open folder...
1741 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1742 int folderScreen;
1743 if (openFolder != null) {
1744 folderScreen = mWorkspace.getScreenForView(openFolder);
1745 // .. and close it
1746 closeFolder(openFolder);
1747 if (folderScreen != mWorkspace.getCurrentScreen()) {
1748 // Close any folder open on the current screen
1749 closeFolder();
1750 // Pull the folder onto this screen
1751 openFolder(folderInfo);
1752 }
1753 }
1754 }
1755 }
1756
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 /**
1758 * Opens the user fodler described by the specified tag. The opening of the folder
1759 * is animated relative to the specified View. If the View is null, no animation
1760 * is played.
1761 *
1762 * @param folderInfo The FolderInfo describing the folder to open.
1763 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001764 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 Folder openFolder;
1766
1767 if (folderInfo instanceof UserFolderInfo) {
1768 openFolder = UserFolder.fromXml(this);
1769 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001770 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 } else {
1772 return;
1773 }
1774
Joe Onorato00acb122009-08-04 16:04:30 -04001775 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 openFolder.setLauncher(this);
1777
1778 openFolder.bind(folderInfo);
1779 folderInfo.opened = true;
1780
Winson Chungaafa03c2010-06-11 17:34:16 -07001781 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1782
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 openFolder.onOpen();
1784 }
1785
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001787 switch (v.getId()) {
1788 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001789 if (!isAllAppsVisible()) {
1790 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1791 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001792 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001793 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001794 return true;
1795 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001796 if (!isAllAppsVisible()) {
1797 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1798 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001799 showPreviews(v);
1800 }
1801 return true;
1802 case R.id.all_apps_button:
1803 if (!isAllAppsVisible()) {
1804 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1805 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1806 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001807 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001808 return true;
1809 }
1810
Joe Onorato9c1289c2009-08-17 11:03:03 -04001811 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 return false;
1813 }
1814
1815 if (!(v instanceof CellLayout)) {
1816 v = (View) v.getParent();
1817 }
1818
1819 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1820
1821 // This happens when long clicking an item with the dpad/trackball
1822 if (cellInfo == null) {
1823 return true;
1824 }
1825
1826 if (mWorkspace.allowLongPress()) {
1827 if (cellInfo.cell == null) {
1828 if (cellInfo.valid) {
1829 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001830 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001831 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1832 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 showAddDialog(cellInfo);
1834 }
1835 } else {
1836 if (!(cellInfo.cell instanceof Folder)) {
1837 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001838 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1839 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 mWorkspace.startDrag(cellInfo);
1841 }
1842 }
1843 }
1844 return true;
1845 }
1846
Romain Guye6b8e2f2009-11-10 11:56:55 -08001847 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001848 private void dismissPreview(final View v) {
1849 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001850 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001851 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1852 public void onDismiss() {
1853 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1854 int count = group.getChildCount();
1855 for (int i = 0; i < count; i++) {
1856 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1857 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001858 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1859 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1860
1861 v.setTag(R.id.workspace, null);
1862 v.setTag(R.id.icon, null);
1863 window.setOnDismissListener(null);
1864 }
1865 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001866 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001867 }
1868 v.setTag(null);
1869 }
1870
Romain Guyf8e6a802009-12-07 17:48:02 -08001871 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001872 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001873 }
1874
Romain Guya6abce82009-11-10 02:54:41 -08001875 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001876 final Resources resources = getResources();
1877 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001878
Romain Guya6abce82009-11-10 02:54:41 -08001879 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001880
Romain Guy9d31e9f2009-11-11 18:54:28 -08001881 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001882
Romain Guyf8e6a802009-12-07 17:48:02 -08001883 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001884 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001885 int extraW = (int) ((r.left + r.right) * max);
1886 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001887
1888 int aW = cell.getWidth() - extraW;
1889 float w = aW / max;
1890
1891 int width = cell.getWidth();
1892 int height = cell.getHeight();
1893 int x = cell.getLeftPadding();
1894 int y = cell.getTopPadding();
1895 width -= (x + cell.getRightPadding());
1896 height -= (y + cell.getBottomPadding());
1897
1898 float scale = w / width;
1899
1900 int count = end - start;
1901
1902 final float sWidth = width * scale;
1903 float sHeight = height * scale;
1904
Romain Guye6b8e2f2009-11-10 11:56:55 -08001905 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001906
Romain Guye6b8e2f2009-11-10 11:56:55 -08001907 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1908 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001909
1910 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001911 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001912 cell = (CellLayout) workspace.getChildAt(i);
1913
Romain Guyf8e6a802009-12-07 17:48:02 -08001914 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001915 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001916
1917 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001918 c.scale(scale, scale);
1919 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1920 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001921
Romain Guy9d31e9f2009-11-11 18:54:28 -08001922 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001923 image.setImageBitmap(bitmap);
1924 image.setTag(i);
1925 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001926 image.setOnFocusChangeListener(handler);
1927 image.setFocusable(true);
1928 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001929
1930 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001931 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1932
Winson Chungaafa03c2010-06-11 17:34:16 -07001933 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001934 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001935
1936 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001937 p.setContentView(preview);
1938 p.setWidth((int) (sWidth * count + extraW));
1939 p.setHeight((int) (sHeight + extraH));
1940 p.setAnimationStyle(R.style.AnimationPreview);
1941 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001942 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001943 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001944 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001945
Romain Guye6b8e2f2009-11-10 11:56:55 -08001946 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1947 public void onDismiss() {
1948 dismissPreview(anchor);
1949 }
1950 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001951
1952 anchor.setTag(p);
1953 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001954 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001955 }
1956
Romain Guy9d31e9f2009-11-11 18:54:28 -08001957 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001958 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001959
Romain Guye6b8e2f2009-11-10 11:56:55 -08001960 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001961 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001962 }
1963
1964 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001965 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001966 v.post(this);
1967 }
1968
1969 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001970 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001971 }
1972
1973 public void onFocusChange(View v, boolean hasFocus) {
1974 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001975 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001976 }
Romain Guya6abce82009-11-10 02:54:41 -08001977 }
1978 }
1979
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 Workspace getWorkspace() {
1981 return mWorkspace;
1982 }
1983
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 @Override
1985 protected Dialog onCreateDialog(int id) {
1986 switch (id) {
1987 case DIALOG_CREATE_SHORTCUT:
1988 return new CreateShortcut().createDialog();
1989 case DIALOG_RENAME_FOLDER:
1990 return new RenameFolder().createDialog();
1991 }
1992
1993 return super.onCreateDialog(id);
1994 }
1995
1996 @Override
1997 protected void onPrepareDialog(int id, Dialog dialog) {
1998 switch (id) {
1999 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 break;
2001 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002002 if (mFolderInfo != null) {
2003 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2004 final CharSequence text = mFolderInfo.title;
2005 input.setText(text);
2006 input.setSelection(0, text.length());
2007 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 break;
2009 }
2010 }
2011
2012 void showRenameDialog(FolderInfo info) {
2013 mFolderInfo = info;
2014 mWaitingForResult = true;
2015 showDialog(DIALOG_RENAME_FOLDER);
2016 }
2017
2018 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2019 mAddItemCellInfo = cellInfo;
2020 mWaitingForResult = true;
2021 showDialog(DIALOG_CREATE_SHORTCUT);
2022 }
2023
Joe Onoratodeb98af2010-02-19 14:59:39 -08002024 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002025 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002026 Bundle bundle = new Bundle();
2027
2028 ArrayList<String> shortcutNames = new ArrayList<String>();
2029 shortcutNames.add(getString(R.string.group_applications));
2030 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2031
2032 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2033 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2034 R.drawable.ic_launcher_application));
2035 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2036
2037 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2038 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002039 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002040 pickIntent.putExtras(bundle);
2041
Joe Onoratodeb98af2010-02-19 14:59:39 -08002042 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002043 }
2044
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 private class RenameFolder {
2046 private EditText mInput;
2047
2048 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2050 mInput = (EditText) layout.findViewById(R.id.folder_name);
2051
2052 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2053 builder.setIcon(0);
2054 builder.setTitle(getString(R.string.rename_folder_title));
2055 builder.setCancelable(true);
2056 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2057 public void onCancel(DialogInterface dialog) {
2058 cleanup();
2059 }
2060 });
2061 builder.setNegativeButton(getString(R.string.cancel_action),
2062 new Dialog.OnClickListener() {
2063 public void onClick(DialogInterface dialog, int which) {
2064 cleanup();
2065 }
2066 }
2067 );
2068 builder.setPositiveButton(getString(R.string.rename_action),
2069 new Dialog.OnClickListener() {
2070 public void onClick(DialogInterface dialog, int which) {
2071 changeFolderName();
2072 }
2073 }
2074 );
2075 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002076
2077 final AlertDialog dialog = builder.create();
2078 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2079 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002080 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002081 mInput.requestFocus();
2082 InputMethodManager inputManager = (InputMethodManager)
2083 getSystemService(Context.INPUT_METHOD_SERVICE);
2084 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002085 }
2086 });
2087
2088 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 }
2090
2091 private void changeFolderName() {
2092 final String name = mInput.getText().toString();
2093 if (!TextUtils.isEmpty(name)) {
2094 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04002095 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 mFolderInfo.title = name;
2097 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2098
Joe Onorato9c1289c2009-08-17 11:03:03 -04002099 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002100 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002101 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 } else {
2103 final FolderIcon folderIcon = (FolderIcon)
2104 mWorkspace.getViewForTag(mFolderInfo);
2105 if (folderIcon != null) {
2106 folderIcon.setText(name);
2107 getWorkspace().requestLayout();
2108 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002109 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002110 mWorkspaceLoading = true;
2111 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 }
2113 }
2114 }
2115 cleanup();
2116 }
2117
2118 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 dismissDialog(DIALOG_RENAME_FOLDER);
2120 mWaitingForResult = false;
2121 mFolderInfo = null;
2122 }
2123 }
2124
Daniel Sandler843e8602010-06-07 14:59:01 -04002125 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2126 public boolean isAllAppsVisible() {
2127 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002128 }
2129
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002130 // AllAppsView.Watcher
2131 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002132 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2133 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002134 mWorkspace.setVisibility(View.GONE);
2135 }
2136 }
2137
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002138 /**
2139 * Zoom the camera out from the workspace to reveal 'toView'.
2140 * Assumes that the view to show is anchored at either the very top or very bottom
2141 * of the screen.
2142 * @param toView The view to show when the animation is complete
2143 * @param above If true, toView will appear from the top of the screen
2144 */
2145 private void cameraZoomOut(final View toView, boolean above) {
2146 final Resources res = getResources();
2147 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2148 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2149 final int height = toView.getHeight();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002150
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002151 // toView should appear right at the end of the workspace shrink animation
2152 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2153
2154 Interpolator interp = new DecelerateInterpolator();
2155
2156 toView.setPivotX(toView.getWidth() / 2.0f);
2157 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2158 // Assumes that the view is normally anchored to either the top or bottom of the screen
2159 final int margin = 200;
2160 if (above) {
2161 toView.setPivotY(height + ((toView.getTop() + height) / scale) + margin);
2162 } else {
2163 toView.setPivotY(0.0f - (toView.getTop() / scale) - margin);
2164 }
2165
2166 Animator scaleXAnim = new PropertyAnimator(duration, toView, "scaleX", scale, 1.0f);
2167 scaleXAnim.setInterpolator(interp);
2168 scaleXAnim.addListener(new AnimatableListenerAdapter() {
2169 public void onAnimationStart(Animatable animation) {
2170 // Prepare the position
2171 toView.setTranslationX(0.0f);
2172 toView.setTranslationY(0.0f);
2173 toView.setVisibility(View.VISIBLE);
2174 }
2175 });
2176
2177 Animator scaleYAnim = new PropertyAnimator(duration, toView, "scaleY", scale, 1.0f);
2178 scaleYAnim.setInterpolator(interp);
2179
2180 Sequencer s = new Sequencer();
2181 s.playTogether(scaleXAnim, scaleYAnim);
2182 s.play(scaleXAnim).after(startDelay);
2183 s.start();
2184 }
2185
2186 /**
2187 * Zoom the camera back into the workspace, hiding 'fromView'.
2188 * This is the opposite of cameraZoomOut.
2189 * @param fromView The currently-focused view, which will be hidden.
2190 */
2191 private void cameraZoomIn(final View fromView) {
2192 Resources res = getResources();
2193 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2194 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2195
2196 Interpolator interp = new AccelerateInterpolator();
2197
2198 Sequencer s = new Sequencer();
2199 Animator scaleXAnim = new PropertyAnimator(duration, fromView, "scaleX", scaleFactor);
2200 scaleXAnim.setInterpolator(interp);
2201 Animator scaleYAnim = new PropertyAnimator(duration, fromView, "scaleY", scaleFactor);
2202 scaleYAnim.setInterpolator(interp);
2203 s.playTogether(scaleXAnim, scaleYAnim);
2204 s.addListener(new AnimatableListenerAdapter() {
2205 public void onAnimationEnd(Animatable animation) {
2206 fromView.setVisibility(View.GONE);
2207 fromView.setScaleX(1.0f);
2208 fromView.setScaleY(1.0f);
2209 }
2210 });
2211 s.start();
2212 }
2213
2214 /**
2215 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2216 * This is the transition used on xlarge screens to go between all apps and
2217 * the home customization drawer.
2218 * @param fromView The view to pan away from.
2219 * @param toView The view to pan into the frame.
2220 */
2221 private void cameraPan(final View fromView, final View toView) {
2222 final int duration = getResources().getInteger(R.integer.config_allAppsCameraPanTime);
2223 final int workspaceHeight = mWorkspace.getHeight();
2224
2225 final boolean panDown = fromView.getY() < toView.getY();
2226
2227 final float fromViewStartY = panDown ? 0.0f : fromView.getY();
2228 final float fromViewEndY = panDown ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2229 final float toViewStartY = panDown ? workspaceHeight * 2 : -toView.getHeight() * 2;
2230 final float toViewEndY = panDown ? workspaceHeight - toView.getHeight() : 0.0f;
2231
2232 Sequencer s = new Sequencer();
2233 s.playTogether(
2234 new PropertyAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2235 new PropertyAnimator(duration, toView, "y", toViewStartY, toViewEndY));
2236 s.addListener(new AnimatableListenerAdapter() {
2237 public void onAnimationStart(Animatable animation) {
2238 toView.setVisibility(View.VISIBLE);
2239 toView.setY(toViewStartY);
2240 }
2241 public void onAnimationEnd(Animatable animation) {
2242 fromView.setVisibility(View.GONE);
2243 }
2244 });
2245 s.start();
2246 }
2247
2248 void showAllApps(boolean animated) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002249 if (mAllAppsGrid.isVisible())
2250 return;
2251
Michael Jurka79212d82010-07-30 16:36:20 -07002252 if (LauncherApplication.isScreenXLarge()) {
2253 mWorkspace.shrinkToBottom(animated);
2254 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002255
Patrick Dubroy558654c2010-07-23 16:48:11 -07002256 if (LauncherApplication.isScreenXLarge() && animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002257 if (isCustomizationDrawerVisible()) {
2258 cameraPan(mHomeCustomizationDrawer, (View) mAllAppsGrid);
Winson Chung80baf5a2010-08-09 16:03:15 -07002259 mCustomizePagedView.cleanup();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002260 } else {
2261 cameraZoomOut((View) mAllAppsGrid, true);
2262 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002263 } else {
2264 mAllAppsGrid.zoom(1.0f, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002265 hideCustomizationDrawer(); // TODO: Should be able to do this un-animated
Patrick Dubroy558654c2010-07-23 16:48:11 -07002266 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002267
Romain Guyc16fea72010-03-12 17:17:56 -08002268 ((View) mAllAppsGrid).setFocusable(true);
2269 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002270
Joe Onorato7c312c12009-08-13 21:36:53 -07002271 // TODO: fade these two too
2272 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002273 }
2274
Joe Onoratob2061212009-11-24 16:13:54 -05002275 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002276 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002277 * - Home from workspace
2278 * - from center screen
2279 * - from other screens
2280 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002281 * - from center screen
2282 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002283 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002284 * - from center screen
2285 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002286 * - Launch app from workspace and quit
2287 * - with back
2288 * - with home
2289 * - Launch app from all apps and quit
2290 * - with back
2291 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002292 * - Go to a screen that's not the default, then all
2293 * apps, and launch and app, and go back
2294 * - with back
2295 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002296 * - On workspace, long press power and go back
2297 * - with back
2298 * - with home
2299 * - On all apps, long press power and go back
2300 * - with back
2301 * - with home
2302 * - On workspace, power off
2303 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002304 * - Launch an app and turn off the screen while in that app
2305 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002306 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002307 * - From all apps
2308 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002309 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2310 * - From all apps
2311 * - From the center workspace
2312 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002313 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002314 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002315 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002316 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002317 if (LauncherApplication.isScreenXLarge() && animated) {
Michael Jurka213d9632010-07-28 11:29:25 -07002318 mWorkspace.unshrink();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002319 cameraZoomIn((View) mAllAppsGrid);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002320 } else {
2321 mAllAppsGrid.zoom(0.0f, animated);
2322 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002323 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002324 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002325 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002326 }
2327
Joe Onorato7c312c12009-08-13 21:36:53 -07002328 void lockAllApps() {
2329 // TODO
2330 }
2331
2332 void unlockAllApps() {
2333 // TODO
2334 }
2335
Patrick Dubroy558654c2010-07-23 16:48:11 -07002336 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002337 return mHomeCustomizationDrawer != null &&
2338 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002339 }
2340
2341 private void showCustomizationDrawer() {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002342 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002343 if (isAllAppsVisible()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002344 cameraPan((View) mAllAppsGrid, mHomeCustomizationDrawer);
2345 } else {
2346 cameraZoomOut(mHomeCustomizationDrawer, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002347 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002348 }
2349
Michael Jurka4bb4f732010-08-04 18:46:01 -07002350 void hideCustomizationDrawer() {
2351 hideCustomizationDrawer(true);
2352 }
2353
2354 void hideCustomizationDrawer(boolean unshrinkWorkspace) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002355 if (isCustomizationDrawerVisible()) {
Michael Jurka4bb4f732010-08-04 18:46:01 -07002356 if (unshrinkWorkspace) {
2357 mWorkspace.unshrink();
2358 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002359 cameraZoomIn(mHomeCustomizationDrawer);
Winson Chung80baf5a2010-08-09 16:03:15 -07002360 mCustomizePagedView.cleanup();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002361 }
2362 }
2363
Michael Jurka213d9632010-07-28 11:29:25 -07002364 void onWorkspaceUnshrink() {
2365 if (isAllAppsVisible()) {
Michael Jurka54dd7542010-07-30 14:47:52 -07002366 closeAllApps(true);
Michael Jurka213d9632010-07-28 11:29:25 -07002367 }
2368 if (isCustomizationDrawerVisible()) {
2369 hideCustomizationDrawer();
2370 }
2371 }
2372
Joe Onorato7404ee42009-07-31 11:54:44 -07002373 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374 * Displays the shortcut creation dialog and launches, if necessary, the
2375 * appropriate activity.
2376 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002377 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002378 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2379 DialogInterface.OnShowListener {
2380
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002382
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002385
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002386 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2387 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002388 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390 builder.setInverseBackgroundForced(true);
2391
2392 AlertDialog dialog = builder.create();
2393 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002394 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002395 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002397 return dialog;
2398 }
2399
2400 public void onCancel(DialogInterface dialog) {
2401 mWaitingForResult = false;
2402 cleanup();
2403 }
2404
Romain Guycbb89e42009-06-08 15:52:54 -07002405 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002406 }
2407
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002409 try {
2410 dismissDialog(DIALOG_CREATE_SHORTCUT);
2411 } catch (Exception e) {
2412 // An exception is thrown if the dialog is not visible, which is fine
2413 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 }
2415
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002416 /**
2417 * Handle the action clicked in the "Add to home" dialog.
2418 */
2419 public void onClick(DialogInterface dialog, int which) {
2420 Resources res = getResources();
2421 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002422
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002423 switch (which) {
2424 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002425 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002426 break;
2427 }
Romain Guycbb89e42009-06-08 15:52:54 -07002428
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002429 case AddAdapter.ITEM_APPWIDGET: {
2430 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002431
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002432 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2433 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002434 // start the pick activity
2435 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2436 break;
2437 }
Romain Guycbb89e42009-06-08 15:52:54 -07002438
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002439 case AddAdapter.ITEM_LIVE_FOLDER: {
2440 // Insert extra item to handle inserting folder
2441 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002442
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002443 ArrayList<String> shortcutNames = new ArrayList<String>();
2444 shortcutNames.add(res.getString(R.string.group_folder));
2445 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002446
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002447 ArrayList<ShortcutIconResource> shortcutIcons =
2448 new ArrayList<ShortcutIconResource>();
2449 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2450 R.drawable.ic_launcher_folder));
2451 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2452
2453 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2454 pickIntent.putExtra(Intent.EXTRA_INTENT,
2455 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2456 pickIntent.putExtra(Intent.EXTRA_TITLE,
2457 getText(R.string.title_select_live_folder));
2458 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002459
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002460 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2461 break;
2462 }
2463
2464 case AddAdapter.ITEM_WALLPAPER: {
2465 startWallpaper();
2466 break;
2467 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 }
2469 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002470
2471 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002472 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474 }
2475
2476 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002477 * Receives notifications when applications are added/removed.
2478 */
2479 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2480 @Override
2481 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002482 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002483 String reason = intent.getStringExtra("reason");
2484 if (!"homekey".equals(reason)) {
2485 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002486 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002487 animate = false;
2488 }
Joe Onoratob2061212009-11-24 16:13:54 -05002489 closeAllApps(animate);
Michael Jurka4cb37242010-08-09 21:05:32 -07002490 mWorkspace.unshrink(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002491 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002492 }
2493 }
2494
2495 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002496 * Receives notifications whenever the appwidgets are reset.
2497 */
2498 private class AppWidgetResetObserver extends ContentObserver {
2499 public AppWidgetResetObserver() {
2500 super(new Handler());
2501 }
2502
2503 @Override
2504 public void onChange(boolean selfChange) {
2505 onAppWidgetReset();
2506 }
2507 }
2508
2509 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002510 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002512 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002513 if (mWorkspace != null) {
2514 return mWorkspace.getCurrentScreen();
2515 } else {
2516 return SCREEN_COUNT / 2;
2517 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002518 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002519
Joe Onorato9c1289c2009-08-17 11:03:03 -04002520 /**
2521 * Refreshes the shortcuts shown on the workspace.
2522 *
2523 * Implementation of the method from LauncherModel.Callbacks.
2524 */
2525 public void startBinding() {
2526 final Workspace workspace = mWorkspace;
2527 int count = workspace.getChildCount();
2528 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002529 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002530 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2531 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002532
Joe Onorato9c1289c2009-08-17 11:03:03 -04002533 if (DEBUG_USER_INTERFACE) {
2534 android.widget.Button finishButton = new android.widget.Button(this);
2535 finishButton.setText("Finish");
2536 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2537
2538 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2539 public void onClick(View v) {
2540 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002541 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002542 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543 }
2544 }
2545
2546 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002547 * Bind the items start-end from the list.
2548 *
2549 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002550 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002551 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2552
2553 final Workspace workspace = mWorkspace;
2554
2555 for (int i=start; i<end; i++) {
2556 final ItemInfo item = shortcuts.get(i);
2557 mDesktopItems.add(item);
2558 switch (item.itemType) {
2559 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2560 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002561 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002562 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2563 false);
2564 break;
2565 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2566 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2567 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2568 (UserFolderInfo) item);
2569 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2570 false);
2571 break;
2572 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2573 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2574 R.layout.live_folder_icon, this,
2575 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2576 (LiveFolderInfo) item);
2577 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2578 false);
2579 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002580 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002581 }
2582
Joe Onorato9c1289c2009-08-17 11:03:03 -04002583 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002584 }
2585
Joe Onorato9c1289c2009-08-17 11:03:03 -04002586 /**
2587 * Implementation of the method from LauncherModel.Callbacks.
2588 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002589 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2590 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002591 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002592 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002593
2594 /**
2595 * Add the views for a widget to the workspace.
2596 *
2597 * Implementation of the method from LauncherModel.Callbacks.
2598 */
2599 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002600 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2601 if (DEBUG_WIDGETS) {
2602 Log.d(TAG, "bindAppWidget: " + item);
2603 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002604 final Workspace workspace = mWorkspace;
2605
2606 final int appWidgetId = item.appWidgetId;
2607 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002608 if (DEBUG_WIDGETS) {
2609 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2610 }
2611
Joe Onorato9c1289c2009-08-17 11:03:03 -04002612 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2613
Joe Onorato9c1289c2009-08-17 11:03:03 -04002614 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2615 item.hostView.setTag(item);
2616
2617 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2618 item.cellY, item.spanX, item.spanY, false);
2619
2620 workspace.requestLayout();
2621
2622 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002623
2624 if (DEBUG_WIDGETS) {
2625 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2626 + (SystemClock.uptimeMillis()-start) + "ms");
2627 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002628 }
2629
2630 /**
2631 * Callback saying that there aren't any more items to bind.
2632 *
2633 * Implementation of the method from LauncherModel.Callbacks.
2634 */
2635 public void finishBindingItems() {
2636 if (mSavedState != null) {
2637 if (!mWorkspace.hasFocus()) {
2638 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2639 }
2640
2641 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2642 if (userFolders != null) {
2643 for (long folderId : userFolders) {
2644 final FolderInfo info = mFolders.get(folderId);
2645 if (info != null) {
2646 openFolder(info);
2647 }
2648 }
2649 final Folder openFolder = mWorkspace.getOpenFolder();
2650 if (openFolder != null) {
2651 openFolder.requestFocus();
2652 }
2653 }
2654
Joe Onorato9c1289c2009-08-17 11:03:03 -04002655 mSavedState = null;
2656 }
2657
2658 if (mSavedInstanceState != null) {
2659 super.onRestoreInstanceState(mSavedInstanceState);
2660 mSavedInstanceState = null;
2661 }
2662
Joe Onorato9c1289c2009-08-17 11:03:03 -04002663 mWorkspaceLoading = false;
2664 }
2665
2666 /**
2667 * Add the icons for all apps.
2668 *
2669 * Implementation of the method from LauncherModel.Callbacks.
2670 */
2671 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002672 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002673 }
2674
2675 /**
2676 * A package was installed.
2677 *
2678 * Implementation of the method from LauncherModel.Callbacks.
2679 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002680 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002681 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002682 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002683 }
2684
2685 /**
2686 * A package was updated.
2687 *
2688 * Implementation of the method from LauncherModel.Callbacks.
2689 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002690 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002691 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002692 mWorkspace.updateShortcuts(apps);
2693 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002694 }
2695
2696 /**
2697 * A package was uninstalled.
2698 *
2699 * Implementation of the method from LauncherModel.Callbacks.
2700 */
Joe Onorato36115782010-06-17 13:28:48 -04002701 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002702 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002703 if (permanent) {
2704 mWorkspace.removeItems(apps);
2705 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002706 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002707 }
Joe Onoratobe386092009-11-17 17:32:16 -08002708
2709 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002710 * A number of packages were updated.
2711 */
2712 public void bindPackagesUpdated() {
2713 // update the customization drawer contents
2714 mCustomizePagedView.update();
2715 }
2716
2717 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002718 * Prints out out state for debugging.
2719 */
2720 public void dumpState() {
2721 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002722 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002723 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2724 Log.d(TAG, "mRestoring=" + mRestoring);
2725 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2726 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2727 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2728 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002729 mModel.dumpState();
2730 mAllAppsGrid.dumpState();
2731 Log.d(TAG, "END launcher2 dump state");
2732 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002733}