blob: 9b5dac71a0512429a7c5da5db3e0bc6954968737 [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;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001528
1529 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 }
1531
1532 @Override
1533 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001534 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 super.startActivityForResult(intent, requestCode);
1536 }
1537
1538 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001539 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001541
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001542 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001543
Karl Rosaen138a0412009-04-23 19:00:21 -07001544 if (initialQuery == null) {
1545 // Use any text typed in the launcher as the initial query
1546 initialQuery = getTypedText();
1547 clearTypedText();
1548 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 if (appSearchData == null) {
1550 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001551 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 }
Romain Guycbb89e42009-06-08 15:52:54 -07001553
Karl Rosaen138a0412009-04-23 19:00:21 -07001554 final SearchManager searchManager =
1555 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001556 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001557 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 }
1559
1560 @Override
1561 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001562 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001563 return false;
1564 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565
1566 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001567
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1569 .setIcon(android.R.drawable.ic_menu_add)
1570 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001571 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1572 .setIcon(android.R.drawable.ic_menu_manage)
1573 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001574 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 .setIcon(android.R.drawable.ic_menu_gallery)
1576 .setAlphabeticShortcut('W');
1577 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1578 .setIcon(android.R.drawable.ic_search_category_default)
1579 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1580 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1581 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1582 .setAlphabeticShortcut('N');
1583
1584 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001585 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1586 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587
1588 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1589 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1590 .setIntent(settings);
1591
1592 return true;
1593 }
1594
1595 @Override
1596 public boolean onPrepareOptionsMenu(Menu menu) {
1597 super.onPrepareOptionsMenu(menu);
1598
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001599 // If all apps is animating, don't show the menu, because we don't know
1600 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001601 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001602 return false;
1603 }
1604
1605 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001606 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001607 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1608 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1609
1610 // Disable add if the workspace is full.
1611 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001612 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1613 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001614 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615
1616 return true;
1617 }
1618
1619 @Override
1620 public boolean onOptionsItemSelected(MenuItem item) {
1621 switch (item.getItemId()) {
1622 case MENU_ADD:
1623 addItems();
1624 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001625 case MENU_MANAGE_APPS:
1626 manageApps();
1627 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 case MENU_WALLPAPER_SETTINGS:
1629 startWallpaper();
1630 return true;
1631 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001632 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 return true;
1634 case MENU_NOTIFICATIONS:
1635 showNotifications();
1636 return true;
1637 }
1638
1639 return super.onOptionsItemSelected(item);
1640 }
Romain Guycbb89e42009-06-08 15:52:54 -07001641
Karl Rosaen138a0412009-04-23 19:00:21 -07001642 /**
1643 * Indicates that we want global search for this activity by setting the globalSearch
1644 * argument for {@link #startSearch} to true.
1645 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001647 @Override
1648 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001649 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001650 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001651 }
1652
Joe Onorato9c1289c2009-08-17 11:03:03 -04001653 public boolean isWorkspaceLocked() {
1654 return mWorkspaceLoading || mWaitingForResult;
1655 }
1656
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001658 if (LauncherApplication.isScreenXLarge()) {
1659 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001660 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001661 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001662 }
1663 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001664 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001665 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001666 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 }
1668
Michael Jurka0280c3b2010-09-17 15:00:07 -07001669 private void resetAddInfo() {
1670 mAddScreen = -1;
1671 mAddIntersectCellX = -1;
1672 mAddIntersectCellY = -1;
1673 mAddDropPosition = null;
1674 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001675
Winson Chung55cef262010-10-28 14:14:18 -07001676 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001677 resetAddInfo();
1678 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001679 mAddDropPosition = position;
1680
Michael Jurkaaf442092010-06-10 17:01:57 -07001681 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001682 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1683 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001684 }
1685
Winson Chung7ad01412010-09-27 11:33:03 -07001686 private void manageApps() {
1687 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1688 }
1689
Michael Jurkaaf442092010-06-10 17:01:57 -07001690 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001691 // TODO: catch bad widget exception when sent
1692 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001693 // TODO: Is this log message meaningful?
1694 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001695 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001696 }
1697
Winson Chung55cef262010-10-28 14:14:18 -07001698 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001699 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700
Bjorn Bringert7984c942009-12-09 15:38:25 +00001701 if (appWidget.configure != null) {
1702 // Launch over to configure widget, if needed
1703 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1704 intent.setComponent(appWidget.configure);
1705 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001706 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001707 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1708 intent.putExtra(
1709 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1710 info.mimeType);
1711
1712 final String mimeType = info.mimeType;
1713 final ClipData clipData = (ClipData) info.configurationData;
1714 final ClipDescription clipDesc = clipData.getDescription();
1715 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1716 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001717 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001718 final CharSequence stringData = item.getText();
1719 final Uri uriData = item.getUri();
1720 final Intent intentData = item.getIntent();
1721 final String key =
1722 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1723 if (uriData != null) {
1724 intent.putExtra(key, uriData);
1725 } else if (intentData != null) {
1726 intent.putExtra(key, intentData);
1727 } else if (stringData != null) {
1728 intent.putExtra(key, stringData);
1729 }
1730 break;
1731 }
1732 }
1733 }
Winson Chung55cef262010-10-28 14:14:18 -07001734 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001735
Romain Guy8e633c52010-03-04 12:51:36 -08001736 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001738 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001739 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 }
1741 }
Romain Guycbb89e42009-06-08 15:52:54 -07001742
Michael Jurka0280c3b2010-09-17 15:00:07 -07001743 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1744 resetAddInfo();
1745 mAddScreen = screen;
1746 mAddDropPosition = position;
1747
1748 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1749 createShortcutIntent.setComponent(componentName);
1750 processShortcut(createShortcutIntent);
1751 }
1752
Joe Onoratodeb98af2010-02-19 14:59:39 -08001753 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001754 // Handle case where user selected "Applications"
1755 String applicationName = getResources().getString(R.string.group_applications);
1756 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001757
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001758 if (applicationName != null && applicationName.equals(shortcutName)) {
1759 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1760 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001761
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001762 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1763 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001764 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001765 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001766 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001767 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001768 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 }
1770
Winson Chung24ab2f12010-09-16 14:10:47 -07001771 void processWallpaper(Intent intent) {
1772 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1773 }
1774
Michael Jurka0280c3b2010-09-17 15:00:07 -07001775 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1776 resetAddInfo();
1777 mAddScreen = screen;
1778 mAddDropPosition = position;
1779
1780 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1781 createFolderIntent.setComponent(componentName);
1782
1783 addLiveFolder(createFolderIntent);
1784 }
1785
1786 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001787 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001788 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001789 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001790
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001791 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001792 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001793 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001794 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001795 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 }
1797
Michael Jurka0280c3b2010-09-17 15:00:07 -07001798 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 UserFolderInfo folderInfo = new UserFolderInfo();
1800 folderInfo.title = getText(R.string.folder_name);
1801
Michael Jurka0280c3b2010-09-17 15:00:07 -07001802 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1803 final int[] cellXY = mTmpAddItemCellCoordinates;
1804 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1805 showOutOfSpaceMessage();
1806 return;
1807 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808
1809 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001810 LauncherModel.addItemToDatabase(this, folderInfo,
1811 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001812 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001813 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814
1815 // Create the view
1816 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001817 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1818 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001819 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 }
Romain Guycbb89e42009-06-08 15:52:54 -07001821
Joe Onorato9c1289c2009-08-17 11:03:03 -04001822 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001823 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001824 }
1825
Michael Jurka28750fb2010-09-24 17:43:49 -07001826 private void completeAddLiveFolder(
1827 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001828 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1829 final int[] cellXY = mTmpAddItemCellCoordinates;
1830 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1831 showOutOfSpaceMessage();
1832 return;
1833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834
Michael Jurka0280c3b2010-09-17 15:00:07 -07001835 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836
1837 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001839 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001840 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 }
1842 }
1843
1844 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001845 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846
1847 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1848 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1849
1850 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 Intent.ShortcutIconResource iconResource = null;
1852
1853 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1854 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1855 try {
1856 iconResource = (Intent.ShortcutIconResource) extra;
1857 final PackageManager packageManager = context.getPackageManager();
1858 Resources resources = packageManager.getResourcesForApplication(
1859 iconResource.packageName);
1860 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1861 icon = resources.getDrawable(id);
1862 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001863 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 }
1865 }
1866
1867 if (icon == null) {
1868 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1869 }
1870
1871 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001872 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 info.title = name;
1874 info.iconResource = iconResource;
1875 info.uri = data.getData();
1876 info.baseIntent = baseIntent;
1877 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1878 LiveFolders.DISPLAY_MODE_GRID);
1879
1880 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001881 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001882 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883
1884 return info;
1885 }
1886
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 private void showNotifications() {
1888 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1889 if (statusBar != null) {
1890 statusBar.expand();
1891 }
1892 }
1893
1894 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001895 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001897 Intent chooser = Intent.createChooser(pickWallpaper,
1898 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001899 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1900 // Removed in Eclair MR1
1901// WallpaperManager wm = (WallpaperManager)
1902// getSystemService(Context.WALLPAPER_SERVICE);
1903// WallpaperInfo wi = wm.getWallpaperInfo();
1904// if (wi != null && wi.getSettingsActivity() != null) {
1905// LabeledIntent li = new LabeledIntent(getPackageName(),
1906// R.string.configure_wallpaper, 0);
1907// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1908// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1909// }
Mike Clerona0618e42009-10-22 13:55:21 -07001910 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 }
1912
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001913 /**
1914 * Registers various content observers. The current implementation registers
1915 * only a favorites observer to keep track of the favorites applications.
1916 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001917 private void registerContentObservers() {
1918 ContentResolver resolver = getContentResolver();
1919 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1920 true, mWidgetObserver);
1921 }
1922
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 @Override
1924 public boolean dispatchKeyEvent(KeyEvent event) {
1925 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1926 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001928 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001929 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001930 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001931 dumpState();
1932 return true;
1933 }
1934 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001935 }
1936 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1937 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001938 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 return true;
1940 }
1941 }
1942
1943 return super.dispatchKeyEvent(event);
1944 }
1945
Joe Onorato88ec0992009-11-19 13:16:06 -08001946 @Override
1947 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001948 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1949 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001950 } else {
1951 closeFolder();
1952 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001953 // Some launcher layouts don't have a previous and next view
1954 if (mPreviousView != null) {
1955 dismissPreview(mPreviousView);
1956 dismissPreview(mNextView);
1957 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001958 }
1959
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 private void closeFolder() {
1961 Folder folder = mWorkspace.getOpenFolder();
1962 if (folder != null) {
1963 closeFolder(folder);
1964 }
1965 }
1966
1967 void closeFolder(Folder folder) {
1968 folder.getInfo().opened = false;
1969 ViewGroup parent = (ViewGroup) folder.getParent();
1970 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001971 CellLayout cl = (CellLayout) parent;
1972 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001973 if (folder instanceof DropTarget) {
1974 // Live folders aren't DropTargets.
1975 mDragController.removeDropTarget((DropTarget)folder);
1976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 }
1978 folder.onClose();
1979 }
1980
1981 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001982 * Re-listen when widgets are reset.
1983 */
1984 private void onAppWidgetReset() {
1985 mAppWidgetHost.startListening();
1986 }
1987
1988 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001989 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1990 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001992 private void unbindDesktopItems() {
1993 for (ItemInfo item: mDesktopItems) {
1994 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001996 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001998
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 /**
2000 * Launches the intent referred by the clicked shortcut.
2001 *
2002 * @param v The view representing the clicked shortcut.
2003 */
2004 public void onClick(View v) {
2005 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002006 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002008 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002009 int[] pos = new int[2];
2010 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002011 intent.setSourceBounds(new Rect(pos[0], pos[1],
2012 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07002013 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 } else if (tag instanceof FolderInfo) {
2015 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002016 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002017 if (mState == State.ALL_APPS) {
2018 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002019 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002020 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
2023 }
2024
Michael Jurka0e260592010-06-30 17:07:39 -07002025 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002026 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002027 // clicking anywhere on the workspace causes the customization drawer to slide down
2028 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002029 return false;
2030 }
2031
Michael Jurkaaf442092010-06-10 17:01:57 -07002032 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002033 * Event handler for the search button
2034 *
2035 * @param v The view that was clicked.
2036 */
2037 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002038 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002039 // Use a custom animation for launching search
2040 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002041 }
2042
2043 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002044 * Event handler for the voice button
2045 *
2046 * @param v The view that was clicked.
2047 */
2048 public void onClickVoiceButton(View v) {
2049 startVoiceSearch();
2050 }
2051
2052 private void startVoiceSearch() {
2053 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2054 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2055 startActivity(intent);
2056 }
2057
2058 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002059 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002060 * enters home screen customization mode.
2061 *
2062 * @param v The view that was clicked.
2063 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002064 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002065 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002066 }
2067
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002068 /**
2069 * Event handler for the "grid" button that appears on the home screen, which
2070 * enters all apps mode.
2071 *
2072 * @param v The view that was clicked.
2073 */
2074 public void onClickAllAppsButton(View v) {
2075 showAllApps(true);
2076 }
2077
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002078 public void onClickAppMarketButton(View v) {
2079 if (mAppMarketIntent != null) {
2080 startActivitySafely(mAppMarketIntent, "app market");
2081 }
2082 }
2083
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002084 void startApplicationDetailsActivity(ComponentName componentName) {
2085 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002086 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2087 Uri.fromParts("package", packageName, null));
2088 startActivity(intent);
2089 }
2090
Patrick Dubroy5539af72010-09-07 15:22:01 -07002091 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2092 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2093 // System applications cannot be installed. For now, show a toast explaining that.
2094 // We may give them the option of disabling apps this way.
2095 int messageId = R.string.uninstall_system_app_text;
2096 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2097 } else {
2098 String packageName = appInfo.componentName.getPackageName();
2099 String className = appInfo.componentName.getClassName();
2100 Intent intent = new Intent(
2101 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2102 startActivity(intent);
2103 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002104 }
2105
Joe Onoratof984e852010-03-25 09:47:45 -07002106 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2108 try {
2109 startActivity(intent);
2110 } catch (ActivityNotFoundException e) {
2111 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002112 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 } catch (SecurityException e) {
2114 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002115 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002117 "or use the exported attribute for this activity. "
2118 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 }
2120 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002121
Romain Guy8e633c52010-03-04 12:51:36 -08002122 void startActivityForResultSafely(Intent intent, int requestCode) {
2123 try {
2124 startActivityForResult(intent, requestCode);
2125 } catch (ActivityNotFoundException e) {
2126 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2127 } catch (SecurityException e) {
2128 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2129 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2130 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2131 "or use the exported attribute for this activity.", e);
2132 }
2133 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134
2135 private void handleFolderClick(FolderInfo folderInfo) {
2136 if (!folderInfo.opened) {
2137 // Close any open folder
2138 closeFolder();
2139 // Open the requested folder
2140 openFolder(folderInfo);
2141 } else {
2142 // Find the open folder...
2143 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2144 int folderScreen;
2145 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002146 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 // .. and close it
2148 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002149 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150 // Close any folder open on the current screen
2151 closeFolder();
2152 // Pull the folder onto this screen
2153 openFolder(folderInfo);
2154 }
2155 }
2156 }
2157 }
2158
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002160 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 * is animated relative to the specified View. If the View is null, no animation
2162 * is played.
2163 *
2164 * @param folderInfo The FolderInfo describing the folder to open.
2165 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002166 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 Folder openFolder;
2168
2169 if (folderInfo instanceof UserFolderInfo) {
2170 openFolder = UserFolder.fromXml(this);
2171 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002172 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173 } else {
2174 return;
2175 }
2176
Joe Onorato00acb122009-08-04 16:04:30 -04002177 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 openFolder.setLauncher(this);
2179
2180 openFolder.bind(folderInfo);
2181 folderInfo.opened = true;
2182
Winson Chungaafa03c2010-06-11 17:34:16 -07002183 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 openFolder.onOpen();
2186 }
2187
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002189 switch (v.getId()) {
2190 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002191 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002192 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2193 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002194 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002195 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002196 return true;
2197 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002198 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002199 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2200 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002201 showPreviews(v);
2202 }
2203 return true;
2204 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002205 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002206 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2207 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2208 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002209 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002210 return true;
2211 }
2212
Joe Onorato9c1289c2009-08-17 11:03:03 -04002213 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 return false;
2215 }
2216
2217 if (!(v instanceof CellLayout)) {
2218 v = (View) v.getParent();
2219 }
2220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221
Michael Jurka0280c3b2010-09-17 15:00:07 -07002222 resetAddInfo();
2223 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002225 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002226 return true;
2227 }
2228
Michael Jurka0280c3b2010-09-17 15:00:07 -07002229 final View itemUnderLongClick = longClickCellInfo.cell;
2230
Winson Chung304dcde2011-01-07 11:17:23 -08002231 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002232 if (itemUnderLongClick == null) {
2233 // User long pressed on empty space
2234 mWorkspace.setAllowLongPress(false);
2235 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2236 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002237 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002238 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 }
2240 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002241 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002243 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2244 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002245 mAddIntersectCellX = longClickCellInfo.cellX;
2246 mAddIntersectCellY = longClickCellInfo.cellY;
2247 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 }
2249 }
2250 }
2251 return true;
2252 }
2253
Romain Guye6b8e2f2009-11-10 11:56:55 -08002254 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002255 private void dismissPreview(final View v) {
2256 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002257 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002258 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2259 public void onDismiss() {
2260 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2261 int count = group.getChildCount();
2262 for (int i = 0; i < count; i++) {
2263 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2264 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002265 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2266 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2267
2268 v.setTag(R.id.workspace, null);
2269 v.setTag(R.id.icon, null);
2270 window.setOnDismissListener(null);
2271 }
2272 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002273 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002274 }
2275 v.setTag(null);
2276 }
2277
Romain Guyf8e6a802009-12-07 17:48:02 -08002278 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002279 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002280 }
2281
Romain Guya6abce82009-11-10 02:54:41 -08002282 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002283 final Resources resources = getResources();
2284 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002285
Romain Guya6abce82009-11-10 02:54:41 -08002286 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002287
Romain Guy9d31e9f2009-11-11 18:54:28 -08002288 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002289
Romain Guyf8e6a802009-12-07 17:48:02 -08002290 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002291 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002292 int extraW = (int) ((r.left + r.right) * max);
2293 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002294
2295 int aW = cell.getWidth() - extraW;
2296 float w = aW / max;
2297
2298 int width = cell.getWidth();
2299 int height = cell.getHeight();
2300 int x = cell.getLeftPadding();
2301 int y = cell.getTopPadding();
2302 width -= (x + cell.getRightPadding());
2303 height -= (y + cell.getBottomPadding());
2304
2305 float scale = w / width;
2306
2307 int count = end - start;
2308
2309 final float sWidth = width * scale;
2310 float sHeight = height * scale;
2311
Romain Guye6b8e2f2009-11-10 11:56:55 -08002312 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002313
Romain Guye6b8e2f2009-11-10 11:56:55 -08002314 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2315 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002316
2317 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002318 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002319 cell = (CellLayout) workspace.getChildAt(i);
2320
Romain Guyf8e6a802009-12-07 17:48:02 -08002321 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002322 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002323
2324 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002325 c.scale(scale, scale);
2326 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002327 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002328
Romain Guy9d31e9f2009-11-11 18:54:28 -08002329 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002330 image.setImageBitmap(bitmap);
2331 image.setTag(i);
2332 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002333 image.setOnFocusChangeListener(handler);
2334 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002335 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002336
2337 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002338 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2339
Winson Chungaafa03c2010-06-11 17:34:16 -07002340 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002341 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002342
2343 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002344 p.setContentView(preview);
2345 p.setWidth((int) (sWidth * count + extraW));
2346 p.setHeight((int) (sHeight + extraH));
2347 p.setAnimationStyle(R.style.AnimationPreview);
2348 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002349 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002350 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002351 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002352
Romain Guye6b8e2f2009-11-10 11:56:55 -08002353 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2354 public void onDismiss() {
2355 dismissPreview(anchor);
2356 }
2357 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002358
2359 anchor.setTag(p);
2360 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002361 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002362 }
2363
Romain Guy9d31e9f2009-11-11 18:54:28 -08002364 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002365 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002366
Romain Guye6b8e2f2009-11-10 11:56:55 -08002367 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002368 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002369 }
2370
2371 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002372 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002373 v.post(this);
2374 }
2375
2376 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002377 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002378 }
2379
2380 public void onFocusChange(View v, boolean hasFocus) {
2381 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002382 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002383 }
Romain Guya6abce82009-11-10 02:54:41 -08002384 }
2385 }
2386
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002387 Workspace getWorkspace() {
2388 return mWorkspace;
2389 }
2390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391 @Override
2392 protected Dialog onCreateDialog(int id) {
2393 switch (id) {
2394 case DIALOG_CREATE_SHORTCUT:
2395 return new CreateShortcut().createDialog();
2396 case DIALOG_RENAME_FOLDER:
2397 return new RenameFolder().createDialog();
2398 }
2399
2400 return super.onCreateDialog(id);
2401 }
2402
2403 @Override
2404 protected void onPrepareDialog(int id, Dialog dialog) {
2405 switch (id) {
2406 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 break;
2408 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002409 if (mFolderInfo != null) {
2410 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2411 final CharSequence text = mFolderInfo.title;
2412 input.setText(text);
2413 input.setSelection(0, text.length());
2414 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415 break;
2416 }
2417 }
2418
2419 void showRenameDialog(FolderInfo info) {
2420 mFolderInfo = info;
2421 mWaitingForResult = true;
2422 showDialog(DIALOG_RENAME_FOLDER);
2423 }
2424
Michael Jurka0280c3b2010-09-17 15:00:07 -07002425 private void showAddDialog(int intersectX, int intersectY) {
2426 resetAddInfo();
2427 mAddIntersectCellX = intersectX;
2428 mAddIntersectCellY = intersectY;
2429 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002430 mWaitingForResult = true;
2431 showDialog(DIALOG_CREATE_SHORTCUT);
2432 }
2433
Joe Onoratodeb98af2010-02-19 14:59:39 -08002434 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002435 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002436 Bundle bundle = new Bundle();
2437
2438 ArrayList<String> shortcutNames = new ArrayList<String>();
2439 shortcutNames.add(getString(R.string.group_applications));
2440 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2441
2442 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2443 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2444 R.drawable.ic_launcher_application));
2445 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2446
2447 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2448 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002449 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002450 pickIntent.putExtras(bundle);
2451
Joe Onoratodeb98af2010-02-19 14:59:39 -08002452 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002453 }
2454
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 private class RenameFolder {
2456 private EditText mInput;
2457
2458 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2460 mInput = (EditText) layout.findViewById(R.id.folder_name);
2461
2462 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2463 builder.setIcon(0);
2464 builder.setTitle(getString(R.string.rename_folder_title));
2465 builder.setCancelable(true);
2466 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2467 public void onCancel(DialogInterface dialog) {
2468 cleanup();
2469 }
2470 });
2471 builder.setNegativeButton(getString(R.string.cancel_action),
2472 new Dialog.OnClickListener() {
2473 public void onClick(DialogInterface dialog, int which) {
2474 cleanup();
2475 }
2476 }
2477 );
2478 builder.setPositiveButton(getString(R.string.rename_action),
2479 new Dialog.OnClickListener() {
2480 public void onClick(DialogInterface dialog, int which) {
2481 changeFolderName();
2482 }
2483 }
2484 );
2485 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002486
2487 final AlertDialog dialog = builder.create();
2488 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2489 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002490 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002491 mInput.requestFocus();
2492 InputMethodManager inputManager = (InputMethodManager)
2493 getSystemService(Context.INPUT_METHOD_SERVICE);
2494 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002495 }
2496 });
2497
2498 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 }
2500
2501 private void changeFolderName() {
2502 final String name = mInput.getText().toString();
2503 if (!TextUtils.isEmpty(name)) {
2504 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002505 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002506 mFolderInfo.title = name;
2507 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2508
Joe Onorato9c1289c2009-08-17 11:03:03 -04002509 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002510 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002511 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002512 } else {
2513 final FolderIcon folderIcon = (FolderIcon)
2514 mWorkspace.getViewForTag(mFolderInfo);
2515 if (folderIcon != null) {
2516 folderIcon.setText(name);
2517 getWorkspace().requestLayout();
2518 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002519 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002520 mWorkspaceLoading = true;
2521 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522 }
2523 }
2524 }
2525 cleanup();
2526 }
2527
2528 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002529 dismissDialog(DIALOG_RENAME_FOLDER);
2530 mWaitingForResult = false;
2531 mFolderInfo = null;
2532 }
2533 }
2534
Daniel Sandler843e8602010-06-07 14:59:01 -04002535 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2536 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002537 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002538 }
2539
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002540 // AllAppsView.Watcher
2541 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002542 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2543 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002544 mWorkspace.setVisibility(View.GONE);
2545 }
2546 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002547
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002548 private void showToolbarButton(View button) {
2549 button.setAlpha(1.0f);
2550 button.setVisibility(View.VISIBLE);
2551 button.setFocusable(true);
2552 button.setClickable(true);
2553 }
2554
2555 private void hideToolbarButton(View button) {
2556 button.setAlpha(0.0f);
2557 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2558 button.setVisibility(View.INVISIBLE);
2559 button.setFocusable(false);
2560 button.setClickable(false);
2561 }
2562
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002563 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002564 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002565 *
2566 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2567 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002568 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002569 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002570 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002571 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002572 final boolean showing = show;
2573 final boolean hiding = !show;
2574
2575 final int duration = show ?
2576 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2577 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2578
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002579 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002580 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2581 anim.setDuration(duration);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002582 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002583 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002584 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002585 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002586 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002587 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002588 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002589 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002590 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002591 });
2592 seq.play(anim);
2593 } else {
2594 if (showing) {
2595 showToolbarButton(view);
2596 } else {
2597 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002598 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002599 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002600 }
2601
2602 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002603 * Show/hide the appropriate toolbar buttons for newState.
2604 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002605 *
2606 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002607 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2608 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002609 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002610 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002611 final View searchButton = findViewById(R.id.search_button_cluster);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002612 final View allAppsButton = findViewById(R.id.all_apps_button);
Michael Jurka800242b2010-12-16 11:39:26 -08002613 final View divider = findViewById(R.id.divider);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002614 final View configureButton = findViewById(R.id.configure_button);
2615
2616 switch (newState) {
2617 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002618 hideOrShowToolbarButton(true, searchButton, showSeq);
2619 hideOrShowToolbarButton(true, allAppsButton, showSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002620 hideOrShowToolbarButton(true, divider, showSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002621 hideOrShowToolbarButton(true, configureButton, showSeq);
Michael Jurkab8e14472010-12-20 16:06:10 -08002622 mDeleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002623 mDeleteZone.setDragAndDropEnabled(true);
2624 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002625 break;
2626 case ALL_APPS:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002627 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002628 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002629 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002630 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002631 mDeleteZone.setDragAndDropEnabled(false);
2632 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002633 break;
2634 case CUSTOMIZE:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002635 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002636 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002637 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002638 hideOrShowToolbarButton(false, configureButton, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002639 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002640 break;
2641 }
2642 }
2643
2644 /**
2645 * Helper method for the cameraZoomIn/cameraZoomOut animations
2646 * @param view The view being animated
2647 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2648 * @param scaleFactor The scale factor used for the zoom
2649 */
2650 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2651 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002652
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002653 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002654 // Set pivotY so that at the starting zoom factor, the view is partially
2655 // visible. Modifying initialHeightFactor changes how much of the view is
2656 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002657 if (state == State.ALL_APPS) {
Adam Cohenf16e5712011-01-13 13:31:45 -08002658 final float initialHeightFactor = 0.165f;
2659 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002660 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002661 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002662 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002663 }
2664 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002665
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002666 /**
2667 * Zoom the camera out from the workspace to reveal 'toView'.
2668 * Assumes that the view to show is anchored at either the very top or very bottom
2669 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002670 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002671 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002672 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002673 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002674 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002675
2676 final int duration = toAllApps ?
2677 res.getInteger(R.integer.config_allAppsZoomInTime) :
2678 res.getInteger(R.integer.config_customizeZoomInTime);
2679
2680 final float scale = toAllApps ?
2681 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2682 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2683
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002684 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002685
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002686 setPivotsForZoom(toView, toState, scale);
2687
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002688 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002689 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002690 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002691 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002692 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002693
2694 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002695 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2696 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2697 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
Adam Cohenf16e5712011-01-13 13:31:45 -08002698
Chet Haase472b2812010-10-14 07:02:04 -07002699 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002700
Adam Cohenf16e5712011-01-13 13:31:45 -08002701 if (toAllApps) {
Michael Jurkad99cf8b2011-01-16 16:53:18 -08002702 toView.setAlpha(0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002703 ObjectAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2704 PropertyValuesHolder.ofFloat("alpha", 1.0f));
2705 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2706 alphaAnim.setDuration(duration);
2707 alphaAnim.start();
2708 }
2709
Adam Cohen61033d32010-11-15 18:29:44 -08002710 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka8edd75c2010-12-17 20:15:06 -08002711 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002712 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002713 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002714 // Prepare the position
2715 toView.setTranslationX(0.0f);
2716 toView.setTranslationY(0.0f);
2717 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002718 if (!toAllApps) {
2719 toView.setAlpha(1.0f);
2720 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002721 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002722 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002723 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002724 // If we don't set the final scale values here, if this animation is cancelled
2725 // it will have the wrong scale value and subsequent cameraPan animations will
2726 // not fix that
2727 toView.setScaleX(1.0f);
2728 toView.setScaleY(1.0f);
2729 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002730 });
2731
Chet Haaseb1254a62010-09-07 13:35:00 -07002732 AnimatorSet toolbarHideAnim = new AnimatorSet();
2733 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002734 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2735
2736 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002737 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002738
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002739 if (mStateAnimation != null) mStateAnimation.cancel();
2740 mStateAnimation = new AnimatorSet();
2741 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2742 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002743
2744 // Show the new toolbar buttons just as the main animation is ending
2745 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002746 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2747 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002748 } else {
2749 toView.setTranslationX(0.0f);
2750 toView.setTranslationY(0.0f);
2751 toView.setScaleX(1.0f);
2752 toView.setScaleY(1.0f);
2753 toView.setVisibility(View.VISIBLE);
2754 hideAndShowToolbarButtons(toState, null, null);
2755 }
Michael Jurkac5b262c2011-01-12 20:24:50 -08002756 mWorkspace.setVerticalWallpaperOffset(toAllApps ?
2757 Workspace.WallpaperVerticalOffset.TOP : Workspace.WallpaperVerticalOffset.BOTTOM);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002758 }
2759
2760 /**
2761 * Zoom the camera back into the workspace, hiding 'fromView'.
2762 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002763 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2764 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002765 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002766 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002767 cameraZoomIn(fromState, animated, false);
2768 }
2769
2770 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002771 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002772 final boolean fromAllApps = (fromState == State.ALL_APPS);
2773
2774 int duration = fromAllApps ?
2775 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2776 res.getInteger(R.integer.config_customizeZoomOutTime);
2777
2778 float scaleFactor = fromAllApps ?
2779 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2780 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2781
2782 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002783
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002784 mCustomizePagedView.endChoiceMode();
2785 mAllAppsPagedView.endChoiceMode();
2786
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002787 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002788
Michael Jurkad3ef3062010-11-23 16:23:58 -08002789 if (!springLoaded) {
2790 mWorkspace.unshrink(animated);
2791 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002792
2793 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002794 if (mStateAnimation != null) mStateAnimation.cancel();
2795 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002796 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2797 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2798 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2799 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002800 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2801
2802 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2803 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2804 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Adam Cohenf16e5712011-01-13 13:31:45 -08002805 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka8edd75c2010-12-17 20:15:06 -08002806 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002807 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002808 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002809 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002810 }
2811 });
2812
Chet Haaseb1254a62010-09-07 13:35:00 -07002813 AnimatorSet toolbarHideAnim = new AnimatorSet();
2814 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002815 if (!springLoaded) {
2816 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2817 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002818
Adam Cohen61033d32010-11-15 18:29:44 -08002819 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002820
2821 // Show the new toolbar buttons at the very end of the whole animation
2822 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2823 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002824 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2825 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002826 } else {
2827 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002828 if (!springLoaded) {
2829 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2830 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002831 }
Michael Jurkac5b262c2011-01-12 20:24:50 -08002832 mWorkspace.setVerticalWallpaperOffset(Workspace.WallpaperVerticalOffset.MIDDLE);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002833 }
2834
2835 /**
2836 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2837 * This is the transition used on xlarge screens to go between all apps and
2838 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002839 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2840 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2841 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002842 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002843 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002844 final Resources res = getResources();
2845 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002846 final int workspaceHeight = mWorkspace.getHeight();
2847
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002848 final boolean fromAllApps = (fromState == State.ALL_APPS);
2849 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2850 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002851
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002852 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2853 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2854 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2855 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002856
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002857 mCustomizePagedView.endChoiceMode();
2858 mAllAppsPagedView.endChoiceMode();
2859
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002860 if (toState == State.ALL_APPS) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002861 mWorkspace.shrink(Workspace.ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002862 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002863 mWorkspace.shrink(Workspace.ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002864 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002865
2866 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002867 if (mStateAnimation != null) mStateAnimation.cancel();
2868 mStateAnimation = new AnimatorSet();
Michael Jurka8edd75c2010-12-17 20:15:06 -08002869 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002870 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002871 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002872 toView.setVisibility(View.VISIBLE);
2873 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002874 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002875 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002876 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002877 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002878 fromView.setVisibility(View.GONE);
2879 }
2880 });
2881
Chet Haaseb1254a62010-09-07 13:35:00 -07002882 AnimatorSet toolbarHideAnim = new AnimatorSet();
2883 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002884 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2885
Chet Haase472b2812010-10-14 07:02:04 -07002886 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2887 fromViewStartY, fromViewEndY);
2888 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002889 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2890 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2891 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2892 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2893 );
Chet Haase472b2812010-10-14 07:02:04 -07002894 fromAnim.setDuration(duration);
2895 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002896
2897 // Show the new toolbar buttons just as the main animation is ending
2898 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002899 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2900 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002901 } else {
2902 fromView.setY(fromViewEndY);
2903 fromView.setVisibility(View.GONE);
2904 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002905 toView.setScaleX(1.0f);
2906 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002907 toView.setVisibility(View.VISIBLE);
2908 hideAndShowToolbarButtons(toState, null, null);
2909 }
Michael Jurkac5b262c2011-01-12 20:24:50 -08002910 mWorkspace.setVerticalWallpaperOffset((toState == State.ALL_APPS) ?
2911 Workspace.WallpaperVerticalOffset.TOP : Workspace.WallpaperVerticalOffset.BOTTOM);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002912 }
2913
2914 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002915 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002916 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002917 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002918
Michael Jurka79212d82010-07-30 16:36:20 -07002919 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002920 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002921 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002922 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002923 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002924 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002925 } else {
2926 mAllAppsGrid.zoom(1.0f, animated);
2927 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002928
Romain Guyc16fea72010-03-12 17:17:56 -08002929 ((View) mAllAppsGrid).setFocusable(true);
2930 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002931
Joe Onorato7c312c12009-08-13 21:36:53 -07002932 // TODO: fade these two too
2933 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002934
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002935 // Change the state *after* we've called all the transition code
2936 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002937
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002938 // send an accessibility event to announce the context change
2939 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2940 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002941
2942 void showWorkspace(boolean animated) {
2943 showWorkspace(animated, null);
2944 }
2945
2946 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002947 if (layout != null) {
2948 // always animated, but that's ok since we never specify a layout and
2949 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002950 mWorkspace.unshrink(layout);
2951 } else {
2952 mWorkspace.unshrink(animated);
2953 }
2954 if (mState == State.ALL_APPS) {
2955 closeAllApps(animated);
2956 } else if (mState == State.CUSTOMIZE) {
2957 hideCustomizationDrawer(animated);
2958 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002959
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002960 // Change the state *after* we've called all the transition code
2961 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002962
2963 // send an accessibility event to announce the context change
2964 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002965 }
2966
Michael Jurkad3ef3062010-11-23 16:23:58 -08002967 void enterSpringLoadedDragMode(CellLayout layout) {
2968 mWorkspace.enterSpringLoadedDragMode(layout);
2969 if (mState == State.ALL_APPS) {
2970 cameraZoomIn(State.ALL_APPS, true, true);
2971 mState = State.ALL_APPS_SPRING_LOADED;
2972 } else if (mState == State.CUSTOMIZE) {
2973 cameraZoomIn(State.CUSTOMIZE, true, true);
2974 mState = State.CUSTOMIZE_SPRING_LOADED;
2975 }/* else {
2976 // we're already in spring loaded mode; don't do anything
2977 }*/
2978 }
2979
2980 void exitSpringLoadedDragMode() {
2981 if (mState == State.ALL_APPS_SPRING_LOADED) {
2982 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2983 cameraZoomOut(State.ALL_APPS, true);
2984 mState = State.ALL_APPS;
2985 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2986 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2987 cameraZoomOut(State.CUSTOMIZE, true);
2988 mState = State.CUSTOMIZE;
2989 }/* else {
2990 // we're not in spring loaded mode; don't do anything
2991 }*/
2992 }
2993
Joe Onoratob2061212009-11-24 16:13:54 -05002994 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002995 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002996 * - Home from workspace
2997 * - from center screen
2998 * - from other screens
2999 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003000 * - from center screen
3001 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003002 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003003 * - from center screen
3004 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003005 * - Launch app from workspace and quit
3006 * - with back
3007 * - with home
3008 * - Launch app from all apps and quit
3009 * - with back
3010 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003011 * - Go to a screen that's not the default, then all
3012 * apps, and launch and app, and go back
3013 * - with back
3014 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003015 * - On workspace, long press power and go back
3016 * - with back
3017 * - with home
3018 * - On all apps, long press power and go back
3019 * - with back
3020 * - with home
3021 * - On workspace, power off
3022 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003023 * - Launch an app and turn off the screen while in that app
3024 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003025 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003026 * - From all apps
3027 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003028 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3029 * - From all apps
3030 * - From the center workspace
3031 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003032 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003033 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003034 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003035 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003036 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003037 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003038 } else {
3039 mAllAppsGrid.zoom(0.0f, animated);
3040 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003041 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003042 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003043 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003044 }
3045
Joe Onorato7c312c12009-08-13 21:36:53 -07003046 void lockAllApps() {
3047 // TODO
3048 }
3049
3050 void unlockAllApps() {
3051 // TODO
3052 }
3053
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003054 // Show the customization drawer (only exists in x-large configuration)
3055 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003056 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003057 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003058 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003059 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003060 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003061 // Change the state *after* we've called all the transition code
3062 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003063 }
3064
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003065 // Hide the customization drawer (only exists in x-large configuration)
3066 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003067 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003068 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003069 }
3070 }
3071
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003072 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
3073 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3074 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07003075 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003076 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003077
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003078 void onWorkspaceClick(CellLayout layout) {
3079 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003080 }
3081
Michael Jurka4ef207b2010-11-29 17:05:45 -08003082 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003083 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003084 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3085 ImageView button = (ImageView) findViewById(buttonId);
3086 Drawable toolbarIcon = null;
3087 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003088 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003089 // Look for the toolbar icon specified in the activity meta-data
3090 Bundle metaData = packageManager.getActivityInfo(
3091 activityName, PackageManager.GET_META_DATA).metaData;
3092 if (metaData != null) {
3093 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3094 if (iconResId != 0) {
3095 Resources res = packageManager.getResourcesForActivity(activityName);
3096 toolbarIcon = res.getDrawable(iconResId);
3097 }
3098 }
3099 } catch (NameNotFoundException e) {
3100 // Do nothing
3101 }
3102 // If we were unable to find the icon via the meta-data, use a generic one
3103 if (toolbarIcon == null) {
3104 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003105 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003106 } else {
3107 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003108 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003109 }
3110 }
3111
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003112 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003113 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003114 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003115 }
3116
Michael Jurka0423dcf2010-10-05 14:56:18 -07003117 private void updateGlobalSearchIcon() {
3118 if (LauncherApplication.isScreenXLarge()) {
3119 final SearchManager searchManager =
3120 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3121 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003122 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003123 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003124 R.id.search_button, activityName, R.drawable.search_button_generic);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003125 } else {
3126 findViewById(R.id.search_button).setVisibility(View.GONE);
3127 }
3128 }
3129 }
3130
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003131 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003132 updateButtonWithDrawable(R.id.search_button, d);
3133 }
3134
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003135 private void updateVoiceSearchIcon() {
3136 if (LauncherApplication.isScreenXLarge()) {
3137 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3138 ComponentName activityName = intent.resolveActivity(getPackageManager());
3139 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003140 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003141 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3142 } else {
3143 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003144 }
3145 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003146 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003147
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003148 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003149 updateButtonWithDrawable(R.id.voice_button, d);
3150 }
3151
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003152 /**
3153 * Sets the app market icon (shown when all apps is visible on x-large screens)
3154 */
3155 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003156 if (LauncherApplication.isScreenXLarge()) {
3157 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3158 // Find the app market activity by resolving an intent.
3159 // (If multiple app markets are installed, it will return the ResolverActivity.)
3160 ComponentName activityName = intent.resolveActivity(getPackageManager());
3161 if (activityName != null) {
3162 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003163 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003164 R.id.market_button, activityName, R.drawable.app_market_generic);
3165 }
3166 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003167 }
3168
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003169 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003170 updateButtonWithDrawable(R.id.market_button, d);
3171 }
3172
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003173 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174 * Displays the shortcut creation dialog and launches, if necessary, the
3175 * appropriate activity.
3176 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003177 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003178 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3179 DialogInterface.OnShowListener {
3180
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003181 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003183 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003184 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003185
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003186 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3187 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003188 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003189
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003190 builder.setInverseBackgroundForced(true);
3191
3192 AlertDialog dialog = builder.create();
3193 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003194 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003195 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003197 return dialog;
3198 }
3199
3200 public void onCancel(DialogInterface dialog) {
3201 mWaitingForResult = false;
3202 cleanup();
3203 }
3204
Romain Guycbb89e42009-06-08 15:52:54 -07003205 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003206 }
3207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003208 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003209 try {
3210 dismissDialog(DIALOG_CREATE_SHORTCUT);
3211 } catch (Exception e) {
3212 // An exception is thrown if the dialog is not visible, which is fine
3213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003214 }
3215
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003216 /**
3217 * Handle the action clicked in the "Add to home" dialog.
3218 */
3219 public void onClick(DialogInterface dialog, int which) {
3220 Resources res = getResources();
3221 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003222
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003223 switch (which) {
3224 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003225 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003226 break;
3227 }
Romain Guycbb89e42009-06-08 15:52:54 -07003228
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003229 case AddAdapter.ITEM_APPWIDGET: {
3230 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003231
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003232 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3233 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003234 // start the pick activity
3235 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3236 break;
3237 }
Romain Guycbb89e42009-06-08 15:52:54 -07003238
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003239 case AddAdapter.ITEM_LIVE_FOLDER: {
3240 // Insert extra item to handle inserting folder
3241 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003242
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003243 ArrayList<String> shortcutNames = new ArrayList<String>();
3244 shortcutNames.add(res.getString(R.string.group_folder));
3245 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003246
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003247 ArrayList<ShortcutIconResource> shortcutIcons =
3248 new ArrayList<ShortcutIconResource>();
3249 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3250 R.drawable.ic_launcher_folder));
3251 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3252
3253 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3254 pickIntent.putExtra(Intent.EXTRA_INTENT,
3255 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3256 pickIntent.putExtra(Intent.EXTRA_TITLE,
3257 getText(R.string.title_select_live_folder));
3258 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003259
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003260 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3261 break;
3262 }
3263
3264 case AddAdapter.ITEM_WALLPAPER: {
3265 startWallpaper();
3266 break;
3267 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003268 }
3269 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003270
3271 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003272 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003273 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003274 }
3275
3276 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003277 * Receives notifications when applications are added/removed.
3278 */
3279 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3280 @Override
3281 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003282 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003283 String reason = intent.getStringExtra("reason");
3284 if (!"homekey".equals(reason)) {
3285 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003286 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003287 animate = false;
3288 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003289 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003290 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003291 }
3292 }
3293
3294 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003295 * Receives notifications whenever the appwidgets are reset.
3296 */
3297 private class AppWidgetResetObserver extends ContentObserver {
3298 public AppWidgetResetObserver() {
3299 super(new Handler());
3300 }
3301
3302 @Override
3303 public void onChange(boolean selfChange) {
3304 onAppWidgetReset();
3305 }
3306 }
3307
3308 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003309 * If the activity is currently paused, signal that we need to re-run the loader
3310 * in onResume.
3311 *
3312 * This needs to be called from incoming places where resources might have been loaded
3313 * while we are paused. That is becaues the Configuration might be wrong
3314 * when we're not running, and if it comes back to what it was when we
3315 * were paused, we are not restarted.
3316 *
3317 * Implementation of the method from LauncherModel.Callbacks.
3318 *
3319 * @return true if we are currently paused. The caller might be able to
3320 * skip some work in that case since we will come back again.
3321 */
3322 public boolean setLoadOnResume() {
3323 if (mPaused) {
3324 Log.i(TAG, "setLoadOnResume");
3325 mOnResumeNeedsLoad = true;
3326 return true;
3327 } else {
3328 return false;
3329 }
3330 }
3331
3332 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003333 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003334 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003335 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003336 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003337 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003338 } else {
3339 return SCREEN_COUNT / 2;
3340 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003341 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003342
Winson Chunga12a2502010-12-20 14:41:35 -08003343 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003344 mAllAppsPagedView = view;
3345 }
3346
Joe Onorato9c1289c2009-08-17 11:03:03 -04003347 /**
3348 * Refreshes the shortcuts shown on the workspace.
3349 *
3350 * Implementation of the method from LauncherModel.Callbacks.
3351 */
3352 public void startBinding() {
3353 final Workspace workspace = mWorkspace;
3354 int count = workspace.getChildCount();
3355 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003356 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003357 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3358 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003359
Joe Onorato9c1289c2009-08-17 11:03:03 -04003360 if (DEBUG_USER_INTERFACE) {
3361 android.widget.Button finishButton = new android.widget.Button(this);
3362 finishButton.setText("Finish");
3363 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3364
3365 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3366 public void onClick(View v) {
3367 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003368 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003369 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003370 }
3371 }
3372
3373 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003374 * Bind the items start-end from the list.
3375 *
3376 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003377 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003378 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3379
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003380 setLoadOnResume();
3381
Joe Onorato9c1289c2009-08-17 11:03:03 -04003382 final Workspace workspace = mWorkspace;
3383
3384 for (int i=start; i<end; i++) {
3385 final ItemInfo item = shortcuts.get(i);
3386 mDesktopItems.add(item);
3387 switch (item.itemType) {
3388 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3389 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003390 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003391 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3392 false);
3393 break;
3394 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3395 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003396 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003397 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003398 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3399 false);
3400 break;
3401 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3402 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3403 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003404 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003405 (LiveFolderInfo) item);
3406 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3407 false);
3408 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003409 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003410 }
3411
Joe Onorato9c1289c2009-08-17 11:03:03 -04003412 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003413 }
3414
Joe Onorato9c1289c2009-08-17 11:03:03 -04003415 /**
3416 * Implementation of the method from LauncherModel.Callbacks.
3417 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003418 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003419 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003420 sFolders.clear();
3421 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003422 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003423
3424 /**
3425 * Add the views for a widget to the workspace.
3426 *
3427 * Implementation of the method from LauncherModel.Callbacks.
3428 */
3429 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003430 setLoadOnResume();
3431
Daniel Sandler843e8602010-06-07 14:59:01 -04003432 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3433 if (DEBUG_WIDGETS) {
3434 Log.d(TAG, "bindAppWidget: " + item);
3435 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003436 final Workspace workspace = mWorkspace;
3437
3438 final int appWidgetId = item.appWidgetId;
3439 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003440 if (DEBUG_WIDGETS) {
3441 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3442 }
3443
Joe Onorato9c1289c2009-08-17 11:03:03 -04003444 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3445
Joe Onorato9c1289c2009-08-17 11:03:03 -04003446 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3447 item.hostView.setTag(item);
3448
3449 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3450 item.cellY, item.spanX, item.spanY, false);
3451
Adam Cohended9f8d2010-11-03 13:25:16 -07003452 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3453
Joe Onorato9c1289c2009-08-17 11:03:03 -04003454 workspace.requestLayout();
3455
3456 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003457
3458 if (DEBUG_WIDGETS) {
3459 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3460 + (SystemClock.uptimeMillis()-start) + "ms");
3461 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003462 }
3463
3464 /**
3465 * Callback saying that there aren't any more items to bind.
3466 *
3467 * Implementation of the method from LauncherModel.Callbacks.
3468 */
3469 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003470 setLoadOnResume();
3471
Joe Onorato9c1289c2009-08-17 11:03:03 -04003472 if (mSavedState != null) {
3473 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003474 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003475 }
3476
3477 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3478 if (userFolders != null) {
3479 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003480 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003481 if (info != null) {
3482 openFolder(info);
3483 }
3484 }
3485 final Folder openFolder = mWorkspace.getOpenFolder();
3486 if (openFolder != null) {
3487 openFolder.requestFocus();
3488 }
3489 }
3490
Joe Onorato9c1289c2009-08-17 11:03:03 -04003491 mSavedState = null;
3492 }
3493
3494 if (mSavedInstanceState != null) {
3495 super.onRestoreInstanceState(mSavedInstanceState);
3496 mSavedInstanceState = null;
3497 }
3498
Joe Onorato9c1289c2009-08-17 11:03:03 -04003499 mWorkspaceLoading = false;
3500 }
3501
3502 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003503 * Updates the icons on the launcher that are affected by changes to the package list
3504 * on the device.
3505 */
3506 private void updateIconsAffectedByPackageManagerChanges() {
3507 updateAppMarketIcon();
3508 updateGlobalSearchIcon();
3509 updateVoiceSearchIcon();
3510 }
3511
3512 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003513 * Add the icons for all apps.
3514 *
3515 * Implementation of the method from LauncherModel.Callbacks.
3516 */
3517 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003518 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003519 if (mCustomizePagedView != null) {
3520 mCustomizePagedView.setApps(apps);
3521 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003522 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003523 }
3524
3525 /**
3526 * A package was installed.
3527 *
3528 * Implementation of the method from LauncherModel.Callbacks.
3529 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003530 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003531 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003532 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003533 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003534 if (mCustomizePagedView != null) {
3535 mCustomizePagedView.addApps(apps);
3536 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003537 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003538 }
3539
3540 /**
3541 * A package was updated.
3542 *
3543 * Implementation of the method from LauncherModel.Callbacks.
3544 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003545 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003546 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003547 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003548 mWorkspace.updateShortcuts(apps);
3549 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003550 if (mCustomizePagedView != null) {
3551 mCustomizePagedView.updateApps(apps);
3552 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003553 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003554 }
3555
3556 /**
3557 * A package was uninstalled.
3558 *
3559 * Implementation of the method from LauncherModel.Callbacks.
3560 */
Joe Onorato36115782010-06-17 13:28:48 -04003561 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003562 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003563 if (permanent) {
3564 mWorkspace.removeItems(apps);
3565 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003566 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003567 if (mCustomizePagedView != null) {
3568 mCustomizePagedView.removeApps(apps);
3569 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003570 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 }
Joe Onoratobe386092009-11-17 17:32:16 -08003572
3573 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003574 * A number of packages were updated.
3575 */
3576 public void bindPackagesUpdated() {
3577 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003578 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003579 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003580 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003581 }
3582
Winson Chung400438b2011-01-16 17:53:48 -08003583 private int mapConfigurationOriActivityInfoOri(int configOri) {
3584 final Display d = getWindowManager().getDefaultDisplay();
3585 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3586 switch (d.getRotation()) {
3587 case Surface.ROTATION_0:
3588 case Surface.ROTATION_180:
3589 // We are currently in the same basic orientation as the natural orientation
3590 naturalOri = configOri;
3591 break;
3592 case Surface.ROTATION_90:
3593 case Surface.ROTATION_270:
3594 // We are currently in the other basic orientation to the natural orientation
3595 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3596 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3597 break;
3598 }
3599
3600 int[] oriMap = {
3601 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3602 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3603 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3604 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3605 };
3606 // Since the map starts at portrait, we need to offset if this device's natural orientation
3607 // is landscape.
3608 int indexOffset = 0;
3609 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3610 indexOffset = 1;
3611 }
3612 return oriMap[(d.getRotation() + indexOffset) % 4];
3613 }
3614 public void lockScreenOrientation() {
3615 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3616 .getConfiguration().orientation));
3617 }
3618 public void unlockScreenOrientation() {
3619 mHandler.postDelayed(new Runnable() {
3620 public void run() {
3621 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3622 }
3623 }, mRestoreScreenOrientationDelay);
3624 }
3625
Winson Chung80baf5a2010-08-09 16:03:15 -07003626 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003627 * Prints out out state for debugging.
3628 */
3629 public void dumpState() {
3630 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003631 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003632 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3633 Log.d(TAG, "mRestoring=" + mRestoring);
3634 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3635 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3636 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003637 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003638 mModel.dumpState();
3639 mAllAppsGrid.dumpState();
3640 Log.d(TAG, "END launcher2 dump state");
3641 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003642}