blob: c4bb69ef979fa75e2f7d39c496840487f9cec58d [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Patrick Dubroy4ed62782010-08-17 15:11:18 -070020import com.android.common.Search;
21import com.android.launcher.R;
22
Patrick Dubroy7247f632010-08-04 16:02:59 -070023import android.animation.Animatable;
24import android.animation.AnimatableListenerAdapter;
25import android.animation.Animator;
26import android.animation.PropertyAnimator;
27import android.animation.Sequencer;
Michael Jurka946ad472010-07-09 18:05:18 -070028import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.Dialog;
31import android.app.SearchManager;
32import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070033import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
41import android.content.DialogInterface;
42import android.content.Intent;
Winson Chung80baf5a2010-08-09 16:03:15 -070043import android.content.Intent.ShortcutIconResource;
Romain Guy5bbc91b2010-08-18 11:38:46 -070044import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040047import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040050import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080053import android.graphics.Canvas;
Winson Chung80baf5a2010-08-09 16:03:15 -070054import android.graphics.Color;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040062import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080063import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070064import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.Selection;
67import android.text.SpannableStringBuilder;
68import android.text.TextUtils;
69import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070070import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080072import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.view.KeyEvent;
74import android.view.LayoutInflater;
75import android.view.Menu;
76import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070077import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.view.View;
Romain Guy5bbc91b2010-08-18 11:38:46 -070079import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070081import android.view.WindowManager;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070082import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070083import android.view.animation.DecelerateInterpolator;
84import android.view.animation.Interpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070087import android.widget.ImageView;
88import android.widget.LinearLayout;
89import android.widget.PopupWindow;
Winson Chung80baf5a2010-08-09 16:03:15 -070090import android.widget.RelativeLayout;
Michael Jurka0e260592010-06-30 17:07:39 -070091import android.widget.TabHost;
Romain Guy5bbc91b2010-08-18 11:38:46 -070092import android.widget.TabHost.OnTabChangeListener;
93import android.widget.TabHost.TabContentFactory;
Winson Chung80baf5a2010-08-09 16:03:15 -070094import android.widget.TabWidget;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
98import java.io.DataInputStream;
99import java.io.DataOutputStream;
100import java.io.FileNotFoundException;
101import java.io.IOException;
102import java.util.ArrayList;
103import java.util.HashMap;
104import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800105
Romain Guy5bbc91b2010-08-18 11:38:46 -0700106import java.io.DataInputStream;
107import java.io.DataOutputStream;
108import java.io.FileNotFoundException;
109import java.io.IOException;
110import java.util.ArrayList;
111import java.util.HashMap;
112import java.util.List;
113
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114/**
115 * Default launcher application.
116 */
Michael Jurka946ad472010-07-09 18:05:18 -0700117public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700118 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
119 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800120 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700121 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Joe Onorato9c1289c2009-08-17 11:03:03 -0400123 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400124 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700126
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 private static final int WALLPAPER_SCREENS_SPAN = 2;
128
129 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800130 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int MENU_ADD = Menu.FIRST + 1;
133 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
134 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
135 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700136 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
138 private static final int REQUEST_CREATE_SHORTCUT = 1;
139 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700140 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 private static final int REQUEST_PICK_APPLICATION = 6;
142 private static final int REQUEST_PICK_SHORTCUT = 7;
143 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700144 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700145 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
147 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
148
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800149 static final int SCREEN_COUNT = 5;
150 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Joe Onorato7c312c12009-08-13 21:36:53 -0700152 static final int DIALOG_CREATE_SHORTCUT = 1;
153 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Romain Guy98d01652009-06-30 16:21:04 -0700155 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
157 // Type: int
158 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
159 // Type: boolean
160 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
161 // Type: long
162 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
169 // Type: int
170 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
171 // Type: int
172 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
173 // Type: int
174 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
175 // Type: int
176 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
177 // Type: int[]
178 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
179 // Type: boolean
180 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
181 // Type: long
182 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
183
Winson Chung80baf5a2010-08-09 16:03:15 -0700184 // tags for the customization tabs
185 private static final String WIDGETS_TAG = "widgets";
186 private static final String FOLDERS_TAG = "folders";
187 private static final String SHORTCUTS_TAG = "shortcuts";
188 private static final String WALLPAPERS_TAG = "wallpapers";
189
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800193 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800195 private final BroadcastReceiver mCloseSystemDialogsReceiver
196 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800197 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 private LayoutInflater mInflater;
200
Joe Onorato00acb122009-08-04 16:04:30 -0400201 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700203
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700204 private AppWidgetManager mAppWidgetManager;
205 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 private CellLayout.CellInfo mAddItemCellInfo;
208 private CellLayout.CellInfo mMenuAddInfo;
209 private final int[] mCellCoordinates = new int[2];
210 private FolderInfo mFolderInfo;
211
Joe Onorato7c312c12009-08-13 21:36:53 -0700212 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700214 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700215 private TabHost mHomeCustomizationDrawer;
Winson Chung80baf5a2010-08-09 16:03:15 -0700216 private CustomizePagedView mCustomizePagedView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 private Bundle mSavedState;
219
220 private SpannableStringBuilder mDefaultKeySsb = null;
221
Joe Onorato9c1289c2009-08-17 11:03:03 -0400222 private boolean mWorkspaceLoading = true;
223
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800224 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 private boolean mRestoring;
226 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
228 private Bundle mSavedInstanceState;
229
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800231 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232
Romain Guy84f296c2009-11-04 15:00:44 -0800233 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
234 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Romain Guy1fbc1c82009-11-09 20:43:08 -0800236 private ImageView mPreviousView;
237 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500238
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400239 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400240 private String[] mHotseatConfig = null;
241 private Intent[] mHotseats = null;
242 private Drawable[] mHotseatIcons = null;
243 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 @Override
246 protected void onCreate(Bundle savedInstanceState) {
247 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700248
Winson Chungaafa03c2010-06-11 17:34:16 -0700249 if (LauncherApplication.isInPlaceRotationEnabled()) {
250 // hide the status bar (temporary until we get the status bar design figured out)
251 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
252 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
253 }
254
Joe Onorato0589f0f2010-02-08 13:44:00 -0800255 LauncherApplication app = ((LauncherApplication)getApplication());
256 mModel = app.setLauncher(this);
257 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400258 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700260
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700261 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700262 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
263 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700264
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 if (PROFILE_STARTUP) {
266 android.os.Debug.startMethodTracing("/sdcard/launcher");
267 }
268
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400269 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 checkForLocaleChange();
271 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700273 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
274 if (mHomeCustomizationDrawer != null) {
275 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700276
Winson Chung80baf5a2010-08-09 16:03:15 -0700277 // share the same customization workspace across all the tabs
278 mCustomizePagedView = new CustomizePagedView(this);
279 TabContentFactory contentFactory = new TabContentFactory() {
280 public View createTabContent(String tag) {
281 return mCustomizePagedView;
282 }
283 };
284
Michael Jurka946ad472010-07-09 18:05:18 -0700285 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700286 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
287 .setIndicator(widgetsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700288 String foldersLabel = getString(R.string.folders_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700289 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(FOLDERS_TAG)
290 .setIndicator(foldersLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700291 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700292 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
293 .setIndicator(shortcutsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700294 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700295 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
296 .setIndicator(wallpapersLabel).setContent(contentFactory));
297
298 // TEMP: just styling the tab widget to be a bit nicer until we get the actual
299 // new assets
300 TabWidget tabWidget = mHomeCustomizationDrawer.getTabWidget();
301 for (int i = 0; i < tabWidget.getChildCount(); ++i) {
302 RelativeLayout tab = (RelativeLayout) tabWidget.getChildTabViewAt(i);
303 TextView text = (TextView) tab.getChildAt(1);
304 text.setTextSize(20.0f);
305 text.setPadding(20, 0, 20, 0);
306 text.setShadowLayer(1.0f, 0.0f, 1.0f, Color.BLACK);
307 tab.setBackgroundDrawable(null);
308 }
309
310 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
311 public void onTabChanged(String tabId) {
312 // animate the changing of the tab content by fading pages in and out
313 final int duration = 150;
314 final float alpha = mCustomizePagedView.getAlpha();
315 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
316 "alpha", alpha, 0.0f);
317 alphaAnim.addListener(new AnimatableListenerAdapter() {
318 public void onAnimationEnd(Animatable animation) {
319 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
320 if (tag == WIDGETS_TAG) {
321 mCustomizePagedView.setCustomizationFilter(
322 CustomizePagedView.CustomizationType.WidgetCustomization);
323 } else if (tag == FOLDERS_TAG) {
324 mCustomizePagedView.setCustomizationFilter(
325 CustomizePagedView.CustomizationType.FolderCustomization);
326 } else if (tag == SHORTCUTS_TAG) {
327 mCustomizePagedView.setCustomizationFilter(
328 CustomizePagedView.CustomizationType.ShortcutCustomization);
329 } else if (tag == WALLPAPERS_TAG) {
330 mCustomizePagedView.setCustomizationFilter(
331 CustomizePagedView.CustomizationType.WallpaperCustomization);
332 }
333
334 final float alpha = mCustomizePagedView.getAlpha();
335 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
336 "alpha", alpha, 1.0f);
337 alphaAnim.start();
338 }
339 });
340 alphaAnim.start();
341 }
342 });
Michael Jurka946ad472010-07-09 18:05:18 -0700343
344 mHomeCustomizationDrawer.setCurrentTab(0);
345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 setupViews();
347
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800348 registerContentObservers();
349
Joe Onorato7c312c12009-08-13 21:36:53 -0700350 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 mSavedState = savedInstanceState;
353 restoreState(mSavedState);
354
355 if (PROFILE_STARTUP) {
356 android.os.Debug.stopMethodTracing();
357 }
358
359 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400360 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 }
362
363 // For handling default keys
364 mDefaultKeySsb = new SpannableStringBuilder();
365 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800366
367 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
368 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 }
Romain Guycbb89e42009-06-08 15:52:54 -0700370
Winson Chungaafa03c2010-06-11 17:34:16 -0700371 @Override
372 public void onConfigurationChanged(Configuration newConfig) {
373 // TODO Auto-generated method stub
374 super.onConfigurationChanged(newConfig);
Winson Chungaafa03c2010-06-11 17:34:16 -0700375 }
376
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700378 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
379 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700380
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 final Configuration configuration = getResources().getConfiguration();
382
Romain Guy98d01652009-06-30 16:21:04 -0700383 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 final String locale = configuration.locale.toString();
385
Romain Guy98d01652009-06-30 16:21:04 -0700386 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 final int mcc = configuration.mcc;
388
Romain Guy98d01652009-06-30 16:21:04 -0700389 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 final int mnc = configuration.mnc;
391
Romain Guy84f296c2009-11-04 15:00:44 -0800392 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393
Romain Guy84f296c2009-11-04 15:00:44 -0800394 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700395 localeConfiguration.locale = locale;
396 localeConfiguration.mcc = mcc;
397 localeConfiguration.mnc = mnc;
398
399 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800400 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400401
402 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700403 }
404 }
405
406 private static class LocaleConfiguration {
407 public String locale;
408 public int mcc = -1;
409 public int mnc = -1;
410 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700411
Romain Guy98d01652009-06-30 16:21:04 -0700412 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
413 DataInputStream in = null;
414 try {
415 in = new DataInputStream(context.openFileInput(PREFERENCES));
416 configuration.locale = in.readUTF();
417 configuration.mcc = in.readInt();
418 configuration.mnc = in.readInt();
419 } catch (FileNotFoundException e) {
420 // Ignore
421 } catch (IOException e) {
422 // Ignore
423 } finally {
424 if (in != null) {
425 try {
426 in.close();
427 } catch (IOException e) {
428 // Ignore
429 }
430 }
431 }
432 }
433
434 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
435 DataOutputStream out = null;
436 try {
437 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
438 out.writeUTF(configuration.locale);
439 out.writeInt(configuration.mcc);
440 out.writeInt(configuration.mnc);
441 out.flush();
442 } catch (FileNotFoundException e) {
443 // Ignore
444 } catch (IOException e) {
445 //noinspection ResultOfMethodCallIgnored
446 context.getFileStreamPath(PREFERENCES).delete();
447 } finally {
448 if (out != null) {
449 try {
450 out.close();
451 } catch (IOException e) {
452 // Ignore
453 }
454 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 }
456 }
457
458 static int getScreen() {
459 synchronized (sLock) {
460 return sScreen;
461 }
462 }
463
464 static void setScreen(int screen) {
465 synchronized (sLock) {
466 sScreen = screen;
467 }
468 }
469
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700471 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472
473 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700474 // TODO: Put back when we decide about scrolling the wallpaper
475 // boolean isPortrait = display.getWidth() < display.getHeight();
476 // final int width = isPortrait ? display.getWidth() : display.getHeight();
477 // final int height = isPortrait ? display.getHeight() : display.getWidth();
478 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
479 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 }
481
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400482 // Note: This doesn't do all the client-id magic that BrowserProvider does
483 // in Browser. (http://b/2425179)
484 private Uri getDefaultBrowserUri() {
485 String url = getString(R.string.default_browser_url);
486 if (url.indexOf("{CID}") != -1) {
487 url = url.replace("{CID}", "android-google");
488 }
489 return Uri.parse(url);
490 }
491
492 // Load the Intent templates from arrays.xml to populate the hotseats. For
493 // each Intent, if it resolves to a single app, use that as the launch
494 // intent & use that app's label as the contentDescription. Otherwise,
495 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400496 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400497 if (mHotseatConfig == null) {
498 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
499 if (mHotseatConfig.length > 0) {
500 mHotseats = new Intent[mHotseatConfig.length];
501 mHotseatLabels = new CharSequence[mHotseatConfig.length];
502 mHotseatIcons = new Drawable[mHotseatConfig.length];
503 } else {
504 mHotseats = null;
505 mHotseatIcons = null;
506 mHotseatLabels = null;
507 }
508
509 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
510 for (int i=0; i<mHotseatConfig.length; i++) {
511 // load icon for this slot; currently unrelated to the actual activity
512 try {
513 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
514 } catch (ArrayIndexOutOfBoundsException ex) {
515 Log.w(TAG, "Missing hotseat_icons array item #" + i);
516 mHotseatIcons[i] = null;
517 }
518 }
519 hotseatIconDrawables.recycle();
520 }
521
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400522 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400523 for (int i=0; i<mHotseatConfig.length; i++) {
524 Intent intent = null;
525 if (mHotseatConfig[i].equals("*BROWSER*")) {
526 // magic value meaning "launch user's default web browser"
527 // replace it with a generic web request so we can see if there is indeed a default
528 String defaultUri = getString(R.string.default_browser_url);
529 intent = new Intent(
530 Intent.ACTION_VIEW,
531 ((defaultUri != null)
532 ? Uri.parse(defaultUri)
533 : getDefaultBrowserUri())
534 ).addCategory(Intent.CATEGORY_BROWSABLE);
535 // note: if the user launches this without a default set, she
536 // will always be taken to the default URL above; this is
537 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700538 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400539 // URL is unavoidably sent to the chosen app.
540 } else {
541 try {
542 intent = Intent.parseUri(mHotseatConfig[i], 0);
543 } catch (java.net.URISyntaxException ex) {
544 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
545 // bogus; leave intent=null
546 }
547 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700548
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400549 if (intent == null) {
550 mHotseats[i] = null;
551 mHotseatLabels[i] = getText(R.string.activity_not_found);
552 continue;
553 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400554
555 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700556 Log.d(TAG, "loadHotseats: hotseat " + i
557 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400558 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400559 + "]");
560 }
561
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400562 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
563 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700564 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400565 Log.d(TAG, "Best match for intent: " + bestMatch);
566 Log.d(TAG, "All matches: ");
567 for (ResolveInfo ri : allMatches) {
568 Log.d(TAG, " --> " + ri);
569 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400570 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400571 // did this resolve to a single app, or the resolver?
572 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700573 // can't find any activity to handle this. let's leave the
574 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400575 // to launch.
576 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400577
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400578 // set accessibility text to "Not installed"
579 mHotseatLabels[i] = getText(R.string.activity_not_found);
580 } else {
581 boolean found = false;
582 for (ResolveInfo ri : allMatches) {
583 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
584 && bestMatch.activityInfo.applicationInfo.packageName
585 .equals(ri.activityInfo.applicationInfo.packageName)) {
586 found = true;
587 break;
588 }
589 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700590
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400591 if (!found) {
592 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
593 // the bestMatch is probably the ResolveActivity, meaning the
594 // user has not yet selected a default
595 // so: we'll keep the original intent for now
596 mHotseats[i] = intent;
597
598 // set the accessibility text to "Select shortcut"
599 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
600 } else {
601 // we have an app!
602 // now reconstruct the intent to launch it through the front
603 // door
604 ComponentName com = new ComponentName(
605 bestMatch.activityInfo.applicationInfo.packageName,
606 bestMatch.activityInfo.name);
607 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
608
609 // load the app label for accessibility
610 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
611 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400612 }
613
614 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700615 Log.d(TAG, "loadHotseats: hotseat " + i
616 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400617 + ((mHotseats[i] == null)
618 ? "null"
619 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400620 + "] label=[" + mHotseatLabels[i]
621 + "]"
622 );
623 }
624 }
625 }
626
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 @Override
628 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700629 mWaitingForResult = false;
630
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 // The pattern used here is that a user PICKs a specific application,
632 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700633
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
635 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700636
Romain Guy94dabf12009-07-21 10:55:43 -0700637 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 switch (requestCode) {
639 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400640 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 break;
642 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800643 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 break;
645 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400646 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 break;
648 case REQUEST_PICK_LIVE_FOLDER:
649 addLiveFolder(data);
650 break;
651 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400652 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700654 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700655 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700657 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700658 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700659 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700661 case REQUEST_PICK_WALLPAPER:
662 // We just wanted the activity result here so we can clear mWaitingForResult
663 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 }
Romain Guy18042c82009-11-06 11:44:55 -0800665 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
666 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
667 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700668 // Clean up the appWidgetId if we canceled
669 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
670 if (appWidgetId != -1) {
671 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 }
673 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 }
675
676 @Override
677 protected void onResume() {
678 super.onResume();
679
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800680 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800681
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400683 mWorkspaceLoading = true;
684 mModel.startLoader(this, true);
685 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 }
687 }
688
689 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700690 protected void onPause() {
691 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700692 // Some launcher layouts don't have a previous and next view
693 if (mPreviousView != null) {
694 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700695 }
696 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700697 dismissPreview(mNextView);
698 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800699 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700700 }
Romain Guycbb89e42009-06-08 15:52:54 -0700701
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700702 @Override
703 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400704 // Flag the loader to stop early before switching
705 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800706 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800707 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700708 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700709
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800710 // We can't hide the IME if it was forced open. So don't bother
711 /*
712 @Override
713 public void onWindowFocusChanged(boolean hasFocus) {
714 super.onWindowFocusChanged(hasFocus);
715
716 if (hasFocus) {
717 final InputMethodManager inputManager = (InputMethodManager)
718 getSystemService(Context.INPUT_METHOD_SERVICE);
719 WindowManager.LayoutParams lp = getWindow().getAttributes();
720 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
721 android.os.Handler()) {
722 protected void onReceiveResult(int resultCode, Bundle resultData) {
723 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
724 }
725 });
726 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
727 }
728 }
729 */
730
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 private boolean acceptFilter() {
732 final InputMethodManager inputManager = (InputMethodManager)
733 getSystemService(Context.INPUT_METHOD_SERVICE);
734 return !inputManager.isFullscreenMode();
735 }
736
737 @Override
738 public boolean onKeyDown(int keyCode, KeyEvent event) {
739 boolean handled = super.onKeyDown(keyCode, event);
740 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
741 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
742 keyCode, event);
743 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700744 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700745 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700746 // showSearchDialog()
747 // If there are multiple keystrokes before the search dialog takes focus,
748 // onSearchRequested() will be called for every keystroke,
749 // but it is idempotent, so it's fine.
750 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 }
752 }
753
Joe Onorato8a9625e2010-01-28 15:55:35 -0800754 // Eat the long press event so the keyboard doesn't come up.
755 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
756 return true;
757 }
758
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 return handled;
760 }
761
Karl Rosaen138a0412009-04-23 19:00:21 -0700762 private String getTypedText() {
763 return mDefaultKeySsb.toString();
764 }
765
766 private void clearTypedText() {
767 mDefaultKeySsb.clear();
768 mDefaultKeySsb.clearSpans();
769 Selection.setSelection(mDefaultKeySsb, 0);
770 }
771
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 /**
773 * Restores the previous state, if it exists.
774 *
775 * @param savedState The previous state.
776 */
777 private void restoreState(Bundle savedState) {
778 if (savedState == null) {
779 return;
780 }
781
Joe Onorato3a8820b2009-11-10 15:06:42 -0800782 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
783 if (allApps) {
784 showAllApps(false);
785 }
786
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
788 if (currentScreen > -1) {
789 mWorkspace.setCurrentScreen(currentScreen);
790 }
791
792 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
793 if (addScreen > -1) {
794 mAddItemCellInfo = new CellLayout.CellInfo();
795 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
796 addItemCellInfo.valid = true;
797 addItemCellInfo.screen = addScreen;
798 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
799 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
800 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
801 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Michael Jurkac28de512010-08-13 11:27:44 -0700802 addItemCellInfo.updateOccupiedCells(
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
804 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
805 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
806 mRestoring = true;
807 }
808
809 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
810 if (renameFolder) {
811 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400812 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 mRestoring = true;
814 }
815 }
816
817 /**
818 * Finds all the views we need and configure them properly.
819 */
820 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400821 DragController dragController = mDragController;
822
Romain Guyb1b69f52009-08-10 15:10:15 -0700823 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400824 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825
Joe Onorato7c312c12009-08-13 21:36:53 -0700826 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700827 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700828 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800829 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700830 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700831 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700832
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700833 if (LauncherApplication.isScreenXLarge()) {
834 // They need to be INVISIBLE initially so that they will be measured in the layout.
835 // Otherwise the animations are messed up when we show them for the first time.
836 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
837 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
838 }
839
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
841 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500842 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700843
Joe Onorato7c312c12009-08-13 21:36:53 -0700844 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
845 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700847 View handleView = findViewById(R.id.all_apps_button);
848 if (handleView != null && handleView instanceof HandleView) {
849 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700850 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700851 mHandleView.setLauncher(this);
852 mHandleView.setOnClickListener(this);
853 mHandleView.setOnLongClickListener(this);
854 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800855
Winson Chung80baf5a2010-08-09 16:03:15 -0700856 if (mCustomizePagedView != null) {
857 mCustomizePagedView.setLauncher(this);
858 mCustomizePagedView.setDragController(dragController);
859 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700860 } else {
861 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
862 hotseatLeft.setContentDescription(mHotseatLabels[0]);
863 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
864 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
865 hotseatRight.setContentDescription(mHotseatLabels[1]);
866 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400867
Michael Jurkaaf442092010-06-10 17:01:57 -0700868 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
869 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800870
Michael Jurkaaf442092010-06-10 17:01:57 -0700871 Drawable previous = mPreviousView.getDrawable();
872 Drawable next = mNextView.getDrawable();
873 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874
Michael Jurkaaf442092010-06-10 17:01:57 -0700875 mPreviousView.setHapticFeedbackEnabled(false);
876 mPreviousView.setOnLongClickListener(this);
877 mNextView.setHapticFeedbackEnabled(false);
878 mNextView.setOnLongClickListener(this);
879 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800880
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400882 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884
885 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400886 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700887 int deleteZoneHandleId;
888 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700889 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700890 } else {
891 deleteZoneHandleId = R.id.all_apps_button_cluster;
892 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700893 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700894 dragController.addDragListener(deleteZone);
895
896 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
897 if (infoButton != null) {
898 infoButton.setLauncher(this);
899 infoButton.setHandle(findViewById(R.id.configure_button));
900 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
901 dragController.addDragListener(infoButton);
902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903
Joe Onorato00acb122009-08-04 16:04:30 -0400904 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400905 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800906 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700907
Joe Onorato00acb122009-08-04 16:04:30 -0400908 // The order here is bottom to top.
909 dragController.addDropTarget(workspace);
910 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700911 if (infoButton != null) {
912 dragController.addDropTarget(infoButton);
913 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 }
915
Romain Guy8a73c512009-11-09 19:19:59 -0800916 @SuppressWarnings({"UnusedDeclaration"})
917 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800918 if (!isAllAppsVisible()) {
919 mWorkspace.scrollLeft();
920 }
Romain Guy8a73c512009-11-09 19:19:59 -0800921 }
922
923 @SuppressWarnings({"UnusedDeclaration"})
924 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800925 if (!isAllAppsVisible()) {
926 mWorkspace.scrollRight();
927 }
Romain Guy8a73c512009-11-09 19:19:59 -0800928 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400929
930 @SuppressWarnings({"UnusedDeclaration"})
931 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400932 if (isAllAppsVisible()) return;
933
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400934 int index = -1;
935 if (v.getId() == R.id.hotseat_left) {
936 index = 0;
937 } else if (v.getId() == R.id.hotseat_right) {
938 index = 1;
939 }
940
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400941 // reload these every tap; you never know when they might change
942 loadHotseats();
943 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
944 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400945 startActivitySafely(
946 mHotseats[index],
947 "hotseat"
948 );
949 }
950 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700951
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 /**
953 * Creates a view representing a shortcut.
954 *
955 * @param info The data structure describing the shortcut.
956 *
957 * @return A View inflated from R.layout.application.
958 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800959 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 return createShortcut(R.layout.application,
961 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
962 }
963
964 /**
965 * Creates a view representing a shortcut inflated from the specified resource.
966 *
967 * @param layoutResId The id of the XML layout used to create the shortcut.
968 * @param parent The group the shortcut belongs to.
969 * @param info The data structure describing the shortcut.
970 *
971 * @return A View inflated from layoutResId.
972 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800973 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
975
Joe Onorato0589f0f2010-02-08 13:44:00 -0800976 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
977 new FastBitmapDrawable(info.getIcon(mIconCache)),
978 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 favorite.setText(info.title);
980 favorite.setTag(info);
981 favorite.setOnClickListener(this);
982
983 return favorite;
984 }
985
986 /**
987 * Add an application shortcut to the workspace.
988 *
989 * @param data The intent describing the application.
990 * @param cellInfo The position on screen where to create the shortcut.
991 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 cellInfo.screen = mWorkspace.getCurrentScreen();
994 if (!findSingleSlot(cellInfo)) return;
995
Joe Onorato0589f0f2010-02-08 13:44:00 -0800996 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
997 data, context);
998
Romain Guy73b979d2009-06-09 12:57:21 -0700999 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001000 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001002 info.container = ItemInfo.NO_ID;
1003 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
1004 } else {
1005 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 }
1007 }
Romain Guycbb89e42009-06-08 15:52:54 -07001008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 /**
1010 * Add a shortcut to the workspace.
1011 *
1012 * @param data The intent describing the shortcut.
1013 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001015 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 cellInfo.screen = mWorkspace.getCurrentScreen();
1017 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001018
Joe Onorato0589f0f2010-02-08 13:44:00 -08001019 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020
1021 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1024 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 }
1026 }
1027
Romain Guycbb89e42009-06-08 15:52:54 -07001028
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001030 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001032 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001033 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 */
Michael Jurkaaf442092010-06-10 17:01:57 -07001035 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001036 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001037
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001038 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001040 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001041
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 // Try finding open space on Launcher screen
1043 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -08001044 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
1045 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1046 return;
1047 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001049 // Build Launcher-specific widget info and save to database
1050 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 launcherInfo.spanX = spans[0];
1052 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 LauncherModel.addItemToDatabase(this, launcherInfo,
1055 LauncherSettings.Favorites.CONTAINER_DESKTOP,
1056 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
1057
1058 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001059 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001060
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001062 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001063
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001064 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001066
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001068 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 }
1070 }
Romain Guycbb89e42009-06-08 15:52:54 -07001071
Joe Onorato9c1289c2009-08-17 11:03:03 -04001072 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1073 mDesktopItems.remove(launcherInfo);
1074 launcherInfo.hostView = null;
1075 }
1076
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001077 public LauncherAppWidgetHost getAppWidgetHost() {
1078 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 }
Romain Guycbb89e42009-06-08 15:52:54 -07001080
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001081 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001082 getWindow().closeAllPanels();
1083
1084 try {
1085 dismissDialog(DIALOG_CREATE_SHORTCUT);
1086 // Unlock the workspace if the dialog was showing
1087 } catch (Exception e) {
1088 // An exception is thrown if the dialog is not visible, which is fine
1089 }
1090
1091 try {
1092 dismissDialog(DIALOG_RENAME_FOLDER);
1093 // Unlock the workspace if the dialog was showing
1094 } catch (Exception e) {
1095 // An exception is thrown if the dialog is not visible, which is fine
1096 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001097
1098 // Whatever we were doing is hereby canceled.
1099 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001100 }
1101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 @Override
1103 protected void onNewIntent(Intent intent) {
1104 super.onNewIntent(intent);
1105
1106 // Close the menu
1107 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001108 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001109 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001110
Joe Onorato14f122b2009-11-19 14:06:36 -08001111 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1112 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1113 boolean allAppsVisible = isAllAppsVisible();
Michael Jurka01f0ed42010-08-20 00:41:17 -07001114 boolean customizationDrawerVisible = isCustomizationDrawerVisible();
1115
Patrick Dubroy558654c2010-07-23 16:48:11 -07001116
Michael Jurka4cb37242010-08-09 21:05:32 -07001117 // in all these cases, only animate if we're already on home
1118 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka01f0ed42010-08-20 00:41:17 -07001119 if (alreadyOnHome && !mWorkspace.isSmall() &&
1120 !allAppsVisible && !customizationDrawerVisible) {
1121 mWorkspace.shrinkToMiddle();
1122 } else {
1123 mWorkspace.unshrink(alreadyOnHome);
1124 }
1125 } else if (!mWorkspace.isDefaultScreenShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001126 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurka01f0ed42010-08-20 00:41:17 -07001127 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001128 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001129 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001130 hideCustomizationDrawer();
Romain Guy1dd3a072009-07-16 13:21:01 -07001131
Joe Onorato3a8820b2009-11-10 15:06:42 -08001132 final View v = getWindow().peekDecorView();
1133 if (v != null && v.getWindowToken() != null) {
1134 InputMethodManager imm = (InputMethodManager)getSystemService(
1135 INPUT_METHOD_SERVICE);
1136 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 }
1138 }
1139 }
1140
1141 @Override
1142 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1143 // Do not call super here
1144 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001145
1146 if (mHomeCustomizationDrawer != null) {
1147 String cur = savedInstanceState.getString("currentTab");
1148 if (cur != null) {
1149 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1150 }
1151 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 }
1153
1154 @Override
1155 protected void onSaveInstanceState(Bundle outState) {
1156 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1157
1158 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1159 if (folders.size() > 0) {
1160 final int count = folders.size();
1161 long[] ids = new long[count];
1162 for (int i = 0; i < count; i++) {
1163 final FolderInfo info = folders.get(i).getInfo();
1164 ids[i] = info.id;
1165 }
1166 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1167 } else {
1168 super.onSaveInstanceState(outState);
1169 }
1170
Joe Onoratofb0ca672009-09-14 17:55:46 -04001171 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001172 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001174 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175
1176 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1177 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1178 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1179
1180 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1181 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1182 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1183 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1184 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1185 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1186 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1187 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001188 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 }
1190
1191 if (mFolderInfo != null && mWaitingForResult) {
1192 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1193 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1194 }
Michael Jurka946ad472010-07-09 18:05:18 -07001195
1196 if (mHomeCustomizationDrawer != null) {
1197 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1198 if (currentTabTag != null) {
1199 outState.putString("currentTab", currentTabTag);
1200 }
1201 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 }
1203
1204 @Override
1205 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001209 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001211 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 }
1213
1214 TextKeyListener.getInstance().release();
1215
Joe Onorato9c1289c2009-08-17 11:03:03 -04001216 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217
Joe Onorato9c1289c2009-08-17 11:03:03 -04001218 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001219
1220 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001221
Patrick Dubroyab962b72010-07-26 12:10:10 -07001222 // Some launcher layouts don't have a previous and next view
1223 if (mPreviousView != null) {
1224 dismissPreview(mPreviousView);
1225 }
1226 if (mNextView != null) {
1227 dismissPreview(mNextView);
1228 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001229
1230 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 }
1232
1233 @Override
1234 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001235 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 super.startActivityForResult(intent, requestCode);
1237 }
1238
1239 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001240 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001242
Joe Onorato7bb17492009-09-24 17:51:01 -07001243 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001244
Karl Rosaen138a0412009-04-23 19:00:21 -07001245 if (initialQuery == null) {
1246 // Use any text typed in the launcher as the initial query
1247 initialQuery = getTypedText();
1248 clearTypedText();
1249 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 if (appSearchData == null) {
1251 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001252 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 }
Romain Guycbb89e42009-06-08 15:52:54 -07001254
Karl Rosaen138a0412009-04-23 19:00:21 -07001255 final SearchManager searchManager =
1256 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001257 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001258 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 }
1260
1261 @Override
1262 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001263 if (isWorkspaceLocked()) {
1264 return false;
1265 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266
1267 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001268
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1270 .setIcon(android.R.drawable.ic_menu_add)
1271 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001272 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 .setIcon(android.R.drawable.ic_menu_gallery)
1274 .setAlphabeticShortcut('W');
1275 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1276 .setIcon(android.R.drawable.ic_search_category_default)
1277 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1278 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1279 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1280 .setAlphabeticShortcut('N');
1281
1282 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001283 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1284 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285
1286 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1287 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1288 .setIntent(settings);
1289
1290 return true;
1291 }
1292
1293 @Override
1294 public boolean onPrepareOptionsMenu(Menu menu) {
1295 super.onPrepareOptionsMenu(menu);
1296
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001297 // If all apps is animating, don't show the menu, because we don't know
1298 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001299 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001300 return false;
1301 }
1302
1303 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001304 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001305 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1306 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1307
1308 // Disable add if the workspace is full.
1309 if (visible) {
Michael Jurkac28de512010-08-13 11:27:44 -07001310 mMenuAddInfo = mWorkspace.updateOccupiedCells(null);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001311 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1312 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313
1314 return true;
1315 }
1316
Michael Jurka0e260592010-06-30 17:07:39 -07001317 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1318 // using the settings menu to add an item, something similar happens in showAddDialog
1319 public void prepareAddItemFromHomeCustomizationDrawer() {
Michael Jurkac28de512010-08-13 11:27:44 -07001320 mMenuAddInfo = mWorkspace.updateOccupiedCells(null);
Michael Jurka0e260592010-06-30 17:07:39 -07001321 mAddItemCellInfo = mMenuAddInfo;
1322 }
1323
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 @Override
1325 public boolean onOptionsItemSelected(MenuItem item) {
1326 switch (item.getItemId()) {
1327 case MENU_ADD:
1328 addItems();
1329 return true;
1330 case MENU_WALLPAPER_SETTINGS:
1331 startWallpaper();
1332 return true;
1333 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001334 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 return true;
1336 case MENU_NOTIFICATIONS:
1337 showNotifications();
1338 return true;
1339 }
1340
1341 return super.onOptionsItemSelected(item);
1342 }
Romain Guycbb89e42009-06-08 15:52:54 -07001343
Karl Rosaen138a0412009-04-23 19:00:21 -07001344 /**
1345 * Indicates that we want global search for this activity by setting the globalSearch
1346 * argument for {@link #startSearch} to true.
1347 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001349 @Override
1350 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001351 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001352 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001353 }
1354
Joe Onorato9c1289c2009-08-17 11:03:03 -04001355 public boolean isWorkspaceLocked() {
1356 return mWorkspaceLoading || mWaitingForResult;
1357 }
1358
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001360 if (LauncherApplication.isScreenXLarge()) {
1361 // Animate the widget chooser up from the bottom of the screen
1362 if (!isCustomizationDrawerVisible()) {
1363 showCustomizationDrawer();
Michael Jurka213d9632010-07-28 11:29:25 -07001364 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001365 }
1366 } else {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001367 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001368 showAddDialog(mMenuAddInfo);
1369 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 }
1371
Michael Jurkaaf442092010-06-10 17:01:57 -07001372 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo) {
1373 mAddItemCellInfo = cellInfo;
1374 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1375 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1376 addAppWidgetImpl(appWidgetId);
1377 }
1378
1379 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001380 // TODO: catch bad widget exception when sent
1381 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001382 // TODO: Is this log message meaningful?
1383 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1384 addAppWidgetImpl(appWidgetId);
1385 }
1386
1387 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001388 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389
Bjorn Bringert7984c942009-12-09 15:38:25 +00001390 if (appWidget.configure != null) {
1391 // Launch over to configure widget, if needed
1392 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1393 intent.setComponent(appWidget.configure);
1394 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1395
Romain Guy8e633c52010-03-04 12:51:36 -08001396 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001398 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001399 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 }
1401 }
Romain Guycbb89e42009-06-08 15:52:54 -07001402
Joe Onoratodeb98af2010-02-19 14:59:39 -08001403 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001404 // Handle case where user selected "Applications"
1405 String applicationName = getResources().getString(R.string.group_applications);
1406 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001407
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001408 if (applicationName != null && applicationName.equals(shortcutName)) {
1409 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1410 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001411
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001412 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1413 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001414 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001415 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001416 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001417 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
1419
1420 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001421 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001422 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001423 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001424
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001425 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001426 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001427 } else {
1428 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1429 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 }
1431
Joe Onorato9c1289c2009-08-17 11:03:03 -04001432 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 UserFolderInfo folderInfo = new UserFolderInfo();
1434 folderInfo.title = getText(R.string.folder_name);
1435
1436 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1437 cellInfo.screen = mWorkspace.getCurrentScreen();
1438 if (!findSingleSlot(cellInfo)) return;
1439
1440 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001441 LauncherModel.addItemToDatabase(this, folderInfo,
1442 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001444 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445
1446 // Create the view
1447 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1448 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1449 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001450 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 }
Romain Guycbb89e42009-06-08 15:52:54 -07001452
Joe Onorato9c1289c2009-08-17 11:03:03 -04001453 void removeFolder(FolderInfo folder) {
1454 mFolders.remove(folder.id);
1455 }
1456
1457 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 cellInfo.screen = mWorkspace.getCurrentScreen();
1459 if (!findSingleSlot(cellInfo)) return;
1460
1461 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1462
1463 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1465 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001466 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1467 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 }
1469 }
1470
1471 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1472 CellLayout.CellInfo cellInfo, boolean notify) {
1473
1474 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1475 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1476
1477 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 Intent.ShortcutIconResource iconResource = null;
1479
1480 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1481 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1482 try {
1483 iconResource = (Intent.ShortcutIconResource) extra;
1484 final PackageManager packageManager = context.getPackageManager();
1485 Resources resources = packageManager.getResourcesForApplication(
1486 iconResource.packageName);
1487 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1488 icon = resources.getDrawable(id);
1489 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001490 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 }
1492 }
1493
1494 if (icon == null) {
1495 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1496 }
1497
1498 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001499 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 info.title = name;
1501 info.iconResource = iconResource;
1502 info.uri = data.getData();
1503 info.baseIntent = baseIntent;
1504 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1505 LiveFolders.DISPLAY_MODE_GRID);
1506
1507 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1508 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001509 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510
1511 return info;
1512 }
1513
1514 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1515 final int[] xy = new int[2];
1516 if (findSlot(cellInfo, xy, 1, 1)) {
1517 cellInfo.cellX = xy[0];
1518 cellInfo.cellY = xy[1];
1519 return true;
1520 }
1521 return false;
1522 }
1523
1524 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1525 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1526 boolean[] occupied = mSavedState != null ?
1527 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
Michael Jurkac28de512010-08-13 11:27:44 -07001528 cellInfo = mWorkspace.updateOccupiedCells(occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1530 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1531 return false;
1532 }
1533 }
1534 return true;
1535 }
1536
1537 private void showNotifications() {
1538 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1539 if (statusBar != null) {
1540 statusBar.expand();
1541 }
1542 }
1543
1544 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001545 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001547 Intent chooser = Intent.createChooser(pickWallpaper,
1548 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001549 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1550 // Removed in Eclair MR1
1551// WallpaperManager wm = (WallpaperManager)
1552// getSystemService(Context.WALLPAPER_SERVICE);
1553// WallpaperInfo wi = wm.getWallpaperInfo();
1554// if (wi != null && wi.getSettingsActivity() != null) {
1555// LabeledIntent li = new LabeledIntent(getPackageName(),
1556// R.string.configure_wallpaper, 0);
1557// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1558// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1559// }
Mike Clerona0618e42009-10-22 13:55:21 -07001560 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 }
1562
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001563 /**
1564 * Registers various content observers. The current implementation registers
1565 * only a favorites observer to keep track of the favorites applications.
1566 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001567 private void registerContentObservers() {
1568 ContentResolver resolver = getContentResolver();
1569 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1570 true, mWidgetObserver);
1571 }
1572
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 @Override
1574 public boolean dispatchKeyEvent(KeyEvent event) {
1575 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1576 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001578 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001579 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001580 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001581 dumpState();
1582 return true;
1583 }
1584 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001585 }
1586 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1587 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001588 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 return true;
1590 }
1591 }
1592
1593 return super.dispatchKeyEvent(event);
1594 }
1595
Joe Onorato88ec0992009-11-19 13:16:06 -08001596 @Override
1597 public void onBackPressed() {
1598 if (isAllAppsVisible()) {
1599 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001600 } else if (isCustomizationDrawerVisible()) {
1601 hideCustomizationDrawer();
Joe Onorato88ec0992009-11-19 13:16:06 -08001602 } else {
1603 closeFolder();
1604 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001605 // Some launcher layouts don't have a previous and next view
1606 if (mPreviousView != null) {
1607 dismissPreview(mPreviousView);
1608 dismissPreview(mNextView);
1609 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001610 }
1611
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 private void closeFolder() {
1613 Folder folder = mWorkspace.getOpenFolder();
1614 if (folder != null) {
1615 closeFolder(folder);
1616 }
1617 }
1618
1619 void closeFolder(Folder folder) {
1620 folder.getInfo().opened = false;
1621 ViewGroup parent = (ViewGroup) folder.getParent();
1622 if (parent != null) {
1623 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001624 if (folder instanceof DropTarget) {
1625 // Live folders aren't DropTargets.
1626 mDragController.removeDropTarget((DropTarget)folder);
1627 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 }
1629 folder.onClose();
1630 }
1631
1632 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001633 * Re-listen when widgets are reset.
1634 */
1635 private void onAppWidgetReset() {
1636 mAppWidgetHost.startListening();
1637 }
1638
1639 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001640 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1641 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001643 private void unbindDesktopItems() {
1644 for (ItemInfo item: mDesktopItems) {
1645 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 }
1647 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001648
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 /**
1650 * Launches the intent referred by the clicked shortcut.
1651 *
1652 * @param v The view representing the clicked shortcut.
1653 */
1654 public void onClick(View v) {
1655 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001656 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001658 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001659 int[] pos = new int[2];
1660 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001661 intent.setSourceBounds(new Rect(pos[0], pos[1],
1662 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001663 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 } else if (tag instanceof FolderInfo) {
1665 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001666 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001667 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001668 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001669 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001670 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001671 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 }
1673 }
1674
Michael Jurka0e260592010-06-30 17:07:39 -07001675 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001676 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001677 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy558654c2010-07-23 16:48:11 -07001678 hideCustomizationDrawer();
Michael Jurka0e260592010-06-30 17:07:39 -07001679 return false;
1680 }
1681
Michael Jurkaaf442092010-06-10 17:01:57 -07001682 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001683 * Event handler for the search button
1684 *
1685 * @param v The view that was clicked.
1686 */
1687 public void onClickSearchButton(View v) {
1688 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1689 View button = findViewById(R.id.search_button);
1690 i.setSourceBounds(
1691 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1692 startActivity(i);
1693 }
1694
1695 /**
1696 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001697 * enters home screen customization mode.
1698 *
1699 * @param v The view that was clicked.
1700 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001701 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001702 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001703 }
1704
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001705 /**
1706 * Event handler for the "grid" button that appears on the home screen, which
1707 * enters all apps mode.
1708 *
1709 * @param v The view that was clicked.
1710 */
1711 public void onClickAllAppsButton(View v) {
1712 showAllApps(true);
1713 }
1714
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001715 void startApplicationDetailsActivity(String packageName) {
1716 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1717 Uri.fromParts("package", packageName, null));
1718 startActivity(intent);
1719 }
1720
Joe Onoratof984e852010-03-25 09:47:45 -07001721 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1723 try {
1724 startActivity(intent);
1725 } catch (ActivityNotFoundException e) {
1726 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001727 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728 } catch (SecurityException e) {
1729 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001730 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001732 "or use the exported attribute for this activity. "
1733 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 }
1735 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001736
Romain Guy8e633c52010-03-04 12:51:36 -08001737 void startActivityForResultSafely(Intent intent, int requestCode) {
1738 try {
1739 startActivityForResult(intent, requestCode);
1740 } catch (ActivityNotFoundException e) {
1741 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1742 } catch (SecurityException e) {
1743 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1744 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1745 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1746 "or use the exported attribute for this activity.", e);
1747 }
1748 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749
1750 private void handleFolderClick(FolderInfo folderInfo) {
1751 if (!folderInfo.opened) {
1752 // Close any open folder
1753 closeFolder();
1754 // Open the requested folder
1755 openFolder(folderInfo);
1756 } else {
1757 // Find the open folder...
1758 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1759 int folderScreen;
1760 if (openFolder != null) {
1761 folderScreen = mWorkspace.getScreenForView(openFolder);
1762 // .. and close it
1763 closeFolder(openFolder);
1764 if (folderScreen != mWorkspace.getCurrentScreen()) {
1765 // Close any folder open on the current screen
1766 closeFolder();
1767 // Pull the folder onto this screen
1768 openFolder(folderInfo);
1769 }
1770 }
1771 }
1772 }
1773
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 /**
1775 * Opens the user fodler described by the specified tag. The opening of the folder
1776 * is animated relative to the specified View. If the View is null, no animation
1777 * is played.
1778 *
1779 * @param folderInfo The FolderInfo describing the folder to open.
1780 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001781 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 Folder openFolder;
1783
1784 if (folderInfo instanceof UserFolderInfo) {
1785 openFolder = UserFolder.fromXml(this);
1786 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001787 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 } else {
1789 return;
1790 }
1791
Joe Onorato00acb122009-08-04 16:04:30 -04001792 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 openFolder.setLauncher(this);
1794
1795 openFolder.bind(folderInfo);
1796 folderInfo.opened = true;
1797
Winson Chungaafa03c2010-06-11 17:34:16 -07001798 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1799
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 openFolder.onOpen();
1801 }
1802
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001804 switch (v.getId()) {
1805 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001806 if (!isAllAppsVisible()) {
1807 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1808 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001809 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001810 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001811 return true;
1812 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001813 if (!isAllAppsVisible()) {
1814 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1815 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001816 showPreviews(v);
1817 }
1818 return true;
1819 case R.id.all_apps_button:
1820 if (!isAllAppsVisible()) {
1821 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1822 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1823 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001824 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001825 return true;
1826 }
1827
Joe Onorato9c1289c2009-08-17 11:03:03 -04001828 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 return false;
1830 }
1831
1832 if (!(v instanceof CellLayout)) {
1833 v = (View) v.getParent();
1834 }
1835
1836 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1837
1838 // This happens when long clicking an item with the dpad/trackball
1839 if (cellInfo == null) {
1840 return true;
1841 }
1842
1843 if (mWorkspace.allowLongPress()) {
1844 if (cellInfo.cell == null) {
1845 if (cellInfo.valid) {
1846 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001847 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001848 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1849 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 showAddDialog(cellInfo);
1851 }
1852 } else {
1853 if (!(cellInfo.cell instanceof Folder)) {
1854 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001855 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1856 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 mWorkspace.startDrag(cellInfo);
1858 }
1859 }
1860 }
1861 return true;
1862 }
1863
Romain Guye6b8e2f2009-11-10 11:56:55 -08001864 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001865 private void dismissPreview(final View v) {
1866 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001867 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001868 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1869 public void onDismiss() {
1870 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1871 int count = group.getChildCount();
1872 for (int i = 0; i < count; i++) {
1873 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1874 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001875 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1876 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1877
1878 v.setTag(R.id.workspace, null);
1879 v.setTag(R.id.icon, null);
1880 window.setOnDismissListener(null);
1881 }
1882 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001883 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001884 }
1885 v.setTag(null);
1886 }
1887
Romain Guyf8e6a802009-12-07 17:48:02 -08001888 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001889 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001890 }
1891
Romain Guya6abce82009-11-10 02:54:41 -08001892 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001893 final Resources resources = getResources();
1894 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001895
Romain Guya6abce82009-11-10 02:54:41 -08001896 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001897
Romain Guy9d31e9f2009-11-11 18:54:28 -08001898 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001899
Romain Guyf8e6a802009-12-07 17:48:02 -08001900 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001901 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001902 int extraW = (int) ((r.left + r.right) * max);
1903 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001904
1905 int aW = cell.getWidth() - extraW;
1906 float w = aW / max;
1907
1908 int width = cell.getWidth();
1909 int height = cell.getHeight();
1910 int x = cell.getLeftPadding();
1911 int y = cell.getTopPadding();
1912 width -= (x + cell.getRightPadding());
1913 height -= (y + cell.getBottomPadding());
1914
1915 float scale = w / width;
1916
1917 int count = end - start;
1918
1919 final float sWidth = width * scale;
1920 float sHeight = height * scale;
1921
Romain Guye6b8e2f2009-11-10 11:56:55 -08001922 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001923
Romain Guye6b8e2f2009-11-10 11:56:55 -08001924 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1925 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001926
1927 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001928 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001929 cell = (CellLayout) workspace.getChildAt(i);
1930
Romain Guyf8e6a802009-12-07 17:48:02 -08001931 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001932 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001933
1934 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001935 c.scale(scale, scale);
1936 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1937 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001938
Romain Guy9d31e9f2009-11-11 18:54:28 -08001939 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001940 image.setImageBitmap(bitmap);
1941 image.setTag(i);
1942 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001943 image.setOnFocusChangeListener(handler);
1944 image.setFocusable(true);
1945 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001946
1947 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001948 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1949
Winson Chungaafa03c2010-06-11 17:34:16 -07001950 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001951 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001952
1953 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001954 p.setContentView(preview);
1955 p.setWidth((int) (sWidth * count + extraW));
1956 p.setHeight((int) (sHeight + extraH));
1957 p.setAnimationStyle(R.style.AnimationPreview);
1958 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001959 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001960 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001961 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001962
Romain Guye6b8e2f2009-11-10 11:56:55 -08001963 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1964 public void onDismiss() {
1965 dismissPreview(anchor);
1966 }
1967 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001968
1969 anchor.setTag(p);
1970 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001971 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001972 }
1973
Romain Guy9d31e9f2009-11-11 18:54:28 -08001974 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001975 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001976
Romain Guye6b8e2f2009-11-10 11:56:55 -08001977 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001978 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001979 }
1980
1981 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001982 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001983 v.post(this);
1984 }
1985
1986 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001987 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001988 }
1989
1990 public void onFocusChange(View v, boolean hasFocus) {
1991 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001992 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001993 }
Romain Guya6abce82009-11-10 02:54:41 -08001994 }
1995 }
1996
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 Workspace getWorkspace() {
1998 return mWorkspace;
1999 }
2000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 @Override
2002 protected Dialog onCreateDialog(int id) {
2003 switch (id) {
2004 case DIALOG_CREATE_SHORTCUT:
2005 return new CreateShortcut().createDialog();
2006 case DIALOG_RENAME_FOLDER:
2007 return new RenameFolder().createDialog();
2008 }
2009
2010 return super.onCreateDialog(id);
2011 }
2012
2013 @Override
2014 protected void onPrepareDialog(int id, Dialog dialog) {
2015 switch (id) {
2016 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 break;
2018 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002019 if (mFolderInfo != null) {
2020 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2021 final CharSequence text = mFolderInfo.title;
2022 input.setText(text);
2023 input.setSelection(0, text.length());
2024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 break;
2026 }
2027 }
2028
2029 void showRenameDialog(FolderInfo info) {
2030 mFolderInfo = info;
2031 mWaitingForResult = true;
2032 showDialog(DIALOG_RENAME_FOLDER);
2033 }
2034
2035 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2036 mAddItemCellInfo = cellInfo;
2037 mWaitingForResult = true;
2038 showDialog(DIALOG_CREATE_SHORTCUT);
2039 }
2040
Joe Onoratodeb98af2010-02-19 14:59:39 -08002041 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002042 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002043 Bundle bundle = new Bundle();
2044
2045 ArrayList<String> shortcutNames = new ArrayList<String>();
2046 shortcutNames.add(getString(R.string.group_applications));
2047 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2048
2049 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2050 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2051 R.drawable.ic_launcher_application));
2052 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2053
2054 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2055 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002056 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002057 pickIntent.putExtras(bundle);
2058
Joe Onoratodeb98af2010-02-19 14:59:39 -08002059 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002060 }
2061
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 private class RenameFolder {
2063 private EditText mInput;
2064
2065 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2067 mInput = (EditText) layout.findViewById(R.id.folder_name);
2068
2069 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2070 builder.setIcon(0);
2071 builder.setTitle(getString(R.string.rename_folder_title));
2072 builder.setCancelable(true);
2073 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2074 public void onCancel(DialogInterface dialog) {
2075 cleanup();
2076 }
2077 });
2078 builder.setNegativeButton(getString(R.string.cancel_action),
2079 new Dialog.OnClickListener() {
2080 public void onClick(DialogInterface dialog, int which) {
2081 cleanup();
2082 }
2083 }
2084 );
2085 builder.setPositiveButton(getString(R.string.rename_action),
2086 new Dialog.OnClickListener() {
2087 public void onClick(DialogInterface dialog, int which) {
2088 changeFolderName();
2089 }
2090 }
2091 );
2092 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002093
2094 final AlertDialog dialog = builder.create();
2095 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2096 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002097 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002098 mInput.requestFocus();
2099 InputMethodManager inputManager = (InputMethodManager)
2100 getSystemService(Context.INPUT_METHOD_SERVICE);
2101 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002102 }
2103 });
2104
2105 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 }
2107
2108 private void changeFolderName() {
2109 final String name = mInput.getText().toString();
2110 if (!TextUtils.isEmpty(name)) {
2111 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04002112 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 mFolderInfo.title = name;
2114 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2115
Joe Onorato9c1289c2009-08-17 11:03:03 -04002116 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002117 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002118 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 } else {
2120 final FolderIcon folderIcon = (FolderIcon)
2121 mWorkspace.getViewForTag(mFolderInfo);
2122 if (folderIcon != null) {
2123 folderIcon.setText(name);
2124 getWorkspace().requestLayout();
2125 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002126 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002127 mWorkspaceLoading = true;
2128 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 }
2130 }
2131 }
2132 cleanup();
2133 }
2134
2135 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 dismissDialog(DIALOG_RENAME_FOLDER);
2137 mWaitingForResult = false;
2138 mFolderInfo = null;
2139 }
2140 }
2141
Daniel Sandler843e8602010-06-07 14:59:01 -04002142 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2143 public boolean isAllAppsVisible() {
Romain Guy5bbc91b2010-08-18 11:38:46 -07002144 return mAllAppsGrid != null && mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04002145 }
2146
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002147 // AllAppsView.Watcher
2148 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002149 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2150 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002151 mWorkspace.setVisibility(View.GONE);
2152 }
2153 }
2154
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002155 /**
2156 * Zoom the camera out from the workspace to reveal 'toView'.
2157 * Assumes that the view to show is anchored at either the very top or very bottom
2158 * of the screen.
2159 * @param toView The view to show when the animation is complete
2160 * @param above If true, toView will appear from the top of the screen
2161 */
2162 private void cameraZoomOut(final View toView, boolean above) {
2163 final Resources res = getResources();
2164 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2165 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2166 final int height = toView.getHeight();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002167
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002168 // toView should appear right at the end of the workspace shrink animation
2169 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2170
2171 Interpolator interp = new DecelerateInterpolator();
2172
2173 toView.setPivotX(toView.getWidth() / 2.0f);
2174 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2175 // Assumes that the view is normally anchored to either the top or bottom of the screen
2176 final int margin = 200;
2177 if (above) {
2178 toView.setPivotY(height + ((toView.getTop() + height) / scale) + margin);
2179 } else {
2180 toView.setPivotY(0.0f - (toView.getTop() / scale) - margin);
2181 }
2182
2183 Animator scaleXAnim = new PropertyAnimator(duration, toView, "scaleX", scale, 1.0f);
2184 scaleXAnim.setInterpolator(interp);
2185 scaleXAnim.addListener(new AnimatableListenerAdapter() {
2186 public void onAnimationStart(Animatable animation) {
2187 // Prepare the position
2188 toView.setTranslationX(0.0f);
2189 toView.setTranslationY(0.0f);
2190 toView.setVisibility(View.VISIBLE);
2191 }
2192 });
2193
2194 Animator scaleYAnim = new PropertyAnimator(duration, toView, "scaleY", scale, 1.0f);
2195 scaleYAnim.setInterpolator(interp);
2196
2197 Sequencer s = new Sequencer();
2198 s.playTogether(scaleXAnim, scaleYAnim);
2199 s.play(scaleXAnim).after(startDelay);
2200 s.start();
2201 }
2202
2203 /**
2204 * Zoom the camera back into the workspace, hiding 'fromView'.
2205 * This is the opposite of cameraZoomOut.
2206 * @param fromView The currently-focused view, which will be hidden.
2207 */
2208 private void cameraZoomIn(final View fromView) {
2209 Resources res = getResources();
2210 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2211 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2212
2213 Interpolator interp = new AccelerateInterpolator();
2214
2215 Sequencer s = new Sequencer();
2216 Animator scaleXAnim = new PropertyAnimator(duration, fromView, "scaleX", scaleFactor);
2217 scaleXAnim.setInterpolator(interp);
2218 Animator scaleYAnim = new PropertyAnimator(duration, fromView, "scaleY", scaleFactor);
2219 scaleYAnim.setInterpolator(interp);
2220 s.playTogether(scaleXAnim, scaleYAnim);
2221 s.addListener(new AnimatableListenerAdapter() {
2222 public void onAnimationEnd(Animatable animation) {
2223 fromView.setVisibility(View.GONE);
2224 fromView.setScaleX(1.0f);
2225 fromView.setScaleY(1.0f);
2226 }
2227 });
2228 s.start();
2229 }
2230
2231 /**
2232 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2233 * This is the transition used on xlarge screens to go between all apps and
2234 * the home customization drawer.
2235 * @param fromView The view to pan away from.
2236 * @param toView The view to pan into the frame.
2237 */
2238 private void cameraPan(final View fromView, final View toView) {
2239 final int duration = getResources().getInteger(R.integer.config_allAppsCameraPanTime);
2240 final int workspaceHeight = mWorkspace.getHeight();
2241
2242 final boolean panDown = fromView.getY() < toView.getY();
2243
2244 final float fromViewStartY = panDown ? 0.0f : fromView.getY();
2245 final float fromViewEndY = panDown ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2246 final float toViewStartY = panDown ? workspaceHeight * 2 : -toView.getHeight() * 2;
2247 final float toViewEndY = panDown ? workspaceHeight - toView.getHeight() : 0.0f;
2248
2249 Sequencer s = new Sequencer();
2250 s.playTogether(
2251 new PropertyAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2252 new PropertyAnimator(duration, toView, "y", toViewStartY, toViewEndY));
2253 s.addListener(new AnimatableListenerAdapter() {
2254 public void onAnimationStart(Animatable animation) {
2255 toView.setVisibility(View.VISIBLE);
2256 toView.setY(toViewStartY);
2257 }
2258 public void onAnimationEnd(Animatable animation) {
2259 fromView.setVisibility(View.GONE);
2260 }
2261 });
2262 s.start();
2263 }
2264
2265 void showAllApps(boolean animated) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002266 if (mAllAppsGrid.isVisible())
2267 return;
2268
Michael Jurka79212d82010-07-30 16:36:20 -07002269 if (LauncherApplication.isScreenXLarge()) {
2270 mWorkspace.shrinkToBottom(animated);
2271 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002272
Patrick Dubroy558654c2010-07-23 16:48:11 -07002273 if (LauncherApplication.isScreenXLarge() && animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002274 if (isCustomizationDrawerVisible()) {
2275 cameraPan(mHomeCustomizationDrawer, (View) mAllAppsGrid);
2276 } else {
2277 cameraZoomOut((View) mAllAppsGrid, true);
2278 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002279 } else {
2280 mAllAppsGrid.zoom(1.0f, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002281 hideCustomizationDrawer(); // TODO: Should be able to do this un-animated
Patrick Dubroy558654c2010-07-23 16:48:11 -07002282 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002283
Romain Guyc16fea72010-03-12 17:17:56 -08002284 ((View) mAllAppsGrid).setFocusable(true);
2285 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002286
Joe Onorato7c312c12009-08-13 21:36:53 -07002287 // TODO: fade these two too
2288 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002289 }
2290
Joe Onoratob2061212009-11-24 16:13:54 -05002291 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002292 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002293 * - Home from workspace
2294 * - from center screen
2295 * - from other screens
2296 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002297 * - from center screen
2298 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002299 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002300 * - from center screen
2301 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002302 * - Launch app from workspace and quit
2303 * - with back
2304 * - with home
2305 * - Launch app from all apps and quit
2306 * - with back
2307 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002308 * - Go to a screen that's not the default, then all
2309 * apps, and launch and app, and go back
2310 * - with back
2311 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002312 * - On workspace, long press power and go back
2313 * - with back
2314 * - with home
2315 * - On all apps, long press power and go back
2316 * - with back
2317 * - with home
2318 * - On workspace, power off
2319 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002320 * - Launch an app and turn off the screen while in that app
2321 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002322 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002323 * - From all apps
2324 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002325 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2326 * - From all apps
2327 * - From the center workspace
2328 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002329 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002330 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002331 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002332 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002333 if (LauncherApplication.isScreenXLarge() && animated) {
Michael Jurka213d9632010-07-28 11:29:25 -07002334 mWorkspace.unshrink();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002335 cameraZoomIn((View) mAllAppsGrid);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002336 } else {
2337 mAllAppsGrid.zoom(0.0f, animated);
2338 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002339 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002340 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002341 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002342 }
2343
Joe Onorato7c312c12009-08-13 21:36:53 -07002344 void lockAllApps() {
2345 // TODO
2346 }
2347
2348 void unlockAllApps() {
2349 // TODO
2350 }
2351
Patrick Dubroy558654c2010-07-23 16:48:11 -07002352 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002353 return mHomeCustomizationDrawer != null &&
2354 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002355 }
2356
2357 private void showCustomizationDrawer() {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002358 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002359 if (isAllAppsVisible()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002360 cameraPan((View) mAllAppsGrid, mHomeCustomizationDrawer);
2361 } else {
2362 cameraZoomOut(mHomeCustomizationDrawer, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002363 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002364 }
2365
Michael Jurka4bb4f732010-08-04 18:46:01 -07002366 void hideCustomizationDrawer() {
2367 hideCustomizationDrawer(true);
2368 }
2369
2370 void hideCustomizationDrawer(boolean unshrinkWorkspace) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002371 if (isCustomizationDrawerVisible()) {
Michael Jurka4bb4f732010-08-04 18:46:01 -07002372 if (unshrinkWorkspace) {
2373 mWorkspace.unshrink();
2374 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002375 cameraZoomIn(mHomeCustomizationDrawer);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002376 }
2377 }
2378
Michael Jurka213d9632010-07-28 11:29:25 -07002379 void onWorkspaceUnshrink() {
2380 if (isAllAppsVisible()) {
Michael Jurka54dd7542010-07-30 14:47:52 -07002381 closeAllApps(true);
Michael Jurka213d9632010-07-28 11:29:25 -07002382 }
2383 if (isCustomizationDrawerVisible()) {
2384 hideCustomizationDrawer();
2385 }
2386 }
2387
Joe Onorato7404ee42009-07-31 11:54:44 -07002388 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002389 * Displays the shortcut creation dialog and launches, if necessary, the
2390 * appropriate activity.
2391 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002392 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002393 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2394 DialogInterface.OnShowListener {
2395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002397
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002399 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002400
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2402 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002403 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002404
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 builder.setInverseBackgroundForced(true);
2406
2407 AlertDialog dialog = builder.create();
2408 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002409 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002410 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 return dialog;
2413 }
2414
2415 public void onCancel(DialogInterface dialog) {
2416 mWaitingForResult = false;
2417 cleanup();
2418 }
2419
Romain Guycbb89e42009-06-08 15:52:54 -07002420 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002421 }
2422
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002424 try {
2425 dismissDialog(DIALOG_CREATE_SHORTCUT);
2426 } catch (Exception e) {
2427 // An exception is thrown if the dialog is not visible, which is fine
2428 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 }
2430
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002431 /**
2432 * Handle the action clicked in the "Add to home" dialog.
2433 */
2434 public void onClick(DialogInterface dialog, int which) {
2435 Resources res = getResources();
2436 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002437
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002438 switch (which) {
2439 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002440 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002441 break;
2442 }
Romain Guycbb89e42009-06-08 15:52:54 -07002443
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002444 case AddAdapter.ITEM_APPWIDGET: {
2445 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002446
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002447 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2448 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002449 // start the pick activity
2450 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2451 break;
2452 }
Romain Guycbb89e42009-06-08 15:52:54 -07002453
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002454 case AddAdapter.ITEM_LIVE_FOLDER: {
2455 // Insert extra item to handle inserting folder
2456 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002457
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002458 ArrayList<String> shortcutNames = new ArrayList<String>();
2459 shortcutNames.add(res.getString(R.string.group_folder));
2460 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002461
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002462 ArrayList<ShortcutIconResource> shortcutIcons =
2463 new ArrayList<ShortcutIconResource>();
2464 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2465 R.drawable.ic_launcher_folder));
2466 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2467
2468 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2469 pickIntent.putExtra(Intent.EXTRA_INTENT,
2470 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2471 pickIntent.putExtra(Intent.EXTRA_TITLE,
2472 getText(R.string.title_select_live_folder));
2473 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002474
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002475 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2476 break;
2477 }
2478
2479 case AddAdapter.ITEM_WALLPAPER: {
2480 startWallpaper();
2481 break;
2482 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002483 }
2484 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002485
2486 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002487 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002488 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002489 }
2490
2491 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002492 * Receives notifications when applications are added/removed.
2493 */
2494 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2495 @Override
2496 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002497 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002498 String reason = intent.getStringExtra("reason");
2499 if (!"homekey".equals(reason)) {
2500 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002501 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002502 animate = false;
2503 }
Joe Onoratob2061212009-11-24 16:13:54 -05002504 closeAllApps(animate);
Michael Jurka4cb37242010-08-09 21:05:32 -07002505 mWorkspace.unshrink(animate);
Michael Jurka909f9152010-08-19 23:08:00 -07002506 hideCustomizationDrawer();
Joe Onoratob2061212009-11-24 16:13:54 -05002507 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002508 }
2509 }
2510
2511 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002512 * Receives notifications whenever the appwidgets are reset.
2513 */
2514 private class AppWidgetResetObserver extends ContentObserver {
2515 public AppWidgetResetObserver() {
2516 super(new Handler());
2517 }
2518
2519 @Override
2520 public void onChange(boolean selfChange) {
2521 onAppWidgetReset();
2522 }
2523 }
2524
2525 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002526 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002527 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002528 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002529 if (mWorkspace != null) {
2530 return mWorkspace.getCurrentScreen();
2531 } else {
2532 return SCREEN_COUNT / 2;
2533 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002534 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002535
Joe Onorato9c1289c2009-08-17 11:03:03 -04002536 /**
2537 * Refreshes the shortcuts shown on the workspace.
2538 *
2539 * Implementation of the method from LauncherModel.Callbacks.
2540 */
2541 public void startBinding() {
2542 final Workspace workspace = mWorkspace;
2543 int count = workspace.getChildCount();
2544 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002545 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002546 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2547 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002548
Joe Onorato9c1289c2009-08-17 11:03:03 -04002549 if (DEBUG_USER_INTERFACE) {
2550 android.widget.Button finishButton = new android.widget.Button(this);
2551 finishButton.setText("Finish");
2552 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2553
2554 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2555 public void onClick(View v) {
2556 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002557 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002558 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002559 }
2560 }
2561
2562 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002563 * Bind the items start-end from the list.
2564 *
2565 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002566 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002567 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2568
2569 final Workspace workspace = mWorkspace;
2570
2571 for (int i=start; i<end; i++) {
2572 final ItemInfo item = shortcuts.get(i);
2573 mDesktopItems.add(item);
2574 switch (item.itemType) {
2575 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2576 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002577 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002578 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2579 false);
2580 break;
2581 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2582 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2583 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2584 (UserFolderInfo) item);
2585 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2586 false);
2587 break;
2588 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2589 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2590 R.layout.live_folder_icon, this,
2591 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2592 (LiveFolderInfo) item);
2593 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2594 false);
2595 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002596 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002597 }
2598
Joe Onorato9c1289c2009-08-17 11:03:03 -04002599 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002600 }
2601
Joe Onorato9c1289c2009-08-17 11:03:03 -04002602 /**
2603 * Implementation of the method from LauncherModel.Callbacks.
2604 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002605 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2606 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002607 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002608 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002609
2610 /**
2611 * Add the views for a widget to the workspace.
2612 *
2613 * Implementation of the method from LauncherModel.Callbacks.
2614 */
2615 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002616 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2617 if (DEBUG_WIDGETS) {
2618 Log.d(TAG, "bindAppWidget: " + item);
2619 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002620 final Workspace workspace = mWorkspace;
2621
2622 final int appWidgetId = item.appWidgetId;
2623 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002624 if (DEBUG_WIDGETS) {
2625 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2626 }
2627
Joe Onorato9c1289c2009-08-17 11:03:03 -04002628 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2629
Joe Onorato9c1289c2009-08-17 11:03:03 -04002630 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2631 item.hostView.setTag(item);
2632
2633 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2634 item.cellY, item.spanX, item.spanY, false);
2635
2636 workspace.requestLayout();
2637
2638 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002639
2640 if (DEBUG_WIDGETS) {
2641 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2642 + (SystemClock.uptimeMillis()-start) + "ms");
2643 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002644 }
2645
2646 /**
2647 * Callback saying that there aren't any more items to bind.
2648 *
2649 * Implementation of the method from LauncherModel.Callbacks.
2650 */
2651 public void finishBindingItems() {
2652 if (mSavedState != null) {
2653 if (!mWorkspace.hasFocus()) {
2654 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2655 }
2656
2657 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2658 if (userFolders != null) {
2659 for (long folderId : userFolders) {
2660 final FolderInfo info = mFolders.get(folderId);
2661 if (info != null) {
2662 openFolder(info);
2663 }
2664 }
2665 final Folder openFolder = mWorkspace.getOpenFolder();
2666 if (openFolder != null) {
2667 openFolder.requestFocus();
2668 }
2669 }
2670
Joe Onorato9c1289c2009-08-17 11:03:03 -04002671 mSavedState = null;
2672 }
2673
2674 if (mSavedInstanceState != null) {
2675 super.onRestoreInstanceState(mSavedInstanceState);
2676 mSavedInstanceState = null;
2677 }
2678
Joe Onorato9c1289c2009-08-17 11:03:03 -04002679 mWorkspaceLoading = false;
2680 }
2681
2682 /**
2683 * Add the icons for all apps.
2684 *
2685 * Implementation of the method from LauncherModel.Callbacks.
2686 */
2687 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002688 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002689 }
2690
2691 /**
2692 * A package was installed.
2693 *
2694 * Implementation of the method from LauncherModel.Callbacks.
2695 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002696 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002697 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002698 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002699 }
2700
2701 /**
2702 * A package was updated.
2703 *
2704 * Implementation of the method from LauncherModel.Callbacks.
2705 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002706 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002707 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002708 mWorkspace.updateShortcuts(apps);
2709 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002710 }
2711
2712 /**
2713 * A package was uninstalled.
2714 *
2715 * Implementation of the method from LauncherModel.Callbacks.
2716 */
Joe Onorato36115782010-06-17 13:28:48 -04002717 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002718 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002719 if (permanent) {
2720 mWorkspace.removeItems(apps);
2721 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002722 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002723 }
Joe Onoratobe386092009-11-17 17:32:16 -08002724
2725 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002726 * A number of packages were updated.
2727 */
2728 public void bindPackagesUpdated() {
2729 // update the customization drawer contents
Winson Chungb3347bb2010-08-19 14:51:28 -07002730 if (mCustomizePagedView != null)
2731 mCustomizePagedView.update();
Winson Chung80baf5a2010-08-09 16:03:15 -07002732 }
2733
2734 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002735 * Prints out out state for debugging.
2736 */
2737 public void dumpState() {
2738 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002739 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002740 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2741 Log.d(TAG, "mRestoring=" + mRestoring);
2742 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2743 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2744 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2745 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002746 mModel.dumpState();
2747 mAllAppsGrid.dumpState();
2748 Log.d(TAG, "END launcher2 dump state");
2749 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002750}