blob: 4378af19c17066acad0c58d629d945ae8c173e1c [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 /**
1682 * Event handler for the "grid" button that appears on the home screen, which
1683 * enters all apps mode.
1684 *
1685 * @param v The view that was clicked.
1686 */
1687 public void onClickAllAppsButton(View v) {
1688 showAllApps(true);
1689 }
1690
Joe Onoratof984e852010-03-25 09:47:45 -07001691 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1693 try {
1694 startActivity(intent);
1695 } catch (ActivityNotFoundException e) {
1696 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001697 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 } catch (SecurityException e) {
1699 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001700 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001702 "or use the exported attribute for this activity. "
1703 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 }
1705 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001706
Romain Guy8e633c52010-03-04 12:51:36 -08001707 void startActivityForResultSafely(Intent intent, int requestCode) {
1708 try {
1709 startActivityForResult(intent, requestCode);
1710 } catch (ActivityNotFoundException e) {
1711 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1712 } catch (SecurityException e) {
1713 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1714 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1715 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1716 "or use the exported attribute for this activity.", e);
1717 }
1718 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719
1720 private void handleFolderClick(FolderInfo folderInfo) {
1721 if (!folderInfo.opened) {
1722 // Close any open folder
1723 closeFolder();
1724 // Open the requested folder
1725 openFolder(folderInfo);
1726 } else {
1727 // Find the open folder...
1728 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1729 int folderScreen;
1730 if (openFolder != null) {
1731 folderScreen = mWorkspace.getScreenForView(openFolder);
1732 // .. and close it
1733 closeFolder(openFolder);
1734 if (folderScreen != mWorkspace.getCurrentScreen()) {
1735 // Close any folder open on the current screen
1736 closeFolder();
1737 // Pull the folder onto this screen
1738 openFolder(folderInfo);
1739 }
1740 }
1741 }
1742 }
1743
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001744 /**
1745 * Opens the user fodler described by the specified tag. The opening of the folder
1746 * is animated relative to the specified View. If the View is null, no animation
1747 * is played.
1748 *
1749 * @param folderInfo The FolderInfo describing the folder to open.
1750 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001751 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 Folder openFolder;
1753
1754 if (folderInfo instanceof UserFolderInfo) {
1755 openFolder = UserFolder.fromXml(this);
1756 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001757 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 } else {
1759 return;
1760 }
1761
Joe Onorato00acb122009-08-04 16:04:30 -04001762 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 openFolder.setLauncher(this);
1764
1765 openFolder.bind(folderInfo);
1766 folderInfo.opened = true;
1767
Winson Chungaafa03c2010-06-11 17:34:16 -07001768 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1769
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 openFolder.onOpen();
1771 }
1772
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001774 switch (v.getId()) {
1775 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001776 if (!isAllAppsVisible()) {
1777 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1778 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001779 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001780 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001781 return true;
1782 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001783 if (!isAllAppsVisible()) {
1784 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1785 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001786 showPreviews(v);
1787 }
1788 return true;
1789 case R.id.all_apps_button:
1790 if (!isAllAppsVisible()) {
1791 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1792 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1793 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001794 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001795 return true;
1796 }
1797
Joe Onorato9c1289c2009-08-17 11:03:03 -04001798 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 return false;
1800 }
1801
1802 if (!(v instanceof CellLayout)) {
1803 v = (View) v.getParent();
1804 }
1805
1806 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1807
1808 // This happens when long clicking an item with the dpad/trackball
1809 if (cellInfo == null) {
1810 return true;
1811 }
1812
1813 if (mWorkspace.allowLongPress()) {
1814 if (cellInfo.cell == null) {
1815 if (cellInfo.valid) {
1816 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001817 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001818 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1819 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 showAddDialog(cellInfo);
1821 }
1822 } else {
1823 if (!(cellInfo.cell instanceof Folder)) {
1824 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001825 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1826 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 mWorkspace.startDrag(cellInfo);
1828 }
1829 }
1830 }
1831 return true;
1832 }
1833
Romain Guye6b8e2f2009-11-10 11:56:55 -08001834 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001835 private void dismissPreview(final View v) {
1836 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001837 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001838 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1839 public void onDismiss() {
1840 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1841 int count = group.getChildCount();
1842 for (int i = 0; i < count; i++) {
1843 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1844 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001845 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1846 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1847
1848 v.setTag(R.id.workspace, null);
1849 v.setTag(R.id.icon, null);
1850 window.setOnDismissListener(null);
1851 }
1852 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001853 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001854 }
1855 v.setTag(null);
1856 }
1857
Romain Guyf8e6a802009-12-07 17:48:02 -08001858 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001859 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001860 }
1861
Romain Guya6abce82009-11-10 02:54:41 -08001862 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001863 final Resources resources = getResources();
1864 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001865
Romain Guya6abce82009-11-10 02:54:41 -08001866 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001867
Romain Guy9d31e9f2009-11-11 18:54:28 -08001868 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001869
Romain Guyf8e6a802009-12-07 17:48:02 -08001870 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001871 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001872 int extraW = (int) ((r.left + r.right) * max);
1873 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001874
1875 int aW = cell.getWidth() - extraW;
1876 float w = aW / max;
1877
1878 int width = cell.getWidth();
1879 int height = cell.getHeight();
1880 int x = cell.getLeftPadding();
1881 int y = cell.getTopPadding();
1882 width -= (x + cell.getRightPadding());
1883 height -= (y + cell.getBottomPadding());
1884
1885 float scale = w / width;
1886
1887 int count = end - start;
1888
1889 final float sWidth = width * scale;
1890 float sHeight = height * scale;
1891
Romain Guye6b8e2f2009-11-10 11:56:55 -08001892 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001893
Romain Guye6b8e2f2009-11-10 11:56:55 -08001894 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1895 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001896
1897 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001898 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001899 cell = (CellLayout) workspace.getChildAt(i);
1900
Romain Guyf8e6a802009-12-07 17:48:02 -08001901 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001902 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001903
1904 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001905 c.scale(scale, scale);
1906 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1907 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001908
Romain Guy9d31e9f2009-11-11 18:54:28 -08001909 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001910 image.setImageBitmap(bitmap);
1911 image.setTag(i);
1912 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001913 image.setOnFocusChangeListener(handler);
1914 image.setFocusable(true);
1915 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001916
1917 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001918 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1919
Winson Chungaafa03c2010-06-11 17:34:16 -07001920 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001921 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001922
1923 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001924 p.setContentView(preview);
1925 p.setWidth((int) (sWidth * count + extraW));
1926 p.setHeight((int) (sHeight + extraH));
1927 p.setAnimationStyle(R.style.AnimationPreview);
1928 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001929 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001930 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001931 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001932
Romain Guye6b8e2f2009-11-10 11:56:55 -08001933 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1934 public void onDismiss() {
1935 dismissPreview(anchor);
1936 }
1937 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001938
1939 anchor.setTag(p);
1940 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001941 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001942 }
1943
Romain Guy9d31e9f2009-11-11 18:54:28 -08001944 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001945 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001946
Romain Guye6b8e2f2009-11-10 11:56:55 -08001947 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001948 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001949 }
1950
1951 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001952 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001953 v.post(this);
1954 }
1955
1956 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001957 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001958 }
1959
1960 public void onFocusChange(View v, boolean hasFocus) {
1961 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001962 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001963 }
Romain Guya6abce82009-11-10 02:54:41 -08001964 }
1965 }
1966
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 Workspace getWorkspace() {
1968 return mWorkspace;
1969 }
1970
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 @Override
1972 protected Dialog onCreateDialog(int id) {
1973 switch (id) {
1974 case DIALOG_CREATE_SHORTCUT:
1975 return new CreateShortcut().createDialog();
1976 case DIALOG_RENAME_FOLDER:
1977 return new RenameFolder().createDialog();
1978 }
1979
1980 return super.onCreateDialog(id);
1981 }
1982
1983 @Override
1984 protected void onPrepareDialog(int id, Dialog dialog) {
1985 switch (id) {
1986 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 break;
1988 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001989 if (mFolderInfo != null) {
1990 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1991 final CharSequence text = mFolderInfo.title;
1992 input.setText(text);
1993 input.setSelection(0, text.length());
1994 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 break;
1996 }
1997 }
1998
1999 void showRenameDialog(FolderInfo info) {
2000 mFolderInfo = info;
2001 mWaitingForResult = true;
2002 showDialog(DIALOG_RENAME_FOLDER);
2003 }
2004
2005 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2006 mAddItemCellInfo = cellInfo;
2007 mWaitingForResult = true;
2008 showDialog(DIALOG_CREATE_SHORTCUT);
2009 }
2010
Joe Onoratodeb98af2010-02-19 14:59:39 -08002011 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002012 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002013 Bundle bundle = new Bundle();
2014
2015 ArrayList<String> shortcutNames = new ArrayList<String>();
2016 shortcutNames.add(getString(R.string.group_applications));
2017 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2018
2019 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2020 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2021 R.drawable.ic_launcher_application));
2022 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2023
2024 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2025 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002026 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002027 pickIntent.putExtras(bundle);
2028
Joe Onoratodeb98af2010-02-19 14:59:39 -08002029 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002030 }
2031
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 private class RenameFolder {
2033 private EditText mInput;
2034
2035 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2037 mInput = (EditText) layout.findViewById(R.id.folder_name);
2038
2039 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2040 builder.setIcon(0);
2041 builder.setTitle(getString(R.string.rename_folder_title));
2042 builder.setCancelable(true);
2043 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2044 public void onCancel(DialogInterface dialog) {
2045 cleanup();
2046 }
2047 });
2048 builder.setNegativeButton(getString(R.string.cancel_action),
2049 new Dialog.OnClickListener() {
2050 public void onClick(DialogInterface dialog, int which) {
2051 cleanup();
2052 }
2053 }
2054 );
2055 builder.setPositiveButton(getString(R.string.rename_action),
2056 new Dialog.OnClickListener() {
2057 public void onClick(DialogInterface dialog, int which) {
2058 changeFolderName();
2059 }
2060 }
2061 );
2062 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002063
2064 final AlertDialog dialog = builder.create();
2065 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2066 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002067 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002068 mInput.requestFocus();
2069 InputMethodManager inputManager = (InputMethodManager)
2070 getSystemService(Context.INPUT_METHOD_SERVICE);
2071 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002072 }
2073 });
2074
2075 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 }
2077
2078 private void changeFolderName() {
2079 final String name = mInput.getText().toString();
2080 if (!TextUtils.isEmpty(name)) {
2081 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04002082 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 mFolderInfo.title = name;
2084 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2085
Joe Onorato9c1289c2009-08-17 11:03:03 -04002086 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002087 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002088 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 } else {
2090 final FolderIcon folderIcon = (FolderIcon)
2091 mWorkspace.getViewForTag(mFolderInfo);
2092 if (folderIcon != null) {
2093 folderIcon.setText(name);
2094 getWorkspace().requestLayout();
2095 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002096 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002097 mWorkspaceLoading = true;
2098 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 }
2100 }
2101 }
2102 cleanup();
2103 }
2104
2105 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 dismissDialog(DIALOG_RENAME_FOLDER);
2107 mWaitingForResult = false;
2108 mFolderInfo = null;
2109 }
2110 }
2111
Daniel Sandler843e8602010-06-07 14:59:01 -04002112 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2113 public boolean isAllAppsVisible() {
2114 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002115 }
2116
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002117 // AllAppsView.Watcher
2118 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002119 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2120 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002121 mWorkspace.setVisibility(View.GONE);
2122 }
2123 }
2124
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002125 /**
2126 * Zoom the camera out from the workspace to reveal 'toView'.
2127 * Assumes that the view to show is anchored at either the very top or very bottom
2128 * of the screen.
2129 * @param toView The view to show when the animation is complete
2130 * @param above If true, toView will appear from the top of the screen
2131 */
2132 private void cameraZoomOut(final View toView, boolean above) {
2133 final Resources res = getResources();
2134 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2135 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2136 final int height = toView.getHeight();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002137
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002138 // toView should appear right at the end of the workspace shrink animation
2139 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2140
2141 Interpolator interp = new DecelerateInterpolator();
2142
2143 toView.setPivotX(toView.getWidth() / 2.0f);
2144 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2145 // Assumes that the view is normally anchored to either the top or bottom of the screen
2146 final int margin = 200;
2147 if (above) {
2148 toView.setPivotY(height + ((toView.getTop() + height) / scale) + margin);
2149 } else {
2150 toView.setPivotY(0.0f - (toView.getTop() / scale) - margin);
2151 }
2152
2153 Animator scaleXAnim = new PropertyAnimator(duration, toView, "scaleX", scale, 1.0f);
2154 scaleXAnim.setInterpolator(interp);
2155 scaleXAnim.addListener(new AnimatableListenerAdapter() {
2156 public void onAnimationStart(Animatable animation) {
2157 // Prepare the position
2158 toView.setTranslationX(0.0f);
2159 toView.setTranslationY(0.0f);
2160 toView.setVisibility(View.VISIBLE);
2161 }
2162 });
2163
2164 Animator scaleYAnim = new PropertyAnimator(duration, toView, "scaleY", scale, 1.0f);
2165 scaleYAnim.setInterpolator(interp);
2166
2167 Sequencer s = new Sequencer();
2168 s.playTogether(scaleXAnim, scaleYAnim);
2169 s.play(scaleXAnim).after(startDelay);
2170 s.start();
2171 }
2172
2173 /**
2174 * Zoom the camera back into the workspace, hiding 'fromView'.
2175 * This is the opposite of cameraZoomOut.
2176 * @param fromView The currently-focused view, which will be hidden.
2177 */
2178 private void cameraZoomIn(final View fromView) {
2179 Resources res = getResources();
2180 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2181 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2182
2183 Interpolator interp = new AccelerateInterpolator();
2184
2185 Sequencer s = new Sequencer();
2186 Animator scaleXAnim = new PropertyAnimator(duration, fromView, "scaleX", scaleFactor);
2187 scaleXAnim.setInterpolator(interp);
2188 Animator scaleYAnim = new PropertyAnimator(duration, fromView, "scaleY", scaleFactor);
2189 scaleYAnim.setInterpolator(interp);
2190 s.playTogether(scaleXAnim, scaleYAnim);
2191 s.addListener(new AnimatableListenerAdapter() {
2192 public void onAnimationEnd(Animatable animation) {
2193 fromView.setVisibility(View.GONE);
2194 fromView.setScaleX(1.0f);
2195 fromView.setScaleY(1.0f);
2196 }
2197 });
2198 s.start();
2199 }
2200
2201 /**
2202 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2203 * This is the transition used on xlarge screens to go between all apps and
2204 * the home customization drawer.
2205 * @param fromView The view to pan away from.
2206 * @param toView The view to pan into the frame.
2207 */
2208 private void cameraPan(final View fromView, final View toView) {
2209 final int duration = getResources().getInteger(R.integer.config_allAppsCameraPanTime);
2210 final int workspaceHeight = mWorkspace.getHeight();
2211
2212 final boolean panDown = fromView.getY() < toView.getY();
2213
2214 final float fromViewStartY = panDown ? 0.0f : fromView.getY();
2215 final float fromViewEndY = panDown ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2216 final float toViewStartY = panDown ? workspaceHeight * 2 : -toView.getHeight() * 2;
2217 final float toViewEndY = panDown ? workspaceHeight - toView.getHeight() : 0.0f;
2218
2219 Sequencer s = new Sequencer();
2220 s.playTogether(
2221 new PropertyAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2222 new PropertyAnimator(duration, toView, "y", toViewStartY, toViewEndY));
2223 s.addListener(new AnimatableListenerAdapter() {
2224 public void onAnimationStart(Animatable animation) {
2225 toView.setVisibility(View.VISIBLE);
2226 toView.setY(toViewStartY);
2227 }
2228 public void onAnimationEnd(Animatable animation) {
2229 fromView.setVisibility(View.GONE);
2230 }
2231 });
2232 s.start();
2233 }
2234
2235 void showAllApps(boolean animated) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002236 if (mAllAppsGrid.isVisible())
2237 return;
2238
Michael Jurka79212d82010-07-30 16:36:20 -07002239 if (LauncherApplication.isScreenXLarge()) {
2240 mWorkspace.shrinkToBottom(animated);
2241 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002242
Patrick Dubroy558654c2010-07-23 16:48:11 -07002243 if (LauncherApplication.isScreenXLarge() && animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002244 if (isCustomizationDrawerVisible()) {
2245 cameraPan(mHomeCustomizationDrawer, (View) mAllAppsGrid);
Winson Chung80baf5a2010-08-09 16:03:15 -07002246 mCustomizePagedView.cleanup();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002247 } else {
2248 cameraZoomOut((View) mAllAppsGrid, true);
2249 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002250 } else {
2251 mAllAppsGrid.zoom(1.0f, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002252 hideCustomizationDrawer(); // TODO: Should be able to do this un-animated
Patrick Dubroy558654c2010-07-23 16:48:11 -07002253 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002254
Romain Guyc16fea72010-03-12 17:17:56 -08002255 ((View) mAllAppsGrid).setFocusable(true);
2256 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002257
Joe Onorato7c312c12009-08-13 21:36:53 -07002258 // TODO: fade these two too
2259 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002260 }
2261
Joe Onoratob2061212009-11-24 16:13:54 -05002262 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002263 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002264 * - Home from workspace
2265 * - from center screen
2266 * - from other screens
2267 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002268 * - from center screen
2269 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002270 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002271 * - from center screen
2272 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002273 * - Launch app from workspace and quit
2274 * - with back
2275 * - with home
2276 * - Launch app from all apps and quit
2277 * - with back
2278 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002279 * - Go to a screen that's not the default, then all
2280 * apps, and launch and app, and go back
2281 * - with back
2282 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002283 * - On workspace, long press power and go back
2284 * - with back
2285 * - with home
2286 * - On all apps, long press power and go back
2287 * - with back
2288 * - with home
2289 * - On workspace, power off
2290 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002291 * - Launch an app and turn off the screen while in that app
2292 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002293 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002294 * - From all apps
2295 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002296 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2297 * - From all apps
2298 * - From the center workspace
2299 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002300 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002301 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002302 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002303 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002304 if (LauncherApplication.isScreenXLarge() && animated) {
Michael Jurka213d9632010-07-28 11:29:25 -07002305 mWorkspace.unshrink();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002306 cameraZoomIn((View) mAllAppsGrid);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002307 } else {
2308 mAllAppsGrid.zoom(0.0f, animated);
2309 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002310 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002311 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002312 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002313 }
2314
Joe Onorato7c312c12009-08-13 21:36:53 -07002315 void lockAllApps() {
2316 // TODO
2317 }
2318
2319 void unlockAllApps() {
2320 // TODO
2321 }
2322
Patrick Dubroy558654c2010-07-23 16:48:11 -07002323 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002324 return mHomeCustomizationDrawer != null &&
2325 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002326 }
2327
2328 private void showCustomizationDrawer() {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002329 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002330 if (isAllAppsVisible()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002331 cameraPan((View) mAllAppsGrid, mHomeCustomizationDrawer);
2332 } else {
2333 cameraZoomOut(mHomeCustomizationDrawer, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002334 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002335 }
2336
Michael Jurka4bb4f732010-08-04 18:46:01 -07002337 void hideCustomizationDrawer() {
2338 hideCustomizationDrawer(true);
2339 }
2340
2341 void hideCustomizationDrawer(boolean unshrinkWorkspace) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002342 if (isCustomizationDrawerVisible()) {
Michael Jurka4bb4f732010-08-04 18:46:01 -07002343 if (unshrinkWorkspace) {
2344 mWorkspace.unshrink();
2345 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002346 cameraZoomIn(mHomeCustomizationDrawer);
Winson Chung80baf5a2010-08-09 16:03:15 -07002347 mCustomizePagedView.cleanup();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002348 }
2349 }
2350
Michael Jurka213d9632010-07-28 11:29:25 -07002351 void onWorkspaceUnshrink() {
2352 if (isAllAppsVisible()) {
Michael Jurka54dd7542010-07-30 14:47:52 -07002353 closeAllApps(true);
Michael Jurka213d9632010-07-28 11:29:25 -07002354 }
2355 if (isCustomizationDrawerVisible()) {
2356 hideCustomizationDrawer();
2357 }
2358 }
2359
Joe Onorato7404ee42009-07-31 11:54:44 -07002360 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002361 * Displays the shortcut creation dialog and launches, if necessary, the
2362 * appropriate activity.
2363 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002364 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002365 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2366 DialogInterface.OnShowListener {
2367
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002368 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002369
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002370 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002371 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002372
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002373 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2374 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002375 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002376
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002377 builder.setInverseBackgroundForced(true);
2378
2379 AlertDialog dialog = builder.create();
2380 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002381 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002382 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 return dialog;
2385 }
2386
2387 public void onCancel(DialogInterface dialog) {
2388 mWaitingForResult = false;
2389 cleanup();
2390 }
2391
Romain Guycbb89e42009-06-08 15:52:54 -07002392 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002393 }
2394
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002395 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002396 try {
2397 dismissDialog(DIALOG_CREATE_SHORTCUT);
2398 } catch (Exception e) {
2399 // An exception is thrown if the dialog is not visible, which is fine
2400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 }
2402
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002403 /**
2404 * Handle the action clicked in the "Add to home" dialog.
2405 */
2406 public void onClick(DialogInterface dialog, int which) {
2407 Resources res = getResources();
2408 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002409
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002410 switch (which) {
2411 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002412 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002413 break;
2414 }
Romain Guycbb89e42009-06-08 15:52:54 -07002415
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002416 case AddAdapter.ITEM_APPWIDGET: {
2417 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002418
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002419 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2420 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002421 // start the pick activity
2422 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2423 break;
2424 }
Romain Guycbb89e42009-06-08 15:52:54 -07002425
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002426 case AddAdapter.ITEM_LIVE_FOLDER: {
2427 // Insert extra item to handle inserting folder
2428 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002429
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002430 ArrayList<String> shortcutNames = new ArrayList<String>();
2431 shortcutNames.add(res.getString(R.string.group_folder));
2432 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002433
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002434 ArrayList<ShortcutIconResource> shortcutIcons =
2435 new ArrayList<ShortcutIconResource>();
2436 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2437 R.drawable.ic_launcher_folder));
2438 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2439
2440 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2441 pickIntent.putExtra(Intent.EXTRA_INTENT,
2442 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2443 pickIntent.putExtra(Intent.EXTRA_TITLE,
2444 getText(R.string.title_select_live_folder));
2445 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002446
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002447 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2448 break;
2449 }
2450
2451 case AddAdapter.ITEM_WALLPAPER: {
2452 startWallpaper();
2453 break;
2454 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 }
2456 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002457
2458 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002459 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002461 }
2462
2463 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002464 * Receives notifications when applications are added/removed.
2465 */
2466 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2467 @Override
2468 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002469 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002470 String reason = intent.getStringExtra("reason");
2471 if (!"homekey".equals(reason)) {
2472 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002473 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002474 animate = false;
2475 }
Joe Onoratob2061212009-11-24 16:13:54 -05002476 closeAllApps(animate);
Michael Jurka4cb37242010-08-09 21:05:32 -07002477 mWorkspace.unshrink(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002478 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002479 }
2480 }
2481
2482 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002483 * Receives notifications whenever the appwidgets are reset.
2484 */
2485 private class AppWidgetResetObserver extends ContentObserver {
2486 public AppWidgetResetObserver() {
2487 super(new Handler());
2488 }
2489
2490 @Override
2491 public void onChange(boolean selfChange) {
2492 onAppWidgetReset();
2493 }
2494 }
2495
2496 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002497 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002499 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002500 if (mWorkspace != null) {
2501 return mWorkspace.getCurrentScreen();
2502 } else {
2503 return SCREEN_COUNT / 2;
2504 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002505 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002506
Joe Onorato9c1289c2009-08-17 11:03:03 -04002507 /**
2508 * Refreshes the shortcuts shown on the workspace.
2509 *
2510 * Implementation of the method from LauncherModel.Callbacks.
2511 */
2512 public void startBinding() {
2513 final Workspace workspace = mWorkspace;
2514 int count = workspace.getChildCount();
2515 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002516 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002517 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2518 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002519
Joe Onorato9c1289c2009-08-17 11:03:03 -04002520 if (DEBUG_USER_INTERFACE) {
2521 android.widget.Button finishButton = new android.widget.Button(this);
2522 finishButton.setText("Finish");
2523 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2524
2525 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2526 public void onClick(View v) {
2527 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002528 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002529 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002530 }
2531 }
2532
2533 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002534 * Bind the items start-end from the list.
2535 *
2536 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002537 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002538 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2539
2540 final Workspace workspace = mWorkspace;
2541
2542 for (int i=start; i<end; i++) {
2543 final ItemInfo item = shortcuts.get(i);
2544 mDesktopItems.add(item);
2545 switch (item.itemType) {
2546 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2547 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002548 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002549 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2550 false);
2551 break;
2552 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2553 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2554 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2555 (UserFolderInfo) item);
2556 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2557 false);
2558 break;
2559 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2560 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2561 R.layout.live_folder_icon, this,
2562 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2563 (LiveFolderInfo) item);
2564 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2565 false);
2566 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002568 }
2569
Joe Onorato9c1289c2009-08-17 11:03:03 -04002570 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002571 }
2572
Joe Onorato9c1289c2009-08-17 11:03:03 -04002573 /**
2574 * Implementation of the method from LauncherModel.Callbacks.
2575 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002576 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2577 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002578 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002579 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002580
2581 /**
2582 * Add the views for a widget to the workspace.
2583 *
2584 * Implementation of the method from LauncherModel.Callbacks.
2585 */
2586 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002587 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2588 if (DEBUG_WIDGETS) {
2589 Log.d(TAG, "bindAppWidget: " + item);
2590 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002591 final Workspace workspace = mWorkspace;
2592
2593 final int appWidgetId = item.appWidgetId;
2594 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002595 if (DEBUG_WIDGETS) {
2596 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2597 }
2598
Joe Onorato9c1289c2009-08-17 11:03:03 -04002599 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2600
Joe Onorato9c1289c2009-08-17 11:03:03 -04002601 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2602 item.hostView.setTag(item);
2603
2604 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2605 item.cellY, item.spanX, item.spanY, false);
2606
2607 workspace.requestLayout();
2608
2609 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002610
2611 if (DEBUG_WIDGETS) {
2612 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2613 + (SystemClock.uptimeMillis()-start) + "ms");
2614 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002615 }
2616
2617 /**
2618 * Callback saying that there aren't any more items to bind.
2619 *
2620 * Implementation of the method from LauncherModel.Callbacks.
2621 */
2622 public void finishBindingItems() {
2623 if (mSavedState != null) {
2624 if (!mWorkspace.hasFocus()) {
2625 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2626 }
2627
2628 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2629 if (userFolders != null) {
2630 for (long folderId : userFolders) {
2631 final FolderInfo info = mFolders.get(folderId);
2632 if (info != null) {
2633 openFolder(info);
2634 }
2635 }
2636 final Folder openFolder = mWorkspace.getOpenFolder();
2637 if (openFolder != null) {
2638 openFolder.requestFocus();
2639 }
2640 }
2641
Joe Onorato9c1289c2009-08-17 11:03:03 -04002642 mSavedState = null;
2643 }
2644
2645 if (mSavedInstanceState != null) {
2646 super.onRestoreInstanceState(mSavedInstanceState);
2647 mSavedInstanceState = null;
2648 }
2649
Joe Onorato9c1289c2009-08-17 11:03:03 -04002650 mWorkspaceLoading = false;
2651 }
2652
2653 /**
2654 * Add the icons for all apps.
2655 *
2656 * Implementation of the method from LauncherModel.Callbacks.
2657 */
2658 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002659 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002660 }
2661
2662 /**
2663 * A package was installed.
2664 *
2665 * Implementation of the method from LauncherModel.Callbacks.
2666 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002667 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002668 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002669 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002670 }
2671
2672 /**
2673 * A package was updated.
2674 *
2675 * Implementation of the method from LauncherModel.Callbacks.
2676 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002677 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002678 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002679 mWorkspace.updateShortcuts(apps);
2680 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002681 }
2682
2683 /**
2684 * A package was uninstalled.
2685 *
2686 * Implementation of the method from LauncherModel.Callbacks.
2687 */
Joe Onorato36115782010-06-17 13:28:48 -04002688 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002689 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002690 if (permanent) {
2691 mWorkspace.removeItems(apps);
2692 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002693 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002694 }
Joe Onoratobe386092009-11-17 17:32:16 -08002695
2696 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002697 * A number of packages were updated.
2698 */
2699 public void bindPackagesUpdated() {
2700 // update the customization drawer contents
2701 mCustomizePagedView.update();
2702 }
2703
2704 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002705 * Prints out out state for debugging.
2706 */
2707 public void dumpState() {
2708 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002709 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002710 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2711 Log.d(TAG, "mRestoring=" + mRestoring);
2712 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2713 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2714 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2715 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002716 mModel.dumpState();
2717 mAllAppsGrid.dumpState();
2718 Log.d(TAG, "END launcher2 dump state");
2719 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002720}