blob: b5d0dbb5de54703ad8642502ee4c267d48c72d36 [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
Patrick Dubroy7247f632010-08-04 16:02:59 -070019import android.animation.Animatable;
20import android.animation.AnimatableListenerAdapter;
21import android.animation.Animator;
22import android.animation.PropertyAnimator;
23import android.animation.Sequencer;
Michael Jurka946ad472010-07-09 18:05:18 -070024import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.app.Dialog;
27import android.app.SearchManager;
28import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070029import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070030import android.appwidget.AppWidgetManager;
31import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080033import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040034import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080035import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.Context;
37import android.content.DialogInterface;
38import android.content.Intent;
Winson Chung80baf5a2010-08-09 16:03:15 -070039import android.content.Intent.ShortcutIconResource;
Romain Guy5bbc91b2010-08-18 11:38:46 -070040import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070041import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040043import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070045import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040046import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080047import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080049import android.graphics.Canvas;
Winson Chung80baf5a2010-08-09 16:03:15 -070050import android.graphics.Color;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080052import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070053import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040054import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080056import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040058import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080059import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070060import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.text.Selection;
62import android.text.SpannableStringBuilder;
63import android.text.TextUtils;
64import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070065import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080067import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.view.KeyEvent;
69import android.view.LayoutInflater;
70import android.view.Menu;
71import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070072import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.view.View;
Romain Guy5bbc91b2010-08-18 11:38:46 -070074import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070076import android.view.WindowManager;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070077import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070078import android.view.animation.DecelerateInterpolator;
79import android.view.animation.Interpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070082import android.widget.ImageView;
83import android.widget.LinearLayout;
84import android.widget.PopupWindow;
Winson Chung80baf5a2010-08-09 16:03:15 -070085import android.widget.RelativeLayout;
Michael Jurka0e260592010-06-30 17:07:39 -070086import android.widget.TabHost;
Romain Guy5bbc91b2010-08-18 11:38:46 -070087import android.widget.TabHost.OnTabChangeListener;
88import android.widget.TabHost.TabContentFactory;
Winson Chung80baf5a2010-08-09 16:03:15 -070089import android.widget.TabWidget;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.widget.TextView;
91import android.widget.Toast;
Winson Chung80baf5a2010-08-09 16:03:15 -070092import com.android.common.Search;
93import com.android.launcher.R;
Romain Guyedcce092010-03-04 13:03:17 -080094
Romain Guy5bbc91b2010-08-18 11:38:46 -070095import java.io.DataInputStream;
96import java.io.DataOutputStream;
97import java.io.FileNotFoundException;
98import java.io.IOException;
99import java.util.ArrayList;
100import java.util.HashMap;
101import java.util.List;
102
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103/**
104 * Default launcher application.
105 */
Michael Jurka946ad472010-07-09 18:05:18 -0700106public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700107 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
108 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800109 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700110 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Joe Onorato9c1289c2009-08-17 11:03:03 -0400112 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400113 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400114 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 private static final int WALLPAPER_SCREENS_SPAN = 2;
117
118 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800119 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 private static final int MENU_ADD = Menu.FIRST + 1;
122 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
123 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
124 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700125 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
127 private static final int REQUEST_CREATE_SHORTCUT = 1;
128 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700129 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 private static final int REQUEST_PICK_APPLICATION = 6;
131 private static final int REQUEST_PICK_SHORTCUT = 7;
132 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700133 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700134 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
136 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
137
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800138 static final int SCREEN_COUNT = 5;
139 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Joe Onorato7c312c12009-08-13 21:36:53 -0700141 static final int DIALOG_CREATE_SHORTCUT = 1;
142 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Romain Guy98d01652009-06-30 16:21:04 -0700144 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
146 // Type: int
147 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
148 // Type: boolean
149 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
150 // Type: long
151 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
152 // Type: int
153 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
154 // Type: int
155 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
156 // Type: int
157 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
158 // Type: int
159 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
160 // Type: int
161 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
162 // Type: int
163 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
164 // Type: int
165 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
166 // Type: int[]
167 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
168 // Type: boolean
169 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
170 // Type: long
171 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
172
Winson Chung80baf5a2010-08-09 16:03:15 -0700173 // tags for the customization tabs
174 private static final String WIDGETS_TAG = "widgets";
175 private static final String FOLDERS_TAG = "folders";
176 private static final String SHORTCUTS_TAG = "shortcuts";
177 private static final String WALLPAPERS_TAG = "wallpapers";
178
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800182 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800184 private final BroadcastReceiver mCloseSystemDialogsReceiver
185 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800186 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private LayoutInflater mInflater;
189
Joe Onorato00acb122009-08-04 16:04:30 -0400190 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700192
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700193 private AppWidgetManager mAppWidgetManager;
194 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 private CellLayout.CellInfo mAddItemCellInfo;
197 private CellLayout.CellInfo mMenuAddInfo;
198 private final int[] mCellCoordinates = new int[2];
199 private FolderInfo mFolderInfo;
200
Joe Onorato7c312c12009-08-13 21:36:53 -0700201 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700202 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700203 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700204 private TabHost mHomeCustomizationDrawer;
Winson Chung80baf5a2010-08-09 16:03:15 -0700205 private CustomizePagedView mCustomizePagedView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 private Bundle mSavedState;
208
209 private SpannableStringBuilder mDefaultKeySsb = null;
210
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 private boolean mWorkspaceLoading = true;
212
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800213 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 private boolean mRestoring;
215 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
217 private Bundle mSavedInstanceState;
218
Joe Onorato9c1289c2009-08-17 11:03:03 -0400219 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800220 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221
Romain Guy84f296c2009-11-04 15:00:44 -0800222 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
223 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700224
Romain Guy1fbc1c82009-11-09 20:43:08 -0800225 private ImageView mPreviousView;
226 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500227
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400228 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400229 private String[] mHotseatConfig = null;
230 private Intent[] mHotseats = null;
231 private Drawable[] mHotseatIcons = null;
232 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400233
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 @Override
235 protected void onCreate(Bundle savedInstanceState) {
236 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700237
Winson Chungaafa03c2010-06-11 17:34:16 -0700238 if (LauncherApplication.isInPlaceRotationEnabled()) {
239 // hide the status bar (temporary until we get the status bar design figured out)
240 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
241 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
242 }
243
Joe Onorato0589f0f2010-02-08 13:44:00 -0800244 LauncherApplication app = ((LauncherApplication)getApplication());
245 mModel = app.setLauncher(this);
246 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400247 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700249
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700250 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700251 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
252 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700253
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 if (PROFILE_STARTUP) {
255 android.os.Debug.startMethodTracing("/sdcard/launcher");
256 }
257
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400258 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 checkForLocaleChange();
260 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700262 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
263 if (mHomeCustomizationDrawer != null) {
264 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700265
Winson Chung80baf5a2010-08-09 16:03:15 -0700266 // share the same customization workspace across all the tabs
267 mCustomizePagedView = new CustomizePagedView(this);
268 TabContentFactory contentFactory = new TabContentFactory() {
269 public View createTabContent(String tag) {
270 return mCustomizePagedView;
271 }
272 };
273
Michael Jurka946ad472010-07-09 18:05:18 -0700274 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700275 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
276 .setIndicator(widgetsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700277 String foldersLabel = getString(R.string.folders_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700278 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(FOLDERS_TAG)
279 .setIndicator(foldersLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700280 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700281 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
282 .setIndicator(shortcutsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700283 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700284 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
285 .setIndicator(wallpapersLabel).setContent(contentFactory));
286
287 // TEMP: just styling the tab widget to be a bit nicer until we get the actual
288 // new assets
289 TabWidget tabWidget = mHomeCustomizationDrawer.getTabWidget();
290 for (int i = 0; i < tabWidget.getChildCount(); ++i) {
291 RelativeLayout tab = (RelativeLayout) tabWidget.getChildTabViewAt(i);
292 TextView text = (TextView) tab.getChildAt(1);
293 text.setTextSize(20.0f);
294 text.setPadding(20, 0, 20, 0);
295 text.setShadowLayer(1.0f, 0.0f, 1.0f, Color.BLACK);
296 tab.setBackgroundDrawable(null);
297 }
298
299 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
300 public void onTabChanged(String tabId) {
301 // animate the changing of the tab content by fading pages in and out
302 final int duration = 150;
303 final float alpha = mCustomizePagedView.getAlpha();
304 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
305 "alpha", alpha, 0.0f);
306 alphaAnim.addListener(new AnimatableListenerAdapter() {
307 public void onAnimationEnd(Animatable animation) {
308 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
309 if (tag == WIDGETS_TAG) {
310 mCustomizePagedView.setCustomizationFilter(
311 CustomizePagedView.CustomizationType.WidgetCustomization);
312 } else if (tag == FOLDERS_TAG) {
313 mCustomizePagedView.setCustomizationFilter(
314 CustomizePagedView.CustomizationType.FolderCustomization);
315 } else if (tag == SHORTCUTS_TAG) {
316 mCustomizePagedView.setCustomizationFilter(
317 CustomizePagedView.CustomizationType.ShortcutCustomization);
318 } else if (tag == WALLPAPERS_TAG) {
319 mCustomizePagedView.setCustomizationFilter(
320 CustomizePagedView.CustomizationType.WallpaperCustomization);
321 }
322
323 final float alpha = mCustomizePagedView.getAlpha();
324 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
325 "alpha", alpha, 1.0f);
326 alphaAnim.start();
327 }
328 });
329 alphaAnim.start();
330 }
331 });
Michael Jurka946ad472010-07-09 18:05:18 -0700332
333 mHomeCustomizationDrawer.setCurrentTab(0);
334 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 setupViews();
336
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800337 registerContentObservers();
338
Joe Onorato7c312c12009-08-13 21:36:53 -0700339 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700340
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 mSavedState = savedInstanceState;
342 restoreState(mSavedState);
343
344 if (PROFILE_STARTUP) {
345 android.os.Debug.stopMethodTracing();
346 }
347
348 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400349 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 }
351
352 // For handling default keys
353 mDefaultKeySsb = new SpannableStringBuilder();
354 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800355
356 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
357 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 }
Romain Guycbb89e42009-06-08 15:52:54 -0700359
Winson Chungaafa03c2010-06-11 17:34:16 -0700360 @Override
361 public void onConfigurationChanged(Configuration newConfig) {
362 // TODO Auto-generated method stub
363 super.onConfigurationChanged(newConfig);
Winson Chungaafa03c2010-06-11 17:34:16 -0700364 }
365
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700367 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
368 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700369
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 final Configuration configuration = getResources().getConfiguration();
371
Romain Guy98d01652009-06-30 16:21:04 -0700372 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 final String locale = configuration.locale.toString();
374
Romain Guy98d01652009-06-30 16:21:04 -0700375 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 final int mcc = configuration.mcc;
377
Romain Guy98d01652009-06-30 16:21:04 -0700378 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 final int mnc = configuration.mnc;
380
Romain Guy84f296c2009-11-04 15:00:44 -0800381 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382
Romain Guy84f296c2009-11-04 15:00:44 -0800383 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700384 localeConfiguration.locale = locale;
385 localeConfiguration.mcc = mcc;
386 localeConfiguration.mnc = mnc;
387
388 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800389 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400390
391 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700392 }
393 }
394
395 private static class LocaleConfiguration {
396 public String locale;
397 public int mcc = -1;
398 public int mnc = -1;
399 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700400
Romain Guy98d01652009-06-30 16:21:04 -0700401 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
402 DataInputStream in = null;
403 try {
404 in = new DataInputStream(context.openFileInput(PREFERENCES));
405 configuration.locale = in.readUTF();
406 configuration.mcc = in.readInt();
407 configuration.mnc = in.readInt();
408 } catch (FileNotFoundException e) {
409 // Ignore
410 } catch (IOException e) {
411 // Ignore
412 } finally {
413 if (in != null) {
414 try {
415 in.close();
416 } catch (IOException e) {
417 // Ignore
418 }
419 }
420 }
421 }
422
423 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
424 DataOutputStream out = null;
425 try {
426 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
427 out.writeUTF(configuration.locale);
428 out.writeInt(configuration.mcc);
429 out.writeInt(configuration.mnc);
430 out.flush();
431 } catch (FileNotFoundException e) {
432 // Ignore
433 } catch (IOException e) {
434 //noinspection ResultOfMethodCallIgnored
435 context.getFileStreamPath(PREFERENCES).delete();
436 } finally {
437 if (out != null) {
438 try {
439 out.close();
440 } catch (IOException e) {
441 // Ignore
442 }
443 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 }
445 }
446
447 static int getScreen() {
448 synchronized (sLock) {
449 return sScreen;
450 }
451 }
452
453 static void setScreen(int screen) {
454 synchronized (sLock) {
455 sScreen = screen;
456 }
457 }
458
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700460 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461
462 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700463 // TODO: Put back when we decide about scrolling the wallpaper
464 // boolean isPortrait = display.getWidth() < display.getHeight();
465 // final int width = isPortrait ? display.getWidth() : display.getHeight();
466 // final int height = isPortrait ? display.getHeight() : display.getWidth();
467 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
468 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 }
470
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400471 // Note: This doesn't do all the client-id magic that BrowserProvider does
472 // in Browser. (http://b/2425179)
473 private Uri getDefaultBrowserUri() {
474 String url = getString(R.string.default_browser_url);
475 if (url.indexOf("{CID}") != -1) {
476 url = url.replace("{CID}", "android-google");
477 }
478 return Uri.parse(url);
479 }
480
481 // Load the Intent templates from arrays.xml to populate the hotseats. For
482 // each Intent, if it resolves to a single app, use that as the launch
483 // intent & use that app's label as the contentDescription. Otherwise,
484 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400485 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400486 if (mHotseatConfig == null) {
487 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
488 if (mHotseatConfig.length > 0) {
489 mHotseats = new Intent[mHotseatConfig.length];
490 mHotseatLabels = new CharSequence[mHotseatConfig.length];
491 mHotseatIcons = new Drawable[mHotseatConfig.length];
492 } else {
493 mHotseats = null;
494 mHotseatIcons = null;
495 mHotseatLabels = null;
496 }
497
498 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
499 for (int i=0; i<mHotseatConfig.length; i++) {
500 // load icon for this slot; currently unrelated to the actual activity
501 try {
502 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
503 } catch (ArrayIndexOutOfBoundsException ex) {
504 Log.w(TAG, "Missing hotseat_icons array item #" + i);
505 mHotseatIcons[i] = null;
506 }
507 }
508 hotseatIconDrawables.recycle();
509 }
510
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400511 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400512 for (int i=0; i<mHotseatConfig.length; i++) {
513 Intent intent = null;
514 if (mHotseatConfig[i].equals("*BROWSER*")) {
515 // magic value meaning "launch user's default web browser"
516 // replace it with a generic web request so we can see if there is indeed a default
517 String defaultUri = getString(R.string.default_browser_url);
518 intent = new Intent(
519 Intent.ACTION_VIEW,
520 ((defaultUri != null)
521 ? Uri.parse(defaultUri)
522 : getDefaultBrowserUri())
523 ).addCategory(Intent.CATEGORY_BROWSABLE);
524 // note: if the user launches this without a default set, she
525 // will always be taken to the default URL above; this is
526 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700527 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400528 // URL is unavoidably sent to the chosen app.
529 } else {
530 try {
531 intent = Intent.parseUri(mHotseatConfig[i], 0);
532 } catch (java.net.URISyntaxException ex) {
533 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
534 // bogus; leave intent=null
535 }
536 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700537
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400538 if (intent == null) {
539 mHotseats[i] = null;
540 mHotseatLabels[i] = getText(R.string.activity_not_found);
541 continue;
542 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400543
544 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700545 Log.d(TAG, "loadHotseats: hotseat " + i
546 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400547 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400548 + "]");
549 }
550
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400551 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
552 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700553 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400554 Log.d(TAG, "Best match for intent: " + bestMatch);
555 Log.d(TAG, "All matches: ");
556 for (ResolveInfo ri : allMatches) {
557 Log.d(TAG, " --> " + ri);
558 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400559 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400560 // did this resolve to a single app, or the resolver?
561 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700562 // can't find any activity to handle this. let's leave the
563 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400564 // to launch.
565 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400566
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400567 // set accessibility text to "Not installed"
568 mHotseatLabels[i] = getText(R.string.activity_not_found);
569 } else {
570 boolean found = false;
571 for (ResolveInfo ri : allMatches) {
572 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
573 && bestMatch.activityInfo.applicationInfo.packageName
574 .equals(ri.activityInfo.applicationInfo.packageName)) {
575 found = true;
576 break;
577 }
578 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700579
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400580 if (!found) {
581 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
582 // the bestMatch is probably the ResolveActivity, meaning the
583 // user has not yet selected a default
584 // so: we'll keep the original intent for now
585 mHotseats[i] = intent;
586
587 // set the accessibility text to "Select shortcut"
588 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
589 } else {
590 // we have an app!
591 // now reconstruct the intent to launch it through the front
592 // door
593 ComponentName com = new ComponentName(
594 bestMatch.activityInfo.applicationInfo.packageName,
595 bestMatch.activityInfo.name);
596 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
597
598 // load the app label for accessibility
599 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
600 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400601 }
602
603 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700604 Log.d(TAG, "loadHotseats: hotseat " + i
605 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400606 + ((mHotseats[i] == null)
607 ? "null"
608 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400609 + "] label=[" + mHotseatLabels[i]
610 + "]"
611 );
612 }
613 }
614 }
615
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 @Override
617 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700618 mWaitingForResult = false;
619
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 // The pattern used here is that a user PICKs a specific application,
621 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700622
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
624 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700625
Romain Guy94dabf12009-07-21 10:55:43 -0700626 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 switch (requestCode) {
628 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400629 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 break;
631 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800632 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 break;
634 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400635 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 break;
637 case REQUEST_PICK_LIVE_FOLDER:
638 addLiveFolder(data);
639 break;
640 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400641 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700643 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700644 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700646 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700647 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700648 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700650 case REQUEST_PICK_WALLPAPER:
651 // We just wanted the activity result here so we can clear mWaitingForResult
652 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 }
Romain Guy18042c82009-11-06 11:44:55 -0800654 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
655 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
656 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700657 // Clean up the appWidgetId if we canceled
658 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
659 if (appWidgetId != -1) {
660 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 }
662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
664
665 @Override
666 protected void onResume() {
667 super.onResume();
668
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800669 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800670
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400672 mWorkspaceLoading = true;
673 mModel.startLoader(this, true);
674 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 }
676 }
677
678 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700679 protected void onPause() {
680 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700681 // Some launcher layouts don't have a previous and next view
682 if (mPreviousView != null) {
683 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700684 }
685 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700686 dismissPreview(mNextView);
687 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800688 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700689 }
Romain Guycbb89e42009-06-08 15:52:54 -0700690
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700691 @Override
692 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400693 // Flag the loader to stop early before switching
694 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800695 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800696 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700697 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700698
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800699 // We can't hide the IME if it was forced open. So don't bother
700 /*
701 @Override
702 public void onWindowFocusChanged(boolean hasFocus) {
703 super.onWindowFocusChanged(hasFocus);
704
705 if (hasFocus) {
706 final InputMethodManager inputManager = (InputMethodManager)
707 getSystemService(Context.INPUT_METHOD_SERVICE);
708 WindowManager.LayoutParams lp = getWindow().getAttributes();
709 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
710 android.os.Handler()) {
711 protected void onReceiveResult(int resultCode, Bundle resultData) {
712 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
713 }
714 });
715 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
716 }
717 }
718 */
719
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 private boolean acceptFilter() {
721 final InputMethodManager inputManager = (InputMethodManager)
722 getSystemService(Context.INPUT_METHOD_SERVICE);
723 return !inputManager.isFullscreenMode();
724 }
725
726 @Override
727 public boolean onKeyDown(int keyCode, KeyEvent event) {
728 boolean handled = super.onKeyDown(keyCode, event);
729 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
730 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
731 keyCode, event);
732 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700733 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700734 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700735 // showSearchDialog()
736 // If there are multiple keystrokes before the search dialog takes focus,
737 // onSearchRequested() will be called for every keystroke,
738 // but it is idempotent, so it's fine.
739 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 }
741 }
742
Joe Onorato8a9625e2010-01-28 15:55:35 -0800743 // Eat the long press event so the keyboard doesn't come up.
744 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
745 return true;
746 }
747
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 return handled;
749 }
750
Karl Rosaen138a0412009-04-23 19:00:21 -0700751 private String getTypedText() {
752 return mDefaultKeySsb.toString();
753 }
754
755 private void clearTypedText() {
756 mDefaultKeySsb.clear();
757 mDefaultKeySsb.clearSpans();
758 Selection.setSelection(mDefaultKeySsb, 0);
759 }
760
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 /**
762 * Restores the previous state, if it exists.
763 *
764 * @param savedState The previous state.
765 */
766 private void restoreState(Bundle savedState) {
767 if (savedState == null) {
768 return;
769 }
770
Joe Onorato3a8820b2009-11-10 15:06:42 -0800771 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
772 if (allApps) {
773 showAllApps(false);
774 }
775
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
777 if (currentScreen > -1) {
778 mWorkspace.setCurrentScreen(currentScreen);
779 }
780
781 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
782 if (addScreen > -1) {
783 mAddItemCellInfo = new CellLayout.CellInfo();
784 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
785 addItemCellInfo.valid = true;
786 addItemCellInfo.screen = addScreen;
787 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
788 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
789 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
790 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Michael Jurkac28de512010-08-13 11:27:44 -0700791 addItemCellInfo.updateOccupiedCells(
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
793 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
794 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
795 mRestoring = true;
796 }
797
798 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
799 if (renameFolder) {
800 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400801 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 mRestoring = true;
803 }
804 }
805
806 /**
807 * Finds all the views we need and configure them properly.
808 */
809 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400810 DragController dragController = mDragController;
811
Romain Guyb1b69f52009-08-10 15:10:15 -0700812 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400813 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814
Joe Onorato7c312c12009-08-13 21:36:53 -0700815 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700816 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700817 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800818 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700819 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700820 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700821
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700822 if (LauncherApplication.isScreenXLarge()) {
823 // They need to be INVISIBLE initially so that they will be measured in the layout.
824 // Otherwise the animations are messed up when we show them for the first time.
825 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
826 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
827 }
828
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
830 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500831 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700832
Joe Onorato7c312c12009-08-13 21:36:53 -0700833 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
834 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700836 View handleView = findViewById(R.id.all_apps_button);
837 if (handleView != null && handleView instanceof HandleView) {
838 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700839 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700840 mHandleView.setLauncher(this);
841 mHandleView.setOnClickListener(this);
842 mHandleView.setOnLongClickListener(this);
843 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800844
Winson Chung80baf5a2010-08-09 16:03:15 -0700845 if (mCustomizePagedView != null) {
846 mCustomizePagedView.setLauncher(this);
847 mCustomizePagedView.setDragController(dragController);
848 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700849 } else {
850 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
851 hotseatLeft.setContentDescription(mHotseatLabels[0]);
852 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
853 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
854 hotseatRight.setContentDescription(mHotseatLabels[1]);
855 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400856
Michael Jurkaaf442092010-06-10 17:01:57 -0700857 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
858 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800859
Michael Jurkaaf442092010-06-10 17:01:57 -0700860 Drawable previous = mPreviousView.getDrawable();
861 Drawable next = mNextView.getDrawable();
862 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863
Michael Jurkaaf442092010-06-10 17:01:57 -0700864 mPreviousView.setHapticFeedbackEnabled(false);
865 mPreviousView.setOnLongClickListener(this);
866 mNextView.setHapticFeedbackEnabled(false);
867 mNextView.setOnLongClickListener(this);
868 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800869
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400871 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873
874 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400875 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700876 int deleteZoneHandleId;
877 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700878 deleteZoneHandleId = R.id.configure_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700879 } else {
880 deleteZoneHandleId = R.id.all_apps_button_cluster;
881 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700882 deleteZone.setHandle(findViewById(deleteZoneHandleId));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883
Joe Onorato00acb122009-08-04 16:04:30 -0400884 dragController.setDragScoller(workspace);
885 dragController.setDragListener(deleteZone);
886 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800887 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700888
Joe Onorato00acb122009-08-04 16:04:30 -0400889 // The order here is bottom to top.
890 dragController.addDropTarget(workspace);
891 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 }
893
Romain Guy8a73c512009-11-09 19:19:59 -0800894 @SuppressWarnings({"UnusedDeclaration"})
895 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800896 if (!isAllAppsVisible()) {
897 mWorkspace.scrollLeft();
898 }
Romain Guy8a73c512009-11-09 19:19:59 -0800899 }
900
901 @SuppressWarnings({"UnusedDeclaration"})
902 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800903 if (!isAllAppsVisible()) {
904 mWorkspace.scrollRight();
905 }
Romain Guy8a73c512009-11-09 19:19:59 -0800906 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400907
908 @SuppressWarnings({"UnusedDeclaration"})
909 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400910 if (isAllAppsVisible()) return;
911
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400912 int index = -1;
913 if (v.getId() == R.id.hotseat_left) {
914 index = 0;
915 } else if (v.getId() == R.id.hotseat_right) {
916 index = 1;
917 }
918
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400919 // reload these every tap; you never know when they might change
920 loadHotseats();
921 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
922 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400923 startActivitySafely(
924 mHotseats[index],
925 "hotseat"
926 );
927 }
928 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700929
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 /**
931 * Creates a view representing a shortcut.
932 *
933 * @param info The data structure describing the shortcut.
934 *
935 * @return A View inflated from R.layout.application.
936 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800937 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 return createShortcut(R.layout.application,
939 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
940 }
941
942 /**
943 * Creates a view representing a shortcut inflated from the specified resource.
944 *
945 * @param layoutResId The id of the XML layout used to create the shortcut.
946 * @param parent The group the shortcut belongs to.
947 * @param info The data structure describing the shortcut.
948 *
949 * @return A View inflated from layoutResId.
950 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800951 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
953
Joe Onorato0589f0f2010-02-08 13:44:00 -0800954 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
955 new FastBitmapDrawable(info.getIcon(mIconCache)),
956 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 favorite.setText(info.title);
958 favorite.setTag(info);
959 favorite.setOnClickListener(this);
960
961 return favorite;
962 }
963
964 /**
965 * Add an application shortcut to the workspace.
966 *
967 * @param data The intent describing the application.
968 * @param cellInfo The position on screen where to create the shortcut.
969 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400970 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 cellInfo.screen = mWorkspace.getCurrentScreen();
972 if (!findSingleSlot(cellInfo)) return;
973
Joe Onorato0589f0f2010-02-08 13:44:00 -0800974 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
975 data, context);
976
Romain Guy73b979d2009-06-09 12:57:21 -0700977 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800978 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800980 info.container = ItemInfo.NO_ID;
981 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
982 } else {
983 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 }
985 }
Romain Guycbb89e42009-06-08 15:52:54 -0700986
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 /**
988 * Add a shortcut to the workspace.
989 *
990 * @param data The intent describing the shortcut.
991 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400993 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 cellInfo.screen = mWorkspace.getCurrentScreen();
995 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700996
Joe Onorato0589f0f2010-02-08 13:44:00 -0800997 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998
999 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001001 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1002 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 }
1004 }
1005
Romain Guycbb89e42009-06-08 15:52:54 -07001006
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001008 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001010 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001011 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 */
Michael Jurkaaf442092010-06-10 17:01:57 -07001013 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001014 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001015
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001016 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001018 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 // Try finding open space on Launcher screen
1021 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -08001022 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
1023 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1024 return;
1025 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001027 // Build Launcher-specific widget info and save to database
1028 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 launcherInfo.spanX = spans[0];
1030 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001031
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 LauncherModel.addItemToDatabase(this, launcherInfo,
1033 LauncherSettings.Favorites.CONTAINER_DESKTOP,
1034 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
1035
1036 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001037 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001040 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001041
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001042 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001044
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001046 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 }
1048 }
Romain Guycbb89e42009-06-08 15:52:54 -07001049
Joe Onorato9c1289c2009-08-17 11:03:03 -04001050 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1051 mDesktopItems.remove(launcherInfo);
1052 launcherInfo.hostView = null;
1053 }
1054
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001055 public LauncherAppWidgetHost getAppWidgetHost() {
1056 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 }
Romain Guycbb89e42009-06-08 15:52:54 -07001058
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001059 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001060 getWindow().closeAllPanels();
1061
1062 try {
1063 dismissDialog(DIALOG_CREATE_SHORTCUT);
1064 // Unlock the workspace if the dialog was showing
1065 } catch (Exception e) {
1066 // An exception is thrown if the dialog is not visible, which is fine
1067 }
1068
1069 try {
1070 dismissDialog(DIALOG_RENAME_FOLDER);
1071 // Unlock the workspace if the dialog was showing
1072 } catch (Exception e) {
1073 // An exception is thrown if the dialog is not visible, which is fine
1074 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001075
1076 // Whatever we were doing is hereby canceled.
1077 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001078 }
1079
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 @Override
1081 protected void onNewIntent(Intent intent) {
1082 super.onNewIntent(intent);
1083
1084 // Close the menu
1085 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001086 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001087 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001088
Joe Onorato14f122b2009-11-19 14:06:36 -08001089 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1090 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1091 boolean allAppsVisible = isAllAppsVisible();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001092
Michael Jurka4cb37242010-08-09 21:05:32 -07001093 // in all these cases, only animate if we're already on home
1094 if (LauncherApplication.isScreenXLarge()) {
1095 mWorkspace.unshrink(alreadyOnHome);
1096 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08001097 if (!mWorkspace.isDefaultScreenShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001098 // on the phone, we don't animate the change to the workspace if all apps is visible
1099 // on xlarge screens, however, we want an animated transition
1100 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1101 (LauncherApplication.isScreenXLarge() || !allAppsVisible));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001102 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001103 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001104 hideCustomizationDrawer();
Romain Guy1dd3a072009-07-16 13:21:01 -07001105
Joe Onorato3a8820b2009-11-10 15:06:42 -08001106 final View v = getWindow().peekDecorView();
1107 if (v != null && v.getWindowToken() != null) {
1108 InputMethodManager imm = (InputMethodManager)getSystemService(
1109 INPUT_METHOD_SERVICE);
1110 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
1112 }
1113 }
1114
1115 @Override
1116 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1117 // Do not call super here
1118 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001119
1120 if (mHomeCustomizationDrawer != null) {
1121 String cur = savedInstanceState.getString("currentTab");
1122 if (cur != null) {
1123 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1124 }
1125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 }
1127
1128 @Override
1129 protected void onSaveInstanceState(Bundle outState) {
1130 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1131
1132 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1133 if (folders.size() > 0) {
1134 final int count = folders.size();
1135 long[] ids = new long[count];
1136 for (int i = 0; i < count; i++) {
1137 final FolderInfo info = folders.get(i).getInfo();
1138 ids[i] = info.id;
1139 }
1140 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1141 } else {
1142 super.onSaveInstanceState(outState);
1143 }
1144
Joe Onoratofb0ca672009-09-14 17:55:46 -04001145 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001146 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001148 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149
1150 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1151 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1152 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1153
1154 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1155 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1156 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1157 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1158 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1159 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1160 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1161 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001162 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 }
1164
1165 if (mFolderInfo != null && mWaitingForResult) {
1166 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1167 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1168 }
Michael Jurka946ad472010-07-09 18:05:18 -07001169
1170 if (mHomeCustomizationDrawer != null) {
1171 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1172 if (currentTabTag != null) {
1173 outState.putString("currentTab", currentTabTag);
1174 }
1175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 }
1177
1178 @Override
1179 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001181
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001183 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001185 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 }
1187
1188 TextKeyListener.getInstance().release();
1189
Joe Onorato9c1289c2009-08-17 11:03:03 -04001190 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191
Joe Onorato9c1289c2009-08-17 11:03:03 -04001192 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001193
1194 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001195
Patrick Dubroyab962b72010-07-26 12:10:10 -07001196 // Some launcher layouts don't have a previous and next view
1197 if (mPreviousView != null) {
1198 dismissPreview(mPreviousView);
1199 }
1200 if (mNextView != null) {
1201 dismissPreview(mNextView);
1202 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001203
1204 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 }
1206
1207 @Override
1208 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001209 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 super.startActivityForResult(intent, requestCode);
1211 }
1212
1213 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001214 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001216
Joe Onorato7bb17492009-09-24 17:51:01 -07001217 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001218
Karl Rosaen138a0412009-04-23 19:00:21 -07001219 if (initialQuery == null) {
1220 // Use any text typed in the launcher as the initial query
1221 initialQuery = getTypedText();
1222 clearTypedText();
1223 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 if (appSearchData == null) {
1225 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001226 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
Romain Guycbb89e42009-06-08 15:52:54 -07001228
Karl Rosaen138a0412009-04-23 19:00:21 -07001229 final SearchManager searchManager =
1230 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001231 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001232 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 }
1234
1235 @Override
1236 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001237 if (isWorkspaceLocked()) {
1238 return false;
1239 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240
1241 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1244 .setIcon(android.R.drawable.ic_menu_add)
1245 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001246 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 .setIcon(android.R.drawable.ic_menu_gallery)
1248 .setAlphabeticShortcut('W');
1249 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1250 .setIcon(android.R.drawable.ic_search_category_default)
1251 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1252 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1253 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1254 .setAlphabeticShortcut('N');
1255
1256 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001257 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1258 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259
1260 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1261 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1262 .setIntent(settings);
1263
1264 return true;
1265 }
1266
1267 @Override
1268 public boolean onPrepareOptionsMenu(Menu menu) {
1269 super.onPrepareOptionsMenu(menu);
1270
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001271 // If all apps is animating, don't show the menu, because we don't know
1272 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001273 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001274 return false;
1275 }
1276
1277 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001278 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001279 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1280 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1281
1282 // Disable add if the workspace is full.
1283 if (visible) {
Michael Jurkac28de512010-08-13 11:27:44 -07001284 mMenuAddInfo = mWorkspace.updateOccupiedCells(null);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001285 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1286 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287
1288 return true;
1289 }
1290
Michael Jurka0e260592010-06-30 17:07:39 -07001291 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1292 // using the settings menu to add an item, something similar happens in showAddDialog
1293 public void prepareAddItemFromHomeCustomizationDrawer() {
Michael Jurkac28de512010-08-13 11:27:44 -07001294 mMenuAddInfo = mWorkspace.updateOccupiedCells(null);
Michael Jurka0e260592010-06-30 17:07:39 -07001295 mAddItemCellInfo = mMenuAddInfo;
1296 }
1297
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 @Override
1299 public boolean onOptionsItemSelected(MenuItem item) {
1300 switch (item.getItemId()) {
1301 case MENU_ADD:
1302 addItems();
1303 return true;
1304 case MENU_WALLPAPER_SETTINGS:
1305 startWallpaper();
1306 return true;
1307 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001308 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 return true;
1310 case MENU_NOTIFICATIONS:
1311 showNotifications();
1312 return true;
1313 }
1314
1315 return super.onOptionsItemSelected(item);
1316 }
Romain Guycbb89e42009-06-08 15:52:54 -07001317
Karl Rosaen138a0412009-04-23 19:00:21 -07001318 /**
1319 * Indicates that we want global search for this activity by setting the globalSearch
1320 * argument for {@link #startSearch} to true.
1321 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001323 @Override
1324 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001325 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001326 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001327 }
1328
Joe Onorato9c1289c2009-08-17 11:03:03 -04001329 public boolean isWorkspaceLocked() {
1330 return mWorkspaceLoading || mWaitingForResult;
1331 }
1332
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001334 if (LauncherApplication.isScreenXLarge()) {
1335 // Animate the widget chooser up from the bottom of the screen
1336 if (!isCustomizationDrawerVisible()) {
1337 showCustomizationDrawer();
Michael Jurka213d9632010-07-28 11:29:25 -07001338 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001339 }
1340 } else {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001341 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001342 showAddDialog(mMenuAddInfo);
1343 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 }
1345
Michael Jurkaaf442092010-06-10 17:01:57 -07001346 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo) {
1347 mAddItemCellInfo = cellInfo;
1348 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1349 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1350 addAppWidgetImpl(appWidgetId);
1351 }
1352
1353 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001354 // TODO: catch bad widget exception when sent
1355 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001356 // TODO: Is this log message meaningful?
1357 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1358 addAppWidgetImpl(appWidgetId);
1359 }
1360
1361 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001362 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363
Bjorn Bringert7984c942009-12-09 15:38:25 +00001364 if (appWidget.configure != null) {
1365 // Launch over to configure widget, if needed
1366 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1367 intent.setComponent(appWidget.configure);
1368 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1369
Romain Guy8e633c52010-03-04 12:51:36 -08001370 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001372 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001373 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 }
1375 }
Romain Guycbb89e42009-06-08 15:52:54 -07001376
Joe Onoratodeb98af2010-02-19 14:59:39 -08001377 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001378 // Handle case where user selected "Applications"
1379 String applicationName = getResources().getString(R.string.group_applications);
1380 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001381
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001382 if (applicationName != null && applicationName.equals(shortcutName)) {
1383 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1384 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001385
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001386 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1387 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001388 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001389 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001390 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001391 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 }
1393
1394 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001395 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001396 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001397 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001398
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001399 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001400 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001401 } else {
1402 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1403 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 }
1405
Joe Onorato9c1289c2009-08-17 11:03:03 -04001406 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 UserFolderInfo folderInfo = new UserFolderInfo();
1408 folderInfo.title = getText(R.string.folder_name);
1409
1410 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1411 cellInfo.screen = mWorkspace.getCurrentScreen();
1412 if (!findSingleSlot(cellInfo)) return;
1413
1414 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001415 LauncherModel.addItemToDatabase(this, folderInfo,
1416 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001418 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419
1420 // Create the view
1421 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1422 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1423 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001424 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 }
Romain Guycbb89e42009-06-08 15:52:54 -07001426
Joe Onorato9c1289c2009-08-17 11:03:03 -04001427 void removeFolder(FolderInfo folder) {
1428 mFolders.remove(folder.id);
1429 }
1430
1431 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 cellInfo.screen = mWorkspace.getCurrentScreen();
1433 if (!findSingleSlot(cellInfo)) return;
1434
1435 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1436
1437 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1439 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001440 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1441 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 }
1443 }
1444
1445 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1446 CellLayout.CellInfo cellInfo, boolean notify) {
1447
1448 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1449 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1450
1451 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 Intent.ShortcutIconResource iconResource = null;
1453
1454 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1455 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1456 try {
1457 iconResource = (Intent.ShortcutIconResource) extra;
1458 final PackageManager packageManager = context.getPackageManager();
1459 Resources resources = packageManager.getResourcesForApplication(
1460 iconResource.packageName);
1461 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1462 icon = resources.getDrawable(id);
1463 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001464 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 }
1466 }
1467
1468 if (icon == null) {
1469 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1470 }
1471
1472 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001473 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 info.title = name;
1475 info.iconResource = iconResource;
1476 info.uri = data.getData();
1477 info.baseIntent = baseIntent;
1478 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1479 LiveFolders.DISPLAY_MODE_GRID);
1480
1481 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1482 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001483 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484
1485 return info;
1486 }
1487
1488 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1489 final int[] xy = new int[2];
1490 if (findSlot(cellInfo, xy, 1, 1)) {
1491 cellInfo.cellX = xy[0];
1492 cellInfo.cellY = xy[1];
1493 return true;
1494 }
1495 return false;
1496 }
1497
1498 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1499 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1500 boolean[] occupied = mSavedState != null ?
1501 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
Michael Jurkac28de512010-08-13 11:27:44 -07001502 cellInfo = mWorkspace.updateOccupiedCells(occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1504 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1505 return false;
1506 }
1507 }
1508 return true;
1509 }
1510
1511 private void showNotifications() {
1512 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1513 if (statusBar != null) {
1514 statusBar.expand();
1515 }
1516 }
1517
1518 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001519 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001521 Intent chooser = Intent.createChooser(pickWallpaper,
1522 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001523 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1524 // Removed in Eclair MR1
1525// WallpaperManager wm = (WallpaperManager)
1526// getSystemService(Context.WALLPAPER_SERVICE);
1527// WallpaperInfo wi = wm.getWallpaperInfo();
1528// if (wi != null && wi.getSettingsActivity() != null) {
1529// LabeledIntent li = new LabeledIntent(getPackageName(),
1530// R.string.configure_wallpaper, 0);
1531// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1532// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1533// }
Mike Clerona0618e42009-10-22 13:55:21 -07001534 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 }
1536
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001537 /**
1538 * Registers various content observers. The current implementation registers
1539 * only a favorites observer to keep track of the favorites applications.
1540 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001541 private void registerContentObservers() {
1542 ContentResolver resolver = getContentResolver();
1543 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1544 true, mWidgetObserver);
1545 }
1546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 @Override
1548 public boolean dispatchKeyEvent(KeyEvent event) {
1549 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1550 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001552 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001553 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001554 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001555 dumpState();
1556 return true;
1557 }
1558 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001559 }
1560 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1561 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001562 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 return true;
1564 }
1565 }
1566
1567 return super.dispatchKeyEvent(event);
1568 }
1569
Joe Onorato88ec0992009-11-19 13:16:06 -08001570 @Override
1571 public void onBackPressed() {
1572 if (isAllAppsVisible()) {
1573 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001574 } else if (isCustomizationDrawerVisible()) {
1575 hideCustomizationDrawer();
Joe Onorato88ec0992009-11-19 13:16:06 -08001576 } else {
1577 closeFolder();
1578 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001579 // Some launcher layouts don't have a previous and next view
1580 if (mPreviousView != null) {
1581 dismissPreview(mPreviousView);
1582 dismissPreview(mNextView);
1583 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001584 }
1585
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 private void closeFolder() {
1587 Folder folder = mWorkspace.getOpenFolder();
1588 if (folder != null) {
1589 closeFolder(folder);
1590 }
1591 }
1592
1593 void closeFolder(Folder folder) {
1594 folder.getInfo().opened = false;
1595 ViewGroup parent = (ViewGroup) folder.getParent();
1596 if (parent != null) {
1597 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001598 if (folder instanceof DropTarget) {
1599 // Live folders aren't DropTargets.
1600 mDragController.removeDropTarget((DropTarget)folder);
1601 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 }
1603 folder.onClose();
1604 }
1605
1606 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001607 * Re-listen when widgets are reset.
1608 */
1609 private void onAppWidgetReset() {
1610 mAppWidgetHost.startListening();
1611 }
1612
1613 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001614 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1615 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001617 private void unbindDesktopItems() {
1618 for (ItemInfo item: mDesktopItems) {
1619 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 }
1621 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001622
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 /**
1624 * Launches the intent referred by the clicked shortcut.
1625 *
1626 * @param v The view representing the clicked shortcut.
1627 */
1628 public void onClick(View v) {
1629 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001630 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001632 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001633 int[] pos = new int[2];
1634 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001635 intent.setSourceBounds(new Rect(pos[0], pos[1],
1636 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001637 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 } else if (tag instanceof FolderInfo) {
1639 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001640 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001641 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001642 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001643 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001644 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001645 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 }
1647 }
1648
Michael Jurka0e260592010-06-30 17:07:39 -07001649 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001650 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001651 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy558654c2010-07-23 16:48:11 -07001652 hideCustomizationDrawer();
Michael Jurka0e260592010-06-30 17:07:39 -07001653 return false;
1654 }
1655
Michael Jurkaaf442092010-06-10 17:01:57 -07001656 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001657 * Event handler for the search button
1658 *
1659 * @param v The view that was clicked.
1660 */
1661 public void onClickSearchButton(View v) {
1662 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1663 View button = findViewById(R.id.search_button);
1664 i.setSourceBounds(
1665 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1666 startActivity(i);
1667 }
1668
1669 /**
1670 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001671 * enters home screen customization mode.
1672 *
1673 * @param v The view that was clicked.
1674 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001675 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001676 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001677 }
1678
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001679 /**
1680 * Event handler for the "grid" button that appears on the home screen, which
1681 * enters all apps mode.
1682 *
1683 * @param v The view that was clicked.
1684 */
1685 public void onClickAllAppsButton(View v) {
1686 showAllApps(true);
1687 }
1688
Joe Onoratof984e852010-03-25 09:47:45 -07001689 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1691 try {
1692 startActivity(intent);
1693 } catch (ActivityNotFoundException e) {
1694 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001695 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 } catch (SecurityException e) {
1697 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001698 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001700 "or use the exported attribute for this activity. "
1701 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 }
1703 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001704
Romain Guy8e633c52010-03-04 12:51:36 -08001705 void startActivityForResultSafely(Intent intent, int requestCode) {
1706 try {
1707 startActivityForResult(intent, requestCode);
1708 } catch (ActivityNotFoundException e) {
1709 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1710 } catch (SecurityException e) {
1711 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1712 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1713 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1714 "or use the exported attribute for this activity.", e);
1715 }
1716 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717
1718 private void handleFolderClick(FolderInfo folderInfo) {
1719 if (!folderInfo.opened) {
1720 // Close any open folder
1721 closeFolder();
1722 // Open the requested folder
1723 openFolder(folderInfo);
1724 } else {
1725 // Find the open folder...
1726 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1727 int folderScreen;
1728 if (openFolder != null) {
1729 folderScreen = mWorkspace.getScreenForView(openFolder);
1730 // .. and close it
1731 closeFolder(openFolder);
1732 if (folderScreen != mWorkspace.getCurrentScreen()) {
1733 // Close any folder open on the current screen
1734 closeFolder();
1735 // Pull the folder onto this screen
1736 openFolder(folderInfo);
1737 }
1738 }
1739 }
1740 }
1741
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 /**
1743 * Opens the user fodler described by the specified tag. The opening of the folder
1744 * is animated relative to the specified View. If the View is null, no animation
1745 * is played.
1746 *
1747 * @param folderInfo The FolderInfo describing the folder to open.
1748 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001749 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 Folder openFolder;
1751
1752 if (folderInfo instanceof UserFolderInfo) {
1753 openFolder = UserFolder.fromXml(this);
1754 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001755 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 } else {
1757 return;
1758 }
1759
Joe Onorato00acb122009-08-04 16:04:30 -04001760 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 openFolder.setLauncher(this);
1762
1763 openFolder.bind(folderInfo);
1764 folderInfo.opened = true;
1765
Winson Chungaafa03c2010-06-11 17:34:16 -07001766 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1767
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 openFolder.onOpen();
1769 }
1770
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001772 switch (v.getId()) {
1773 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001774 if (!isAllAppsVisible()) {
1775 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1776 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001777 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001778 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001779 return true;
1780 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001781 if (!isAllAppsVisible()) {
1782 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1783 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001784 showPreviews(v);
1785 }
1786 return true;
1787 case R.id.all_apps_button:
1788 if (!isAllAppsVisible()) {
1789 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1790 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1791 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001792 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001793 return true;
1794 }
1795
Joe Onorato9c1289c2009-08-17 11:03:03 -04001796 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 return false;
1798 }
1799
1800 if (!(v instanceof CellLayout)) {
1801 v = (View) v.getParent();
1802 }
1803
1804 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1805
1806 // This happens when long clicking an item with the dpad/trackball
1807 if (cellInfo == null) {
1808 return true;
1809 }
1810
1811 if (mWorkspace.allowLongPress()) {
1812 if (cellInfo.cell == null) {
1813 if (cellInfo.valid) {
1814 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001815 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001816 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1817 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 showAddDialog(cellInfo);
1819 }
1820 } else {
1821 if (!(cellInfo.cell instanceof Folder)) {
1822 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001823 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1824 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 mWorkspace.startDrag(cellInfo);
1826 }
1827 }
1828 }
1829 return true;
1830 }
1831
Romain Guye6b8e2f2009-11-10 11:56:55 -08001832 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001833 private void dismissPreview(final View v) {
1834 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001835 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001836 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1837 public void onDismiss() {
1838 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1839 int count = group.getChildCount();
1840 for (int i = 0; i < count; i++) {
1841 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1842 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001843 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1844 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1845
1846 v.setTag(R.id.workspace, null);
1847 v.setTag(R.id.icon, null);
1848 window.setOnDismissListener(null);
1849 }
1850 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001851 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001852 }
1853 v.setTag(null);
1854 }
1855
Romain Guyf8e6a802009-12-07 17:48:02 -08001856 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001857 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001858 }
1859
Romain Guya6abce82009-11-10 02:54:41 -08001860 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001861 final Resources resources = getResources();
1862 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001863
Romain Guya6abce82009-11-10 02:54:41 -08001864 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001865
Romain Guy9d31e9f2009-11-11 18:54:28 -08001866 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001867
Romain Guyf8e6a802009-12-07 17:48:02 -08001868 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001869 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001870 int extraW = (int) ((r.left + r.right) * max);
1871 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001872
1873 int aW = cell.getWidth() - extraW;
1874 float w = aW / max;
1875
1876 int width = cell.getWidth();
1877 int height = cell.getHeight();
1878 int x = cell.getLeftPadding();
1879 int y = cell.getTopPadding();
1880 width -= (x + cell.getRightPadding());
1881 height -= (y + cell.getBottomPadding());
1882
1883 float scale = w / width;
1884
1885 int count = end - start;
1886
1887 final float sWidth = width * scale;
1888 float sHeight = height * scale;
1889
Romain Guye6b8e2f2009-11-10 11:56:55 -08001890 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001891
Romain Guye6b8e2f2009-11-10 11:56:55 -08001892 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1893 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001894
1895 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001896 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001897 cell = (CellLayout) workspace.getChildAt(i);
1898
Romain Guyf8e6a802009-12-07 17:48:02 -08001899 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001900 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001901
1902 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001903 c.scale(scale, scale);
1904 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1905 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001906
Romain Guy9d31e9f2009-11-11 18:54:28 -08001907 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001908 image.setImageBitmap(bitmap);
1909 image.setTag(i);
1910 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001911 image.setOnFocusChangeListener(handler);
1912 image.setFocusable(true);
1913 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001914
1915 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001916 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1917
Winson Chungaafa03c2010-06-11 17:34:16 -07001918 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001919 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001920
1921 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001922 p.setContentView(preview);
1923 p.setWidth((int) (sWidth * count + extraW));
1924 p.setHeight((int) (sHeight + extraH));
1925 p.setAnimationStyle(R.style.AnimationPreview);
1926 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001927 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001928 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001929 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001930
Romain Guye6b8e2f2009-11-10 11:56:55 -08001931 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1932 public void onDismiss() {
1933 dismissPreview(anchor);
1934 }
1935 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001936
1937 anchor.setTag(p);
1938 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001939 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001940 }
1941
Romain Guy9d31e9f2009-11-11 18:54:28 -08001942 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001943 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001944
Romain Guye6b8e2f2009-11-10 11:56:55 -08001945 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001946 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001947 }
1948
1949 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001950 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001951 v.post(this);
1952 }
1953
1954 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001955 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001956 }
1957
1958 public void onFocusChange(View v, boolean hasFocus) {
1959 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001960 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001961 }
Romain Guya6abce82009-11-10 02:54:41 -08001962 }
1963 }
1964
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 Workspace getWorkspace() {
1966 return mWorkspace;
1967 }
1968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 @Override
1970 protected Dialog onCreateDialog(int id) {
1971 switch (id) {
1972 case DIALOG_CREATE_SHORTCUT:
1973 return new CreateShortcut().createDialog();
1974 case DIALOG_RENAME_FOLDER:
1975 return new RenameFolder().createDialog();
1976 }
1977
1978 return super.onCreateDialog(id);
1979 }
1980
1981 @Override
1982 protected void onPrepareDialog(int id, Dialog dialog) {
1983 switch (id) {
1984 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 break;
1986 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001987 if (mFolderInfo != null) {
1988 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1989 final CharSequence text = mFolderInfo.title;
1990 input.setText(text);
1991 input.setSelection(0, text.length());
1992 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 break;
1994 }
1995 }
1996
1997 void showRenameDialog(FolderInfo info) {
1998 mFolderInfo = info;
1999 mWaitingForResult = true;
2000 showDialog(DIALOG_RENAME_FOLDER);
2001 }
2002
2003 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2004 mAddItemCellInfo = cellInfo;
2005 mWaitingForResult = true;
2006 showDialog(DIALOG_CREATE_SHORTCUT);
2007 }
2008
Joe Onoratodeb98af2010-02-19 14:59:39 -08002009 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002010 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002011 Bundle bundle = new Bundle();
2012
2013 ArrayList<String> shortcutNames = new ArrayList<String>();
2014 shortcutNames.add(getString(R.string.group_applications));
2015 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2016
2017 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2018 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2019 R.drawable.ic_launcher_application));
2020 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2021
2022 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2023 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002024 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002025 pickIntent.putExtras(bundle);
2026
Joe Onoratodeb98af2010-02-19 14:59:39 -08002027 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002028 }
2029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 private class RenameFolder {
2031 private EditText mInput;
2032
2033 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2035 mInput = (EditText) layout.findViewById(R.id.folder_name);
2036
2037 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2038 builder.setIcon(0);
2039 builder.setTitle(getString(R.string.rename_folder_title));
2040 builder.setCancelable(true);
2041 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2042 public void onCancel(DialogInterface dialog) {
2043 cleanup();
2044 }
2045 });
2046 builder.setNegativeButton(getString(R.string.cancel_action),
2047 new Dialog.OnClickListener() {
2048 public void onClick(DialogInterface dialog, int which) {
2049 cleanup();
2050 }
2051 }
2052 );
2053 builder.setPositiveButton(getString(R.string.rename_action),
2054 new Dialog.OnClickListener() {
2055 public void onClick(DialogInterface dialog, int which) {
2056 changeFolderName();
2057 }
2058 }
2059 );
2060 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002061
2062 final AlertDialog dialog = builder.create();
2063 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2064 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002065 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002066 mInput.requestFocus();
2067 InputMethodManager inputManager = (InputMethodManager)
2068 getSystemService(Context.INPUT_METHOD_SERVICE);
2069 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002070 }
2071 });
2072
2073 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 }
2075
2076 private void changeFolderName() {
2077 final String name = mInput.getText().toString();
2078 if (!TextUtils.isEmpty(name)) {
2079 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04002080 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 mFolderInfo.title = name;
2082 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2083
Joe Onorato9c1289c2009-08-17 11:03:03 -04002084 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002085 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002086 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 } else {
2088 final FolderIcon folderIcon = (FolderIcon)
2089 mWorkspace.getViewForTag(mFolderInfo);
2090 if (folderIcon != null) {
2091 folderIcon.setText(name);
2092 getWorkspace().requestLayout();
2093 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002094 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002095 mWorkspaceLoading = true;
2096 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 }
2098 }
2099 }
2100 cleanup();
2101 }
2102
2103 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 dismissDialog(DIALOG_RENAME_FOLDER);
2105 mWaitingForResult = false;
2106 mFolderInfo = null;
2107 }
2108 }
2109
Daniel Sandler843e8602010-06-07 14:59:01 -04002110 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2111 public boolean isAllAppsVisible() {
Romain Guy5bbc91b2010-08-18 11:38:46 -07002112 return mAllAppsGrid != null && mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04002113 }
2114
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002115 // AllAppsView.Watcher
2116 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002117 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2118 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002119 mWorkspace.setVisibility(View.GONE);
2120 }
2121 }
2122
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002123 /**
2124 * Zoom the camera out from the workspace to reveal 'toView'.
2125 * Assumes that the view to show is anchored at either the very top or very bottom
2126 * of the screen.
2127 * @param toView The view to show when the animation is complete
2128 * @param above If true, toView will appear from the top of the screen
2129 */
2130 private void cameraZoomOut(final View toView, boolean above) {
2131 final Resources res = getResources();
2132 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2133 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2134 final int height = toView.getHeight();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002135
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002136 // toView should appear right at the end of the workspace shrink animation
2137 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2138
2139 Interpolator interp = new DecelerateInterpolator();
2140
2141 toView.setPivotX(toView.getWidth() / 2.0f);
2142 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2143 // Assumes that the view is normally anchored to either the top or bottom of the screen
2144 final int margin = 200;
2145 if (above) {
2146 toView.setPivotY(height + ((toView.getTop() + height) / scale) + margin);
2147 } else {
2148 toView.setPivotY(0.0f - (toView.getTop() / scale) - margin);
2149 }
2150
2151 Animator scaleXAnim = new PropertyAnimator(duration, toView, "scaleX", scale, 1.0f);
2152 scaleXAnim.setInterpolator(interp);
2153 scaleXAnim.addListener(new AnimatableListenerAdapter() {
2154 public void onAnimationStart(Animatable animation) {
2155 // Prepare the position
2156 toView.setTranslationX(0.0f);
2157 toView.setTranslationY(0.0f);
2158 toView.setVisibility(View.VISIBLE);
2159 }
2160 });
2161
2162 Animator scaleYAnim = new PropertyAnimator(duration, toView, "scaleY", scale, 1.0f);
2163 scaleYAnim.setInterpolator(interp);
2164
2165 Sequencer s = new Sequencer();
2166 s.playTogether(scaleXAnim, scaleYAnim);
2167 s.play(scaleXAnim).after(startDelay);
2168 s.start();
2169 }
2170
2171 /**
2172 * Zoom the camera back into the workspace, hiding 'fromView'.
2173 * This is the opposite of cameraZoomOut.
2174 * @param fromView The currently-focused view, which will be hidden.
2175 */
2176 private void cameraZoomIn(final View fromView) {
2177 Resources res = getResources();
2178 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2179 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2180
2181 Interpolator interp = new AccelerateInterpolator();
2182
2183 Sequencer s = new Sequencer();
2184 Animator scaleXAnim = new PropertyAnimator(duration, fromView, "scaleX", scaleFactor);
2185 scaleXAnim.setInterpolator(interp);
2186 Animator scaleYAnim = new PropertyAnimator(duration, fromView, "scaleY", scaleFactor);
2187 scaleYAnim.setInterpolator(interp);
2188 s.playTogether(scaleXAnim, scaleYAnim);
2189 s.addListener(new AnimatableListenerAdapter() {
2190 public void onAnimationEnd(Animatable animation) {
2191 fromView.setVisibility(View.GONE);
2192 fromView.setScaleX(1.0f);
2193 fromView.setScaleY(1.0f);
2194 }
2195 });
2196 s.start();
2197 }
2198
2199 /**
2200 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2201 * This is the transition used on xlarge screens to go between all apps and
2202 * the home customization drawer.
2203 * @param fromView The view to pan away from.
2204 * @param toView The view to pan into the frame.
2205 */
2206 private void cameraPan(final View fromView, final View toView) {
2207 final int duration = getResources().getInteger(R.integer.config_allAppsCameraPanTime);
2208 final int workspaceHeight = mWorkspace.getHeight();
2209
2210 final boolean panDown = fromView.getY() < toView.getY();
2211
2212 final float fromViewStartY = panDown ? 0.0f : fromView.getY();
2213 final float fromViewEndY = panDown ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2214 final float toViewStartY = panDown ? workspaceHeight * 2 : -toView.getHeight() * 2;
2215 final float toViewEndY = panDown ? workspaceHeight - toView.getHeight() : 0.0f;
2216
2217 Sequencer s = new Sequencer();
2218 s.playTogether(
2219 new PropertyAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2220 new PropertyAnimator(duration, toView, "y", toViewStartY, toViewEndY));
2221 s.addListener(new AnimatableListenerAdapter() {
2222 public void onAnimationStart(Animatable animation) {
2223 toView.setVisibility(View.VISIBLE);
2224 toView.setY(toViewStartY);
2225 }
2226 public void onAnimationEnd(Animatable animation) {
2227 fromView.setVisibility(View.GONE);
2228 }
2229 });
2230 s.start();
2231 }
2232
2233 void showAllApps(boolean animated) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002234 if (mAllAppsGrid.isVisible())
2235 return;
2236
Michael Jurka79212d82010-07-30 16:36:20 -07002237 if (LauncherApplication.isScreenXLarge()) {
2238 mWorkspace.shrinkToBottom(animated);
2239 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002240
Patrick Dubroy558654c2010-07-23 16:48:11 -07002241 if (LauncherApplication.isScreenXLarge() && animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002242 if (isCustomizationDrawerVisible()) {
2243 cameraPan(mHomeCustomizationDrawer, (View) mAllAppsGrid);
Winson Chung80baf5a2010-08-09 16:03:15 -07002244 mCustomizePagedView.cleanup();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002245 } else {
2246 cameraZoomOut((View) mAllAppsGrid, true);
2247 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002248 } else {
2249 mAllAppsGrid.zoom(1.0f, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002250 hideCustomizationDrawer(); // TODO: Should be able to do this un-animated
Patrick Dubroy558654c2010-07-23 16:48:11 -07002251 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002252
Romain Guyc16fea72010-03-12 17:17:56 -08002253 ((View) mAllAppsGrid).setFocusable(true);
2254 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002255
Joe Onorato7c312c12009-08-13 21:36:53 -07002256 // TODO: fade these two too
2257 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002258 }
2259
Joe Onoratob2061212009-11-24 16:13:54 -05002260 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002261 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002262 * - Home from workspace
2263 * - from center screen
2264 * - from other screens
2265 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002266 * - from center screen
2267 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002268 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002269 * - from center screen
2270 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002271 * - Launch app from workspace and quit
2272 * - with back
2273 * - with home
2274 * - Launch app from all apps and quit
2275 * - with back
2276 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002277 * - Go to a screen that's not the default, then all
2278 * apps, and launch and app, and go back
2279 * - with back
2280 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002281 * - On workspace, long press power and go back
2282 * - with back
2283 * - with home
2284 * - On all apps, long press power and go back
2285 * - with back
2286 * - with home
2287 * - On workspace, power off
2288 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002289 * - Launch an app and turn off the screen while in that app
2290 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002291 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002292 * - From all apps
2293 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002294 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2295 * - From all apps
2296 * - From the center workspace
2297 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002298 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002299 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002300 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002301 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002302 if (LauncherApplication.isScreenXLarge() && animated) {
Michael Jurka213d9632010-07-28 11:29:25 -07002303 mWorkspace.unshrink();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002304 cameraZoomIn((View) mAllAppsGrid);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002305 } else {
2306 mAllAppsGrid.zoom(0.0f, animated);
2307 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002308 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002309 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002310 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002311 }
2312
Joe Onorato7c312c12009-08-13 21:36:53 -07002313 void lockAllApps() {
2314 // TODO
2315 }
2316
2317 void unlockAllApps() {
2318 // TODO
2319 }
2320
Patrick Dubroy558654c2010-07-23 16:48:11 -07002321 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002322 return mHomeCustomizationDrawer != null &&
2323 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002324 }
2325
2326 private void showCustomizationDrawer() {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002327 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002328 if (isAllAppsVisible()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002329 cameraPan((View) mAllAppsGrid, mHomeCustomizationDrawer);
2330 } else {
2331 cameraZoomOut(mHomeCustomizationDrawer, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002332 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002333 }
2334
Michael Jurka4bb4f732010-08-04 18:46:01 -07002335 void hideCustomizationDrawer() {
2336 hideCustomizationDrawer(true);
2337 }
2338
2339 void hideCustomizationDrawer(boolean unshrinkWorkspace) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002340 if (isCustomizationDrawerVisible()) {
Michael Jurka4bb4f732010-08-04 18:46:01 -07002341 if (unshrinkWorkspace) {
2342 mWorkspace.unshrink();
2343 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002344 cameraZoomIn(mHomeCustomizationDrawer);
Winson Chung80baf5a2010-08-09 16:03:15 -07002345 mCustomizePagedView.cleanup();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002346 }
2347 }
2348
Michael Jurka213d9632010-07-28 11:29:25 -07002349 void onWorkspaceUnshrink() {
2350 if (isAllAppsVisible()) {
Michael Jurka54dd7542010-07-30 14:47:52 -07002351 closeAllApps(true);
Michael Jurka213d9632010-07-28 11:29:25 -07002352 }
2353 if (isCustomizationDrawerVisible()) {
2354 hideCustomizationDrawer();
2355 }
2356 }
2357
Joe Onorato7404ee42009-07-31 11:54:44 -07002358 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002359 * Displays the shortcut creation dialog and launches, if necessary, the
2360 * appropriate activity.
2361 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002362 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002363 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2364 DialogInterface.OnShowListener {
2365
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002367
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002368 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002369 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002370
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002371 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2372 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002373 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002374
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002375 builder.setInverseBackgroundForced(true);
2376
2377 AlertDialog dialog = builder.create();
2378 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002379 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002380 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002382 return dialog;
2383 }
2384
2385 public void onCancel(DialogInterface dialog) {
2386 mWaitingForResult = false;
2387 cleanup();
2388 }
2389
Romain Guycbb89e42009-06-08 15:52:54 -07002390 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002391 }
2392
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002393 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002394 try {
2395 dismissDialog(DIALOG_CREATE_SHORTCUT);
2396 } catch (Exception e) {
2397 // An exception is thrown if the dialog is not visible, which is fine
2398 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002399 }
2400
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002401 /**
2402 * Handle the action clicked in the "Add to home" dialog.
2403 */
2404 public void onClick(DialogInterface dialog, int which) {
2405 Resources res = getResources();
2406 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002407
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002408 switch (which) {
2409 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002410 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002411 break;
2412 }
Romain Guycbb89e42009-06-08 15:52:54 -07002413
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002414 case AddAdapter.ITEM_APPWIDGET: {
2415 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002416
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002417 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2418 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002419 // start the pick activity
2420 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2421 break;
2422 }
Romain Guycbb89e42009-06-08 15:52:54 -07002423
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002424 case AddAdapter.ITEM_LIVE_FOLDER: {
2425 // Insert extra item to handle inserting folder
2426 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002427
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002428 ArrayList<String> shortcutNames = new ArrayList<String>();
2429 shortcutNames.add(res.getString(R.string.group_folder));
2430 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002431
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002432 ArrayList<ShortcutIconResource> shortcutIcons =
2433 new ArrayList<ShortcutIconResource>();
2434 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2435 R.drawable.ic_launcher_folder));
2436 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2437
2438 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2439 pickIntent.putExtra(Intent.EXTRA_INTENT,
2440 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2441 pickIntent.putExtra(Intent.EXTRA_TITLE,
2442 getText(R.string.title_select_live_folder));
2443 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002444
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002445 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2446 break;
2447 }
2448
2449 case AddAdapter.ITEM_WALLPAPER: {
2450 startWallpaper();
2451 break;
2452 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 }
2454 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002455
2456 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002457 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002458 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 }
2460
2461 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002462 * Receives notifications when applications are added/removed.
2463 */
2464 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2465 @Override
2466 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002467 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002468 String reason = intent.getStringExtra("reason");
2469 if (!"homekey".equals(reason)) {
2470 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002471 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002472 animate = false;
2473 }
Joe Onoratob2061212009-11-24 16:13:54 -05002474 closeAllApps(animate);
Michael Jurka4cb37242010-08-09 21:05:32 -07002475 mWorkspace.unshrink(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002476 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002477 }
2478 }
2479
2480 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002481 * Receives notifications whenever the appwidgets are reset.
2482 */
2483 private class AppWidgetResetObserver extends ContentObserver {
2484 public AppWidgetResetObserver() {
2485 super(new Handler());
2486 }
2487
2488 @Override
2489 public void onChange(boolean selfChange) {
2490 onAppWidgetReset();
2491 }
2492 }
2493
2494 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002495 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002496 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002497 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002498 if (mWorkspace != null) {
2499 return mWorkspace.getCurrentScreen();
2500 } else {
2501 return SCREEN_COUNT / 2;
2502 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002503 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002504
Joe Onorato9c1289c2009-08-17 11:03:03 -04002505 /**
2506 * Refreshes the shortcuts shown on the workspace.
2507 *
2508 * Implementation of the method from LauncherModel.Callbacks.
2509 */
2510 public void startBinding() {
2511 final Workspace workspace = mWorkspace;
2512 int count = workspace.getChildCount();
2513 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002514 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002515 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2516 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002517
Joe Onorato9c1289c2009-08-17 11:03:03 -04002518 if (DEBUG_USER_INTERFACE) {
2519 android.widget.Button finishButton = new android.widget.Button(this);
2520 finishButton.setText("Finish");
2521 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2522
2523 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2524 public void onClick(View v) {
2525 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002526 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002527 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002528 }
2529 }
2530
2531 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002532 * Bind the items start-end from the list.
2533 *
2534 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002536 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2537
2538 final Workspace workspace = mWorkspace;
2539
2540 for (int i=start; i<end; i++) {
2541 final ItemInfo item = shortcuts.get(i);
2542 mDesktopItems.add(item);
2543 switch (item.itemType) {
2544 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2545 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002546 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002547 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2548 false);
2549 break;
2550 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2551 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2552 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2553 (UserFolderInfo) item);
2554 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2555 false);
2556 break;
2557 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2558 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2559 R.layout.live_folder_icon, this,
2560 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2561 (LiveFolderInfo) item);
2562 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2563 false);
2564 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002565 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002566 }
2567
Joe Onorato9c1289c2009-08-17 11:03:03 -04002568 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002569 }
2570
Joe Onorato9c1289c2009-08-17 11:03:03 -04002571 /**
2572 * Implementation of the method from LauncherModel.Callbacks.
2573 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002574 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2575 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002576 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002578
2579 /**
2580 * Add the views for a widget to the workspace.
2581 *
2582 * Implementation of the method from LauncherModel.Callbacks.
2583 */
2584 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002585 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2586 if (DEBUG_WIDGETS) {
2587 Log.d(TAG, "bindAppWidget: " + item);
2588 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002589 final Workspace workspace = mWorkspace;
2590
2591 final int appWidgetId = item.appWidgetId;
2592 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002593 if (DEBUG_WIDGETS) {
2594 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2595 }
2596
Joe Onorato9c1289c2009-08-17 11:03:03 -04002597 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2598
Joe Onorato9c1289c2009-08-17 11:03:03 -04002599 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2600 item.hostView.setTag(item);
2601
2602 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2603 item.cellY, item.spanX, item.spanY, false);
2604
2605 workspace.requestLayout();
2606
2607 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002608
2609 if (DEBUG_WIDGETS) {
2610 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2611 + (SystemClock.uptimeMillis()-start) + "ms");
2612 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002613 }
2614
2615 /**
2616 * Callback saying that there aren't any more items to bind.
2617 *
2618 * Implementation of the method from LauncherModel.Callbacks.
2619 */
2620 public void finishBindingItems() {
2621 if (mSavedState != null) {
2622 if (!mWorkspace.hasFocus()) {
2623 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2624 }
2625
2626 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2627 if (userFolders != null) {
2628 for (long folderId : userFolders) {
2629 final FolderInfo info = mFolders.get(folderId);
2630 if (info != null) {
2631 openFolder(info);
2632 }
2633 }
2634 final Folder openFolder = mWorkspace.getOpenFolder();
2635 if (openFolder != null) {
2636 openFolder.requestFocus();
2637 }
2638 }
2639
Joe Onorato9c1289c2009-08-17 11:03:03 -04002640 mSavedState = null;
2641 }
2642
2643 if (mSavedInstanceState != null) {
2644 super.onRestoreInstanceState(mSavedInstanceState);
2645 mSavedInstanceState = null;
2646 }
2647
Joe Onorato9c1289c2009-08-17 11:03:03 -04002648 mWorkspaceLoading = false;
2649 }
2650
2651 /**
2652 * Add the icons for all apps.
2653 *
2654 * Implementation of the method from LauncherModel.Callbacks.
2655 */
2656 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002657 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002658 }
2659
2660 /**
2661 * A package was installed.
2662 *
2663 * Implementation of the method from LauncherModel.Callbacks.
2664 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002665 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002666 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002667 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002668 }
2669
2670 /**
2671 * A package was updated.
2672 *
2673 * Implementation of the method from LauncherModel.Callbacks.
2674 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002675 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002676 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002677 mWorkspace.updateShortcuts(apps);
2678 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002679 }
2680
2681 /**
2682 * A package was uninstalled.
2683 *
2684 * Implementation of the method from LauncherModel.Callbacks.
2685 */
Joe Onorato36115782010-06-17 13:28:48 -04002686 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002687 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002688 if (permanent) {
2689 mWorkspace.removeItems(apps);
2690 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002691 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002692 }
Joe Onoratobe386092009-11-17 17:32:16 -08002693
2694 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002695 * A number of packages were updated.
2696 */
2697 public void bindPackagesUpdated() {
2698 // update the customization drawer contents
2699 mCustomizePagedView.update();
2700 }
2701
2702 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002703 * Prints out out state for debugging.
2704 */
2705 public void dumpState() {
2706 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002707 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002708 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2709 Log.d(TAG, "mRestoring=" + mRestoring);
2710 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2711 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2712 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2713 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002714 mModel.dumpState();
2715 mAllAppsGrid.dumpState();
2716 Log.d(TAG, "END launcher2 dump state");
2717 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002718}