blob: e5c11def76f5c2099de82a0b7c782502754f23d4 [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
Winson Chung400438b2011-01-16 17:53:48 -080020import java.io.DataInputStream;
21import java.io.DataOutputStream;
22import java.io.FileNotFoundException;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.List;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070027
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080029import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070031import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070032import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070033import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070034import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.app.Dialog;
37import android.app.SearchManager;
38import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070039import android.appwidget.AppWidgetManager;
40import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080042import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070043import android.content.ClipData;
44import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040045import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080046import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.Context;
48import android.content.DialogInterface;
49import android.content.Intent;
Adam Cohended9f8d2010-11-03 13:25:16 -070050import android.content.Intent.ShortcutIconResource;
Winson Chung400438b2011-01-16 17:53:48 -080051import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070052import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.content.pm.PackageManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070054import android.content.pm.PackageManager.NameNotFoundException;
Winson Chung400438b2011-01-16 17:53:48 -080055import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070057import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040058import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080059import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080061import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070062import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080063import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070064import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040065import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070066import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020068import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080069import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070070import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040072import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080073import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070074import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070075import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080076import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.text.Selection;
78import android.text.SpannableStringBuilder;
79import android.text.TextUtils;
80import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070081import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080082import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080083import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084import android.view.KeyEvent;
85import android.view.LayoutInflater;
86import android.view.Menu;
87import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070088import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080089import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.View;
Winson Chung400438b2011-01-16 17:53:48 -080091import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070093import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080094import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080095import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070097import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070099import android.widget.ImageView;
100import android.widget.LinearLayout;
101import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700102import android.widget.TabHost;
Winson Chung400438b2011-01-16 17:53:48 -0800103import android.widget.TabHost.OnTabChangeListener;
104import android.widget.TabHost.TabContentFactory;
Winson Chung49767ae2010-11-29 14:48:30 -0800105import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -0700106import android.widget.TextView;
107import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700108
Winson Chung400438b2011-01-16 17:53:48 -0800109import com.android.common.Search;
110import com.android.launcher.R;
111import com.android.launcher2.CustomizePagedView.CustomizationType;
112import com.android.launcher2.Workspace.ShrinkState;
Romain Guyedcce092010-03-04 13:03:17 -0800113
Adam Cohended9f8d2010-11-03 13:25:16 -0700114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115/**
116 * Default launcher application.
117 */
Michael Jurka946ad472010-07-09 18:05:18 -0700118public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700119 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
120 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800121 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato9c1289c2009-08-17 11:03:03 -0400124 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400125 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400126 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800129 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700132 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
133 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 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";
Michael Jurka883f55b2010-10-21 15:47:14 -0700159 // Type: int
160 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 // 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";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: boolean
170 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
171 // Type: long
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
173
Winson Chung80baf5a2010-08-09 16:03:15 -0700174 // tags for the customization tabs
175 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700176 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700177 private static final String SHORTCUTS_TAG = "shortcuts";
178 private static final String WALLPAPERS_TAG = "wallpapers";
179
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700180 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
181
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700182 /** The different states that Launcher can be in. */
Michael Jurkad3ef3062010-11-23 16:23:58 -0800183 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW,
184 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700185 private State mState = State.WORKSPACE;
186 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700187
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800191 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800193 private final BroadcastReceiver mCloseSystemDialogsReceiver
194 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800195 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 private LayoutInflater mInflater;
198
Joe Onorato00acb122009-08-04 16:04:30 -0400199 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700201
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700202 private AppWidgetManager mAppWidgetManager;
203 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700204
Michael Jurka0280c3b2010-09-17 15:00:07 -0700205 private int mAddScreen = -1;
206 private int mAddIntersectCellX = -1;
207 private int mAddIntersectCellY = -1;
208 private int[] mAddDropPosition;
209 private int[] mTmpAddItemCellCoordinates = new int[2];
210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 private FolderInfo mFolderInfo;
212
Joe Onorato7c312c12009-08-13 21:36:53 -0700213 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700214 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700215 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700216 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700217 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700218
Winson Chunga12a2502010-12-20 14:41:35 -0800219 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700220 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 private Bundle mSavedState;
223
224 private SpannableStringBuilder mDefaultKeySsb = null;
225
Joe Onorato9c1289c2009-08-17 11:03:03 -0400226 private boolean mWorkspaceLoading = true;
227
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800228 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 private boolean mRestoring;
230 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700231 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232
233 private Bundle mSavedInstanceState;
234
Joe Onorato9c1289c2009-08-17 11:03:03 -0400235 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800236 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800237 private boolean mUserPresent = true;
238 private boolean mVisible = false;
239 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400240
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700241 private static LocaleConfiguration sLocaleConfiguration = null;
242
Romain Guy84f296c2009-11-04 15:00:44 -0800243 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700244
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700245 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700246
Romain Guy1fbc1c82009-11-09 20:43:08 -0800247 private ImageView mPreviousView;
248 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500249
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400250 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400251 private String[] mHotseatConfig = null;
252 private Intent[] mHotseats = null;
253 private Drawable[] mHotseatIcons = null;
254 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400255
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700256 private Intent mAppMarketIntent = null;
257
Adam Cohended9f8d2010-11-03 13:25:16 -0700258 // Related to the auto-advancing of widgets
259 private final int ADVANCE_MSG = 1;
260 private final int mAdvanceInterval = 20000;
261 private final int mAdvanceStagger = 250;
262 private long mAutoAdvanceSentTime;
263 private long mAutoAdvanceTimeLeft = -1;
264 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
265 new HashMap<View, AppWidgetProviderInfo>();
266
Winson Chung400438b2011-01-16 17:53:48 -0800267 // Determines how long to wait after a rotation before restoring the screen orientation to
268 // match the sensor state.
269 private final int mRestoreScreenOrientationDelay = 500;
270
Michael Jurka4ef207b2010-11-29 17:05:45 -0800271 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800272 private static Drawable.ConstantState sGlobalSearchIcon;
273 private static Drawable.ConstantState sVoiceSearchIcon;
274 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800275
Winson Chunga12a2502010-12-20 14:41:35 -0800276 private CustomizationType getCustomizeFilterForTabTag(String tag) {
277 if (tag.equals(WIDGETS_TAG)) {
278 return CustomizationType.WidgetCustomization;
279 } else if (tag.equals(APPLICATIONS_TAG)) {
280 return CustomizationType.ApplicationCustomization;
281 } else if (tag.equals(WALLPAPERS_TAG)) {
282 return CustomizePagedView.CustomizationType.WallpaperCustomization;
283 } else if (tag.equals(SHORTCUTS_TAG)) {
284 return CustomizePagedView.CustomizationType.ShortcutCustomization;
285 }
286 return CustomizationType.WidgetCustomization;
287 }
288
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 @Override
290 protected void onCreate(Bundle savedInstanceState) {
291 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700292
Winson Chungaafa03c2010-06-11 17:34:16 -0700293 if (LauncherApplication.isInPlaceRotationEnabled()) {
294 // hide the status bar (temporary until we get the status bar design figured out)
295 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
296 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
297 }
298
Joe Onorato0589f0f2010-02-08 13:44:00 -0800299 LauncherApplication app = ((LauncherApplication)getApplication());
300 mModel = app.setLauncher(this);
301 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400302 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700304
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700305 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700306 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
307 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700308
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200310 android.os.Debug.startMethodTracing(
311 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 }
313
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400314 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800317 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700318 if (mHomeCustomizationDrawer != null) {
319 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700320
Winson Chung80baf5a2010-08-09 16:03:15 -0700321 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700322 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
323 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700324 TabContentFactory contentFactory = new TabContentFactory() {
325 public View createTabContent(String tag) {
326 return mCustomizePagedView;
327 }
328 };
329
Winson Chung49767ae2010-11-29 14:48:30 -0800330
331 TextView tabView;
332 TabWidget tabWidget = (TabWidget)
333 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
334
335 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
336 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700337 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800338 .setIndicator(tabView).setContent(contentFactory));
339 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
340 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700341 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800342 .setIndicator(tabView).setContent(contentFactory));
343 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
344 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700345 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800346 .setIndicator(tabView).setContent(contentFactory));
347 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
348 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700349 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800350 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700351 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
352 public void onTabChanged(String tabId) {
Winson Chunga12a2502010-12-20 14:41:35 -0800353 final CustomizePagedView.CustomizationType newType =
354 getCustomizeFilterForTabTag(tabId);
355 if (newType != mCustomizePagedView.getCustomizationFilter()) {
356 // animate the changing of the tab content by fading pages in and out
357 final Resources res = getResources();
358 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
359 final float alpha = mCustomizePagedView.getAlpha();
360 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
361 "alpha", alpha, 0.0f);
362 alphaAnim.setDuration(duration);
363 alphaAnim.addListener(new AnimatorListenerAdapter() {
364 @Override
365 public void onAnimationEnd(Animator animation) {
366 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
367 mCustomizePagedView.setCustomizationFilter(newType);
Winson Chung80baf5a2010-08-09 16:03:15 -0700368
Winson Chunga12a2502010-12-20 14:41:35 -0800369 final float alpha = mCustomizePagedView.getAlpha();
370 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
371 mCustomizePagedView, "alpha", alpha, 1.0f);
372 alphaAnim.setDuration(duration);
373 alphaAnim.start();
374 }
375 });
376 alphaAnim.start();
377 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700378 }
379 });
Michael Jurka946ad472010-07-09 18:05:18 -0700380 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 setupViews();
382
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800383 registerContentObservers();
384
Joe Onorato7c312c12009-08-13 21:36:53 -0700385 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 mSavedState = savedInstanceState;
388 restoreState(mSavedState);
389
Winson Chunga12a2502010-12-20 14:41:35 -0800390 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800391 if (mCustomizePagedView != null) {
392 mCustomizePagedView.update();
393 }
Winson Chunga12a2502010-12-20 14:41:35 -0800394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 if (PROFILE_STARTUP) {
396 android.os.Debug.stopMethodTracing();
397 }
398
399 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400400 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 }
402
403 // For handling default keys
404 mDefaultKeySsb = new SpannableStringBuilder();
405 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800406
407 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
408 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800409
410 // If we have a saved version of these external icons, we load them up immediately
411 if (LauncherApplication.isScreenXLarge()) {
412 if (sGlobalSearchIcon != null) {
413 updateGlobalSearchIcon(sGlobalSearchIcon);
414 }
415 if (sVoiceSearchIcon != null) {
416 updateVoiceSearchIcon(sVoiceSearchIcon);
417 }
418 if (sAppMarketIcon != null) {
419 updateAppMarketIcon(sAppMarketIcon);
420 }
421 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 }
Romain Guycbb89e42009-06-08 15:52:54 -0700423
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800424 @Override
425 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
426 super.dispatchPopulateAccessibilityEvent(event);
427
428 // we want to take over text population so it is context dependent
429 event.getText().clear();
430 if (mState == State.ALL_APPS) {
431 event.getText().add(getString(R.string.all_apps_button_label));
432 } else if (mState == State.WORKSPACE) {
433 event.getText().add(getString(R.string.all_apps_home_button_label));
434 }
435
436 return true;
437 }
438
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700440 if (sLocaleConfiguration == null) {
441 new AsyncTask<Void, Void, LocaleConfiguration>() {
442 @Override
443 protected LocaleConfiguration doInBackground(Void... unused) {
444 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
445 readConfiguration(Launcher.this, localeConfiguration);
446 return localeConfiguration;
447 }
448
449 @Override
450 protected void onPostExecute(LocaleConfiguration result) {
451 sLocaleConfiguration = result;
452 checkForLocaleChange(); // recursive, but now with a locale configuration
453 }
454 }.execute();
455 return;
456 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700457
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 final Configuration configuration = getResources().getConfiguration();
459
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700460 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 final String locale = configuration.locale.toString();
462
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700463 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 final int mcc = configuration.mcc;
465
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700466 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 final int mnc = configuration.mnc;
468
Romain Guy84f296c2009-11-04 15:00:44 -0800469 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470
Romain Guy84f296c2009-11-04 15:00:44 -0800471 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700472 sLocaleConfiguration.locale = locale;
473 sLocaleConfiguration.mcc = mcc;
474 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700475
Joe Onorato0589f0f2010-02-08 13:44:00 -0800476 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400477 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700478
479 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
480 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700481 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700482 public void run() {
483 writeConfiguration(Launcher.this, localeConfiguration);
484 }
485 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700486 }
487 }
488
489 private static class LocaleConfiguration {
490 public String locale;
491 public int mcc = -1;
492 public int mnc = -1;
493 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700494
Romain Guy98d01652009-06-30 16:21:04 -0700495 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
496 DataInputStream in = null;
497 try {
498 in = new DataInputStream(context.openFileInput(PREFERENCES));
499 configuration.locale = in.readUTF();
500 configuration.mcc = in.readInt();
501 configuration.mnc = in.readInt();
502 } catch (FileNotFoundException e) {
503 // Ignore
504 } catch (IOException e) {
505 // Ignore
506 } finally {
507 if (in != null) {
508 try {
509 in.close();
510 } catch (IOException e) {
511 // Ignore
512 }
513 }
514 }
515 }
516
517 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
518 DataOutputStream out = null;
519 try {
520 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
521 out.writeUTF(configuration.locale);
522 out.writeInt(configuration.mcc);
523 out.writeInt(configuration.mnc);
524 out.flush();
525 } catch (FileNotFoundException e) {
526 // Ignore
527 } catch (IOException e) {
528 //noinspection ResultOfMethodCallIgnored
529 context.getFileStreamPath(PREFERENCES).delete();
530 } finally {
531 if (out != null) {
532 try {
533 out.close();
534 } catch (IOException e) {
535 // Ignore
536 }
537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 }
539 }
540
541 static int getScreen() {
542 synchronized (sLock) {
543 return sScreen;
544 }
545 }
546
547 static void setScreen(int screen) {
548 synchronized (sLock) {
549 sScreen = screen;
550 }
551 }
552
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400553 // Note: This doesn't do all the client-id magic that BrowserProvider does
554 // in Browser. (http://b/2425179)
555 private Uri getDefaultBrowserUri() {
556 String url = getString(R.string.default_browser_url);
557 if (url.indexOf("{CID}") != -1) {
558 url = url.replace("{CID}", "android-google");
559 }
560 return Uri.parse(url);
561 }
562
563 // Load the Intent templates from arrays.xml to populate the hotseats. For
564 // each Intent, if it resolves to a single app, use that as the launch
565 // intent & use that app's label as the contentDescription. Otherwise,
566 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400567 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400568 if (mHotseatConfig == null) {
569 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
570 if (mHotseatConfig.length > 0) {
571 mHotseats = new Intent[mHotseatConfig.length];
572 mHotseatLabels = new CharSequence[mHotseatConfig.length];
573 mHotseatIcons = new Drawable[mHotseatConfig.length];
574 } else {
575 mHotseats = null;
576 mHotseatIcons = null;
577 mHotseatLabels = null;
578 }
579
580 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
581 for (int i=0; i<mHotseatConfig.length; i++) {
582 // load icon for this slot; currently unrelated to the actual activity
583 try {
584 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
585 } catch (ArrayIndexOutOfBoundsException ex) {
586 Log.w(TAG, "Missing hotseat_icons array item #" + i);
587 mHotseatIcons[i] = null;
588 }
589 }
590 hotseatIconDrawables.recycle();
591 }
592
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400593 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400594 for (int i=0; i<mHotseatConfig.length; i++) {
595 Intent intent = null;
596 if (mHotseatConfig[i].equals("*BROWSER*")) {
597 // magic value meaning "launch user's default web browser"
598 // replace it with a generic web request so we can see if there is indeed a default
599 String defaultUri = getString(R.string.default_browser_url);
600 intent = new Intent(
601 Intent.ACTION_VIEW,
602 ((defaultUri != null)
603 ? Uri.parse(defaultUri)
604 : getDefaultBrowserUri())
605 ).addCategory(Intent.CATEGORY_BROWSABLE);
606 // note: if the user launches this without a default set, she
607 // will always be taken to the default URL above; this is
608 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700609 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400610 // URL is unavoidably sent to the chosen app.
611 } else {
612 try {
613 intent = Intent.parseUri(mHotseatConfig[i], 0);
614 } catch (java.net.URISyntaxException ex) {
615 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
616 // bogus; leave intent=null
617 }
618 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700619
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400620 if (intent == null) {
621 mHotseats[i] = null;
622 mHotseatLabels[i] = getText(R.string.activity_not_found);
623 continue;
624 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400625
626 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700627 Log.d(TAG, "loadHotseats: hotseat " + i
628 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400629 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400630 + "]");
631 }
632
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400633 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
634 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700635 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400636 Log.d(TAG, "Best match for intent: " + bestMatch);
637 Log.d(TAG, "All matches: ");
638 for (ResolveInfo ri : allMatches) {
639 Log.d(TAG, " --> " + ri);
640 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400641 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400642 // did this resolve to a single app, or the resolver?
643 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700644 // can't find any activity to handle this. let's leave the
645 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400646 // to launch.
647 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400648
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400649 // set accessibility text to "Not installed"
650 mHotseatLabels[i] = getText(R.string.activity_not_found);
651 } else {
652 boolean found = false;
653 for (ResolveInfo ri : allMatches) {
654 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
655 && bestMatch.activityInfo.applicationInfo.packageName
656 .equals(ri.activityInfo.applicationInfo.packageName)) {
657 found = true;
658 break;
659 }
660 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700661
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400662 if (!found) {
663 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
664 // the bestMatch is probably the ResolveActivity, meaning the
665 // user has not yet selected a default
666 // so: we'll keep the original intent for now
667 mHotseats[i] = intent;
668
669 // set the accessibility text to "Select shortcut"
670 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
671 } else {
672 // we have an app!
673 // now reconstruct the intent to launch it through the front
674 // door
675 ComponentName com = new ComponentName(
676 bestMatch.activityInfo.applicationInfo.packageName,
677 bestMatch.activityInfo.name);
678 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
679
680 // load the app label for accessibility
681 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
682 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400683 }
684
685 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700686 Log.d(TAG, "loadHotseats: hotseat " + i
687 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400688 + ((mHotseats[i] == null)
689 ? "null"
690 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400691 + "] label=[" + mHotseatLabels[i]
692 + "]"
693 );
694 }
695 }
696 }
697
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 @Override
699 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700700 mWaitingForResult = false;
701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 // The pattern used here is that a user PICKs a specific application,
703 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700704
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
706 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700707
Michael Jurka0280c3b2010-09-17 15:00:07 -0700708 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 switch (requestCode) {
710 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700711 completeAddApplication(
712 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 break;
714 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800715 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 break;
717 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700718 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 break;
720 case REQUEST_PICK_LIVE_FOLDER:
721 addLiveFolder(data);
722 break;
723 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700724 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700726 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700727 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700729 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700730 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700731 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700733 case REQUEST_PICK_WALLPAPER:
734 // We just wanted the activity result here so we can clear mWaitingForResult
735 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 }
Romain Guy18042c82009-11-06 11:44:55 -0800737 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
738 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
739 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700740 // Clean up the appWidgetId if we canceled
741 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
742 if (appWidgetId != -1) {
743 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 }
745 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 }
747
748 @Override
749 protected void onResume() {
750 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800751 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700752 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753 mWorkspaceLoading = true;
754 mModel.startLoader(this, true);
755 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700756 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700758 // When we resume Launcher, a different Activity might be responsible for the app
759 // market intent, so refresh the icon
760 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 }
762
763 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700764 protected void onPause() {
765 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700766 // Some launcher layouts don't have a previous and next view
767 if (mPreviousView != null) {
768 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700769 }
770 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700771 dismissPreview(mNextView);
772 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700773 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800774 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700775 }
Romain Guycbb89e42009-06-08 15:52:54 -0700776
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700777 @Override
778 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400779 // Flag the loader to stop early before switching
780 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800781 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800782 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700783 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700784
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800785 // We can't hide the IME if it was forced open. So don't bother
786 /*
787 @Override
788 public void onWindowFocusChanged(boolean hasFocus) {
789 super.onWindowFocusChanged(hasFocus);
790
791 if (hasFocus) {
792 final InputMethodManager inputManager = (InputMethodManager)
793 getSystemService(Context.INPUT_METHOD_SERVICE);
794 WindowManager.LayoutParams lp = getWindow().getAttributes();
795 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
796 android.os.Handler()) {
797 protected void onReceiveResult(int resultCode, Bundle resultData) {
798 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
799 }
800 });
801 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
802 }
803 }
804 */
805
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 private boolean acceptFilter() {
807 final InputMethodManager inputManager = (InputMethodManager)
808 getSystemService(Context.INPUT_METHOD_SERVICE);
809 return !inputManager.isFullscreenMode();
810 }
811
812 @Override
813 public boolean onKeyDown(int keyCode, KeyEvent event) {
814 boolean handled = super.onKeyDown(keyCode, event);
815 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
816 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
817 keyCode, event);
818 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700819 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700820 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700821 // showSearchDialog()
822 // If there are multiple keystrokes before the search dialog takes focus,
823 // onSearchRequested() will be called for every keystroke,
824 // but it is idempotent, so it's fine.
825 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 }
827 }
828
Joe Onorato8a9625e2010-01-28 15:55:35 -0800829 // Eat the long press event so the keyboard doesn't come up.
830 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
831 return true;
832 }
833
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 return handled;
835 }
836
Karl Rosaen138a0412009-04-23 19:00:21 -0700837 private String getTypedText() {
838 return mDefaultKeySsb.toString();
839 }
840
841 private void clearTypedText() {
842 mDefaultKeySsb.clear();
843 mDefaultKeySsb.clearSpans();
844 Selection.setSelection(mDefaultKeySsb, 0);
845 }
846
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700848 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
849 * State
850 */
851 private static State intToState(int stateOrdinal) {
852 State state = State.WORKSPACE;
853 final State[] stateValues = State.values();
854 for (int i = 0; i < stateValues.length; i++) {
855 if (stateValues[i].ordinal() == stateOrdinal) {
856 state = stateValues[i];
857 break;
858 }
859 }
860 return state;
861 }
862
863 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 * Restores the previous state, if it exists.
865 *
866 * @param savedState The previous state.
867 */
868 private void restoreState(Bundle savedState) {
869 if (savedState == null) {
870 return;
871 }
872
Michael Jurka883f55b2010-10-21 15:47:14 -0700873 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
874
875 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800876 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700877 } else if (state == State.CUSTOMIZE) {
878 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800879 }
880
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
882 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700883 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 }
885
886 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700887
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700889 mAddScreen = addScreen;
890 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
891 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 mRestoring = true;
893 }
894
895 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
896 if (renameFolder) {
897 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700898 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 mRestoring = true;
900 }
Winson Chunga12a2502010-12-20 14:41:35 -0800901
902 // Restore the current AllApps drawer tab
903 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
904 String curTab = savedState.getString("allapps_currentTab");
905 if (curTab != null) {
906 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
907 tabhost.setCurrentTabByTag(curTab);
908 }
909 int curPage = savedState.getInt("allapps_currentPage", -1);
910 if (curPage > -1) {
911 mAllAppsPagedView.setRestorePage(curPage);
912 }
913 }
914
915 // Restore the current customization drawer tab
916 if (mHomeCustomizationDrawer != null) {
917 String curTab = savedState.getString("customize_currentTab");
918 if (curTab != null) {
919 // We set this directly so that there is no delay before the tab is set
920 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
921 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
922 }
923
924 // Note: currently we do not restore the page for the customization tray because unlike
925 // AllApps, the page content can change drastically
926 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 }
928
929 /**
930 * Finds all the views we need and configure them properly.
931 */
932 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700933 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400934
Romain Guyb1b69f52009-08-10 15:10:15 -0700935 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400936 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937
Joe Onorato7c312c12009-08-13 21:36:53 -0700938 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700939 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700940 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800941 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700942 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700943 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700944
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700945 if (LauncherApplication.isScreenXLarge()) {
946 // They need to be INVISIBLE initially so that they will be measured in the layout.
947 // Otherwise the animations are messed up when we show them for the first time.
948 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
949 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
950 }
951
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700953
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500955 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700956
Joe Onorato7c312c12009-08-13 21:36:53 -0700957 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
958 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700960 View handleView = findViewById(R.id.all_apps_button);
961 if (handleView != null && handleView instanceof HandleView) {
962 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700963 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700964 mHandleView.setLauncher(this);
965 mHandleView.setOnClickListener(this);
966 mHandleView.setOnLongClickListener(this);
967 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800968
Winson Chung80baf5a2010-08-09 16:03:15 -0700969 if (mCustomizePagedView != null) {
970 mCustomizePagedView.setLauncher(this);
971 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700972 } else {
973 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
974 hotseatLeft.setContentDescription(mHotseatLabels[0]);
975 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
976 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
977 hotseatRight.setContentDescription(mHotseatLabels[1]);
978 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400979
Michael Jurkaaf442092010-06-10 17:01:57 -0700980 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
981 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800982
Michael Jurkaaf442092010-06-10 17:01:57 -0700983 Drawable previous = mPreviousView.getDrawable();
984 Drawable next = mNextView.getDrawable();
985 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986
Michael Jurkaaf442092010-06-10 17:01:57 -0700987 mPreviousView.setHapticFeedbackEnabled(false);
988 mPreviousView.setOnLongClickListener(this);
989 mNextView.setHapticFeedbackEnabled(false);
990 mNextView.setOnLongClickListener(this);
991 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800992
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400994 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -0800996 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997
998 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400999 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -07001000 int deleteZoneHandleId;
Michael Jurkab8e14472010-12-20 16:06:10 -08001001
1002 final View allAppsButton = findViewById(R.id.all_apps_button);
1003 final View divider = findViewById(R.id.divider);
1004 final View configureButton = findViewById(R.id.configure_button);
1005
Michael Jurka54f7ac32010-08-02 13:56:46 -07001006 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001007 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -07001008 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001009 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001010 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001011 dragController.addDragListener(deleteZone);
1012
Adam Cohencdc30d52010-12-01 15:09:47 -08001013 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1014 if (allAppsDeleteZone != null) {
1015 allAppsDeleteZone.setLauncher(this);
1016 allAppsDeleteZone.setDragController(dragController);
1017 allAppsDeleteZone.setDragAndDropEnabled(false);
1018 dragController.addDragListener(allAppsDeleteZone);
1019 dragController.addDropTarget(allAppsDeleteZone);
1020 }
1021
1022 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1023 findViewById(R.id.all_apps_info_target);
1024 if (allAppsInfoTarget != null) {
1025 allAppsInfoTarget.setLauncher(this);
1026 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001027 allAppsInfoTarget.setDragAndDropEnabled(false);
1028 View marketButton = findViewById(R.id.market_button);
1029 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001030 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -08001031 }
1032 }
1033
Joe Onorato00acb122009-08-04 16:04:30 -04001034 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001035 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001036 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001037
Joe Onorato00acb122009-08-04 16:04:30 -04001038 // The order here is bottom to top.
1039 dragController.addDropTarget(workspace);
1040 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001041 if (allAppsInfoTarget != null) {
1042 dragController.addDropTarget(allAppsInfoTarget);
1043 }
1044 if (allAppsDeleteZone != null) {
1045 dragController.addDropTarget(allAppsDeleteZone);
1046 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 }
1048
Romain Guy8a73c512009-11-09 19:19:59 -08001049 @SuppressWarnings({"UnusedDeclaration"})
1050 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001051 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001052 mWorkspace.scrollLeft();
1053 }
Romain Guy8a73c512009-11-09 19:19:59 -08001054 }
1055
1056 @SuppressWarnings({"UnusedDeclaration"})
1057 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001058 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001059 mWorkspace.scrollRight();
1060 }
Romain Guy8a73c512009-11-09 19:19:59 -08001061 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001062
1063 @SuppressWarnings({"UnusedDeclaration"})
1064 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001065 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001066
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001067 int index = -1;
1068 if (v.getId() == R.id.hotseat_left) {
1069 index = 0;
1070 } else if (v.getId() == R.id.hotseat_right) {
1071 index = 1;
1072 }
1073
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001074 // reload these every tap; you never know when they might change
1075 loadHotseats();
1076 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
1077 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001078 startActivitySafely(
1079 mHotseats[index],
1080 "hotseat"
1081 );
1082 }
1083 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 /**
1086 * Creates a view representing a shortcut.
1087 *
1088 * @param info The data structure describing the shortcut.
1089 *
1090 * @return A View inflated from R.layout.application.
1091 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001092 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001094 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 }
1096
1097 /**
1098 * Creates a view representing a shortcut inflated from the specified resource.
1099 *
1100 * @param layoutResId The id of the XML layout used to create the shortcut.
1101 * @param parent The group the shortcut belongs to.
1102 * @param info The data structure describing the shortcut.
1103 *
1104 * @return A View inflated from layoutResId.
1105 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001106 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001107 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1108 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 return favorite;
1111 }
1112
1113 /**
1114 * Add an application shortcut to the workspace.
1115 *
1116 * @param data The intent describing the application.
1117 * @param cellInfo The position on screen where to create the shortcut.
1118 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001119 void completeAddApplication(Context context, Intent data, int screen,
1120 int intersectCellX, int intersectCellY) {
1121 final int[] cellXY = mTmpAddItemCellCoordinates;
1122 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1123
1124 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1125 showOutOfSpaceMessage();
1126 return;
1127 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128
Joe Onorato0589f0f2010-02-08 13:44:00 -08001129 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1130 data, context);
1131
Romain Guy73b979d2009-06-09 12:57:21 -07001132 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001133 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001135 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001136 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1137 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001138 } else {
1139 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
1141 }
Romain Guycbb89e42009-06-08 15:52:54 -07001142
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 /**
1144 * Add a shortcut to the workspace.
1145 *
1146 * @param data The intent describing the shortcut.
1147 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001149 private void completeAddShortcut(Intent data, int screen,
1150 int intersectCellX, int intersectCellY) {
1151 final int[] cellXY = mTmpAddItemCellCoordinates;
1152 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001153
Michael Jurkad3ef3062010-11-23 16:23:58 -08001154 int[] touchXY = null;
1155 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1156 touchXY = mAddDropPosition;
1157 }
1158 boolean foundCellSpan = false;
1159 if (touchXY != null) {
1160 // when dragging and dropping, just find the closest free spot
1161 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1162 int[] result = screenLayout.findNearestVacantArea(
1163 touchXY[0], touchXY[1], 1, 1, cellXY);
1164 foundCellSpan = (result != null);
1165 } else {
1166 foundCellSpan = layout.findCellForSpanThatIntersects(
1167 cellXY, 1, 1, intersectCellX, intersectCellY);
1168 }
1169
1170 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001171 showOutOfSpaceMessage();
1172 return;
1173 }
1174
1175 final ShortcutInfo info = mModel.addShortcut(
1176 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177
1178 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001180 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 }
1182 }
1183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001185 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001187 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001188 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001190 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001191 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001192
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001193 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001194 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1195 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001198 // We have saved the position to which the widget was dragged-- this really only matters
1199 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001200 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001201
1202 // For now, we don't save the coordinate where we dropped the icon because we're not
1203 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1204 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001205 int[] touchXY = null;
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001206 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
Winson Chung55cef262010-10-28 14:14:18 -07001207 touchXY = mAddDropPosition;
1208 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001209 boolean foundCellSpan = false;
1210 if (touchXY != null) {
1211 // when dragging and dropping, just find the closest free spot
1212 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1213 int[] result = screenLayout.findNearestVacantArea(
1214 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001215 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001216 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001217 // if we long pressed on an empty cell to bring up a menu,
1218 // make sure we intersect the empty cell
1219 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1220 // findCellForSpanThatIntersects will just ignore them
1221 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1222 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001223 }
1224
Michael Jurka0280c3b2010-09-17 15:00:07 -07001225 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001226 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001227 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001228 return;
1229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001231 // Build Launcher-specific widget info and save to database
1232 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001233 launcherInfo.spanX = spanXY[0];
1234 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 LauncherModel.addItemToDatabase(this, launcherInfo,
1237 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001238 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239
1240 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001241 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001244 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001245
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001246 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001248
Michael Jurka0280c3b2010-09-17 15:00:07 -07001249 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001250 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001251
1252 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 }
1254 }
Romain Guycbb89e42009-06-08 15:52:54 -07001255
Adam Cohended9f8d2010-11-03 13:25:16 -07001256 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1257 @Override
1258 public void onReceive(Context context, Intent intent) {
1259 final String action = intent.getAction();
1260 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1261 mUserPresent = false;
1262 updateRunning();
1263 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1264 mUserPresent = true;
1265 updateRunning();
1266 }
1267 }
1268 };
1269
1270 @Override
1271 public void onAttachedToWindow() {
1272 super.onAttachedToWindow();
1273
1274 // Listen for broadcasts related to user-presence
1275 final IntentFilter filter = new IntentFilter();
1276 filter.addAction(Intent.ACTION_SCREEN_OFF);
1277 filter.addAction(Intent.ACTION_USER_PRESENT);
1278 registerReceiver(mReceiver, filter);
1279
Adam Cohend113e0c2010-11-11 10:48:05 -08001280 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001281 mVisible = true;
1282 }
1283
1284 @Override
1285 public void onDetachedFromWindow() {
1286 super.onDetachedFromWindow();
1287 mVisible = false;
1288
Adam Cohend113e0c2010-11-11 10:48:05 -08001289 if (mAttached) {
1290 unregisterReceiver(mReceiver);
1291 mAttached = false;
1292 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001293 updateRunning();
1294 }
1295
1296 public void onWindowVisibilityChanged(int visibility) {
1297 mVisible = visibility == View.VISIBLE;
1298 updateRunning();
1299 }
1300
1301 private void sendAdvanceMessage(long delay) {
1302 mHandler.removeMessages(ADVANCE_MSG);
1303 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1304 mHandler.sendMessageDelayed(msg, delay);
1305 mAutoAdvanceSentTime = System.currentTimeMillis();
1306 }
1307
1308 private void updateRunning() {
1309 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1310 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1311 mAutoAdvanceRunning = autoAdvanceRunning;
1312 if (autoAdvanceRunning) {
1313 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1314 sendAdvanceMessage(delay);
1315 } else {
1316 if (!mWidgetsToAdvance.isEmpty()) {
1317 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1318 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1319 }
1320 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001321 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001322 }
1323 }
1324 }
1325
1326 private final Handler mHandler = new Handler() {
1327 @Override
1328 public void handleMessage(Message msg) {
1329 if (msg.what == ADVANCE_MSG) {
1330 int i = 0;
1331 for (View key: mWidgetsToAdvance.keySet()) {
1332 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1333 final int delay = mAdvanceStagger * i;
1334 if (v instanceof Advanceable) {
1335 postDelayed(new Runnable() {
1336 public void run() {
1337 ((Advanceable) v).advance();
1338 }
1339 }, delay);
1340 }
1341 i++;
1342 }
1343 sendAdvanceMessage(mAdvanceInterval);
1344 }
1345 }
1346 };
1347
1348 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1349 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1350 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1351 if (v instanceof Advanceable) {
1352 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1353 ((Advanceable) v).willBeAdvancedByHost();
1354 updateRunning();
1355 }
1356 }
1357
1358 void removeWidgetToAutoAdvance(View hostView) {
1359 if (mWidgetsToAdvance.containsKey(hostView)) {
1360 mWidgetsToAdvance.remove(hostView);
1361 updateRunning();
1362 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001363 }
1364
Joe Onorato9c1289c2009-08-17 11:03:03 -04001365 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1366 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001367 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001368 launcherInfo.hostView = null;
1369 }
1370
Adam Cohended9f8d2010-11-03 13:25:16 -07001371 void showOutOfSpaceMessage() {
1372 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1373 }
1374
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001375 public LauncherAppWidgetHost getAppWidgetHost() {
1376 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 }
Romain Guycbb89e42009-06-08 15:52:54 -07001378
Winson Chunga9abd0e2010-10-27 17:18:37 -07001379 public LauncherModel getModel() {
1380 return mModel;
1381 }
1382
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001383 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001384 getWindow().closeAllPanels();
1385
1386 try {
1387 dismissDialog(DIALOG_CREATE_SHORTCUT);
1388 // Unlock the workspace if the dialog was showing
1389 } catch (Exception e) {
1390 // An exception is thrown if the dialog is not visible, which is fine
1391 }
1392
1393 try {
1394 dismissDialog(DIALOG_RENAME_FOLDER);
1395 // Unlock the workspace if the dialog was showing
1396 } catch (Exception e) {
1397 // An exception is thrown if the dialog is not visible, which is fine
1398 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001399
1400 // Whatever we were doing is hereby canceled.
1401 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001402 }
1403
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 @Override
1405 protected void onNewIntent(Intent intent) {
1406 super.onNewIntent(intent);
1407
1408 // Close the menu
1409 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001410 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001411 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001412
Joe Onorato14f122b2009-11-19 14:06:36 -08001413 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1414 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001415
Michael Jurka4cb37242010-08-09 21:05:32 -07001416 // in all these cases, only animate if we're already on home
1417 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001418 mWorkspace.unshrink(alreadyOnHome);
1419 }
1420 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001421 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac5b262c2011-01-12 20:24:50 -08001422 boolean animate = alreadyOnHome &&
1423 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS);
1424 mWorkspace.moveToDefaultScreen(animate);
1425 if (!animate) mWorkspace.updateWallpaperOffsetImmediately();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001426 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001427 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001428
Joe Onorato3a8820b2009-11-10 15:06:42 -08001429 final View v = getWindow().peekDecorView();
1430 if (v != null && v.getWindowToken() != null) {
1431 InputMethodManager imm = (InputMethodManager)getSystemService(
1432 INPUT_METHOD_SERVICE);
1433 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 }
1435 }
1436 }
1437
1438 @Override
1439 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1440 // Do not call super here
1441 mSavedInstanceState = savedInstanceState;
1442 }
1443
1444 @Override
1445 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001446 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447
1448 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1449 if (folders.size() > 0) {
1450 final int count = folders.size();
1451 long[] ids = new long[count];
1452 for (int i = 0; i < count; i++) {
1453 final FolderInfo info = folders.get(i).getInfo();
1454 ids[i] = info.id;
1455 }
1456 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1457 } else {
1458 super.onSaveInstanceState(outState);
1459 }
1460
Michael Jurka883f55b2010-10-21 15:47:14 -07001461 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462
Michael Jurka0280c3b2010-09-17 15:00:07 -07001463 if (mAddScreen > -1 && mWaitingForResult) {
1464 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1465 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1466 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
1468
1469 if (mFolderInfo != null && mWaitingForResult) {
1470 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1471 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1472 }
Michael Jurka946ad472010-07-09 18:05:18 -07001473
Winson Chung43b119d2010-12-06 16:45:05 -08001474 // Save the current AllApps drawer tab
1475 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1476 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1477 String currentTabTag = tabhost.getCurrentTabTag();
1478 if (currentTabTag != null) {
1479 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001480 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001481 }
1482 }
1483
1484 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001485 if (mHomeCustomizationDrawer != null) {
1486 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1487 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001488 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001489 }
1490 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 }
1492
1493 @Override
1494 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001496
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001498 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001500 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001502 mAppWidgetHost = null;
1503
1504 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505
1506 TextKeyListener.getInstance().release();
1507
Joe Onorato9c1289c2009-08-17 11:03:03 -04001508 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509
Joe Onorato9c1289c2009-08-17 11:03:03 -04001510 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001511
1512 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001513
Patrick Dubroyab962b72010-07-26 12:10:10 -07001514 // Some launcher layouts don't have a previous and next view
1515 if (mPreviousView != null) {
1516 dismissPreview(mPreviousView);
1517 }
1518 if (mNextView != null) {
1519 dismissPreview(mNextView);
1520 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001521
1522 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001523
1524 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1525 mWorkspace.removeAllViews();
1526 mWorkspace = null;
1527 mDragController = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 }
1529
1530 @Override
1531 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001532 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 super.startActivityForResult(intent, requestCode);
1534 }
1535
1536 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001537 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001539
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001540 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001541
Karl Rosaen138a0412009-04-23 19:00:21 -07001542 if (initialQuery == null) {
1543 // Use any text typed in the launcher as the initial query
1544 initialQuery = getTypedText();
1545 clearTypedText();
1546 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 if (appSearchData == null) {
1548 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001549 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 }
Romain Guycbb89e42009-06-08 15:52:54 -07001551
Karl Rosaen138a0412009-04-23 19:00:21 -07001552 final SearchManager searchManager =
1553 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001554 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001555 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 }
1557
1558 @Override
1559 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001560 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001561 return false;
1562 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563
1564 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001565
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1567 .setIcon(android.R.drawable.ic_menu_add)
1568 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001569 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1570 .setIcon(android.R.drawable.ic_menu_manage)
1571 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001572 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 .setIcon(android.R.drawable.ic_menu_gallery)
1574 .setAlphabeticShortcut('W');
1575 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1576 .setIcon(android.R.drawable.ic_search_category_default)
1577 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1578 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1579 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1580 .setAlphabeticShortcut('N');
1581
1582 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001583 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1584 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585
1586 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1587 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1588 .setIntent(settings);
1589
1590 return true;
1591 }
1592
1593 @Override
1594 public boolean onPrepareOptionsMenu(Menu menu) {
1595 super.onPrepareOptionsMenu(menu);
1596
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001597 // If all apps is animating, don't show the menu, because we don't know
1598 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001599 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001600 return false;
1601 }
1602
1603 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001604 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001605 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1606 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1607
1608 // Disable add if the workspace is full.
1609 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001610 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1611 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001612 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613
1614 return true;
1615 }
1616
1617 @Override
1618 public boolean onOptionsItemSelected(MenuItem item) {
1619 switch (item.getItemId()) {
1620 case MENU_ADD:
1621 addItems();
1622 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001623 case MENU_MANAGE_APPS:
1624 manageApps();
1625 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 case MENU_WALLPAPER_SETTINGS:
1627 startWallpaper();
1628 return true;
1629 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001630 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 return true;
1632 case MENU_NOTIFICATIONS:
1633 showNotifications();
1634 return true;
1635 }
1636
1637 return super.onOptionsItemSelected(item);
1638 }
Romain Guycbb89e42009-06-08 15:52:54 -07001639
Karl Rosaen138a0412009-04-23 19:00:21 -07001640 /**
1641 * Indicates that we want global search for this activity by setting the globalSearch
1642 * argument for {@link #startSearch} to true.
1643 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001645 @Override
1646 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001647 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001648 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001649 }
1650
Joe Onorato9c1289c2009-08-17 11:03:03 -04001651 public boolean isWorkspaceLocked() {
1652 return mWorkspaceLoading || mWaitingForResult;
1653 }
1654
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001656 if (LauncherApplication.isScreenXLarge()) {
1657 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001658 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001659 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001660 }
1661 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001662 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001663 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001664 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 }
1666
Michael Jurka0280c3b2010-09-17 15:00:07 -07001667 private void resetAddInfo() {
1668 mAddScreen = -1;
1669 mAddIntersectCellX = -1;
1670 mAddIntersectCellY = -1;
1671 mAddDropPosition = null;
1672 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001673
Winson Chung55cef262010-10-28 14:14:18 -07001674 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001675 resetAddInfo();
1676 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001677 mAddDropPosition = position;
1678
Michael Jurkaaf442092010-06-10 17:01:57 -07001679 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001680 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1681 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001682 }
1683
Winson Chung7ad01412010-09-27 11:33:03 -07001684 private void manageApps() {
1685 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1686 }
1687
Michael Jurkaaf442092010-06-10 17:01:57 -07001688 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001689 // TODO: catch bad widget exception when sent
1690 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001691 // TODO: Is this log message meaningful?
1692 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001693 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001694 }
1695
Winson Chung55cef262010-10-28 14:14:18 -07001696 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001697 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698
Bjorn Bringert7984c942009-12-09 15:38:25 +00001699 if (appWidget.configure != null) {
1700 // Launch over to configure widget, if needed
1701 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1702 intent.setComponent(appWidget.configure);
1703 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001704 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001705 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1706 intent.putExtra(
1707 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1708 info.mimeType);
1709
1710 final String mimeType = info.mimeType;
1711 final ClipData clipData = (ClipData) info.configurationData;
1712 final ClipDescription clipDesc = clipData.getDescription();
1713 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1714 if (clipDesc.getMimeType(i).equals(mimeType)) {
1715 final ClipData.Item item = clipData.getItem(i);
1716 final CharSequence stringData = item.getText();
1717 final Uri uriData = item.getUri();
1718 final Intent intentData = item.getIntent();
1719 final String key =
1720 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1721 if (uriData != null) {
1722 intent.putExtra(key, uriData);
1723 } else if (intentData != null) {
1724 intent.putExtra(key, intentData);
1725 } else if (stringData != null) {
1726 intent.putExtra(key, stringData);
1727 }
1728 break;
1729 }
1730 }
1731 }
Winson Chung55cef262010-10-28 14:14:18 -07001732 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001733
Romain Guy8e633c52010-03-04 12:51:36 -08001734 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001736 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001737 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 }
1739 }
Romain Guycbb89e42009-06-08 15:52:54 -07001740
Michael Jurka0280c3b2010-09-17 15:00:07 -07001741 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1742 resetAddInfo();
1743 mAddScreen = screen;
1744 mAddDropPosition = position;
1745
1746 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1747 createShortcutIntent.setComponent(componentName);
1748 processShortcut(createShortcutIntent);
1749 }
1750
Joe Onoratodeb98af2010-02-19 14:59:39 -08001751 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001752 // Handle case where user selected "Applications"
1753 String applicationName = getResources().getString(R.string.group_applications);
1754 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001755
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001756 if (applicationName != null && applicationName.equals(shortcutName)) {
1757 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1758 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001759
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001760 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1761 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001762 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001763 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001764 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001765 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001766 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 }
1768
Winson Chung24ab2f12010-09-16 14:10:47 -07001769 void processWallpaper(Intent intent) {
1770 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1771 }
1772
Michael Jurka0280c3b2010-09-17 15:00:07 -07001773 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1774 resetAddInfo();
1775 mAddScreen = screen;
1776 mAddDropPosition = position;
1777
1778 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1779 createFolderIntent.setComponent(componentName);
1780
1781 addLiveFolder(createFolderIntent);
1782 }
1783
1784 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001785 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001786 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001787 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001788
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001789 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001790 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001791 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001792 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001793 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 }
1795
Michael Jurka0280c3b2010-09-17 15:00:07 -07001796 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 UserFolderInfo folderInfo = new UserFolderInfo();
1798 folderInfo.title = getText(R.string.folder_name);
1799
Michael Jurka0280c3b2010-09-17 15:00:07 -07001800 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1801 final int[] cellXY = mTmpAddItemCellCoordinates;
1802 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1803 showOutOfSpaceMessage();
1804 return;
1805 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806
1807 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001808 LauncherModel.addItemToDatabase(this, folderInfo,
1809 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001810 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001811 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812
1813 // Create the view
1814 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001815 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1816 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001817 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 }
Romain Guycbb89e42009-06-08 15:52:54 -07001819
Joe Onorato9c1289c2009-08-17 11:03:03 -04001820 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001821 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001822 }
1823
Michael Jurka28750fb2010-09-24 17:43:49 -07001824 private void completeAddLiveFolder(
1825 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001826 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1827 final int[] cellXY = mTmpAddItemCellCoordinates;
1828 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1829 showOutOfSpaceMessage();
1830 return;
1831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832
Michael Jurka0280c3b2010-09-17 15:00:07 -07001833 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834
1835 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001837 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001838 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 }
1840 }
1841
1842 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001843 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844
1845 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1846 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1847
1848 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 Intent.ShortcutIconResource iconResource = null;
1850
1851 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1852 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1853 try {
1854 iconResource = (Intent.ShortcutIconResource) extra;
1855 final PackageManager packageManager = context.getPackageManager();
1856 Resources resources = packageManager.getResourcesForApplication(
1857 iconResource.packageName);
1858 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1859 icon = resources.getDrawable(id);
1860 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001861 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 }
1863 }
1864
1865 if (icon == null) {
1866 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1867 }
1868
1869 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001870 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 info.title = name;
1872 info.iconResource = iconResource;
1873 info.uri = data.getData();
1874 info.baseIntent = baseIntent;
1875 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1876 LiveFolders.DISPLAY_MODE_GRID);
1877
1878 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001879 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001880 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881
1882 return info;
1883 }
1884
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 private void showNotifications() {
1886 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1887 if (statusBar != null) {
1888 statusBar.expand();
1889 }
1890 }
1891
1892 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001893 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001895 Intent chooser = Intent.createChooser(pickWallpaper,
1896 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001897 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1898 // Removed in Eclair MR1
1899// WallpaperManager wm = (WallpaperManager)
1900// getSystemService(Context.WALLPAPER_SERVICE);
1901// WallpaperInfo wi = wm.getWallpaperInfo();
1902// if (wi != null && wi.getSettingsActivity() != null) {
1903// LabeledIntent li = new LabeledIntent(getPackageName(),
1904// R.string.configure_wallpaper, 0);
1905// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1906// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1907// }
Mike Clerona0618e42009-10-22 13:55:21 -07001908 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 }
1910
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001911 /**
1912 * Registers various content observers. The current implementation registers
1913 * only a favorites observer to keep track of the favorites applications.
1914 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001915 private void registerContentObservers() {
1916 ContentResolver resolver = getContentResolver();
1917 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1918 true, mWidgetObserver);
1919 }
1920
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 @Override
1922 public boolean dispatchKeyEvent(KeyEvent event) {
1923 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1924 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001926 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001927 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001928 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001929 dumpState();
1930 return true;
1931 }
1932 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001933 }
1934 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1935 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001936 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 return true;
1938 }
1939 }
1940
1941 return super.dispatchKeyEvent(event);
1942 }
1943
Joe Onorato88ec0992009-11-19 13:16:06 -08001944 @Override
1945 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001946 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1947 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001948 } else {
1949 closeFolder();
1950 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001951 // Some launcher layouts don't have a previous and next view
1952 if (mPreviousView != null) {
1953 dismissPreview(mPreviousView);
1954 dismissPreview(mNextView);
1955 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001956 }
1957
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 private void closeFolder() {
1959 Folder folder = mWorkspace.getOpenFolder();
1960 if (folder != null) {
1961 closeFolder(folder);
1962 }
1963 }
1964
1965 void closeFolder(Folder folder) {
1966 folder.getInfo().opened = false;
1967 ViewGroup parent = (ViewGroup) folder.getParent();
1968 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001969 CellLayout cl = (CellLayout) parent;
1970 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001971 if (folder instanceof DropTarget) {
1972 // Live folders aren't DropTargets.
1973 mDragController.removeDropTarget((DropTarget)folder);
1974 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 }
1976 folder.onClose();
1977 }
1978
1979 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001980 * Re-listen when widgets are reset.
1981 */
1982 private void onAppWidgetReset() {
1983 mAppWidgetHost.startListening();
1984 }
1985
1986 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001987 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1988 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001990 private void unbindDesktopItems() {
1991 for (ItemInfo item: mDesktopItems) {
1992 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001994 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001996
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 /**
1998 * Launches the intent referred by the clicked shortcut.
1999 *
2000 * @param v The view representing the clicked shortcut.
2001 */
2002 public void onClick(View v) {
2003 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002004 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002006 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002007 int[] pos = new int[2];
2008 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002009 intent.setSourceBounds(new Rect(pos[0], pos[1],
2010 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07002011 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 } else if (tag instanceof FolderInfo) {
2013 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002014 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002015 if (mState == State.ALL_APPS) {
2016 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002017 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002018 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002019 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 }
2021 }
2022
Michael Jurka0e260592010-06-30 17:07:39 -07002023 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002024 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002025 // clicking anywhere on the workspace causes the customization drawer to slide down
2026 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002027 return false;
2028 }
2029
Michael Jurkaaf442092010-06-10 17:01:57 -07002030 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002031 * Event handler for the search button
2032 *
2033 * @param v The view that was clicked.
2034 */
2035 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002036 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002037 // Use a custom animation for launching search
2038 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002039 }
2040
2041 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002042 * Event handler for the voice button
2043 *
2044 * @param v The view that was clicked.
2045 */
2046 public void onClickVoiceButton(View v) {
2047 startVoiceSearch();
2048 }
2049
2050 private void startVoiceSearch() {
2051 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2052 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2053 startActivity(intent);
2054 }
2055
2056 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002057 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002058 * enters home screen customization mode.
2059 *
2060 * @param v The view that was clicked.
2061 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002062 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002063 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002064 }
2065
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002066 /**
2067 * Event handler for the "grid" button that appears on the home screen, which
2068 * enters all apps mode.
2069 *
2070 * @param v The view that was clicked.
2071 */
2072 public void onClickAllAppsButton(View v) {
2073 showAllApps(true);
2074 }
2075
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002076 public void onClickAppMarketButton(View v) {
2077 if (mAppMarketIntent != null) {
2078 startActivitySafely(mAppMarketIntent, "app market");
2079 }
2080 }
2081
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002082 void startApplicationDetailsActivity(ComponentName componentName) {
2083 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002084 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2085 Uri.fromParts("package", packageName, null));
2086 startActivity(intent);
2087 }
2088
Patrick Dubroy5539af72010-09-07 15:22:01 -07002089 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2090 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2091 // System applications cannot be installed. For now, show a toast explaining that.
2092 // We may give them the option of disabling apps this way.
2093 int messageId = R.string.uninstall_system_app_text;
2094 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2095 } else {
2096 String packageName = appInfo.componentName.getPackageName();
2097 String className = appInfo.componentName.getClassName();
2098 Intent intent = new Intent(
2099 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2100 startActivity(intent);
2101 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002102 }
2103
Joe Onoratof984e852010-03-25 09:47:45 -07002104 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2106 try {
2107 startActivity(intent);
2108 } catch (ActivityNotFoundException e) {
2109 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002110 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 } catch (SecurityException e) {
2112 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002113 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002115 "or use the exported attribute for this activity. "
2116 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 }
2118 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002119
Romain Guy8e633c52010-03-04 12:51:36 -08002120 void startActivityForResultSafely(Intent intent, int requestCode) {
2121 try {
2122 startActivityForResult(intent, requestCode);
2123 } catch (ActivityNotFoundException e) {
2124 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2125 } catch (SecurityException e) {
2126 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2127 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2128 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2129 "or use the exported attribute for this activity.", e);
2130 }
2131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132
2133 private void handleFolderClick(FolderInfo folderInfo) {
2134 if (!folderInfo.opened) {
2135 // Close any open folder
2136 closeFolder();
2137 // Open the requested folder
2138 openFolder(folderInfo);
2139 } else {
2140 // Find the open folder...
2141 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2142 int folderScreen;
2143 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002144 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002145 // .. and close it
2146 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002147 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148 // Close any folder open on the current screen
2149 closeFolder();
2150 // Pull the folder onto this screen
2151 openFolder(folderInfo);
2152 }
2153 }
2154 }
2155 }
2156
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002157 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002158 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159 * is animated relative to the specified View. If the View is null, no animation
2160 * is played.
2161 *
2162 * @param folderInfo The FolderInfo describing the folder to open.
2163 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002164 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002165 Folder openFolder;
2166
2167 if (folderInfo instanceof UserFolderInfo) {
2168 openFolder = UserFolder.fromXml(this);
2169 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002170 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 } else {
2172 return;
2173 }
2174
Joe Onorato00acb122009-08-04 16:04:30 -04002175 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 openFolder.setLauncher(this);
2177
2178 openFolder.bind(folderInfo);
2179 folderInfo.opened = true;
2180
Winson Chungaafa03c2010-06-11 17:34:16 -07002181 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 openFolder.onOpen();
2184 }
2185
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002187 switch (v.getId()) {
2188 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002189 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002190 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2191 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002192 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002193 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002194 return true;
2195 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002196 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002197 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2198 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002199 showPreviews(v);
2200 }
2201 return true;
2202 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002203 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002204 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2205 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2206 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002207 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002208 return true;
2209 }
2210
Joe Onorato9c1289c2009-08-17 11:03:03 -04002211 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 return false;
2213 }
2214
2215 if (!(v instanceof CellLayout)) {
2216 v = (View) v.getParent();
2217 }
2218
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219
Michael Jurka0280c3b2010-09-17 15:00:07 -07002220 resetAddInfo();
2221 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002223 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 return true;
2225 }
2226
Michael Jurka0280c3b2010-09-17 15:00:07 -07002227 final View itemUnderLongClick = longClickCellInfo.cell;
2228
Winson Chung304dcde2011-01-07 11:17:23 -08002229 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002230 if (itemUnderLongClick == null) {
2231 // User long pressed on empty space
2232 mWorkspace.setAllowLongPress(false);
2233 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2234 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002235 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002236 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 }
2238 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002239 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002241 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2242 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002243 mAddIntersectCellX = longClickCellInfo.cellX;
2244 mAddIntersectCellY = longClickCellInfo.cellY;
2245 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 }
2247 }
2248 }
2249 return true;
2250 }
2251
Romain Guye6b8e2f2009-11-10 11:56:55 -08002252 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002253 private void dismissPreview(final View v) {
2254 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002255 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002256 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2257 public void onDismiss() {
2258 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2259 int count = group.getChildCount();
2260 for (int i = 0; i < count; i++) {
2261 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2262 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002263 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2264 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2265
2266 v.setTag(R.id.workspace, null);
2267 v.setTag(R.id.icon, null);
2268 window.setOnDismissListener(null);
2269 }
2270 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002271 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002272 }
2273 v.setTag(null);
2274 }
2275
Romain Guyf8e6a802009-12-07 17:48:02 -08002276 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002277 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002278 }
2279
Romain Guya6abce82009-11-10 02:54:41 -08002280 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002281 final Resources resources = getResources();
2282 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002283
Romain Guya6abce82009-11-10 02:54:41 -08002284 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002285
Romain Guy9d31e9f2009-11-11 18:54:28 -08002286 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002287
Romain Guyf8e6a802009-12-07 17:48:02 -08002288 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002289 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002290 int extraW = (int) ((r.left + r.right) * max);
2291 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002292
2293 int aW = cell.getWidth() - extraW;
2294 float w = aW / max;
2295
2296 int width = cell.getWidth();
2297 int height = cell.getHeight();
2298 int x = cell.getLeftPadding();
2299 int y = cell.getTopPadding();
2300 width -= (x + cell.getRightPadding());
2301 height -= (y + cell.getBottomPadding());
2302
2303 float scale = w / width;
2304
2305 int count = end - start;
2306
2307 final float sWidth = width * scale;
2308 float sHeight = height * scale;
2309
Romain Guye6b8e2f2009-11-10 11:56:55 -08002310 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002311
Romain Guye6b8e2f2009-11-10 11:56:55 -08002312 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2313 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002314
2315 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002316 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002317 cell = (CellLayout) workspace.getChildAt(i);
2318
Romain Guyf8e6a802009-12-07 17:48:02 -08002319 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002320 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002321
2322 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002323 c.scale(scale, scale);
2324 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002325 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002326
Romain Guy9d31e9f2009-11-11 18:54:28 -08002327 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002328 image.setImageBitmap(bitmap);
2329 image.setTag(i);
2330 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002331 image.setOnFocusChangeListener(handler);
2332 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002333 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002334
2335 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002336 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2337
Winson Chungaafa03c2010-06-11 17:34:16 -07002338 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002339 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002340
2341 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002342 p.setContentView(preview);
2343 p.setWidth((int) (sWidth * count + extraW));
2344 p.setHeight((int) (sHeight + extraH));
2345 p.setAnimationStyle(R.style.AnimationPreview);
2346 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002347 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002348 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002349 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002350
Romain Guye6b8e2f2009-11-10 11:56:55 -08002351 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2352 public void onDismiss() {
2353 dismissPreview(anchor);
2354 }
2355 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002356
2357 anchor.setTag(p);
2358 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002359 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002360 }
2361
Romain Guy9d31e9f2009-11-11 18:54:28 -08002362 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002363 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002364
Romain Guye6b8e2f2009-11-10 11:56:55 -08002365 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002366 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002367 }
2368
2369 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002370 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002371 v.post(this);
2372 }
2373
2374 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002375 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002376 }
2377
2378 public void onFocusChange(View v, boolean hasFocus) {
2379 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002380 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002381 }
Romain Guya6abce82009-11-10 02:54:41 -08002382 }
2383 }
2384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002385 Workspace getWorkspace() {
2386 return mWorkspace;
2387 }
2388
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002389 @Override
2390 protected Dialog onCreateDialog(int id) {
2391 switch (id) {
2392 case DIALOG_CREATE_SHORTCUT:
2393 return new CreateShortcut().createDialog();
2394 case DIALOG_RENAME_FOLDER:
2395 return new RenameFolder().createDialog();
2396 }
2397
2398 return super.onCreateDialog(id);
2399 }
2400
2401 @Override
2402 protected void onPrepareDialog(int id, Dialog dialog) {
2403 switch (id) {
2404 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 break;
2406 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002407 if (mFolderInfo != null) {
2408 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2409 final CharSequence text = mFolderInfo.title;
2410 input.setText(text);
2411 input.setSelection(0, text.length());
2412 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 break;
2414 }
2415 }
2416
2417 void showRenameDialog(FolderInfo info) {
2418 mFolderInfo = info;
2419 mWaitingForResult = true;
2420 showDialog(DIALOG_RENAME_FOLDER);
2421 }
2422
Michael Jurka0280c3b2010-09-17 15:00:07 -07002423 private void showAddDialog(int intersectX, int intersectY) {
2424 resetAddInfo();
2425 mAddIntersectCellX = intersectX;
2426 mAddIntersectCellY = intersectY;
2427 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 mWaitingForResult = true;
2429 showDialog(DIALOG_CREATE_SHORTCUT);
2430 }
2431
Joe Onoratodeb98af2010-02-19 14:59:39 -08002432 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002433 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002434 Bundle bundle = new Bundle();
2435
2436 ArrayList<String> shortcutNames = new ArrayList<String>();
2437 shortcutNames.add(getString(R.string.group_applications));
2438 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2439
2440 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2441 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2442 R.drawable.ic_launcher_application));
2443 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2444
2445 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2446 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002447 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002448 pickIntent.putExtras(bundle);
2449
Joe Onoratodeb98af2010-02-19 14:59:39 -08002450 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002451 }
2452
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 private class RenameFolder {
2454 private EditText mInput;
2455
2456 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002457 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2458 mInput = (EditText) layout.findViewById(R.id.folder_name);
2459
2460 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2461 builder.setIcon(0);
2462 builder.setTitle(getString(R.string.rename_folder_title));
2463 builder.setCancelable(true);
2464 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2465 public void onCancel(DialogInterface dialog) {
2466 cleanup();
2467 }
2468 });
2469 builder.setNegativeButton(getString(R.string.cancel_action),
2470 new Dialog.OnClickListener() {
2471 public void onClick(DialogInterface dialog, int which) {
2472 cleanup();
2473 }
2474 }
2475 );
2476 builder.setPositiveButton(getString(R.string.rename_action),
2477 new Dialog.OnClickListener() {
2478 public void onClick(DialogInterface dialog, int which) {
2479 changeFolderName();
2480 }
2481 }
2482 );
2483 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002484
2485 final AlertDialog dialog = builder.create();
2486 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2487 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002488 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002489 mInput.requestFocus();
2490 InputMethodManager inputManager = (InputMethodManager)
2491 getSystemService(Context.INPUT_METHOD_SERVICE);
2492 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002493 }
2494 });
2495
2496 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 }
2498
2499 private void changeFolderName() {
2500 final String name = mInput.getText().toString();
2501 if (!TextUtils.isEmpty(name)) {
2502 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002503 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002504 mFolderInfo.title = name;
2505 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2506
Joe Onorato9c1289c2009-08-17 11:03:03 -04002507 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002508 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002509 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002510 } else {
2511 final FolderIcon folderIcon = (FolderIcon)
2512 mWorkspace.getViewForTag(mFolderInfo);
2513 if (folderIcon != null) {
2514 folderIcon.setText(name);
2515 getWorkspace().requestLayout();
2516 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002517 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002518 mWorkspaceLoading = true;
2519 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 }
2521 }
2522 }
2523 cleanup();
2524 }
2525
2526 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002527 dismissDialog(DIALOG_RENAME_FOLDER);
2528 mWaitingForResult = false;
2529 mFolderInfo = null;
2530 }
2531 }
2532
Daniel Sandler843e8602010-06-07 14:59:01 -04002533 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2534 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002535 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002536 }
2537
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002538 // AllAppsView.Watcher
2539 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002540 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2541 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002542 mWorkspace.setVisibility(View.GONE);
2543 }
2544 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002545
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002546 private void showToolbarButton(View button) {
2547 button.setAlpha(1.0f);
2548 button.setVisibility(View.VISIBLE);
2549 button.setFocusable(true);
2550 button.setClickable(true);
2551 }
2552
2553 private void hideToolbarButton(View button) {
2554 button.setAlpha(0.0f);
2555 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2556 button.setVisibility(View.INVISIBLE);
2557 button.setFocusable(false);
2558 button.setClickable(false);
2559 }
2560
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002561 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002562 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002563 *
2564 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2565 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002566 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002567 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002568 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002569 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002570 final boolean showing = show;
2571 final boolean hiding = !show;
2572
2573 final int duration = show ?
2574 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2575 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2576
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002577 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002578 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2579 anim.setDuration(duration);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002580 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002581 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002582 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002583 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002584 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002585 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002586 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002587 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002588 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002589 });
2590 seq.play(anim);
2591 } else {
2592 if (showing) {
2593 showToolbarButton(view);
2594 } else {
2595 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002596 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002597 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002598 }
2599
2600 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002601 * Show/hide the appropriate toolbar buttons for newState.
2602 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002603 *
2604 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002605 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2606 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002607 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002608 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002609 final View searchButton = findViewById(R.id.search_button_cluster);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002610 final View allAppsButton = findViewById(R.id.all_apps_button);
Michael Jurka800242b2010-12-16 11:39:26 -08002611 final View divider = findViewById(R.id.divider);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002612 final View configureButton = findViewById(R.id.configure_button);
2613
2614 switch (newState) {
2615 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002616 hideOrShowToolbarButton(true, searchButton, showSeq);
2617 hideOrShowToolbarButton(true, allAppsButton, showSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002618 hideOrShowToolbarButton(true, divider, showSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002619 hideOrShowToolbarButton(true, configureButton, showSeq);
Michael Jurkab8e14472010-12-20 16:06:10 -08002620 mDeleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002621 mDeleteZone.setDragAndDropEnabled(true);
2622 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002623 break;
2624 case ALL_APPS:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002625 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002626 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002627 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002628 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002629 mDeleteZone.setDragAndDropEnabled(false);
2630 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002631 break;
2632 case CUSTOMIZE:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002633 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002634 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002635 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002636 hideOrShowToolbarButton(false, configureButton, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002637 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002638 break;
2639 }
2640 }
2641
2642 /**
2643 * Helper method for the cameraZoomIn/cameraZoomOut animations
2644 * @param view The view being animated
2645 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2646 * @param scaleFactor The scale factor used for the zoom
2647 */
2648 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2649 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002650
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002651 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002652 // Set pivotY so that at the starting zoom factor, the view is partially
2653 // visible. Modifying initialHeightFactor changes how much of the view is
2654 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002655 if (state == State.ALL_APPS) {
Adam Cohenf16e5712011-01-13 13:31:45 -08002656 final float initialHeightFactor = 0.165f;
2657 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002658 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002659 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002660 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002661 }
2662 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002663
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002664 /**
2665 * Zoom the camera out from the workspace to reveal 'toView'.
2666 * Assumes that the view to show is anchored at either the very top or very bottom
2667 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002668 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002669 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002670 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002671 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002672 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002673
2674 final int duration = toAllApps ?
2675 res.getInteger(R.integer.config_allAppsZoomInTime) :
2676 res.getInteger(R.integer.config_customizeZoomInTime);
2677
2678 final float scale = toAllApps ?
2679 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2680 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2681
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002682 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002683
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002684 setPivotsForZoom(toView, toState, scale);
2685
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002686 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002687 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Adam Cohenf16e5712011-01-13 13:31:45 -08002688 toView.setAlpha(0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002689 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002690 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002691 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002692
2693 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002694 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2695 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2696 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
Adam Cohenf16e5712011-01-13 13:31:45 -08002697
Chet Haase472b2812010-10-14 07:02:04 -07002698 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002699
Adam Cohenf16e5712011-01-13 13:31:45 -08002700 if (toAllApps) {
2701 ObjectAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2702 PropertyValuesHolder.ofFloat("alpha", 1.0f));
2703 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2704 alphaAnim.setDuration(duration);
2705 alphaAnim.start();
2706 }
2707
Adam Cohen61033d32010-11-15 18:29:44 -08002708 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka8edd75c2010-12-17 20:15:06 -08002709 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002710 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002711 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002712 // Prepare the position
2713 toView.setTranslationX(0.0f);
2714 toView.setTranslationY(0.0f);
2715 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002716 if (!toAllApps) {
2717 toView.setAlpha(1.0f);
2718 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002719 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002720 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002721 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002722 // If we don't set the final scale values here, if this animation is cancelled
2723 // it will have the wrong scale value and subsequent cameraPan animations will
2724 // not fix that
2725 toView.setScaleX(1.0f);
2726 toView.setScaleY(1.0f);
2727 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002728 });
2729
Chet Haaseb1254a62010-09-07 13:35:00 -07002730 AnimatorSet toolbarHideAnim = new AnimatorSet();
2731 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002732 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2733
2734 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002735 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002736
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002737 if (mStateAnimation != null) mStateAnimation.cancel();
2738 mStateAnimation = new AnimatorSet();
2739 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2740 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002741
2742 // Show the new toolbar buttons just as the main animation is ending
2743 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002744 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2745 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002746 } else {
2747 toView.setTranslationX(0.0f);
2748 toView.setTranslationY(0.0f);
2749 toView.setScaleX(1.0f);
2750 toView.setScaleY(1.0f);
2751 toView.setVisibility(View.VISIBLE);
2752 hideAndShowToolbarButtons(toState, null, null);
2753 }
Michael Jurkac5b262c2011-01-12 20:24:50 -08002754 mWorkspace.setVerticalWallpaperOffset(toAllApps ?
2755 Workspace.WallpaperVerticalOffset.TOP : Workspace.WallpaperVerticalOffset.BOTTOM);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002756 }
2757
2758 /**
2759 * Zoom the camera back into the workspace, hiding 'fromView'.
2760 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002761 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2762 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002763 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002764 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002765 cameraZoomIn(fromState, animated, false);
2766 }
2767
2768 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002769 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002770 final boolean fromAllApps = (fromState == State.ALL_APPS);
2771
2772 int duration = fromAllApps ?
2773 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2774 res.getInteger(R.integer.config_customizeZoomOutTime);
2775
2776 float scaleFactor = fromAllApps ?
2777 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2778 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2779
2780 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002781
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002782 mCustomizePagedView.endChoiceMode();
2783 mAllAppsPagedView.endChoiceMode();
2784
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002785 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002786
Michael Jurkad3ef3062010-11-23 16:23:58 -08002787 if (!springLoaded) {
2788 mWorkspace.unshrink(animated);
2789 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002790
2791 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002792 if (mStateAnimation != null) mStateAnimation.cancel();
2793 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002794 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2795 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2796 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2797 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002798 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2799
2800 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2801 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2802 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Adam Cohenf16e5712011-01-13 13:31:45 -08002803 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka8edd75c2010-12-17 20:15:06 -08002804 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002805 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002806 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002807 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002808 }
2809 });
2810
Chet Haaseb1254a62010-09-07 13:35:00 -07002811 AnimatorSet toolbarHideAnim = new AnimatorSet();
2812 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002813 if (!springLoaded) {
2814 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2815 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002816
Adam Cohen61033d32010-11-15 18:29:44 -08002817 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002818
2819 // Show the new toolbar buttons at the very end of the whole animation
2820 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2821 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002822 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2823 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002824 } else {
2825 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002826 if (!springLoaded) {
2827 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2828 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002829 }
Michael Jurkac5b262c2011-01-12 20:24:50 -08002830 mWorkspace.setVerticalWallpaperOffset(Workspace.WallpaperVerticalOffset.MIDDLE);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002831 }
2832
2833 /**
2834 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2835 * This is the transition used on xlarge screens to go between all apps and
2836 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002837 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2838 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2839 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002840 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002841 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002842 final Resources res = getResources();
2843 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002844 final int workspaceHeight = mWorkspace.getHeight();
2845
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002846 final boolean fromAllApps = (fromState == State.ALL_APPS);
2847 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2848 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002849
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002850 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2851 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2852 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2853 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002854
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002855 mCustomizePagedView.endChoiceMode();
2856 mAllAppsPagedView.endChoiceMode();
2857
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002858 if (toState == State.ALL_APPS) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002859 mWorkspace.shrink(Workspace.ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002860 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002861 mWorkspace.shrink(Workspace.ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002862 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002863
2864 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002865 if (mStateAnimation != null) mStateAnimation.cancel();
2866 mStateAnimation = new AnimatorSet();
Michael Jurka8edd75c2010-12-17 20:15:06 -08002867 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002868 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002869 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002870 toView.setVisibility(View.VISIBLE);
2871 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002872 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002873 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002874 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002875 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002876 fromView.setVisibility(View.GONE);
2877 }
2878 });
2879
Chet Haaseb1254a62010-09-07 13:35:00 -07002880 AnimatorSet toolbarHideAnim = new AnimatorSet();
2881 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002882 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2883
Chet Haase472b2812010-10-14 07:02:04 -07002884 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2885 fromViewStartY, fromViewEndY);
2886 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002887 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2888 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2889 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2890 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2891 );
Chet Haase472b2812010-10-14 07:02:04 -07002892 fromAnim.setDuration(duration);
2893 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002894
2895 // Show the new toolbar buttons just as the main animation is ending
2896 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002897 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2898 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002899 } else {
2900 fromView.setY(fromViewEndY);
2901 fromView.setVisibility(View.GONE);
2902 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002903 toView.setScaleX(1.0f);
2904 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002905 toView.setVisibility(View.VISIBLE);
2906 hideAndShowToolbarButtons(toState, null, null);
2907 }
Michael Jurkac5b262c2011-01-12 20:24:50 -08002908 mWorkspace.setVerticalWallpaperOffset((toState == State.ALL_APPS) ?
2909 Workspace.WallpaperVerticalOffset.TOP : Workspace.WallpaperVerticalOffset.BOTTOM);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002910 }
2911
2912 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002913 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002914 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002915 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002916
Michael Jurka79212d82010-07-30 16:36:20 -07002917 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002918 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002919 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002920 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002921 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002922 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002923 } else {
2924 mAllAppsGrid.zoom(1.0f, animated);
2925 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002926
Romain Guyc16fea72010-03-12 17:17:56 -08002927 ((View) mAllAppsGrid).setFocusable(true);
2928 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002929
Joe Onorato7c312c12009-08-13 21:36:53 -07002930 // TODO: fade these two too
2931 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002932
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002933 // Change the state *after* we've called all the transition code
2934 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002935
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002936 // send an accessibility event to announce the context change
2937 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2938 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002939
2940 void showWorkspace(boolean animated) {
2941 showWorkspace(animated, null);
2942 }
2943
2944 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002945 if (layout != null) {
2946 // always animated, but that's ok since we never specify a layout and
2947 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002948 mWorkspace.unshrink(layout);
2949 } else {
2950 mWorkspace.unshrink(animated);
2951 }
2952 if (mState == State.ALL_APPS) {
2953 closeAllApps(animated);
2954 } else if (mState == State.CUSTOMIZE) {
2955 hideCustomizationDrawer(animated);
2956 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002957
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002958 // Change the state *after* we've called all the transition code
2959 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002960
2961 // send an accessibility event to announce the context change
2962 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002963 }
2964
Michael Jurkad3ef3062010-11-23 16:23:58 -08002965 void enterSpringLoadedDragMode(CellLayout layout) {
2966 mWorkspace.enterSpringLoadedDragMode(layout);
2967 if (mState == State.ALL_APPS) {
2968 cameraZoomIn(State.ALL_APPS, true, true);
2969 mState = State.ALL_APPS_SPRING_LOADED;
2970 } else if (mState == State.CUSTOMIZE) {
2971 cameraZoomIn(State.CUSTOMIZE, true, true);
2972 mState = State.CUSTOMIZE_SPRING_LOADED;
2973 }/* else {
2974 // we're already in spring loaded mode; don't do anything
2975 }*/
2976 }
2977
2978 void exitSpringLoadedDragMode() {
2979 if (mState == State.ALL_APPS_SPRING_LOADED) {
2980 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2981 cameraZoomOut(State.ALL_APPS, true);
2982 mState = State.ALL_APPS;
2983 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2984 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2985 cameraZoomOut(State.CUSTOMIZE, true);
2986 mState = State.CUSTOMIZE;
2987 }/* else {
2988 // we're not in spring loaded mode; don't do anything
2989 }*/
2990 }
2991
Joe Onoratob2061212009-11-24 16:13:54 -05002992 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002993 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002994 * - Home from workspace
2995 * - from center screen
2996 * - from other screens
2997 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002998 * - from center screen
2999 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003000 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003001 * - from center screen
3002 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003003 * - Launch app from workspace and quit
3004 * - with back
3005 * - with home
3006 * - Launch app from all apps and quit
3007 * - with back
3008 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003009 * - Go to a screen that's not the default, then all
3010 * apps, and launch and app, and go back
3011 * - with back
3012 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003013 * - On workspace, long press power and go back
3014 * - with back
3015 * - with home
3016 * - On all apps, long press power and go back
3017 * - with back
3018 * - with home
3019 * - On workspace, power off
3020 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003021 * - Launch an app and turn off the screen while in that app
3022 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003023 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003024 * - From all apps
3025 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003026 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3027 * - From all apps
3028 * - From the center workspace
3029 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003030 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003031 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003032 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003033 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003034 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003035 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003036 } else {
3037 mAllAppsGrid.zoom(0.0f, animated);
3038 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003039 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003040 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003041 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003042 }
3043
Joe Onorato7c312c12009-08-13 21:36:53 -07003044 void lockAllApps() {
3045 // TODO
3046 }
3047
3048 void unlockAllApps() {
3049 // TODO
3050 }
3051
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003052 // Show the customization drawer (only exists in x-large configuration)
3053 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003054 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003055 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003056 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003057 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003058 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003059 // Change the state *after* we've called all the transition code
3060 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003061 }
3062
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003063 // Hide the customization drawer (only exists in x-large configuration)
3064 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003065 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003066 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003067 }
3068 }
3069
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003070 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
3071 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3072 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07003073 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003074 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003075
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003076 void onWorkspaceClick(CellLayout layout) {
3077 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003078 }
3079
Michael Jurka4ef207b2010-11-29 17:05:45 -08003080 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003081 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003082 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3083 ImageView button = (ImageView) findViewById(buttonId);
3084 Drawable toolbarIcon = null;
3085 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003086 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003087 // Look for the toolbar icon specified in the activity meta-data
3088 Bundle metaData = packageManager.getActivityInfo(
3089 activityName, PackageManager.GET_META_DATA).metaData;
3090 if (metaData != null) {
3091 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3092 if (iconResId != 0) {
3093 Resources res = packageManager.getResourcesForActivity(activityName);
3094 toolbarIcon = res.getDrawable(iconResId);
3095 }
3096 }
3097 } catch (NameNotFoundException e) {
3098 // Do nothing
3099 }
3100 // If we were unable to find the icon via the meta-data, use a generic one
3101 if (toolbarIcon == null) {
3102 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003103 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003104 } else {
3105 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003106 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003107 }
3108 }
3109
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003110 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003111 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003112 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003113 }
3114
Michael Jurka0423dcf2010-10-05 14:56:18 -07003115 private void updateGlobalSearchIcon() {
3116 if (LauncherApplication.isScreenXLarge()) {
3117 final SearchManager searchManager =
3118 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3119 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003120 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003121 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003122 R.id.search_button, activityName, R.drawable.search_button_generic);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003123 } else {
3124 findViewById(R.id.search_button).setVisibility(View.GONE);
3125 }
3126 }
3127 }
3128
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003129 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003130 updateButtonWithDrawable(R.id.search_button, d);
3131 }
3132
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003133 private void updateVoiceSearchIcon() {
3134 if (LauncherApplication.isScreenXLarge()) {
3135 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3136 ComponentName activityName = intent.resolveActivity(getPackageManager());
3137 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003138 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003139 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3140 } else {
3141 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003142 }
3143 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003144 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003145
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003146 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003147 updateButtonWithDrawable(R.id.voice_button, d);
3148 }
3149
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003150 /**
3151 * Sets the app market icon (shown when all apps is visible on x-large screens)
3152 */
3153 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003154 if (LauncherApplication.isScreenXLarge()) {
3155 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3156 // Find the app market activity by resolving an intent.
3157 // (If multiple app markets are installed, it will return the ResolverActivity.)
3158 ComponentName activityName = intent.resolveActivity(getPackageManager());
3159 if (activityName != null) {
3160 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003161 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003162 R.id.market_button, activityName, R.drawable.app_market_generic);
3163 }
3164 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003165 }
3166
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003167 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003168 updateButtonWithDrawable(R.id.market_button, d);
3169 }
3170
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003171 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003172 * Displays the shortcut creation dialog and launches, if necessary, the
3173 * appropriate activity.
3174 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003175 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003176 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3177 DialogInterface.OnShowListener {
3178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003179 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003180
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003181 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003182 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003184 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3185 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003186 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003187
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003188 builder.setInverseBackgroundForced(true);
3189
3190 AlertDialog dialog = builder.create();
3191 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003192 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003193 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003194
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003195 return dialog;
3196 }
3197
3198 public void onCancel(DialogInterface dialog) {
3199 mWaitingForResult = false;
3200 cleanup();
3201 }
3202
Romain Guycbb89e42009-06-08 15:52:54 -07003203 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003204 }
3205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003206 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003207 try {
3208 dismissDialog(DIALOG_CREATE_SHORTCUT);
3209 } catch (Exception e) {
3210 // An exception is thrown if the dialog is not visible, which is fine
3211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003212 }
3213
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003214 /**
3215 * Handle the action clicked in the "Add to home" dialog.
3216 */
3217 public void onClick(DialogInterface dialog, int which) {
3218 Resources res = getResources();
3219 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003220
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003221 switch (which) {
3222 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003223 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003224 break;
3225 }
Romain Guycbb89e42009-06-08 15:52:54 -07003226
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003227 case AddAdapter.ITEM_APPWIDGET: {
3228 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003229
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003230 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3231 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003232 // start the pick activity
3233 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3234 break;
3235 }
Romain Guycbb89e42009-06-08 15:52:54 -07003236
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003237 case AddAdapter.ITEM_LIVE_FOLDER: {
3238 // Insert extra item to handle inserting folder
3239 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003240
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003241 ArrayList<String> shortcutNames = new ArrayList<String>();
3242 shortcutNames.add(res.getString(R.string.group_folder));
3243 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003244
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003245 ArrayList<ShortcutIconResource> shortcutIcons =
3246 new ArrayList<ShortcutIconResource>();
3247 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3248 R.drawable.ic_launcher_folder));
3249 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3250
3251 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3252 pickIntent.putExtra(Intent.EXTRA_INTENT,
3253 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3254 pickIntent.putExtra(Intent.EXTRA_TITLE,
3255 getText(R.string.title_select_live_folder));
3256 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003257
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003258 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3259 break;
3260 }
3261
3262 case AddAdapter.ITEM_WALLPAPER: {
3263 startWallpaper();
3264 break;
3265 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003266 }
3267 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003268
3269 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003270 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003271 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003272 }
3273
3274 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003275 * Receives notifications when applications are added/removed.
3276 */
3277 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3278 @Override
3279 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003280 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003281 String reason = intent.getStringExtra("reason");
3282 if (!"homekey".equals(reason)) {
3283 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003284 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003285 animate = false;
3286 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003287 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003288 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003289 }
3290 }
3291
3292 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003293 * Receives notifications whenever the appwidgets are reset.
3294 */
3295 private class AppWidgetResetObserver extends ContentObserver {
3296 public AppWidgetResetObserver() {
3297 super(new Handler());
3298 }
3299
3300 @Override
3301 public void onChange(boolean selfChange) {
3302 onAppWidgetReset();
3303 }
3304 }
3305
3306 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003307 * If the activity is currently paused, signal that we need to re-run the loader
3308 * in onResume.
3309 *
3310 * This needs to be called from incoming places where resources might have been loaded
3311 * while we are paused. That is becaues the Configuration might be wrong
3312 * when we're not running, and if it comes back to what it was when we
3313 * were paused, we are not restarted.
3314 *
3315 * Implementation of the method from LauncherModel.Callbacks.
3316 *
3317 * @return true if we are currently paused. The caller might be able to
3318 * skip some work in that case since we will come back again.
3319 */
3320 public boolean setLoadOnResume() {
3321 if (mPaused) {
3322 Log.i(TAG, "setLoadOnResume");
3323 mOnResumeNeedsLoad = true;
3324 return true;
3325 } else {
3326 return false;
3327 }
3328 }
3329
3330 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003331 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003332 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003333 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003334 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003335 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003336 } else {
3337 return SCREEN_COUNT / 2;
3338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003339 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003340
Winson Chunga12a2502010-12-20 14:41:35 -08003341 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003342 mAllAppsPagedView = view;
3343 }
3344
Joe Onorato9c1289c2009-08-17 11:03:03 -04003345 /**
3346 * Refreshes the shortcuts shown on the workspace.
3347 *
3348 * Implementation of the method from LauncherModel.Callbacks.
3349 */
3350 public void startBinding() {
3351 final Workspace workspace = mWorkspace;
3352 int count = workspace.getChildCount();
3353 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003354 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003355 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3356 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003357
Joe Onorato9c1289c2009-08-17 11:03:03 -04003358 if (DEBUG_USER_INTERFACE) {
3359 android.widget.Button finishButton = new android.widget.Button(this);
3360 finishButton.setText("Finish");
3361 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3362
3363 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3364 public void onClick(View v) {
3365 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003366 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003367 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003368 }
3369 }
3370
3371 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003372 * Bind the items start-end from the list.
3373 *
3374 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003375 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003376 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3377
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003378 setLoadOnResume();
3379
Joe Onorato9c1289c2009-08-17 11:03:03 -04003380 final Workspace workspace = mWorkspace;
3381
3382 for (int i=start; i<end; i++) {
3383 final ItemInfo item = shortcuts.get(i);
3384 mDesktopItems.add(item);
3385 switch (item.itemType) {
3386 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3387 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003388 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003389 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3390 false);
3391 break;
3392 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3393 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003394 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003395 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003396 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3397 false);
3398 break;
3399 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3400 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3401 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003402 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003403 (LiveFolderInfo) item);
3404 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3405 false);
3406 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003407 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003408 }
3409
Joe Onorato9c1289c2009-08-17 11:03:03 -04003410 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003411 }
3412
Joe Onorato9c1289c2009-08-17 11:03:03 -04003413 /**
3414 * Implementation of the method from LauncherModel.Callbacks.
3415 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003416 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003417 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003418 sFolders.clear();
3419 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003420 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003421
3422 /**
3423 * Add the views for a widget to the workspace.
3424 *
3425 * Implementation of the method from LauncherModel.Callbacks.
3426 */
3427 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003428 setLoadOnResume();
3429
Daniel Sandler843e8602010-06-07 14:59:01 -04003430 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3431 if (DEBUG_WIDGETS) {
3432 Log.d(TAG, "bindAppWidget: " + item);
3433 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003434 final Workspace workspace = mWorkspace;
3435
3436 final int appWidgetId = item.appWidgetId;
3437 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003438 if (DEBUG_WIDGETS) {
3439 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3440 }
3441
Joe Onorato9c1289c2009-08-17 11:03:03 -04003442 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3443
Joe Onorato9c1289c2009-08-17 11:03:03 -04003444 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3445 item.hostView.setTag(item);
3446
3447 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3448 item.cellY, item.spanX, item.spanY, false);
3449
Adam Cohended9f8d2010-11-03 13:25:16 -07003450 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3451
Joe Onorato9c1289c2009-08-17 11:03:03 -04003452 workspace.requestLayout();
3453
3454 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003455
3456 if (DEBUG_WIDGETS) {
3457 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3458 + (SystemClock.uptimeMillis()-start) + "ms");
3459 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003460 }
3461
3462 /**
3463 * Callback saying that there aren't any more items to bind.
3464 *
3465 * Implementation of the method from LauncherModel.Callbacks.
3466 */
3467 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003468 setLoadOnResume();
3469
Joe Onorato9c1289c2009-08-17 11:03:03 -04003470 if (mSavedState != null) {
3471 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003472 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003473 }
3474
3475 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3476 if (userFolders != null) {
3477 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003478 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003479 if (info != null) {
3480 openFolder(info);
3481 }
3482 }
3483 final Folder openFolder = mWorkspace.getOpenFolder();
3484 if (openFolder != null) {
3485 openFolder.requestFocus();
3486 }
3487 }
3488
Joe Onorato9c1289c2009-08-17 11:03:03 -04003489 mSavedState = null;
3490 }
3491
3492 if (mSavedInstanceState != null) {
3493 super.onRestoreInstanceState(mSavedInstanceState);
3494 mSavedInstanceState = null;
3495 }
3496
Joe Onorato9c1289c2009-08-17 11:03:03 -04003497 mWorkspaceLoading = false;
3498 }
3499
3500 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003501 * Updates the icons on the launcher that are affected by changes to the package list
3502 * on the device.
3503 */
3504 private void updateIconsAffectedByPackageManagerChanges() {
3505 updateAppMarketIcon();
3506 updateGlobalSearchIcon();
3507 updateVoiceSearchIcon();
3508 }
3509
3510 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003511 * Add the icons for all apps.
3512 *
3513 * Implementation of the method from LauncherModel.Callbacks.
3514 */
3515 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003516 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003517 if (mCustomizePagedView != null) {
3518 mCustomizePagedView.setApps(apps);
3519 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003520 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003521 }
3522
3523 /**
3524 * A package was installed.
3525 *
3526 * Implementation of the method from LauncherModel.Callbacks.
3527 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003528 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003529 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003530 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003531 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003532 if (mCustomizePagedView != null) {
3533 mCustomizePagedView.addApps(apps);
3534 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003535 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003536 }
3537
3538 /**
3539 * A package was updated.
3540 *
3541 * Implementation of the method from LauncherModel.Callbacks.
3542 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003543 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003544 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003545 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003546 mWorkspace.updateShortcuts(apps);
3547 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003548 if (mCustomizePagedView != null) {
3549 mCustomizePagedView.updateApps(apps);
3550 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003551 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003552 }
3553
3554 /**
3555 * A package was uninstalled.
3556 *
3557 * Implementation of the method from LauncherModel.Callbacks.
3558 */
Joe Onorato36115782010-06-17 13:28:48 -04003559 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003560 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003561 if (permanent) {
3562 mWorkspace.removeItems(apps);
3563 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003564 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003565 if (mCustomizePagedView != null) {
3566 mCustomizePagedView.removeApps(apps);
3567 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003568 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 }
Joe Onoratobe386092009-11-17 17:32:16 -08003570
3571 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003572 * A number of packages were updated.
3573 */
3574 public void bindPackagesUpdated() {
3575 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003576 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003577 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003578 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003579 }
3580
Winson Chung400438b2011-01-16 17:53:48 -08003581 private int mapConfigurationOriActivityInfoOri(int configOri) {
3582 final Display d = getWindowManager().getDefaultDisplay();
3583 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3584 switch (d.getRotation()) {
3585 case Surface.ROTATION_0:
3586 case Surface.ROTATION_180:
3587 // We are currently in the same basic orientation as the natural orientation
3588 naturalOri = configOri;
3589 break;
3590 case Surface.ROTATION_90:
3591 case Surface.ROTATION_270:
3592 // We are currently in the other basic orientation to the natural orientation
3593 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3594 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3595 break;
3596 }
3597
3598 int[] oriMap = {
3599 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3600 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3601 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3602 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3603 };
3604 // Since the map starts at portrait, we need to offset if this device's natural orientation
3605 // is landscape.
3606 int indexOffset = 0;
3607 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3608 indexOffset = 1;
3609 }
3610 return oriMap[(d.getRotation() + indexOffset) % 4];
3611 }
3612 public void lockScreenOrientation() {
3613 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3614 .getConfiguration().orientation));
3615 }
3616 public void unlockScreenOrientation() {
3617 mHandler.postDelayed(new Runnable() {
3618 public void run() {
3619 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3620 }
3621 }, mRestoreScreenOrientationDelay);
3622 }
3623
Winson Chung80baf5a2010-08-09 16:03:15 -07003624 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003625 * Prints out out state for debugging.
3626 */
3627 public void dumpState() {
3628 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003629 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003630 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3631 Log.d(TAG, "mRestoring=" + mRestoring);
3632 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3633 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3634 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003635 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003636 mModel.dumpState();
3637 mAllAppsGrid.dumpState();
3638 Log.d(TAG, "END launcher2 dump state");
3639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003640}