blob: ca6539f46fd33f447ea6ac261ae60ff7a2d11ab2 [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 Dubroy4ed62782010-08-17 15:11:18 -070019import com.android.common.Search;
20import com.android.launcher.R;
21
Patrick Dubroy7247f632010-08-04 16:02:59 -070022import android.animation.Animatable;
23import android.animation.AnimatableListenerAdapter;
24import android.animation.Animator;
25import android.animation.PropertyAnimator;
26import android.animation.Sequencer;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.Dialog;
30import android.app.SearchManager;
31import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070032import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
Winson Chung80baf5a2010-08-09 16:03:15 -070042import android.content.Intent.ShortcutIconResource;
Romain Guy5bbc91b2010-08-18 11:38:46 -070043import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040046import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070048import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040049import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080052import android.graphics.Canvas;
Winson Chung80baf5a2010-08-09 16:03:15 -070053import android.graphics.Color;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080055import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080059import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040061import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080062import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070063import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070064import android.provider.Settings;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.text.Selection;
66import android.text.SpannableStringBuilder;
67import android.text.TextUtils;
68import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070069import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080071import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.view.KeyEvent;
73import android.view.LayoutInflater;
74import android.view.Menu;
75import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070076import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.view.View;
Romain Guy5bbc91b2010-08-18 11:38:46 -070078import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070080import android.view.WindowManager;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070081import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070082import android.view.animation.DecelerateInterpolator;
83import android.view.animation.Interpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070086import android.widget.ImageView;
87import android.widget.LinearLayout;
88import android.widget.PopupWindow;
Winson Chung80baf5a2010-08-09 16:03:15 -070089import android.widget.RelativeLayout;
Michael Jurka0e260592010-06-30 17:07:39 -070090import android.widget.TabHost;
Romain Guy5bbc91b2010-08-18 11:38:46 -070091import android.widget.TabHost.OnTabChangeListener;
92import android.widget.TabHost.TabContentFactory;
Winson Chung80baf5a2010-08-09 16:03:15 -070093import android.widget.TabWidget;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.widget.TextView;
95import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070096
97import java.io.DataInputStream;
98import java.io.DataOutputStream;
99import java.io.FileNotFoundException;
100import java.io.IOException;
101import java.util.ArrayList;
102import java.util.HashMap;
103import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800104
Romain Guy5bbc91b2010-08-18 11:38:46 -0700105import java.io.DataInputStream;
106import java.io.DataOutputStream;
107import java.io.FileNotFoundException;
108import java.io.IOException;
109import java.util.ArrayList;
110import java.util.HashMap;
111import java.util.List;
112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113/**
114 * Default launcher application.
115 */
Michael Jurka946ad472010-07-09 18:05:18 -0700116public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700117 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
118 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800119 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700120 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Joe Onorato9c1289c2009-08-17 11:03:03 -0400122 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400123 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400124 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int WALLPAPER_SCREENS_SPAN = 2;
127
128 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800129 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 private static final int MENU_ADD = Menu.FIRST + 1;
132 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
133 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
134 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700135 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 private static final int REQUEST_CREATE_SHORTCUT = 1;
138 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 private static final int REQUEST_PICK_APPLICATION = 6;
141 private static final int REQUEST_PICK_SHORTCUT = 7;
142 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700143 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700144 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
146 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
147
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800148 static final int SCREEN_COUNT = 5;
149 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Joe Onorato7c312c12009-08-13 21:36:53 -0700151 static final int DIALOG_CREATE_SHORTCUT = 1;
152 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Romain Guy98d01652009-06-30 16:21:04 -0700154 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
156 // Type: int
157 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
158 // Type: boolean
159 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
160 // Type: long
161 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
162 // Type: int
163 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
164 // Type: int
165 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
166 // Type: int
167 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
168 // Type: int
169 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
170 // Type: int
171 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
172 // Type: int
173 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
174 // Type: int
175 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
176 // Type: int[]
177 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
178 // Type: boolean
179 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
180 // Type: long
181 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
182
Winson Chung80baf5a2010-08-09 16:03:15 -0700183 // tags for the customization tabs
184 private static final String WIDGETS_TAG = "widgets";
185 private static final String FOLDERS_TAG = "folders";
186 private static final String SHORTCUTS_TAG = "shortcuts";
187 private static final String WALLPAPERS_TAG = "wallpapers";
188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800192 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800194 private final BroadcastReceiver mCloseSystemDialogsReceiver
195 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800196 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private LayoutInflater mInflater;
199
Joe Onorato00acb122009-08-04 16:04:30 -0400200 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700202
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 private AppWidgetManager mAppWidgetManager;
204 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private CellLayout.CellInfo mAddItemCellInfo;
207 private CellLayout.CellInfo mMenuAddInfo;
208 private final int[] mCellCoordinates = new int[2];
209 private FolderInfo mFolderInfo;
210
Joe Onorato7c312c12009-08-13 21:36:53 -0700211 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700212 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700213 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700214 private TabHost mHomeCustomizationDrawer;
Winson Chung80baf5a2010-08-09 16:03:15 -0700215 private CustomizePagedView mCustomizePagedView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private Bundle mSavedState;
218
219 private SpannableStringBuilder mDefaultKeySsb = null;
220
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221 private boolean mWorkspaceLoading = true;
222
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800223 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 private boolean mRestoring;
225 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226
227 private Bundle mSavedInstanceState;
228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800230 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231
Romain Guy84f296c2009-11-04 15:00:44 -0800232 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
233 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Romain Guy1fbc1c82009-11-09 20:43:08 -0800235 private ImageView mPreviousView;
236 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500237
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400238 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400239 private String[] mHotseatConfig = null;
240 private Intent[] mHotseats = null;
241 private Drawable[] mHotseatIcons = null;
242 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 @Override
245 protected void onCreate(Bundle savedInstanceState) {
246 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700247
Winson Chungaafa03c2010-06-11 17:34:16 -0700248 if (LauncherApplication.isInPlaceRotationEnabled()) {
249 // hide the status bar (temporary until we get the status bar design figured out)
250 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
251 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
252 }
253
Joe Onorato0589f0f2010-02-08 13:44:00 -0800254 LauncherApplication app = ((LauncherApplication)getApplication());
255 mModel = app.setLauncher(this);
256 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400257 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700259
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700260 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700261 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
262 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 if (PROFILE_STARTUP) {
265 android.os.Debug.startMethodTracing("/sdcard/launcher");
266 }
267
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400268 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 checkForLocaleChange();
270 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700272 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
273 if (mHomeCustomizationDrawer != null) {
274 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700275
Winson Chung80baf5a2010-08-09 16:03:15 -0700276 // share the same customization workspace across all the tabs
277 mCustomizePagedView = new CustomizePagedView(this);
278 TabContentFactory contentFactory = new TabContentFactory() {
279 public View createTabContent(String tag) {
280 return mCustomizePagedView;
281 }
282 };
283
Michael Jurka946ad472010-07-09 18:05:18 -0700284 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700285 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
286 .setIndicator(widgetsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700287 String foldersLabel = getString(R.string.folders_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700288 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(FOLDERS_TAG)
289 .setIndicator(foldersLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700290 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700291 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
292 .setIndicator(shortcutsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700293 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700294 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
295 .setIndicator(wallpapersLabel).setContent(contentFactory));
296
297 // TEMP: just styling the tab widget to be a bit nicer until we get the actual
298 // new assets
299 TabWidget tabWidget = mHomeCustomizationDrawer.getTabWidget();
300 for (int i = 0; i < tabWidget.getChildCount(); ++i) {
301 RelativeLayout tab = (RelativeLayout) tabWidget.getChildTabViewAt(i);
302 TextView text = (TextView) tab.getChildAt(1);
303 text.setTextSize(20.0f);
304 text.setPadding(20, 0, 20, 0);
305 text.setShadowLayer(1.0f, 0.0f, 1.0f, Color.BLACK);
306 tab.setBackgroundDrawable(null);
307 }
308
309 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
310 public void onTabChanged(String tabId) {
311 // animate the changing of the tab content by fading pages in and out
312 final int duration = 150;
313 final float alpha = mCustomizePagedView.getAlpha();
314 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
315 "alpha", alpha, 0.0f);
316 alphaAnim.addListener(new AnimatableListenerAdapter() {
317 public void onAnimationEnd(Animatable animation) {
318 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
319 if (tag == WIDGETS_TAG) {
320 mCustomizePagedView.setCustomizationFilter(
321 CustomizePagedView.CustomizationType.WidgetCustomization);
322 } else if (tag == FOLDERS_TAG) {
323 mCustomizePagedView.setCustomizationFilter(
324 CustomizePagedView.CustomizationType.FolderCustomization);
325 } else if (tag == SHORTCUTS_TAG) {
326 mCustomizePagedView.setCustomizationFilter(
327 CustomizePagedView.CustomizationType.ShortcutCustomization);
328 } else if (tag == WALLPAPERS_TAG) {
329 mCustomizePagedView.setCustomizationFilter(
330 CustomizePagedView.CustomizationType.WallpaperCustomization);
331 }
332
333 final float alpha = mCustomizePagedView.getAlpha();
334 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
335 "alpha", alpha, 1.0f);
336 alphaAnim.start();
337 }
338 });
339 alphaAnim.start();
340 }
341 });
Michael Jurka946ad472010-07-09 18:05:18 -0700342
343 mHomeCustomizationDrawer.setCurrentTab(0);
344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 setupViews();
346
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800347 registerContentObservers();
348
Joe Onorato7c312c12009-08-13 21:36:53 -0700349 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 mSavedState = savedInstanceState;
352 restoreState(mSavedState);
353
354 if (PROFILE_STARTUP) {
355 android.os.Debug.stopMethodTracing();
356 }
357
358 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400359 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 }
361
362 // For handling default keys
363 mDefaultKeySsb = new SpannableStringBuilder();
364 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800365
366 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
367 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 }
Romain Guycbb89e42009-06-08 15:52:54 -0700369
Winson Chungaafa03c2010-06-11 17:34:16 -0700370 @Override
371 public void onConfigurationChanged(Configuration newConfig) {
372 // TODO Auto-generated method stub
373 super.onConfigurationChanged(newConfig);
Winson Chungaafa03c2010-06-11 17:34:16 -0700374 }
375
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700377 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
378 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700379
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 final Configuration configuration = getResources().getConfiguration();
381
Romain Guy98d01652009-06-30 16:21:04 -0700382 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 final String locale = configuration.locale.toString();
384
Romain Guy98d01652009-06-30 16:21:04 -0700385 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 final int mcc = configuration.mcc;
387
Romain Guy98d01652009-06-30 16:21:04 -0700388 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 final int mnc = configuration.mnc;
390
Romain Guy84f296c2009-11-04 15:00:44 -0800391 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392
Romain Guy84f296c2009-11-04 15:00:44 -0800393 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700394 localeConfiguration.locale = locale;
395 localeConfiguration.mcc = mcc;
396 localeConfiguration.mnc = mnc;
397
398 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800399 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400400
401 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700402 }
403 }
404
405 private static class LocaleConfiguration {
406 public String locale;
407 public int mcc = -1;
408 public int mnc = -1;
409 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700410
Romain Guy98d01652009-06-30 16:21:04 -0700411 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
412 DataInputStream in = null;
413 try {
414 in = new DataInputStream(context.openFileInput(PREFERENCES));
415 configuration.locale = in.readUTF();
416 configuration.mcc = in.readInt();
417 configuration.mnc = in.readInt();
418 } catch (FileNotFoundException e) {
419 // Ignore
420 } catch (IOException e) {
421 // Ignore
422 } finally {
423 if (in != null) {
424 try {
425 in.close();
426 } catch (IOException e) {
427 // Ignore
428 }
429 }
430 }
431 }
432
433 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
434 DataOutputStream out = null;
435 try {
436 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
437 out.writeUTF(configuration.locale);
438 out.writeInt(configuration.mcc);
439 out.writeInt(configuration.mnc);
440 out.flush();
441 } catch (FileNotFoundException e) {
442 // Ignore
443 } catch (IOException e) {
444 //noinspection ResultOfMethodCallIgnored
445 context.getFileStreamPath(PREFERENCES).delete();
446 } finally {
447 if (out != null) {
448 try {
449 out.close();
450 } catch (IOException e) {
451 // Ignore
452 }
453 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 }
455 }
456
457 static int getScreen() {
458 synchronized (sLock) {
459 return sScreen;
460 }
461 }
462
463 static void setScreen(int screen) {
464 synchronized (sLock) {
465 sScreen = screen;
466 }
467 }
468
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700470 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471
472 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700473 // TODO: Put back when we decide about scrolling the wallpaper
474 // boolean isPortrait = display.getWidth() < display.getHeight();
475 // final int width = isPortrait ? display.getWidth() : display.getHeight();
476 // final int height = isPortrait ? display.getHeight() : display.getWidth();
477 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
478 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 }
480
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400481 // Note: This doesn't do all the client-id magic that BrowserProvider does
482 // in Browser. (http://b/2425179)
483 private Uri getDefaultBrowserUri() {
484 String url = getString(R.string.default_browser_url);
485 if (url.indexOf("{CID}") != -1) {
486 url = url.replace("{CID}", "android-google");
487 }
488 return Uri.parse(url);
489 }
490
491 // Load the Intent templates from arrays.xml to populate the hotseats. For
492 // each Intent, if it resolves to a single app, use that as the launch
493 // intent & use that app's label as the contentDescription. Otherwise,
494 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400495 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400496 if (mHotseatConfig == null) {
497 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
498 if (mHotseatConfig.length > 0) {
499 mHotseats = new Intent[mHotseatConfig.length];
500 mHotseatLabels = new CharSequence[mHotseatConfig.length];
501 mHotseatIcons = new Drawable[mHotseatConfig.length];
502 } else {
503 mHotseats = null;
504 mHotseatIcons = null;
505 mHotseatLabels = null;
506 }
507
508 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
509 for (int i=0; i<mHotseatConfig.length; i++) {
510 // load icon for this slot; currently unrelated to the actual activity
511 try {
512 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
513 } catch (ArrayIndexOutOfBoundsException ex) {
514 Log.w(TAG, "Missing hotseat_icons array item #" + i);
515 mHotseatIcons[i] = null;
516 }
517 }
518 hotseatIconDrawables.recycle();
519 }
520
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400521 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400522 for (int i=0; i<mHotseatConfig.length; i++) {
523 Intent intent = null;
524 if (mHotseatConfig[i].equals("*BROWSER*")) {
525 // magic value meaning "launch user's default web browser"
526 // replace it with a generic web request so we can see if there is indeed a default
527 String defaultUri = getString(R.string.default_browser_url);
528 intent = new Intent(
529 Intent.ACTION_VIEW,
530 ((defaultUri != null)
531 ? Uri.parse(defaultUri)
532 : getDefaultBrowserUri())
533 ).addCategory(Intent.CATEGORY_BROWSABLE);
534 // note: if the user launches this without a default set, she
535 // will always be taken to the default URL above; this is
536 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700537 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400538 // URL is unavoidably sent to the chosen app.
539 } else {
540 try {
541 intent = Intent.parseUri(mHotseatConfig[i], 0);
542 } catch (java.net.URISyntaxException ex) {
543 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
544 // bogus; leave intent=null
545 }
546 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700547
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400548 if (intent == null) {
549 mHotseats[i] = null;
550 mHotseatLabels[i] = getText(R.string.activity_not_found);
551 continue;
552 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400553
554 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700555 Log.d(TAG, "loadHotseats: hotseat " + i
556 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400557 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400558 + "]");
559 }
560
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400561 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
562 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700563 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400564 Log.d(TAG, "Best match for intent: " + bestMatch);
565 Log.d(TAG, "All matches: ");
566 for (ResolveInfo ri : allMatches) {
567 Log.d(TAG, " --> " + ri);
568 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400569 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400570 // did this resolve to a single app, or the resolver?
571 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700572 // can't find any activity to handle this. let's leave the
573 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400574 // to launch.
575 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400576
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400577 // set accessibility text to "Not installed"
578 mHotseatLabels[i] = getText(R.string.activity_not_found);
579 } else {
580 boolean found = false;
581 for (ResolveInfo ri : allMatches) {
582 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
583 && bestMatch.activityInfo.applicationInfo.packageName
584 .equals(ri.activityInfo.applicationInfo.packageName)) {
585 found = true;
586 break;
587 }
588 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700589
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400590 if (!found) {
591 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
592 // the bestMatch is probably the ResolveActivity, meaning the
593 // user has not yet selected a default
594 // so: we'll keep the original intent for now
595 mHotseats[i] = intent;
596
597 // set the accessibility text to "Select shortcut"
598 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
599 } else {
600 // we have an app!
601 // now reconstruct the intent to launch it through the front
602 // door
603 ComponentName com = new ComponentName(
604 bestMatch.activityInfo.applicationInfo.packageName,
605 bestMatch.activityInfo.name);
606 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
607
608 // load the app label for accessibility
609 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
610 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400611 }
612
613 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700614 Log.d(TAG, "loadHotseats: hotseat " + i
615 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400616 + ((mHotseats[i] == null)
617 ? "null"
618 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400619 + "] label=[" + mHotseatLabels[i]
620 + "]"
621 );
622 }
623 }
624 }
625
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 @Override
627 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700628 mWaitingForResult = false;
629
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 // The pattern used here is that a user PICKs a specific application,
631 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
634 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700635
Romain Guy94dabf12009-07-21 10:55:43 -0700636 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 switch (requestCode) {
638 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400639 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 break;
641 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800642 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 break;
644 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400645 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 break;
647 case REQUEST_PICK_LIVE_FOLDER:
648 addLiveFolder(data);
649 break;
650 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400651 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700653 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700654 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700656 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700657 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700658 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700660 case REQUEST_PICK_WALLPAPER:
661 // We just wanted the activity result here so we can clear mWaitingForResult
662 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
Romain Guy18042c82009-11-06 11:44:55 -0800664 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
665 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
666 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700667 // Clean up the appWidgetId if we canceled
668 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
669 if (appWidgetId != -1) {
670 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
672 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 }
674
675 @Override
676 protected void onResume() {
677 super.onResume();
678
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800679 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800680
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400682 mWorkspaceLoading = true;
683 mModel.startLoader(this, true);
684 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 }
686 }
687
688 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700689 protected void onPause() {
690 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700691 // Some launcher layouts don't have a previous and next view
692 if (mPreviousView != null) {
693 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700694 }
695 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700696 dismissPreview(mNextView);
697 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800698 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700699 }
Romain Guycbb89e42009-06-08 15:52:54 -0700700
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700701 @Override
702 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400703 // Flag the loader to stop early before switching
704 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800705 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800706 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700707 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700708
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800709 // We can't hide the IME if it was forced open. So don't bother
710 /*
711 @Override
712 public void onWindowFocusChanged(boolean hasFocus) {
713 super.onWindowFocusChanged(hasFocus);
714
715 if (hasFocus) {
716 final InputMethodManager inputManager = (InputMethodManager)
717 getSystemService(Context.INPUT_METHOD_SERVICE);
718 WindowManager.LayoutParams lp = getWindow().getAttributes();
719 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
720 android.os.Handler()) {
721 protected void onReceiveResult(int resultCode, Bundle resultData) {
722 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
723 }
724 });
725 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
726 }
727 }
728 */
729
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 private boolean acceptFilter() {
731 final InputMethodManager inputManager = (InputMethodManager)
732 getSystemService(Context.INPUT_METHOD_SERVICE);
733 return !inputManager.isFullscreenMode();
734 }
735
736 @Override
737 public boolean onKeyDown(int keyCode, KeyEvent event) {
738 boolean handled = super.onKeyDown(keyCode, event);
739 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
740 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
741 keyCode, event);
742 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700743 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700744 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700745 // showSearchDialog()
746 // If there are multiple keystrokes before the search dialog takes focus,
747 // onSearchRequested() will be called for every keystroke,
748 // but it is idempotent, so it's fine.
749 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 }
751 }
752
Joe Onorato8a9625e2010-01-28 15:55:35 -0800753 // Eat the long press event so the keyboard doesn't come up.
754 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
755 return true;
756 }
757
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 return handled;
759 }
760
Karl Rosaen138a0412009-04-23 19:00:21 -0700761 private String getTypedText() {
762 return mDefaultKeySsb.toString();
763 }
764
765 private void clearTypedText() {
766 mDefaultKeySsb.clear();
767 mDefaultKeySsb.clearSpans();
768 Selection.setSelection(mDefaultKeySsb, 0);
769 }
770
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 /**
772 * Restores the previous state, if it exists.
773 *
774 * @param savedState The previous state.
775 */
776 private void restoreState(Bundle savedState) {
777 if (savedState == null) {
778 return;
779 }
780
Joe Onorato3a8820b2009-11-10 15:06:42 -0800781 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
782 if (allApps) {
783 showAllApps(false);
784 }
785
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
787 if (currentScreen > -1) {
788 mWorkspace.setCurrentScreen(currentScreen);
789 }
790
791 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
792 if (addScreen > -1) {
793 mAddItemCellInfo = new CellLayout.CellInfo();
794 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
795 addItemCellInfo.valid = true;
796 addItemCellInfo.screen = addScreen;
797 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
798 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
799 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
800 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Michael Jurkac28de512010-08-13 11:27:44 -0700801 addItemCellInfo.updateOccupiedCells(
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
803 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
804 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
805 mRestoring = true;
806 }
807
808 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
809 if (renameFolder) {
810 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400811 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 mRestoring = true;
813 }
814 }
815
816 /**
817 * Finds all the views we need and configure them properly.
818 */
819 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400820 DragController dragController = mDragController;
821
Romain Guyb1b69f52009-08-10 15:10:15 -0700822 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400823 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824
Joe Onorato7c312c12009-08-13 21:36:53 -0700825 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700826 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700827 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800828 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700829 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700830 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700831
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700832 if (LauncherApplication.isScreenXLarge()) {
833 // They need to be INVISIBLE initially so that they will be measured in the layout.
834 // Otherwise the animations are messed up when we show them for the first time.
835 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
836 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
837 }
838
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
840 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500841 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700842
Joe Onorato7c312c12009-08-13 21:36:53 -0700843 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
844 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700846 View handleView = findViewById(R.id.all_apps_button);
847 if (handleView != null && handleView instanceof HandleView) {
848 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700849 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700850 mHandleView.setLauncher(this);
851 mHandleView.setOnClickListener(this);
852 mHandleView.setOnLongClickListener(this);
853 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800854
Winson Chung80baf5a2010-08-09 16:03:15 -0700855 if (mCustomizePagedView != null) {
856 mCustomizePagedView.setLauncher(this);
857 mCustomizePagedView.setDragController(dragController);
858 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700859 } else {
860 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
861 hotseatLeft.setContentDescription(mHotseatLabels[0]);
862 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
863 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
864 hotseatRight.setContentDescription(mHotseatLabels[1]);
865 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400866
Michael Jurkaaf442092010-06-10 17:01:57 -0700867 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
868 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800869
Michael Jurkaaf442092010-06-10 17:01:57 -0700870 Drawable previous = mPreviousView.getDrawable();
871 Drawable next = mNextView.getDrawable();
872 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873
Michael Jurkaaf442092010-06-10 17:01:57 -0700874 mPreviousView.setHapticFeedbackEnabled(false);
875 mPreviousView.setOnLongClickListener(this);
876 mNextView.setHapticFeedbackEnabled(false);
877 mNextView.setOnLongClickListener(this);
878 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800879
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400881 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883
884 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400885 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700886 int deleteZoneHandleId;
887 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700888 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700889 } else {
890 deleteZoneHandleId = R.id.all_apps_button_cluster;
891 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700892 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700893 dragController.addDragListener(deleteZone);
894
895 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
896 if (infoButton != null) {
897 infoButton.setLauncher(this);
898 infoButton.setHandle(findViewById(R.id.configure_button));
899 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
900 dragController.addDragListener(infoButton);
901 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902
Joe Onorato00acb122009-08-04 16:04:30 -0400903 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400904 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800905 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700906
Joe Onorato00acb122009-08-04 16:04:30 -0400907 // The order here is bottom to top.
908 dragController.addDropTarget(workspace);
909 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700910 if (infoButton != null) {
911 dragController.addDropTarget(infoButton);
912 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 }
914
Romain Guy8a73c512009-11-09 19:19:59 -0800915 @SuppressWarnings({"UnusedDeclaration"})
916 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800917 if (!isAllAppsVisible()) {
918 mWorkspace.scrollLeft();
919 }
Romain Guy8a73c512009-11-09 19:19:59 -0800920 }
921
922 @SuppressWarnings({"UnusedDeclaration"})
923 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800924 if (!isAllAppsVisible()) {
925 mWorkspace.scrollRight();
926 }
Romain Guy8a73c512009-11-09 19:19:59 -0800927 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400928
929 @SuppressWarnings({"UnusedDeclaration"})
930 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400931 if (isAllAppsVisible()) return;
932
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400933 int index = -1;
934 if (v.getId() == R.id.hotseat_left) {
935 index = 0;
936 } else if (v.getId() == R.id.hotseat_right) {
937 index = 1;
938 }
939
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400940 // reload these every tap; you never know when they might change
941 loadHotseats();
942 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
943 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400944 startActivitySafely(
945 mHotseats[index],
946 "hotseat"
947 );
948 }
949 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700950
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 /**
952 * Creates a view representing a shortcut.
953 *
954 * @param info The data structure describing the shortcut.
955 *
956 * @return A View inflated from R.layout.application.
957 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800958 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 return createShortcut(R.layout.application,
960 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
961 }
962
963 /**
964 * Creates a view representing a shortcut inflated from the specified resource.
965 *
966 * @param layoutResId The id of the XML layout used to create the shortcut.
967 * @param parent The group the shortcut belongs to.
968 * @param info The data structure describing the shortcut.
969 *
970 * @return A View inflated from layoutResId.
971 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800972 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
974
Joe Onorato0589f0f2010-02-08 13:44:00 -0800975 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
976 new FastBitmapDrawable(info.getIcon(mIconCache)),
977 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 favorite.setText(info.title);
979 favorite.setTag(info);
980 favorite.setOnClickListener(this);
981
982 return favorite;
983 }
984
985 /**
986 * Add an application shortcut to the workspace.
987 *
988 * @param data The intent describing the application.
989 * @param cellInfo The position on screen where to create the shortcut.
990 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400991 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 cellInfo.screen = mWorkspace.getCurrentScreen();
993 if (!findSingleSlot(cellInfo)) return;
994
Joe Onorato0589f0f2010-02-08 13:44:00 -0800995 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
996 data, context);
997
Romain Guy73b979d2009-06-09 12:57:21 -0700998 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800999 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001001 info.container = ItemInfo.NO_ID;
1002 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
1003 } else {
1004 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 }
1006 }
Romain Guycbb89e42009-06-08 15:52:54 -07001007
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 /**
1009 * Add a shortcut to the workspace.
1010 *
1011 * @param data The intent describing the shortcut.
1012 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001014 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 cellInfo.screen = mWorkspace.getCurrentScreen();
1016 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001017
Joe Onorato0589f0f2010-02-08 13:44:00 -08001018 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019
1020 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001022 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1023 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 }
1025 }
1026
Romain Guycbb89e42009-06-08 15:52:54 -07001027
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001029 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001031 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001032 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 */
Michael Jurkaaf442092010-06-10 17:01:57 -07001034 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001035 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001036
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001037 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001039 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 // Try finding open space on Launcher screen
1042 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -08001043 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
1044 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1045 return;
1046 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001048 // Build Launcher-specific widget info and save to database
1049 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 launcherInfo.spanX = spans[0];
1051 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 LauncherModel.addItemToDatabase(this, launcherInfo,
1054 LauncherSettings.Favorites.CONTAINER_DESKTOP,
1055 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
1056
1057 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001058 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001059
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001061 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001062
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001063 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001065
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001067 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 }
1069 }
Romain Guycbb89e42009-06-08 15:52:54 -07001070
Joe Onorato9c1289c2009-08-17 11:03:03 -04001071 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1072 mDesktopItems.remove(launcherInfo);
1073 launcherInfo.hostView = null;
1074 }
1075
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001076 public LauncherAppWidgetHost getAppWidgetHost() {
1077 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 }
Romain Guycbb89e42009-06-08 15:52:54 -07001079
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001080 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001081 getWindow().closeAllPanels();
1082
1083 try {
1084 dismissDialog(DIALOG_CREATE_SHORTCUT);
1085 // Unlock the workspace if the dialog was showing
1086 } catch (Exception e) {
1087 // An exception is thrown if the dialog is not visible, which is fine
1088 }
1089
1090 try {
1091 dismissDialog(DIALOG_RENAME_FOLDER);
1092 // Unlock the workspace if the dialog was showing
1093 } catch (Exception e) {
1094 // An exception is thrown if the dialog is not visible, which is fine
1095 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001096
1097 // Whatever we were doing is hereby canceled.
1098 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001099 }
1100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 @Override
1102 protected void onNewIntent(Intent intent) {
1103 super.onNewIntent(intent);
1104
1105 // Close the menu
1106 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001107 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001108 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001109
Joe Onorato14f122b2009-11-19 14:06:36 -08001110 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1111 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1112 boolean allAppsVisible = isAllAppsVisible();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001113
Michael Jurka4cb37242010-08-09 21:05:32 -07001114 // in all these cases, only animate if we're already on home
1115 if (LauncherApplication.isScreenXLarge()) {
1116 mWorkspace.unshrink(alreadyOnHome);
1117 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08001118 if (!mWorkspace.isDefaultScreenShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001119 // on the phone, we don't animate the change to the workspace if all apps is visible
1120 // on xlarge screens, however, we want an animated transition
1121 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1122 (LauncherApplication.isScreenXLarge() || !allAppsVisible));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001123 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001124 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001125 hideCustomizationDrawer();
Romain Guy1dd3a072009-07-16 13:21:01 -07001126
Joe Onorato3a8820b2009-11-10 15:06:42 -08001127 final View v = getWindow().peekDecorView();
1128 if (v != null && v.getWindowToken() != null) {
1129 InputMethodManager imm = (InputMethodManager)getSystemService(
1130 INPUT_METHOD_SERVICE);
1131 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 }
1133 }
1134 }
1135
1136 @Override
1137 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1138 // Do not call super here
1139 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001140
1141 if (mHomeCustomizationDrawer != null) {
1142 String cur = savedInstanceState.getString("currentTab");
1143 if (cur != null) {
1144 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1145 }
1146 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 }
1148
1149 @Override
1150 protected void onSaveInstanceState(Bundle outState) {
1151 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1152
1153 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1154 if (folders.size() > 0) {
1155 final int count = folders.size();
1156 long[] ids = new long[count];
1157 for (int i = 0; i < count; i++) {
1158 final FolderInfo info = folders.get(i).getInfo();
1159 ids[i] = info.id;
1160 }
1161 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1162 } else {
1163 super.onSaveInstanceState(outState);
1164 }
1165
Joe Onoratofb0ca672009-09-14 17:55:46 -04001166 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001167 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001169 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170
1171 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1172 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1173 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1174
1175 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1176 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1177 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1178 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1179 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1180 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1181 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1182 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001183 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 }
1185
1186 if (mFolderInfo != null && mWaitingForResult) {
1187 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1188 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1189 }
Michael Jurka946ad472010-07-09 18:05:18 -07001190
1191 if (mHomeCustomizationDrawer != null) {
1192 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1193 if (currentTabTag != null) {
1194 outState.putString("currentTab", currentTabTag);
1195 }
1196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 }
1198
1199 @Override
1200 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001204 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001206 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 }
1208
1209 TextKeyListener.getInstance().release();
1210
Joe Onorato9c1289c2009-08-17 11:03:03 -04001211 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212
Joe Onorato9c1289c2009-08-17 11:03:03 -04001213 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001214
1215 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001216
Patrick Dubroyab962b72010-07-26 12:10:10 -07001217 // Some launcher layouts don't have a previous and next view
1218 if (mPreviousView != null) {
1219 dismissPreview(mPreviousView);
1220 }
1221 if (mNextView != null) {
1222 dismissPreview(mNextView);
1223 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001224
1225 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 }
1227
1228 @Override
1229 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001230 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 super.startActivityForResult(intent, requestCode);
1232 }
1233
1234 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001235 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001237
Joe Onorato7bb17492009-09-24 17:51:01 -07001238 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001239
Karl Rosaen138a0412009-04-23 19:00:21 -07001240 if (initialQuery == null) {
1241 // Use any text typed in the launcher as the initial query
1242 initialQuery = getTypedText();
1243 clearTypedText();
1244 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 if (appSearchData == null) {
1246 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001247 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 }
Romain Guycbb89e42009-06-08 15:52:54 -07001249
Karl Rosaen138a0412009-04-23 19:00:21 -07001250 final SearchManager searchManager =
1251 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001252 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001253 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 }
1255
1256 @Override
1257 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001258 if (isWorkspaceLocked()) {
1259 return false;
1260 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261
1262 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001263
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1265 .setIcon(android.R.drawable.ic_menu_add)
1266 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001267 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 .setIcon(android.R.drawable.ic_menu_gallery)
1269 .setAlphabeticShortcut('W');
1270 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1271 .setIcon(android.R.drawable.ic_search_category_default)
1272 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1273 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1274 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1275 .setAlphabeticShortcut('N');
1276
1277 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001278 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1279 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280
1281 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1282 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1283 .setIntent(settings);
1284
1285 return true;
1286 }
1287
1288 @Override
1289 public boolean onPrepareOptionsMenu(Menu menu) {
1290 super.onPrepareOptionsMenu(menu);
1291
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001292 // If all apps is animating, don't show the menu, because we don't know
1293 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001294 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001295 return false;
1296 }
1297
1298 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001299 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001300 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1301 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1302
1303 // Disable add if the workspace is full.
1304 if (visible) {
Michael Jurkac28de512010-08-13 11:27:44 -07001305 mMenuAddInfo = mWorkspace.updateOccupiedCells(null);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001306 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1307 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308
1309 return true;
1310 }
1311
Michael Jurka0e260592010-06-30 17:07:39 -07001312 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1313 // using the settings menu to add an item, something similar happens in showAddDialog
1314 public void prepareAddItemFromHomeCustomizationDrawer() {
Michael Jurkac28de512010-08-13 11:27:44 -07001315 mMenuAddInfo = mWorkspace.updateOccupiedCells(null);
Michael Jurka0e260592010-06-30 17:07:39 -07001316 mAddItemCellInfo = mMenuAddInfo;
1317 }
1318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 @Override
1320 public boolean onOptionsItemSelected(MenuItem item) {
1321 switch (item.getItemId()) {
1322 case MENU_ADD:
1323 addItems();
1324 return true;
1325 case MENU_WALLPAPER_SETTINGS:
1326 startWallpaper();
1327 return true;
1328 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001329 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 return true;
1331 case MENU_NOTIFICATIONS:
1332 showNotifications();
1333 return true;
1334 }
1335
1336 return super.onOptionsItemSelected(item);
1337 }
Romain Guycbb89e42009-06-08 15:52:54 -07001338
Karl Rosaen138a0412009-04-23 19:00:21 -07001339 /**
1340 * Indicates that we want global search for this activity by setting the globalSearch
1341 * argument for {@link #startSearch} to true.
1342 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001344 @Override
1345 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001346 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001347 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001348 }
1349
Joe Onorato9c1289c2009-08-17 11:03:03 -04001350 public boolean isWorkspaceLocked() {
1351 return mWorkspaceLoading || mWaitingForResult;
1352 }
1353
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001355 if (LauncherApplication.isScreenXLarge()) {
1356 // Animate the widget chooser up from the bottom of the screen
1357 if (!isCustomizationDrawerVisible()) {
1358 showCustomizationDrawer();
Michael Jurka213d9632010-07-28 11:29:25 -07001359 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001360 }
1361 } else {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001362 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001363 showAddDialog(mMenuAddInfo);
1364 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 }
1366
Michael Jurkaaf442092010-06-10 17:01:57 -07001367 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo) {
1368 mAddItemCellInfo = cellInfo;
1369 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1370 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1371 addAppWidgetImpl(appWidgetId);
1372 }
1373
1374 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001375 // TODO: catch bad widget exception when sent
1376 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001377 // TODO: Is this log message meaningful?
1378 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1379 addAppWidgetImpl(appWidgetId);
1380 }
1381
1382 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001383 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384
Bjorn Bringert7984c942009-12-09 15:38:25 +00001385 if (appWidget.configure != null) {
1386 // Launch over to configure widget, if needed
1387 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1388 intent.setComponent(appWidget.configure);
1389 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1390
Romain Guy8e633c52010-03-04 12:51:36 -08001391 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001393 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001394 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 }
1396 }
Romain Guycbb89e42009-06-08 15:52:54 -07001397
Joe Onoratodeb98af2010-02-19 14:59:39 -08001398 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001399 // Handle case where user selected "Applications"
1400 String applicationName = getResources().getString(R.string.group_applications);
1401 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001402
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001403 if (applicationName != null && applicationName.equals(shortcutName)) {
1404 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1405 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001406
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001407 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1408 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001409 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001410 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001411 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001412 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 }
1414
1415 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001416 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001417 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001418 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001419
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001420 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001421 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001422 } else {
1423 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1424 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 }
1426
Joe Onorato9c1289c2009-08-17 11:03:03 -04001427 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 UserFolderInfo folderInfo = new UserFolderInfo();
1429 folderInfo.title = getText(R.string.folder_name);
1430
1431 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1432 cellInfo.screen = mWorkspace.getCurrentScreen();
1433 if (!findSingleSlot(cellInfo)) return;
1434
1435 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001436 LauncherModel.addItemToDatabase(this, folderInfo,
1437 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001439 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440
1441 // Create the view
1442 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1443 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1444 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001445 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 }
Romain Guycbb89e42009-06-08 15:52:54 -07001447
Joe Onorato9c1289c2009-08-17 11:03:03 -04001448 void removeFolder(FolderInfo folder) {
1449 mFolders.remove(folder.id);
1450 }
1451
1452 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 cellInfo.screen = mWorkspace.getCurrentScreen();
1454 if (!findSingleSlot(cellInfo)) return;
1455
1456 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1457
1458 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1460 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001461 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1462 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 }
1464 }
1465
1466 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1467 CellLayout.CellInfo cellInfo, boolean notify) {
1468
1469 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1470 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1471
1472 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 Intent.ShortcutIconResource iconResource = null;
1474
1475 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1476 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1477 try {
1478 iconResource = (Intent.ShortcutIconResource) extra;
1479 final PackageManager packageManager = context.getPackageManager();
1480 Resources resources = packageManager.getResourcesForApplication(
1481 iconResource.packageName);
1482 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1483 icon = resources.getDrawable(id);
1484 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001485 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 }
1487 }
1488
1489 if (icon == null) {
1490 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1491 }
1492
1493 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001494 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 info.title = name;
1496 info.iconResource = iconResource;
1497 info.uri = data.getData();
1498 info.baseIntent = baseIntent;
1499 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1500 LiveFolders.DISPLAY_MODE_GRID);
1501
1502 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1503 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001504 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505
1506 return info;
1507 }
1508
1509 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1510 final int[] xy = new int[2];
1511 if (findSlot(cellInfo, xy, 1, 1)) {
1512 cellInfo.cellX = xy[0];
1513 cellInfo.cellY = xy[1];
1514 return true;
1515 }
1516 return false;
1517 }
1518
1519 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1520 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1521 boolean[] occupied = mSavedState != null ?
1522 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
Michael Jurkac28de512010-08-13 11:27:44 -07001523 cellInfo = mWorkspace.updateOccupiedCells(occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1525 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1526 return false;
1527 }
1528 }
1529 return true;
1530 }
1531
1532 private void showNotifications() {
1533 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1534 if (statusBar != null) {
1535 statusBar.expand();
1536 }
1537 }
1538
1539 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001540 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001542 Intent chooser = Intent.createChooser(pickWallpaper,
1543 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001544 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1545 // Removed in Eclair MR1
1546// WallpaperManager wm = (WallpaperManager)
1547// getSystemService(Context.WALLPAPER_SERVICE);
1548// WallpaperInfo wi = wm.getWallpaperInfo();
1549// if (wi != null && wi.getSettingsActivity() != null) {
1550// LabeledIntent li = new LabeledIntent(getPackageName(),
1551// R.string.configure_wallpaper, 0);
1552// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1553// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1554// }
Mike Clerona0618e42009-10-22 13:55:21 -07001555 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 }
1557
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001558 /**
1559 * Registers various content observers. The current implementation registers
1560 * only a favorites observer to keep track of the favorites applications.
1561 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001562 private void registerContentObservers() {
1563 ContentResolver resolver = getContentResolver();
1564 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1565 true, mWidgetObserver);
1566 }
1567
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 @Override
1569 public boolean dispatchKeyEvent(KeyEvent event) {
1570 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1571 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001573 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001574 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001575 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001576 dumpState();
1577 return true;
1578 }
1579 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001580 }
1581 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1582 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001583 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 return true;
1585 }
1586 }
1587
1588 return super.dispatchKeyEvent(event);
1589 }
1590
Joe Onorato88ec0992009-11-19 13:16:06 -08001591 @Override
1592 public void onBackPressed() {
1593 if (isAllAppsVisible()) {
1594 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001595 } else if (isCustomizationDrawerVisible()) {
1596 hideCustomizationDrawer();
Joe Onorato88ec0992009-11-19 13:16:06 -08001597 } else {
1598 closeFolder();
1599 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001600 // Some launcher layouts don't have a previous and next view
1601 if (mPreviousView != null) {
1602 dismissPreview(mPreviousView);
1603 dismissPreview(mNextView);
1604 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001605 }
1606
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 private void closeFolder() {
1608 Folder folder = mWorkspace.getOpenFolder();
1609 if (folder != null) {
1610 closeFolder(folder);
1611 }
1612 }
1613
1614 void closeFolder(Folder folder) {
1615 folder.getInfo().opened = false;
1616 ViewGroup parent = (ViewGroup) folder.getParent();
1617 if (parent != null) {
1618 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001619 if (folder instanceof DropTarget) {
1620 // Live folders aren't DropTargets.
1621 mDragController.removeDropTarget((DropTarget)folder);
1622 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 }
1624 folder.onClose();
1625 }
1626
1627 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001628 * Re-listen when widgets are reset.
1629 */
1630 private void onAppWidgetReset() {
1631 mAppWidgetHost.startListening();
1632 }
1633
1634 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001635 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1636 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001638 private void unbindDesktopItems() {
1639 for (ItemInfo item: mDesktopItems) {
1640 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 }
1642 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001643
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 /**
1645 * Launches the intent referred by the clicked shortcut.
1646 *
1647 * @param v The view representing the clicked shortcut.
1648 */
1649 public void onClick(View v) {
1650 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001651 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001653 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001654 int[] pos = new int[2];
1655 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001656 intent.setSourceBounds(new Rect(pos[0], pos[1],
1657 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001658 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 } else if (tag instanceof FolderInfo) {
1660 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001661 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001662 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001663 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001664 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001665 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001666 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 }
1668 }
1669
Michael Jurka0e260592010-06-30 17:07:39 -07001670 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001671 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001672 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy558654c2010-07-23 16:48:11 -07001673 hideCustomizationDrawer();
Michael Jurka0e260592010-06-30 17:07:39 -07001674 return false;
1675 }
1676
Michael Jurkaaf442092010-06-10 17:01:57 -07001677 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001678 * Event handler for the search button
1679 *
1680 * @param v The view that was clicked.
1681 */
1682 public void onClickSearchButton(View v) {
1683 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1684 View button = findViewById(R.id.search_button);
1685 i.setSourceBounds(
1686 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1687 startActivity(i);
1688 }
1689
1690 /**
1691 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001692 * enters home screen customization mode.
1693 *
1694 * @param v The view that was clicked.
1695 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001696 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001697 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001698 }
1699
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001700 /**
1701 * Event handler for the "grid" button that appears on the home screen, which
1702 * enters all apps mode.
1703 *
1704 * @param v The view that was clicked.
1705 */
1706 public void onClickAllAppsButton(View v) {
1707 showAllApps(true);
1708 }
1709
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001710 void startApplicationDetailsActivity(String packageName) {
1711 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1712 Uri.fromParts("package", packageName, null));
1713 startActivity(intent);
1714 }
1715
Joe Onoratof984e852010-03-25 09:47:45 -07001716 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1718 try {
1719 startActivity(intent);
1720 } catch (ActivityNotFoundException e) {
1721 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001722 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 } catch (SecurityException e) {
1724 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001725 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001727 "or use the exported attribute for this activity. "
1728 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 }
1730 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001731
Romain Guy8e633c52010-03-04 12:51:36 -08001732 void startActivityForResultSafely(Intent intent, int requestCode) {
1733 try {
1734 startActivityForResult(intent, requestCode);
1735 } catch (ActivityNotFoundException e) {
1736 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1737 } catch (SecurityException e) {
1738 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1739 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1740 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1741 "or use the exported attribute for this activity.", e);
1742 }
1743 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001744
1745 private void handleFolderClick(FolderInfo folderInfo) {
1746 if (!folderInfo.opened) {
1747 // Close any open folder
1748 closeFolder();
1749 // Open the requested folder
1750 openFolder(folderInfo);
1751 } else {
1752 // Find the open folder...
1753 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1754 int folderScreen;
1755 if (openFolder != null) {
1756 folderScreen = mWorkspace.getScreenForView(openFolder);
1757 // .. and close it
1758 closeFolder(openFolder);
1759 if (folderScreen != mWorkspace.getCurrentScreen()) {
1760 // Close any folder open on the current screen
1761 closeFolder();
1762 // Pull the folder onto this screen
1763 openFolder(folderInfo);
1764 }
1765 }
1766 }
1767 }
1768
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 /**
1770 * Opens the user fodler described by the specified tag. The opening of the folder
1771 * is animated relative to the specified View. If the View is null, no animation
1772 * is played.
1773 *
1774 * @param folderInfo The FolderInfo describing the folder to open.
1775 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001776 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 Folder openFolder;
1778
1779 if (folderInfo instanceof UserFolderInfo) {
1780 openFolder = UserFolder.fromXml(this);
1781 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001782 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 } else {
1784 return;
1785 }
1786
Joe Onorato00acb122009-08-04 16:04:30 -04001787 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 openFolder.setLauncher(this);
1789
1790 openFolder.bind(folderInfo);
1791 folderInfo.opened = true;
1792
Winson Chungaafa03c2010-06-11 17:34:16 -07001793 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1794
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 openFolder.onOpen();
1796 }
1797
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001799 switch (v.getId()) {
1800 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001801 if (!isAllAppsVisible()) {
1802 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1803 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001804 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001805 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001806 return true;
1807 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001808 if (!isAllAppsVisible()) {
1809 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1810 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001811 showPreviews(v);
1812 }
1813 return true;
1814 case R.id.all_apps_button:
1815 if (!isAllAppsVisible()) {
1816 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1817 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1818 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001819 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001820 return true;
1821 }
1822
Joe Onorato9c1289c2009-08-17 11:03:03 -04001823 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 return false;
1825 }
1826
1827 if (!(v instanceof CellLayout)) {
1828 v = (View) v.getParent();
1829 }
1830
1831 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1832
1833 // This happens when long clicking an item with the dpad/trackball
1834 if (cellInfo == null) {
1835 return true;
1836 }
1837
1838 if (mWorkspace.allowLongPress()) {
1839 if (cellInfo.cell == null) {
1840 if (cellInfo.valid) {
1841 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001842 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001843 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1844 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 showAddDialog(cellInfo);
1846 }
1847 } else {
1848 if (!(cellInfo.cell instanceof Folder)) {
1849 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001850 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1851 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 mWorkspace.startDrag(cellInfo);
1853 }
1854 }
1855 }
1856 return true;
1857 }
1858
Romain Guye6b8e2f2009-11-10 11:56:55 -08001859 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001860 private void dismissPreview(final View v) {
1861 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001862 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001863 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1864 public void onDismiss() {
1865 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1866 int count = group.getChildCount();
1867 for (int i = 0; i < count; i++) {
1868 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1869 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001870 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1871 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1872
1873 v.setTag(R.id.workspace, null);
1874 v.setTag(R.id.icon, null);
1875 window.setOnDismissListener(null);
1876 }
1877 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001878 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001879 }
1880 v.setTag(null);
1881 }
1882
Romain Guyf8e6a802009-12-07 17:48:02 -08001883 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001884 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001885 }
1886
Romain Guya6abce82009-11-10 02:54:41 -08001887 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001888 final Resources resources = getResources();
1889 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001890
Romain Guya6abce82009-11-10 02:54:41 -08001891 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001892
Romain Guy9d31e9f2009-11-11 18:54:28 -08001893 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001894
Romain Guyf8e6a802009-12-07 17:48:02 -08001895 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001896 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001897 int extraW = (int) ((r.left + r.right) * max);
1898 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001899
1900 int aW = cell.getWidth() - extraW;
1901 float w = aW / max;
1902
1903 int width = cell.getWidth();
1904 int height = cell.getHeight();
1905 int x = cell.getLeftPadding();
1906 int y = cell.getTopPadding();
1907 width -= (x + cell.getRightPadding());
1908 height -= (y + cell.getBottomPadding());
1909
1910 float scale = w / width;
1911
1912 int count = end - start;
1913
1914 final float sWidth = width * scale;
1915 float sHeight = height * scale;
1916
Romain Guye6b8e2f2009-11-10 11:56:55 -08001917 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001918
Romain Guye6b8e2f2009-11-10 11:56:55 -08001919 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1920 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001921
1922 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001923 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001924 cell = (CellLayout) workspace.getChildAt(i);
1925
Romain Guyf8e6a802009-12-07 17:48:02 -08001926 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001927 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001928
1929 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001930 c.scale(scale, scale);
1931 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1932 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001933
Romain Guy9d31e9f2009-11-11 18:54:28 -08001934 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001935 image.setImageBitmap(bitmap);
1936 image.setTag(i);
1937 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001938 image.setOnFocusChangeListener(handler);
1939 image.setFocusable(true);
1940 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001941
1942 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001943 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1944
Winson Chungaafa03c2010-06-11 17:34:16 -07001945 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001946 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001947
1948 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001949 p.setContentView(preview);
1950 p.setWidth((int) (sWidth * count + extraW));
1951 p.setHeight((int) (sHeight + extraH));
1952 p.setAnimationStyle(R.style.AnimationPreview);
1953 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001954 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001955 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001956 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001957
Romain Guye6b8e2f2009-11-10 11:56:55 -08001958 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1959 public void onDismiss() {
1960 dismissPreview(anchor);
1961 }
1962 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001963
1964 anchor.setTag(p);
1965 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001966 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001967 }
1968
Romain Guy9d31e9f2009-11-11 18:54:28 -08001969 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001970 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001971
Romain Guye6b8e2f2009-11-10 11:56:55 -08001972 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001973 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001974 }
1975
1976 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001977 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001978 v.post(this);
1979 }
1980
1981 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001982 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001983 }
1984
1985 public void onFocusChange(View v, boolean hasFocus) {
1986 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001987 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001988 }
Romain Guya6abce82009-11-10 02:54:41 -08001989 }
1990 }
1991
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 Workspace getWorkspace() {
1993 return mWorkspace;
1994 }
1995
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 @Override
1997 protected Dialog onCreateDialog(int id) {
1998 switch (id) {
1999 case DIALOG_CREATE_SHORTCUT:
2000 return new CreateShortcut().createDialog();
2001 case DIALOG_RENAME_FOLDER:
2002 return new RenameFolder().createDialog();
2003 }
2004
2005 return super.onCreateDialog(id);
2006 }
2007
2008 @Override
2009 protected void onPrepareDialog(int id, Dialog dialog) {
2010 switch (id) {
2011 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 break;
2013 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002014 if (mFolderInfo != null) {
2015 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2016 final CharSequence text = mFolderInfo.title;
2017 input.setText(text);
2018 input.setSelection(0, text.length());
2019 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 break;
2021 }
2022 }
2023
2024 void showRenameDialog(FolderInfo info) {
2025 mFolderInfo = info;
2026 mWaitingForResult = true;
2027 showDialog(DIALOG_RENAME_FOLDER);
2028 }
2029
2030 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2031 mAddItemCellInfo = cellInfo;
2032 mWaitingForResult = true;
2033 showDialog(DIALOG_CREATE_SHORTCUT);
2034 }
2035
Joe Onoratodeb98af2010-02-19 14:59:39 -08002036 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002037 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002038 Bundle bundle = new Bundle();
2039
2040 ArrayList<String> shortcutNames = new ArrayList<String>();
2041 shortcutNames.add(getString(R.string.group_applications));
2042 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2043
2044 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2045 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2046 R.drawable.ic_launcher_application));
2047 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2048
2049 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2050 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002051 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002052 pickIntent.putExtras(bundle);
2053
Joe Onoratodeb98af2010-02-19 14:59:39 -08002054 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002055 }
2056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 private class RenameFolder {
2058 private EditText mInput;
2059
2060 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2062 mInput = (EditText) layout.findViewById(R.id.folder_name);
2063
2064 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2065 builder.setIcon(0);
2066 builder.setTitle(getString(R.string.rename_folder_title));
2067 builder.setCancelable(true);
2068 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2069 public void onCancel(DialogInterface dialog) {
2070 cleanup();
2071 }
2072 });
2073 builder.setNegativeButton(getString(R.string.cancel_action),
2074 new Dialog.OnClickListener() {
2075 public void onClick(DialogInterface dialog, int which) {
2076 cleanup();
2077 }
2078 }
2079 );
2080 builder.setPositiveButton(getString(R.string.rename_action),
2081 new Dialog.OnClickListener() {
2082 public void onClick(DialogInterface dialog, int which) {
2083 changeFolderName();
2084 }
2085 }
2086 );
2087 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002088
2089 final AlertDialog dialog = builder.create();
2090 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2091 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002092 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002093 mInput.requestFocus();
2094 InputMethodManager inputManager = (InputMethodManager)
2095 getSystemService(Context.INPUT_METHOD_SERVICE);
2096 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002097 }
2098 });
2099
2100 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 }
2102
2103 private void changeFolderName() {
2104 final String name = mInput.getText().toString();
2105 if (!TextUtils.isEmpty(name)) {
2106 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04002107 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 mFolderInfo.title = name;
2109 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2110
Joe Onorato9c1289c2009-08-17 11:03:03 -04002111 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002112 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002113 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 } else {
2115 final FolderIcon folderIcon = (FolderIcon)
2116 mWorkspace.getViewForTag(mFolderInfo);
2117 if (folderIcon != null) {
2118 folderIcon.setText(name);
2119 getWorkspace().requestLayout();
2120 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002121 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002122 mWorkspaceLoading = true;
2123 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 }
2125 }
2126 }
2127 cleanup();
2128 }
2129
2130 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131 dismissDialog(DIALOG_RENAME_FOLDER);
2132 mWaitingForResult = false;
2133 mFolderInfo = null;
2134 }
2135 }
2136
Daniel Sandler843e8602010-06-07 14:59:01 -04002137 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2138 public boolean isAllAppsVisible() {
Romain Guy5bbc91b2010-08-18 11:38:46 -07002139 return mAllAppsGrid != null && mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04002140 }
2141
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002142 // AllAppsView.Watcher
2143 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002144 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2145 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002146 mWorkspace.setVisibility(View.GONE);
2147 }
2148 }
2149
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002150 /**
2151 * Zoom the camera out from the workspace to reveal 'toView'.
2152 * Assumes that the view to show is anchored at either the very top or very bottom
2153 * of the screen.
2154 * @param toView The view to show when the animation is complete
2155 * @param above If true, toView will appear from the top of the screen
2156 */
2157 private void cameraZoomOut(final View toView, boolean above) {
2158 final Resources res = getResources();
2159 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2160 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2161 final int height = toView.getHeight();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002162
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002163 // toView should appear right at the end of the workspace shrink animation
2164 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2165
2166 Interpolator interp = new DecelerateInterpolator();
2167
2168 toView.setPivotX(toView.getWidth() / 2.0f);
2169 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2170 // Assumes that the view is normally anchored to either the top or bottom of the screen
2171 final int margin = 200;
2172 if (above) {
2173 toView.setPivotY(height + ((toView.getTop() + height) / scale) + margin);
2174 } else {
2175 toView.setPivotY(0.0f - (toView.getTop() / scale) - margin);
2176 }
2177
2178 Animator scaleXAnim = new PropertyAnimator(duration, toView, "scaleX", scale, 1.0f);
2179 scaleXAnim.setInterpolator(interp);
2180 scaleXAnim.addListener(new AnimatableListenerAdapter() {
2181 public void onAnimationStart(Animatable animation) {
2182 // Prepare the position
2183 toView.setTranslationX(0.0f);
2184 toView.setTranslationY(0.0f);
2185 toView.setVisibility(View.VISIBLE);
2186 }
2187 });
2188
2189 Animator scaleYAnim = new PropertyAnimator(duration, toView, "scaleY", scale, 1.0f);
2190 scaleYAnim.setInterpolator(interp);
2191
2192 Sequencer s = new Sequencer();
2193 s.playTogether(scaleXAnim, scaleYAnim);
2194 s.play(scaleXAnim).after(startDelay);
2195 s.start();
2196 }
2197
2198 /**
2199 * Zoom the camera back into the workspace, hiding 'fromView'.
2200 * This is the opposite of cameraZoomOut.
2201 * @param fromView The currently-focused view, which will be hidden.
2202 */
2203 private void cameraZoomIn(final View fromView) {
2204 Resources res = getResources();
2205 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2206 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2207
2208 Interpolator interp = new AccelerateInterpolator();
2209
2210 Sequencer s = new Sequencer();
2211 Animator scaleXAnim = new PropertyAnimator(duration, fromView, "scaleX", scaleFactor);
2212 scaleXAnim.setInterpolator(interp);
2213 Animator scaleYAnim = new PropertyAnimator(duration, fromView, "scaleY", scaleFactor);
2214 scaleYAnim.setInterpolator(interp);
2215 s.playTogether(scaleXAnim, scaleYAnim);
2216 s.addListener(new AnimatableListenerAdapter() {
2217 public void onAnimationEnd(Animatable animation) {
2218 fromView.setVisibility(View.GONE);
2219 fromView.setScaleX(1.0f);
2220 fromView.setScaleY(1.0f);
2221 }
2222 });
2223 s.start();
2224 }
2225
2226 /**
2227 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2228 * This is the transition used on xlarge screens to go between all apps and
2229 * the home customization drawer.
2230 * @param fromView The view to pan away from.
2231 * @param toView The view to pan into the frame.
2232 */
2233 private void cameraPan(final View fromView, final View toView) {
2234 final int duration = getResources().getInteger(R.integer.config_allAppsCameraPanTime);
2235 final int workspaceHeight = mWorkspace.getHeight();
2236
2237 final boolean panDown = fromView.getY() < toView.getY();
2238
2239 final float fromViewStartY = panDown ? 0.0f : fromView.getY();
2240 final float fromViewEndY = panDown ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2241 final float toViewStartY = panDown ? workspaceHeight * 2 : -toView.getHeight() * 2;
2242 final float toViewEndY = panDown ? workspaceHeight - toView.getHeight() : 0.0f;
2243
2244 Sequencer s = new Sequencer();
2245 s.playTogether(
2246 new PropertyAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2247 new PropertyAnimator(duration, toView, "y", toViewStartY, toViewEndY));
2248 s.addListener(new AnimatableListenerAdapter() {
2249 public void onAnimationStart(Animatable animation) {
2250 toView.setVisibility(View.VISIBLE);
2251 toView.setY(toViewStartY);
2252 }
2253 public void onAnimationEnd(Animatable animation) {
2254 fromView.setVisibility(View.GONE);
2255 }
2256 });
2257 s.start();
2258 }
2259
2260 void showAllApps(boolean animated) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002261 if (mAllAppsGrid.isVisible())
2262 return;
2263
Michael Jurka79212d82010-07-30 16:36:20 -07002264 if (LauncherApplication.isScreenXLarge()) {
2265 mWorkspace.shrinkToBottom(animated);
2266 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002267
Patrick Dubroy558654c2010-07-23 16:48:11 -07002268 if (LauncherApplication.isScreenXLarge() && animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002269 if (isCustomizationDrawerVisible()) {
2270 cameraPan(mHomeCustomizationDrawer, (View) mAllAppsGrid);
2271 } else {
2272 cameraZoomOut((View) mAllAppsGrid, true);
2273 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002274 } else {
2275 mAllAppsGrid.zoom(1.0f, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002276 hideCustomizationDrawer(); // TODO: Should be able to do this un-animated
Patrick Dubroy558654c2010-07-23 16:48:11 -07002277 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002278
Romain Guyc16fea72010-03-12 17:17:56 -08002279 ((View) mAllAppsGrid).setFocusable(true);
2280 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002281
Joe Onorato7c312c12009-08-13 21:36:53 -07002282 // TODO: fade these two too
2283 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002284 }
2285
Joe Onoratob2061212009-11-24 16:13:54 -05002286 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002287 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002288 * - Home from workspace
2289 * - from center screen
2290 * - from other screens
2291 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002292 * - from center screen
2293 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002294 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002295 * - from center screen
2296 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002297 * - Launch app from workspace and quit
2298 * - with back
2299 * - with home
2300 * - Launch app from all apps and quit
2301 * - with back
2302 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002303 * - Go to a screen that's not the default, then all
2304 * apps, and launch and app, and go back
2305 * - with back
2306 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002307 * - On workspace, long press power and go back
2308 * - with back
2309 * - with home
2310 * - On all apps, long press power and go back
2311 * - with back
2312 * - with home
2313 * - On workspace, power off
2314 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002315 * - Launch an app and turn off the screen while in that app
2316 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002317 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002318 * - From all apps
2319 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002320 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2321 * - From all apps
2322 * - From the center workspace
2323 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002324 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002325 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002326 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002327 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002328 if (LauncherApplication.isScreenXLarge() && animated) {
Michael Jurka213d9632010-07-28 11:29:25 -07002329 mWorkspace.unshrink();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002330 cameraZoomIn((View) mAllAppsGrid);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002331 } else {
2332 mAllAppsGrid.zoom(0.0f, animated);
2333 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002334 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002335 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002336 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002337 }
2338
Joe Onorato7c312c12009-08-13 21:36:53 -07002339 void lockAllApps() {
2340 // TODO
2341 }
2342
2343 void unlockAllApps() {
2344 // TODO
2345 }
2346
Patrick Dubroy558654c2010-07-23 16:48:11 -07002347 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002348 return mHomeCustomizationDrawer != null &&
2349 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002350 }
2351
2352 private void showCustomizationDrawer() {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002353 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002354 if (isAllAppsVisible()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002355 cameraPan((View) mAllAppsGrid, mHomeCustomizationDrawer);
2356 } else {
2357 cameraZoomOut(mHomeCustomizationDrawer, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002358 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002359 }
2360
Michael Jurka4bb4f732010-08-04 18:46:01 -07002361 void hideCustomizationDrawer() {
2362 hideCustomizationDrawer(true);
2363 }
2364
2365 void hideCustomizationDrawer(boolean unshrinkWorkspace) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002366 if (isCustomizationDrawerVisible()) {
Michael Jurka4bb4f732010-08-04 18:46:01 -07002367 if (unshrinkWorkspace) {
2368 mWorkspace.unshrink();
2369 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002370 cameraZoomIn(mHomeCustomizationDrawer);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002371 }
2372 }
2373
Michael Jurka213d9632010-07-28 11:29:25 -07002374 void onWorkspaceUnshrink() {
2375 if (isAllAppsVisible()) {
Michael Jurka54dd7542010-07-30 14:47:52 -07002376 closeAllApps(true);
Michael Jurka213d9632010-07-28 11:29:25 -07002377 }
2378 if (isCustomizationDrawerVisible()) {
2379 hideCustomizationDrawer();
2380 }
2381 }
2382
Joe Onorato7404ee42009-07-31 11:54:44 -07002383 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 * Displays the shortcut creation dialog and launches, if necessary, the
2385 * appropriate activity.
2386 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002387 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002388 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2389 DialogInterface.OnShowListener {
2390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002392
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002393 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2397 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002398 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002399
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 builder.setInverseBackgroundForced(true);
2401
2402 AlertDialog dialog = builder.create();
2403 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002404 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002405 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 return dialog;
2408 }
2409
2410 public void onCancel(DialogInterface dialog) {
2411 mWaitingForResult = false;
2412 cleanup();
2413 }
2414
Romain Guycbb89e42009-06-08 15:52:54 -07002415 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002416 }
2417
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002419 try {
2420 dismissDialog(DIALOG_CREATE_SHORTCUT);
2421 } catch (Exception e) {
2422 // An exception is thrown if the dialog is not visible, which is fine
2423 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002424 }
2425
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002426 /**
2427 * Handle the action clicked in the "Add to home" dialog.
2428 */
2429 public void onClick(DialogInterface dialog, int which) {
2430 Resources res = getResources();
2431 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002432
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002433 switch (which) {
2434 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002435 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002436 break;
2437 }
Romain Guycbb89e42009-06-08 15:52:54 -07002438
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002439 case AddAdapter.ITEM_APPWIDGET: {
2440 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002441
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002442 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2443 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002444 // start the pick activity
2445 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2446 break;
2447 }
Romain Guycbb89e42009-06-08 15:52:54 -07002448
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002449 case AddAdapter.ITEM_LIVE_FOLDER: {
2450 // Insert extra item to handle inserting folder
2451 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002452
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002453 ArrayList<String> shortcutNames = new ArrayList<String>();
2454 shortcutNames.add(res.getString(R.string.group_folder));
2455 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002456
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002457 ArrayList<ShortcutIconResource> shortcutIcons =
2458 new ArrayList<ShortcutIconResource>();
2459 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2460 R.drawable.ic_launcher_folder));
2461 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2462
2463 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2464 pickIntent.putExtra(Intent.EXTRA_INTENT,
2465 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2466 pickIntent.putExtra(Intent.EXTRA_TITLE,
2467 getText(R.string.title_select_live_folder));
2468 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002469
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002470 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2471 break;
2472 }
2473
2474 case AddAdapter.ITEM_WALLPAPER: {
2475 startWallpaper();
2476 break;
2477 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478 }
2479 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002480
2481 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002482 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002483 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484 }
2485
2486 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002487 * Receives notifications when applications are added/removed.
2488 */
2489 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2490 @Override
2491 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002492 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002493 String reason = intent.getStringExtra("reason");
2494 if (!"homekey".equals(reason)) {
2495 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002496 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002497 animate = false;
2498 }
Joe Onoratob2061212009-11-24 16:13:54 -05002499 closeAllApps(animate);
Michael Jurka4cb37242010-08-09 21:05:32 -07002500 mWorkspace.unshrink(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002501 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002502 }
2503 }
2504
2505 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002506 * Receives notifications whenever the appwidgets are reset.
2507 */
2508 private class AppWidgetResetObserver extends ContentObserver {
2509 public AppWidgetResetObserver() {
2510 super(new Handler());
2511 }
2512
2513 @Override
2514 public void onChange(boolean selfChange) {
2515 onAppWidgetReset();
2516 }
2517 }
2518
2519 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002520 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002521 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002522 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002523 if (mWorkspace != null) {
2524 return mWorkspace.getCurrentScreen();
2525 } else {
2526 return SCREEN_COUNT / 2;
2527 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002528 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002529
Joe Onorato9c1289c2009-08-17 11:03:03 -04002530 /**
2531 * Refreshes the shortcuts shown on the workspace.
2532 *
2533 * Implementation of the method from LauncherModel.Callbacks.
2534 */
2535 public void startBinding() {
2536 final Workspace workspace = mWorkspace;
2537 int count = workspace.getChildCount();
2538 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002539 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002540 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2541 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002542
Joe Onorato9c1289c2009-08-17 11:03:03 -04002543 if (DEBUG_USER_INTERFACE) {
2544 android.widget.Button finishButton = new android.widget.Button(this);
2545 finishButton.setText("Finish");
2546 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2547
2548 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2549 public void onClick(View v) {
2550 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002551 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002552 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 }
2554 }
2555
2556 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002557 * Bind the items start-end from the list.
2558 *
2559 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002561 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2562
2563 final Workspace workspace = mWorkspace;
2564
2565 for (int i=start; i<end; i++) {
2566 final ItemInfo item = shortcuts.get(i);
2567 mDesktopItems.add(item);
2568 switch (item.itemType) {
2569 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2570 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002571 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002572 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2573 false);
2574 break;
2575 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2576 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2577 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2578 (UserFolderInfo) item);
2579 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2580 false);
2581 break;
2582 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2583 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2584 R.layout.live_folder_icon, this,
2585 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2586 (LiveFolderInfo) item);
2587 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2588 false);
2589 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002590 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002591 }
2592
Joe Onorato9c1289c2009-08-17 11:03:03 -04002593 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002594 }
2595
Joe Onorato9c1289c2009-08-17 11:03:03 -04002596 /**
2597 * Implementation of the method from LauncherModel.Callbacks.
2598 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002599 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2600 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002601 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002602 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002603
2604 /**
2605 * Add the views for a widget to the workspace.
2606 *
2607 * Implementation of the method from LauncherModel.Callbacks.
2608 */
2609 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002610 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2611 if (DEBUG_WIDGETS) {
2612 Log.d(TAG, "bindAppWidget: " + item);
2613 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002614 final Workspace workspace = mWorkspace;
2615
2616 final int appWidgetId = item.appWidgetId;
2617 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002618 if (DEBUG_WIDGETS) {
2619 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2620 }
2621
Joe Onorato9c1289c2009-08-17 11:03:03 -04002622 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2623
Joe Onorato9c1289c2009-08-17 11:03:03 -04002624 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2625 item.hostView.setTag(item);
2626
2627 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2628 item.cellY, item.spanX, item.spanY, false);
2629
2630 workspace.requestLayout();
2631
2632 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002633
2634 if (DEBUG_WIDGETS) {
2635 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2636 + (SystemClock.uptimeMillis()-start) + "ms");
2637 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002638 }
2639
2640 /**
2641 * Callback saying that there aren't any more items to bind.
2642 *
2643 * Implementation of the method from LauncherModel.Callbacks.
2644 */
2645 public void finishBindingItems() {
2646 if (mSavedState != null) {
2647 if (!mWorkspace.hasFocus()) {
2648 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2649 }
2650
2651 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2652 if (userFolders != null) {
2653 for (long folderId : userFolders) {
2654 final FolderInfo info = mFolders.get(folderId);
2655 if (info != null) {
2656 openFolder(info);
2657 }
2658 }
2659 final Folder openFolder = mWorkspace.getOpenFolder();
2660 if (openFolder != null) {
2661 openFolder.requestFocus();
2662 }
2663 }
2664
Joe Onorato9c1289c2009-08-17 11:03:03 -04002665 mSavedState = null;
2666 }
2667
2668 if (mSavedInstanceState != null) {
2669 super.onRestoreInstanceState(mSavedInstanceState);
2670 mSavedInstanceState = null;
2671 }
2672
Joe Onorato9c1289c2009-08-17 11:03:03 -04002673 mWorkspaceLoading = false;
2674 }
2675
2676 /**
2677 * Add the icons for all apps.
2678 *
2679 * Implementation of the method from LauncherModel.Callbacks.
2680 */
2681 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002682 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002683 }
2684
2685 /**
2686 * A package was installed.
2687 *
2688 * Implementation of the method from LauncherModel.Callbacks.
2689 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002690 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002691 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002692 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002693 }
2694
2695 /**
2696 * A package was updated.
2697 *
2698 * Implementation of the method from LauncherModel.Callbacks.
2699 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002700 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002701 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002702 mWorkspace.updateShortcuts(apps);
2703 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002704 }
2705
2706 /**
2707 * A package was uninstalled.
2708 *
2709 * Implementation of the method from LauncherModel.Callbacks.
2710 */
Joe Onorato36115782010-06-17 13:28:48 -04002711 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002712 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002713 if (permanent) {
2714 mWorkspace.removeItems(apps);
2715 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002716 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002717 }
Joe Onoratobe386092009-11-17 17:32:16 -08002718
2719 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002720 * A number of packages were updated.
2721 */
2722 public void bindPackagesUpdated() {
2723 // update the customization drawer contents
Winson Chungb3347bb2010-08-19 14:51:28 -07002724 if (mCustomizePagedView != null)
2725 mCustomizePagedView.update();
Winson Chung80baf5a2010-08-09 16:03:15 -07002726 }
2727
2728 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002729 * Prints out out state for debugging.
2730 */
2731 public void dumpState() {
2732 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002733 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002734 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2735 Log.d(TAG, "mRestoring=" + mRestoring);
2736 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2737 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2738 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2739 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002740 mModel.dumpState();
2741 mAllAppsGrid.dumpState();
2742 Log.d(TAG, "END launcher2 dump state");
2743 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002744}