blob: dc2e5de02ed68fead933a9271519c4d5d1aece14 [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
Michael Jurka742574b2011-02-02 23:51:01 -080020import com.android.common.Search;
21import com.android.launcher.R;
22import com.android.launcher2.CustomizePagedView.CustomizationType;
23import com.android.launcher2.Workspace.ShrinkState;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070024
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080026import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070028import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070029import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080031import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070032import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.app.Dialog;
35import android.app.SearchManager;
36import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070037import android.appwidget.AppWidgetManager;
38import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080040import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070041import android.content.ClipData;
42import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040043import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080044import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.Context;
46import android.content.DialogInterface;
47import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080048import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080049import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080052import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080053import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070055import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040056import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080057import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080059import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080061import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070062import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040063import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070064import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020066import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080067import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070068import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040070import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080071import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070072import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070073import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080074import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.text.Selection;
76import android.text.SpannableStringBuilder;
77import android.text.TextUtils;
78import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070079import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080080import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080081import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082import android.view.KeyEvent;
83import android.view.LayoutInflater;
84import android.view.Menu;
85import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070086import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080087import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.View;
89import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070090import android.view.WindowManager;
Adam Cohen860f4c52011-01-27 20:16:13 -080091import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080092import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080093import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070095import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070097import android.widget.ImageView;
98import android.widget.LinearLayout;
99import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700100import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -0800101import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -0700102import android.widget.TextView;
103import android.widget.Toast;
Adam Cohen860f4c52011-01-27 20:16:13 -0800104import android.widget.TabHost.OnTabChangeListener;
105import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700106
Michael Jurka742574b2011-02-02 23:51:01 -0800107import java.io.DataInputStream;
108import java.io.DataOutputStream;
109import java.io.FileNotFoundException;
110import java.io.IOException;
111import java.util.ArrayList;
112import java.util.HashMap;
113import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800114
Adam Cohended9f8d2010-11-03 13:25:16 -0700115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116/**
117 * Default launcher application.
118 */
Michael Jurka946ad472010-07-09 18:05:18 -0700119public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700120 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
121 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800122 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700123 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400126 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400127 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800130 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700133 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
134 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
136 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700137 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
139 private static final int REQUEST_CREATE_SHORTCUT = 1;
140 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700141 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 private static final int REQUEST_PICK_APPLICATION = 6;
143 private static final int REQUEST_PICK_SHORTCUT = 7;
144 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700146 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
148 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
149
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800150 static final int SCREEN_COUNT = 5;
151 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Joe Onorato7c312c12009-08-13 21:36:53 -0700153 static final int DIALOG_CREATE_SHORTCUT = 1;
154 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Romain Guy98d01652009-06-30 16:21:04 -0700156 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
158 // Type: int
159 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700160 // Type: int
161 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: long
163 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
164 // Type: int
165 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
166 // Type: int
167 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
168 // Type: int
169 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 // Type: boolean
171 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
172 // Type: long
173 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
174
Winson Chung80baf5a2010-08-09 16:03:15 -0700175 // tags for the customization tabs
176 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700177 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700178 private static final String SHORTCUTS_TAG = "shortcuts";
179 private static final String WALLPAPERS_TAG = "wallpapers";
180
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700181 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
182
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700183 /** The different states that Launcher can be in. */
Michael Jurkad3ef3062010-11-23 16:23:58 -0800184 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW,
185 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700186 private State mState = State.WORKSPACE;
187 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800192 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800194 private final BroadcastReceiver mCloseSystemDialogsReceiver
195 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800196 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private LayoutInflater mInflater;
199
Joe Onorato00acb122009-08-04 16:04:30 -0400200 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700202
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 private AppWidgetManager mAppWidgetManager;
204 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700205
Michael Jurka0280c3b2010-09-17 15:00:07 -0700206 private int mAddScreen = -1;
207 private int mAddIntersectCellX = -1;
208 private int mAddIntersectCellY = -1;
209 private int[] mAddDropPosition;
210 private int[] mTmpAddItemCellCoordinates = new int[2];
211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 private FolderInfo mFolderInfo;
213
Joe Onorato7c312c12009-08-13 21:36:53 -0700214 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700216 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700217 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700218 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700219
Michael Jurka838a4ca2011-02-07 13:33:06 -0800220 private View mButtonCluster;
221 private View mAllAppsButton;
222 private View mDivider;
223 private View mConfigureButton;
224
Winson Chunga12a2502010-12-20 14:41:35 -0800225 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700226 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 private Bundle mSavedState;
229
230 private SpannableStringBuilder mDefaultKeySsb = null;
231
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232 private boolean mWorkspaceLoading = true;
233
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800234 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 private boolean mRestoring;
236 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700237 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238
239 private Bundle mSavedInstanceState;
240
Joe Onorato9c1289c2009-08-17 11:03:03 -0400241 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800242 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800243 private boolean mUserPresent = true;
244 private boolean mVisible = false;
245 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700247 private static LocaleConfiguration sLocaleConfiguration = null;
248
Romain Guy84f296c2009-11-04 15:00:44 -0800249 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700250
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700251 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700252
Romain Guy1fbc1c82009-11-09 20:43:08 -0800253 private ImageView mPreviousView;
254 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500255
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400256 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400257 private String[] mHotseatConfig = null;
258 private Intent[] mHotseats = null;
259 private Drawable[] mHotseatIcons = null;
260 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400261
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700262 private Intent mAppMarketIntent = null;
263
Adam Cohended9f8d2010-11-03 13:25:16 -0700264 // Related to the auto-advancing of widgets
265 private final int ADVANCE_MSG = 1;
266 private final int mAdvanceInterval = 20000;
267 private final int mAdvanceStagger = 250;
268 private long mAutoAdvanceSentTime;
269 private long mAutoAdvanceTimeLeft = -1;
270 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
271 new HashMap<View, AppWidgetProviderInfo>();
272
Winson Chung400438b2011-01-16 17:53:48 -0800273 // Determines how long to wait after a rotation before restoring the screen orientation to
274 // match the sensor state.
275 private final int mRestoreScreenOrientationDelay = 500;
276
Michael Jurka4ef207b2010-11-29 17:05:45 -0800277 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800278 private static Drawable.ConstantState sGlobalSearchIcon;
279 private static Drawable.ConstantState sVoiceSearchIcon;
280 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800281
Winson Chunga12a2502010-12-20 14:41:35 -0800282 private CustomizationType getCustomizeFilterForTabTag(String tag) {
283 if (tag.equals(WIDGETS_TAG)) {
284 return CustomizationType.WidgetCustomization;
285 } else if (tag.equals(APPLICATIONS_TAG)) {
286 return CustomizationType.ApplicationCustomization;
287 } else if (tag.equals(WALLPAPERS_TAG)) {
288 return CustomizePagedView.CustomizationType.WallpaperCustomization;
289 } else if (tag.equals(SHORTCUTS_TAG)) {
290 return CustomizePagedView.CustomizationType.ShortcutCustomization;
291 }
292 return CustomizationType.WidgetCustomization;
293 }
294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 @Override
296 protected void onCreate(Bundle savedInstanceState) {
297 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700298
Winson Chungaafa03c2010-06-11 17:34:16 -0700299 if (LauncherApplication.isInPlaceRotationEnabled()) {
300 // hide the status bar (temporary until we get the status bar design figured out)
301 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
302 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
303 }
304
Joe Onorato0589f0f2010-02-08 13:44:00 -0800305 LauncherApplication app = ((LauncherApplication)getApplication());
306 mModel = app.setLauncher(this);
307 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400308 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700310
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700311 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700312 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
313 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700314
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200316 android.os.Debug.startMethodTracing(
317 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 }
319
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400320 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800323 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700324 if (mHomeCustomizationDrawer != null) {
325 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700326
Winson Chung80baf5a2010-08-09 16:03:15 -0700327 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700328 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
329 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700330 TabContentFactory contentFactory = new TabContentFactory() {
331 public View createTabContent(String tag) {
332 return mCustomizePagedView;
333 }
334 };
335
Winson Chung49767ae2010-11-29 14:48:30 -0800336
337 TextView tabView;
338 TabWidget tabWidget = (TabWidget)
339 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
340
341 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
342 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700343 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800344 .setIndicator(tabView).setContent(contentFactory));
345 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
346 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700347 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800348 .setIndicator(tabView).setContent(contentFactory));
349 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
350 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700351 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800352 .setIndicator(tabView).setContent(contentFactory));
353 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
354 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700355 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800356 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700357 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
358 public void onTabChanged(String tabId) {
Winson Chunga12a2502010-12-20 14:41:35 -0800359 final CustomizePagedView.CustomizationType newType =
360 getCustomizeFilterForTabTag(tabId);
361 if (newType != mCustomizePagedView.getCustomizationFilter()) {
362 // animate the changing of the tab content by fading pages in and out
363 final Resources res = getResources();
364 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
365 final float alpha = mCustomizePagedView.getAlpha();
366 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
367 "alpha", alpha, 0.0f);
368 alphaAnim.setDuration(duration);
369 alphaAnim.addListener(new AnimatorListenerAdapter() {
370 @Override
371 public void onAnimationEnd(Animator animation) {
Winson Chunga12a2502010-12-20 14:41:35 -0800372 mCustomizePagedView.setCustomizationFilter(newType);
Winson Chung80baf5a2010-08-09 16:03:15 -0700373
Winson Chunga12a2502010-12-20 14:41:35 -0800374 final float alpha = mCustomizePagedView.getAlpha();
375 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
376 mCustomizePagedView, "alpha", alpha, 1.0f);
377 alphaAnim.setDuration(duration);
378 alphaAnim.start();
379 }
380 });
381 alphaAnim.start();
382 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700383 }
384 });
Michael Jurka946ad472010-07-09 18:05:18 -0700385 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 setupViews();
387
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800388 registerContentObservers();
389
Joe Onorato7c312c12009-08-13 21:36:53 -0700390 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700391
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 mSavedState = savedInstanceState;
393 restoreState(mSavedState);
394
Winson Chunga12a2502010-12-20 14:41:35 -0800395 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800396 if (mCustomizePagedView != null) {
397 mCustomizePagedView.update();
398 }
Winson Chunga12a2502010-12-20 14:41:35 -0800399
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 if (PROFILE_STARTUP) {
401 android.os.Debug.stopMethodTracing();
402 }
403
404 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400405 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 }
407
408 // For handling default keys
409 mDefaultKeySsb = new SpannableStringBuilder();
410 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800411
412 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
413 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800414
415 // If we have a saved version of these external icons, we load them up immediately
416 if (LauncherApplication.isScreenXLarge()) {
417 if (sGlobalSearchIcon != null) {
418 updateGlobalSearchIcon(sGlobalSearchIcon);
419 }
420 if (sVoiceSearchIcon != null) {
421 updateVoiceSearchIcon(sVoiceSearchIcon);
422 }
423 if (sAppMarketIcon != null) {
424 updateAppMarketIcon(sAppMarketIcon);
425 }
426 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 }
Romain Guycbb89e42009-06-08 15:52:54 -0700428
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800429 @Override
430 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
431 super.dispatchPopulateAccessibilityEvent(event);
432
433 // we want to take over text population so it is context dependent
434 event.getText().clear();
435 if (mState == State.ALL_APPS) {
436 event.getText().add(getString(R.string.all_apps_button_label));
437 } else if (mState == State.WORKSPACE) {
438 event.getText().add(getString(R.string.all_apps_home_button_label));
439 }
440
441 return true;
442 }
443
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700445 if (sLocaleConfiguration == null) {
446 new AsyncTask<Void, Void, LocaleConfiguration>() {
447 @Override
448 protected LocaleConfiguration doInBackground(Void... unused) {
449 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
450 readConfiguration(Launcher.this, localeConfiguration);
451 return localeConfiguration;
452 }
453
454 @Override
455 protected void onPostExecute(LocaleConfiguration result) {
456 sLocaleConfiguration = result;
457 checkForLocaleChange(); // recursive, but now with a locale configuration
458 }
459 }.execute();
460 return;
461 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700462
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 final Configuration configuration = getResources().getConfiguration();
464
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700465 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 final String locale = configuration.locale.toString();
467
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700468 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 final int mcc = configuration.mcc;
470
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700471 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 final int mnc = configuration.mnc;
473
Romain Guy84f296c2009-11-04 15:00:44 -0800474 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475
Romain Guy84f296c2009-11-04 15:00:44 -0800476 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700477 sLocaleConfiguration.locale = locale;
478 sLocaleConfiguration.mcc = mcc;
479 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700480
Joe Onorato0589f0f2010-02-08 13:44:00 -0800481 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400482 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700483
484 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
485 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700486 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700487 public void run() {
488 writeConfiguration(Launcher.this, localeConfiguration);
489 }
490 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700491 }
492 }
493
494 private static class LocaleConfiguration {
495 public String locale;
496 public int mcc = -1;
497 public int mnc = -1;
498 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700499
Romain Guy98d01652009-06-30 16:21:04 -0700500 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
501 DataInputStream in = null;
502 try {
503 in = new DataInputStream(context.openFileInput(PREFERENCES));
504 configuration.locale = in.readUTF();
505 configuration.mcc = in.readInt();
506 configuration.mnc = in.readInt();
507 } catch (FileNotFoundException e) {
508 // Ignore
509 } catch (IOException e) {
510 // Ignore
511 } finally {
512 if (in != null) {
513 try {
514 in.close();
515 } catch (IOException e) {
516 // Ignore
517 }
518 }
519 }
520 }
521
522 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
523 DataOutputStream out = null;
524 try {
525 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
526 out.writeUTF(configuration.locale);
527 out.writeInt(configuration.mcc);
528 out.writeInt(configuration.mnc);
529 out.flush();
530 } catch (FileNotFoundException e) {
531 // Ignore
532 } catch (IOException e) {
533 //noinspection ResultOfMethodCallIgnored
534 context.getFileStreamPath(PREFERENCES).delete();
535 } finally {
536 if (out != null) {
537 try {
538 out.close();
539 } catch (IOException e) {
540 // Ignore
541 }
542 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 }
544 }
545
546 static int getScreen() {
547 synchronized (sLock) {
548 return sScreen;
549 }
550 }
551
552 static void setScreen(int screen) {
553 synchronized (sLock) {
554 sScreen = screen;
555 }
556 }
557
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400558 // Note: This doesn't do all the client-id magic that BrowserProvider does
559 // in Browser. (http://b/2425179)
560 private Uri getDefaultBrowserUri() {
561 String url = getString(R.string.default_browser_url);
562 if (url.indexOf("{CID}") != -1) {
563 url = url.replace("{CID}", "android-google");
564 }
565 return Uri.parse(url);
566 }
567
568 // Load the Intent templates from arrays.xml to populate the hotseats. For
569 // each Intent, if it resolves to a single app, use that as the launch
570 // intent & use that app's label as the contentDescription. Otherwise,
571 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400572 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400573 if (mHotseatConfig == null) {
574 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
575 if (mHotseatConfig.length > 0) {
576 mHotseats = new Intent[mHotseatConfig.length];
577 mHotseatLabels = new CharSequence[mHotseatConfig.length];
578 mHotseatIcons = new Drawable[mHotseatConfig.length];
579 } else {
580 mHotseats = null;
581 mHotseatIcons = null;
582 mHotseatLabels = null;
583 }
584
585 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
586 for (int i=0; i<mHotseatConfig.length; i++) {
587 // load icon for this slot; currently unrelated to the actual activity
588 try {
589 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
590 } catch (ArrayIndexOutOfBoundsException ex) {
591 Log.w(TAG, "Missing hotseat_icons array item #" + i);
592 mHotseatIcons[i] = null;
593 }
594 }
595 hotseatIconDrawables.recycle();
596 }
597
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400598 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400599 for (int i=0; i<mHotseatConfig.length; i++) {
600 Intent intent = null;
601 if (mHotseatConfig[i].equals("*BROWSER*")) {
602 // magic value meaning "launch user's default web browser"
603 // replace it with a generic web request so we can see if there is indeed a default
604 String defaultUri = getString(R.string.default_browser_url);
605 intent = new Intent(
606 Intent.ACTION_VIEW,
607 ((defaultUri != null)
608 ? Uri.parse(defaultUri)
609 : getDefaultBrowserUri())
610 ).addCategory(Intent.CATEGORY_BROWSABLE);
611 // note: if the user launches this without a default set, she
612 // will always be taken to the default URL above; this is
613 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700614 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400615 // URL is unavoidably sent to the chosen app.
616 } else {
617 try {
618 intent = Intent.parseUri(mHotseatConfig[i], 0);
619 } catch (java.net.URISyntaxException ex) {
620 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
621 // bogus; leave intent=null
622 }
623 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700624
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400625 if (intent == null) {
626 mHotseats[i] = null;
627 mHotseatLabels[i] = getText(R.string.activity_not_found);
628 continue;
629 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400630
631 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700632 Log.d(TAG, "loadHotseats: hotseat " + i
633 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400634 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400635 + "]");
636 }
637
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400638 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
639 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700640 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400641 Log.d(TAG, "Best match for intent: " + bestMatch);
642 Log.d(TAG, "All matches: ");
643 for (ResolveInfo ri : allMatches) {
644 Log.d(TAG, " --> " + ri);
645 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400646 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400647 // did this resolve to a single app, or the resolver?
648 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700649 // can't find any activity to handle this. let's leave the
650 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400651 // to launch.
652 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400653
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400654 // set accessibility text to "Not installed"
655 mHotseatLabels[i] = getText(R.string.activity_not_found);
656 } else {
657 boolean found = false;
658 for (ResolveInfo ri : allMatches) {
659 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
660 && bestMatch.activityInfo.applicationInfo.packageName
661 .equals(ri.activityInfo.applicationInfo.packageName)) {
662 found = true;
663 break;
664 }
665 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700666
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400667 if (!found) {
668 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
669 // the bestMatch is probably the ResolveActivity, meaning the
670 // user has not yet selected a default
671 // so: we'll keep the original intent for now
672 mHotseats[i] = intent;
673
674 // set the accessibility text to "Select shortcut"
675 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
676 } else {
677 // we have an app!
678 // now reconstruct the intent to launch it through the front
679 // door
680 ComponentName com = new ComponentName(
681 bestMatch.activityInfo.applicationInfo.packageName,
682 bestMatch.activityInfo.name);
683 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
684
685 // load the app label for accessibility
686 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
687 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400688 }
689
690 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700691 Log.d(TAG, "loadHotseats: hotseat " + i
692 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400693 + ((mHotseats[i] == null)
694 ? "null"
695 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400696 + "] label=[" + mHotseatLabels[i]
697 + "]"
698 );
699 }
700 }
701 }
702
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 @Override
704 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700705 mWaitingForResult = false;
706
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 // The pattern used here is that a user PICKs a specific application,
708 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700709
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
711 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700712
Michael Jurka0280c3b2010-09-17 15:00:07 -0700713 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 switch (requestCode) {
715 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700716 completeAddApplication(
717 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 break;
719 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800720 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 break;
722 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700723 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 break;
725 case REQUEST_PICK_LIVE_FOLDER:
726 addLiveFolder(data);
727 break;
728 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700729 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700731 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700732 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700734 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700735 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700736 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700738 case REQUEST_PICK_WALLPAPER:
739 // We just wanted the activity result here so we can clear mWaitingForResult
740 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 }
Romain Guy18042c82009-11-06 11:44:55 -0800742 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
743 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
744 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700745 // Clean up the appWidgetId if we canceled
746 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
747 if (appWidgetId != -1) {
748 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 }
750 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 }
752
753 @Override
754 protected void onResume() {
755 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800756 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700757 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400758 mWorkspaceLoading = true;
759 mModel.startLoader(this, true);
760 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700761 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700763 // When we resume Launcher, a different Activity might be responsible for the app
764 // market intent, so refresh the icon
765 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 }
767
768 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700769 protected void onPause() {
770 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700771 // Some launcher layouts don't have a previous and next view
772 if (mPreviousView != null) {
773 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700774 }
775 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700776 dismissPreview(mNextView);
777 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700778 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800779 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700780 }
Romain Guycbb89e42009-06-08 15:52:54 -0700781
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700782 @Override
783 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400784 // Flag the loader to stop early before switching
785 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800786 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800787 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700788 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700789
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800790 // We can't hide the IME if it was forced open. So don't bother
791 /*
792 @Override
793 public void onWindowFocusChanged(boolean hasFocus) {
794 super.onWindowFocusChanged(hasFocus);
795
796 if (hasFocus) {
797 final InputMethodManager inputManager = (InputMethodManager)
798 getSystemService(Context.INPUT_METHOD_SERVICE);
799 WindowManager.LayoutParams lp = getWindow().getAttributes();
800 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
801 android.os.Handler()) {
802 protected void onReceiveResult(int resultCode, Bundle resultData) {
803 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
804 }
805 });
806 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
807 }
808 }
809 */
810
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 private boolean acceptFilter() {
812 final InputMethodManager inputManager = (InputMethodManager)
813 getSystemService(Context.INPUT_METHOD_SERVICE);
814 return !inputManager.isFullscreenMode();
815 }
816
817 @Override
818 public boolean onKeyDown(int keyCode, KeyEvent event) {
819 boolean handled = super.onKeyDown(keyCode, event);
820 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
821 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
822 keyCode, event);
823 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700824 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700825 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700826 // showSearchDialog()
827 // If there are multiple keystrokes before the search dialog takes focus,
828 // onSearchRequested() will be called for every keystroke,
829 // but it is idempotent, so it's fine.
830 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 }
832 }
833
Joe Onorato8a9625e2010-01-28 15:55:35 -0800834 // Eat the long press event so the keyboard doesn't come up.
835 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
836 return true;
837 }
838
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 return handled;
840 }
841
Karl Rosaen138a0412009-04-23 19:00:21 -0700842 private String getTypedText() {
843 return mDefaultKeySsb.toString();
844 }
845
846 private void clearTypedText() {
847 mDefaultKeySsb.clear();
848 mDefaultKeySsb.clearSpans();
849 Selection.setSelection(mDefaultKeySsb, 0);
850 }
851
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700853 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
854 * State
855 */
856 private static State intToState(int stateOrdinal) {
857 State state = State.WORKSPACE;
858 final State[] stateValues = State.values();
859 for (int i = 0; i < stateValues.length; i++) {
860 if (stateValues[i].ordinal() == stateOrdinal) {
861 state = stateValues[i];
862 break;
863 }
864 }
865 return state;
866 }
867
868 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 * Restores the previous state, if it exists.
870 *
871 * @param savedState The previous state.
872 */
873 private void restoreState(Bundle savedState) {
874 if (savedState == null) {
875 return;
876 }
877
Michael Jurka883f55b2010-10-21 15:47:14 -0700878 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
879
880 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800881 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700882 } else if (state == State.CUSTOMIZE) {
883 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800884 }
885
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
887 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700888 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 }
890
891 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700892
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700894 mAddScreen = addScreen;
895 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
896 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 mRestoring = true;
898 }
899
900 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
901 if (renameFolder) {
902 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700903 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 mRestoring = true;
905 }
Winson Chunga12a2502010-12-20 14:41:35 -0800906
907 // Restore the current AllApps drawer tab
908 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
909 String curTab = savedState.getString("allapps_currentTab");
910 if (curTab != null) {
911 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
912 tabhost.setCurrentTabByTag(curTab);
913 }
914 int curPage = savedState.getInt("allapps_currentPage", -1);
915 if (curPage > -1) {
916 mAllAppsPagedView.setRestorePage(curPage);
917 }
918 }
919
920 // Restore the current customization drawer tab
921 if (mHomeCustomizationDrawer != null) {
922 String curTab = savedState.getString("customize_currentTab");
923 if (curTab != null) {
924 // We set this directly so that there is no delay before the tab is set
925 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
926 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
927 }
928
929 // Note: currently we do not restore the page for the customization tray because unlike
930 // AllApps, the page content can change drastically
931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 }
933
934 /**
935 * Finds all the views we need and configure them properly.
936 */
937 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700938 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400939
Romain Guyb1b69f52009-08-10 15:10:15 -0700940 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400941 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942
Joe Onorato7c312c12009-08-13 21:36:53 -0700943 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700944 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700945 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800946 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700947 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700948 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700949
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700950 if (LauncherApplication.isScreenXLarge()) {
951 // They need to be INVISIBLE initially so that they will be measured in the layout.
952 // Otherwise the animations are messed up when we show them for the first time.
953 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
954 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
955 }
956
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700958
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500960 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700961
Joe Onorato7c312c12009-08-13 21:36:53 -0700962 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
963 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700965 View handleView = findViewById(R.id.all_apps_button);
966 if (handleView != null && handleView instanceof HandleView) {
967 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700968 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700969 mHandleView.setLauncher(this);
970 mHandleView.setOnClickListener(this);
971 mHandleView.setOnLongClickListener(this);
972 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800973
Winson Chung80baf5a2010-08-09 16:03:15 -0700974 if (mCustomizePagedView != null) {
975 mCustomizePagedView.setLauncher(this);
976 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700977 } else {
978 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
979 hotseatLeft.setContentDescription(mHotseatLabels[0]);
980 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
981 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
982 hotseatRight.setContentDescription(mHotseatLabels[1]);
983 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400984
Michael Jurkaaf442092010-06-10 17:01:57 -0700985 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
986 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800987
Michael Jurkaaf442092010-06-10 17:01:57 -0700988 Drawable previous = mPreviousView.getDrawable();
989 Drawable next = mNextView.getDrawable();
990 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991
Michael Jurkaaf442092010-06-10 17:01:57 -0700992 mPreviousView.setHapticFeedbackEnabled(false);
993 mPreviousView.setOnLongClickListener(this);
994 mNextView.setHapticFeedbackEnabled(false);
995 mNextView.setOnLongClickListener(this);
996 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800997
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400999 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -08001001 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002
1003 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001004 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -08001005
1006 final View allAppsButton = findViewById(R.id.all_apps_button);
1007 final View divider = findViewById(R.id.divider);
1008 final View configureButton = findViewById(R.id.configure_button);
1009
Michael Jurka54f7ac32010-08-02 13:56:46 -07001010 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001011 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -07001012 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001013 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001014 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001015 dragController.addDragListener(deleteZone);
1016
Adam Cohencdc30d52010-12-01 15:09:47 -08001017 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1018 if (allAppsDeleteZone != null) {
1019 allAppsDeleteZone.setLauncher(this);
1020 allAppsDeleteZone.setDragController(dragController);
1021 allAppsDeleteZone.setDragAndDropEnabled(false);
1022 dragController.addDragListener(allAppsDeleteZone);
1023 dragController.addDropTarget(allAppsDeleteZone);
1024 }
1025
1026 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1027 findViewById(R.id.all_apps_info_target);
1028 if (allAppsInfoTarget != null) {
1029 allAppsInfoTarget.setLauncher(this);
1030 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001031 allAppsInfoTarget.setDragAndDropEnabled(false);
1032 View marketButton = findViewById(R.id.market_button);
1033 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001034 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -08001035 }
1036 }
1037
Joe Onorato00acb122009-08-04 16:04:30 -04001038 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001039 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001040 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001041
Joe Onorato00acb122009-08-04 16:04:30 -04001042 // The order here is bottom to top.
1043 dragController.addDropTarget(workspace);
1044 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001045 if (allAppsInfoTarget != null) {
1046 dragController.addDropTarget(allAppsInfoTarget);
1047 }
1048 if (allAppsDeleteZone != null) {
1049 dragController.addDropTarget(allAppsDeleteZone);
1050 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001051 mButtonCluster = findViewById(R.id.all_apps_button_cluster);
1052
1053 mAllAppsButton = findViewById(R.id.all_apps_button);
1054 mDivider = findViewById(R.id.divider);
1055 mConfigureButton = findViewById(R.id.configure_button);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 }
1057
Romain Guy8a73c512009-11-09 19:19:59 -08001058 @SuppressWarnings({"UnusedDeclaration"})
1059 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001060 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001061 mWorkspace.scrollLeft();
1062 }
Romain Guy8a73c512009-11-09 19:19:59 -08001063 }
1064
1065 @SuppressWarnings({"UnusedDeclaration"})
1066 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001067 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001068 mWorkspace.scrollRight();
1069 }
Romain Guy8a73c512009-11-09 19:19:59 -08001070 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001071
1072 @SuppressWarnings({"UnusedDeclaration"})
1073 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001074 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001075
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001076 int index = -1;
1077 if (v.getId() == R.id.hotseat_left) {
1078 index = 0;
1079 } else if (v.getId() == R.id.hotseat_right) {
1080 index = 1;
1081 }
1082
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001083 // reload these every tap; you never know when they might change
1084 loadHotseats();
1085 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001086 startActivitySafely(
1087 mHotseats[index],
1088 "hotseat"
1089 );
1090 }
1091 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 /**
1094 * Creates a view representing a shortcut.
1095 *
1096 * @param info The data structure describing the shortcut.
1097 *
1098 * @return A View inflated from R.layout.application.
1099 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001100 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001102 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 }
1104
1105 /**
1106 * Creates a view representing a shortcut inflated from the specified resource.
1107 *
1108 * @param layoutResId The id of the XML layout used to create the shortcut.
1109 * @param parent The group the shortcut belongs to.
1110 * @param info The data structure describing the shortcut.
1111 *
1112 * @return A View inflated from layoutResId.
1113 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001114 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001115 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1116 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 return favorite;
1119 }
1120
1121 /**
1122 * Add an application shortcut to the workspace.
1123 *
1124 * @param data The intent describing the application.
1125 * @param cellInfo The position on screen where to create the shortcut.
1126 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001127 void completeAddApplication(Context context, Intent data, int screen,
1128 int intersectCellX, int intersectCellY) {
1129 final int[] cellXY = mTmpAddItemCellCoordinates;
1130 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1131
1132 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1133 showOutOfSpaceMessage();
1134 return;
1135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136
Joe Onorato0589f0f2010-02-08 13:44:00 -08001137 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1138 data, context);
1139
Romain Guy73b979d2009-06-09 12:57:21 -07001140 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001141 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001143 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001144 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1145 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001146 } else {
1147 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 }
1149 }
Romain Guycbb89e42009-06-08 15:52:54 -07001150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 /**
1152 * Add a shortcut to the workspace.
1153 *
1154 * @param data The intent describing the shortcut.
1155 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001157 private void completeAddShortcut(Intent data, int screen,
1158 int intersectCellX, int intersectCellY) {
1159 final int[] cellXY = mTmpAddItemCellCoordinates;
1160 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001161
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001162 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001163 boolean foundCellSpan = false;
1164 if (touchXY != null) {
1165 // when dragging and dropping, just find the closest free spot
1166 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1167 int[] result = screenLayout.findNearestVacantArea(
1168 touchXY[0], touchXY[1], 1, 1, cellXY);
1169 foundCellSpan = (result != null);
1170 } else {
1171 foundCellSpan = layout.findCellForSpanThatIntersects(
1172 cellXY, 1, 1, intersectCellX, intersectCellY);
1173 }
1174
1175 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001176 showOutOfSpaceMessage();
1177 return;
1178 }
1179
1180 final ShortcutInfo info = mModel.addShortcut(
1181 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182
1183 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001185 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 }
1187 }
1188
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001190 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001192 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001193 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001195 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001196 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001197
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001198 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001199 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1200 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001203 // We have saved the position to which the widget was dragged-- this really only matters
1204 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001205 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001206
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001207 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001208 boolean foundCellSpan = false;
1209 if (touchXY != null) {
1210 // when dragging and dropping, just find the closest free spot
1211 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1212 int[] result = screenLayout.findNearestVacantArea(
1213 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001214 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001215 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001216 // if we long pressed on an empty cell to bring up a menu,
1217 // make sure we intersect the empty cell
1218 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1219 // findCellForSpanThatIntersects will just ignore them
1220 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1221 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001222 }
1223
Michael Jurka0280c3b2010-09-17 15:00:07 -07001224 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001225 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001226 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001227 return;
1228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001230 // Build Launcher-specific widget info and save to database
1231 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001232 launcherInfo.spanX = spanXY[0];
1233 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001234
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 LauncherModel.addItemToDatabase(this, launcherInfo,
1236 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001237 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238
1239 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001240 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001243 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001244
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001245 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001247
Michael Jurka0280c3b2010-09-17 15:00:07 -07001248 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001249 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001250
1251 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 }
1253 }
Romain Guycbb89e42009-06-08 15:52:54 -07001254
Adam Cohended9f8d2010-11-03 13:25:16 -07001255 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1256 @Override
1257 public void onReceive(Context context, Intent intent) {
1258 final String action = intent.getAction();
1259 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1260 mUserPresent = false;
1261 updateRunning();
1262 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1263 mUserPresent = true;
1264 updateRunning();
1265 }
1266 }
1267 };
1268
1269 @Override
1270 public void onAttachedToWindow() {
1271 super.onAttachedToWindow();
1272
1273 // Listen for broadcasts related to user-presence
1274 final IntentFilter filter = new IntentFilter();
1275 filter.addAction(Intent.ACTION_SCREEN_OFF);
1276 filter.addAction(Intent.ACTION_USER_PRESENT);
1277 registerReceiver(mReceiver, filter);
1278
Adam Cohend113e0c2010-11-11 10:48:05 -08001279 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001280 mVisible = true;
1281 }
1282
1283 @Override
1284 public void onDetachedFromWindow() {
1285 super.onDetachedFromWindow();
1286 mVisible = false;
1287
Adam Cohend113e0c2010-11-11 10:48:05 -08001288 if (mAttached) {
1289 unregisterReceiver(mReceiver);
1290 mAttached = false;
1291 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001292 updateRunning();
1293 }
1294
1295 public void onWindowVisibilityChanged(int visibility) {
1296 mVisible = visibility == View.VISIBLE;
1297 updateRunning();
1298 }
1299
1300 private void sendAdvanceMessage(long delay) {
1301 mHandler.removeMessages(ADVANCE_MSG);
1302 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1303 mHandler.sendMessageDelayed(msg, delay);
1304 mAutoAdvanceSentTime = System.currentTimeMillis();
1305 }
1306
1307 private void updateRunning() {
1308 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1309 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1310 mAutoAdvanceRunning = autoAdvanceRunning;
1311 if (autoAdvanceRunning) {
1312 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1313 sendAdvanceMessage(delay);
1314 } else {
1315 if (!mWidgetsToAdvance.isEmpty()) {
1316 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1317 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1318 }
1319 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001320 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001321 }
1322 }
1323 }
1324
1325 private final Handler mHandler = new Handler() {
1326 @Override
1327 public void handleMessage(Message msg) {
1328 if (msg.what == ADVANCE_MSG) {
1329 int i = 0;
1330 for (View key: mWidgetsToAdvance.keySet()) {
1331 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1332 final int delay = mAdvanceStagger * i;
1333 if (v instanceof Advanceable) {
1334 postDelayed(new Runnable() {
1335 public void run() {
1336 ((Advanceable) v).advance();
1337 }
1338 }, delay);
1339 }
1340 i++;
1341 }
1342 sendAdvanceMessage(mAdvanceInterval);
1343 }
1344 }
1345 };
1346
1347 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1348 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1349 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1350 if (v instanceof Advanceable) {
1351 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001352 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001353 updateRunning();
1354 }
1355 }
1356
1357 void removeWidgetToAutoAdvance(View hostView) {
1358 if (mWidgetsToAdvance.containsKey(hostView)) {
1359 mWidgetsToAdvance.remove(hostView);
1360 updateRunning();
1361 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001362 }
1363
Joe Onorato9c1289c2009-08-17 11:03:03 -04001364 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1365 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001366 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001367 launcherInfo.hostView = null;
1368 }
1369
Adam Cohended9f8d2010-11-03 13:25:16 -07001370 void showOutOfSpaceMessage() {
1371 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1372 }
1373
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001374 public LauncherAppWidgetHost getAppWidgetHost() {
1375 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 }
Romain Guycbb89e42009-06-08 15:52:54 -07001377
Winson Chunga9abd0e2010-10-27 17:18:37 -07001378 public LauncherModel getModel() {
1379 return mModel;
1380 }
1381
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001382 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001383 getWindow().closeAllPanels();
1384
1385 try {
1386 dismissDialog(DIALOG_CREATE_SHORTCUT);
1387 // Unlock the workspace if the dialog was showing
1388 } catch (Exception e) {
1389 // An exception is thrown if the dialog is not visible, which is fine
1390 }
1391
1392 try {
1393 dismissDialog(DIALOG_RENAME_FOLDER);
1394 // Unlock the workspace if the dialog was showing
1395 } catch (Exception e) {
1396 // An exception is thrown if the dialog is not visible, which is fine
1397 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001398
1399 // Whatever we were doing is hereby canceled.
1400 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001401 }
1402
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 @Override
1404 protected void onNewIntent(Intent intent) {
1405 super.onNewIntent(intent);
1406
1407 // Close the menu
1408 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001409 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001410 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001411
Joe Onorato14f122b2009-11-19 14:06:36 -08001412 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1413 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001414
Michael Jurka4cb37242010-08-09 21:05:32 -07001415 // in all these cases, only animate if we're already on home
1416 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001417 mWorkspace.unshrink(alreadyOnHome);
1418 }
1419 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001420 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac5b262c2011-01-12 20:24:50 -08001421 boolean animate = alreadyOnHome &&
1422 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS);
1423 mWorkspace.moveToDefaultScreen(animate);
1424 if (!animate) mWorkspace.updateWallpaperOffsetImmediately();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001425 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001426 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001427
Joe Onorato3a8820b2009-11-10 15:06:42 -08001428 final View v = getWindow().peekDecorView();
1429 if (v != null && v.getWindowToken() != null) {
1430 InputMethodManager imm = (InputMethodManager)getSystemService(
1431 INPUT_METHOD_SERVICE);
1432 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 }
1434 }
1435 }
1436
1437 @Override
1438 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1439 // Do not call super here
1440 mSavedInstanceState = savedInstanceState;
1441 }
1442
1443 @Override
1444 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001445 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446
1447 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1448 if (folders.size() > 0) {
1449 final int count = folders.size();
1450 long[] ids = new long[count];
1451 for (int i = 0; i < count; i++) {
1452 final FolderInfo info = folders.get(i).getInfo();
1453 ids[i] = info.id;
1454 }
1455 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1456 } else {
1457 super.onSaveInstanceState(outState);
1458 }
1459
Michael Jurka883f55b2010-10-21 15:47:14 -07001460 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461
Michael Jurka0280c3b2010-09-17 15:00:07 -07001462 if (mAddScreen > -1 && mWaitingForResult) {
1463 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1464 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1465 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 }
1467
1468 if (mFolderInfo != null && mWaitingForResult) {
1469 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1470 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1471 }
Michael Jurka946ad472010-07-09 18:05:18 -07001472
Winson Chung43b119d2010-12-06 16:45:05 -08001473 // Save the current AllApps drawer tab
1474 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1475 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1476 String currentTabTag = tabhost.getCurrentTabTag();
1477 if (currentTabTag != null) {
1478 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001479 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001480 }
1481 }
1482
1483 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001484 if (mHomeCustomizationDrawer != null) {
1485 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1486 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001487 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001488 }
1489 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 }
1491
1492 @Override
1493 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001497 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001499 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001501 mAppWidgetHost = null;
1502
1503 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504
1505 TextKeyListener.getInstance().release();
1506
Joe Onorato9c1289c2009-08-17 11:03:03 -04001507 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508
Joe Onorato9c1289c2009-08-17 11:03:03 -04001509 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001510
1511 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001512
Patrick Dubroyab962b72010-07-26 12:10:10 -07001513 // Some launcher layouts don't have a previous and next view
1514 if (mPreviousView != null) {
1515 dismissPreview(mPreviousView);
1516 }
1517 if (mNextView != null) {
1518 dismissPreview(mNextView);
1519 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001520
1521 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001522
1523 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1524 mWorkspace.removeAllViews();
1525 mWorkspace = null;
1526 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001527
1528 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 }
1530
1531 @Override
1532 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001533 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 super.startActivityForResult(intent, requestCode);
1535 }
1536
1537 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001538 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001540
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001541 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001542
Karl Rosaen138a0412009-04-23 19:00:21 -07001543 if (initialQuery == null) {
1544 // Use any text typed in the launcher as the initial query
1545 initialQuery = getTypedText();
1546 clearTypedText();
1547 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 if (appSearchData == null) {
1549 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001550 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 }
Romain Guycbb89e42009-06-08 15:52:54 -07001552
Karl Rosaen138a0412009-04-23 19:00:21 -07001553 final SearchManager searchManager =
1554 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001555 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001556 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 }
1558
1559 @Override
1560 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001561 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001562 return false;
1563 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564
1565 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001566
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1568 .setIcon(android.R.drawable.ic_menu_add)
1569 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001570 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1571 .setIcon(android.R.drawable.ic_menu_manage)
1572 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001573 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 .setIcon(android.R.drawable.ic_menu_gallery)
1575 .setAlphabeticShortcut('W');
1576 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1577 .setIcon(android.R.drawable.ic_search_category_default)
1578 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1579 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1580 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1581 .setAlphabeticShortcut('N');
1582
1583 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001584 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1585 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586
1587 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1588 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1589 .setIntent(settings);
1590
1591 return true;
1592 }
1593
1594 @Override
1595 public boolean onPrepareOptionsMenu(Menu menu) {
1596 super.onPrepareOptionsMenu(menu);
1597
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001598 // If all apps is animating, don't show the menu, because we don't know
1599 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001600 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001601 return false;
1602 }
1603
1604 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001605 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001606 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1607 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1608
1609 // Disable add if the workspace is full.
1610 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001611 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1612 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001613 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614
1615 return true;
1616 }
1617
1618 @Override
1619 public boolean onOptionsItemSelected(MenuItem item) {
1620 switch (item.getItemId()) {
1621 case MENU_ADD:
1622 addItems();
1623 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001624 case MENU_MANAGE_APPS:
1625 manageApps();
1626 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 case MENU_WALLPAPER_SETTINGS:
1628 startWallpaper();
1629 return true;
1630 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001631 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 return true;
1633 case MENU_NOTIFICATIONS:
1634 showNotifications();
1635 return true;
1636 }
1637
1638 return super.onOptionsItemSelected(item);
1639 }
Romain Guycbb89e42009-06-08 15:52:54 -07001640
Karl Rosaen138a0412009-04-23 19:00:21 -07001641 /**
1642 * Indicates that we want global search for this activity by setting the globalSearch
1643 * argument for {@link #startSearch} to true.
1644 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001646 @Override
1647 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001648 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001649 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001650 }
1651
Joe Onorato9c1289c2009-08-17 11:03:03 -04001652 public boolean isWorkspaceLocked() {
1653 return mWorkspaceLoading || mWaitingForResult;
1654 }
1655
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001657 if (LauncherApplication.isScreenXLarge()) {
1658 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001659 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001660 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001661 }
1662 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001663 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001664 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001665 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 }
1667
Michael Jurka0280c3b2010-09-17 15:00:07 -07001668 private void resetAddInfo() {
1669 mAddScreen = -1;
1670 mAddIntersectCellX = -1;
1671 mAddIntersectCellY = -1;
1672 mAddDropPosition = null;
1673 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001674
Winson Chung55cef262010-10-28 14:14:18 -07001675 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001676 resetAddInfo();
1677 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001678 mAddDropPosition = position;
1679
Michael Jurkaaf442092010-06-10 17:01:57 -07001680 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001681 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1682 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001683 }
1684
Winson Chung7ad01412010-09-27 11:33:03 -07001685 private void manageApps() {
1686 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1687 }
1688
Michael Jurkaaf442092010-06-10 17:01:57 -07001689 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001690 // TODO: catch bad widget exception when sent
1691 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001692 // TODO: Is this log message meaningful?
1693 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001694 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001695 }
1696
Winson Chung55cef262010-10-28 14:14:18 -07001697 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001698 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699
Bjorn Bringert7984c942009-12-09 15:38:25 +00001700 if (appWidget.configure != null) {
1701 // Launch over to configure widget, if needed
1702 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1703 intent.setComponent(appWidget.configure);
1704 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001705 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001706 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1707 intent.putExtra(
1708 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1709 info.mimeType);
1710
1711 final String mimeType = info.mimeType;
1712 final ClipData clipData = (ClipData) info.configurationData;
1713 final ClipDescription clipDesc = clipData.getDescription();
1714 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1715 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001716 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001717 final CharSequence stringData = item.getText();
1718 final Uri uriData = item.getUri();
1719 final Intent intentData = item.getIntent();
1720 final String key =
1721 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1722 if (uriData != null) {
1723 intent.putExtra(key, uriData);
1724 } else if (intentData != null) {
1725 intent.putExtra(key, intentData);
1726 } else if (stringData != null) {
1727 intent.putExtra(key, stringData);
1728 }
1729 break;
1730 }
1731 }
1732 }
Winson Chung55cef262010-10-28 14:14:18 -07001733 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001734
Romain Guy8e633c52010-03-04 12:51:36 -08001735 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001737 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001738 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 }
1740 }
Romain Guycbb89e42009-06-08 15:52:54 -07001741
Michael Jurka0280c3b2010-09-17 15:00:07 -07001742 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1743 resetAddInfo();
1744 mAddScreen = screen;
1745 mAddDropPosition = position;
1746
1747 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1748 createShortcutIntent.setComponent(componentName);
1749 processShortcut(createShortcutIntent);
1750 }
1751
Joe Onoratodeb98af2010-02-19 14:59:39 -08001752 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001753 // Handle case where user selected "Applications"
1754 String applicationName = getResources().getString(R.string.group_applications);
1755 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001756
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001757 if (applicationName != null && applicationName.equals(shortcutName)) {
1758 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1759 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001760
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001761 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1762 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001763 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001764 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001765 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001766 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001767 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 }
1769
Winson Chung24ab2f12010-09-16 14:10:47 -07001770 void processWallpaper(Intent intent) {
1771 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1772 }
1773
Michael Jurka0280c3b2010-09-17 15:00:07 -07001774 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1775 resetAddInfo();
1776 mAddScreen = screen;
1777 mAddDropPosition = position;
1778
1779 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1780 createFolderIntent.setComponent(componentName);
1781
1782 addLiveFolder(createFolderIntent);
1783 }
1784
1785 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001786 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001787 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001788 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001789
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001790 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001791 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001792 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001793 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001794 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 }
1796
Michael Jurka0280c3b2010-09-17 15:00:07 -07001797 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 UserFolderInfo folderInfo = new UserFolderInfo();
1799 folderInfo.title = getText(R.string.folder_name);
1800
Michael Jurka0280c3b2010-09-17 15:00:07 -07001801 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1802 final int[] cellXY = mTmpAddItemCellCoordinates;
1803 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1804 showOutOfSpaceMessage();
1805 return;
1806 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807
1808 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001809 LauncherModel.addItemToDatabase(this, folderInfo,
1810 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001811 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001812 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813
1814 // Create the view
1815 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001816 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1817 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001818 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 }
Romain Guycbb89e42009-06-08 15:52:54 -07001820
Joe Onorato9c1289c2009-08-17 11:03:03 -04001821 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001822 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001823 }
1824
Michael Jurka28750fb2010-09-24 17:43:49 -07001825 private void completeAddLiveFolder(
1826 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001827 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1828 final int[] cellXY = mTmpAddItemCellCoordinates;
1829 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1830 showOutOfSpaceMessage();
1831 return;
1832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833
Michael Jurka0280c3b2010-09-17 15:00:07 -07001834 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835
1836 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001838 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001839 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 }
1841 }
1842
1843 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001844 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845
1846 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1847 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1848
1849 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 Intent.ShortcutIconResource iconResource = null;
1851
1852 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1853 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1854 try {
1855 iconResource = (Intent.ShortcutIconResource) extra;
1856 final PackageManager packageManager = context.getPackageManager();
1857 Resources resources = packageManager.getResourcesForApplication(
1858 iconResource.packageName);
1859 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1860 icon = resources.getDrawable(id);
1861 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001862 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 }
1864 }
1865
1866 if (icon == null) {
1867 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1868 }
1869
1870 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001871 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 info.title = name;
1873 info.iconResource = iconResource;
1874 info.uri = data.getData();
1875 info.baseIntent = baseIntent;
1876 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1877 LiveFolders.DISPLAY_MODE_GRID);
1878
1879 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001880 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001881 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882
1883 return info;
1884 }
1885
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 private void showNotifications() {
1887 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1888 if (statusBar != null) {
1889 statusBar.expand();
1890 }
1891 }
1892
1893 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001894 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001896 Intent chooser = Intent.createChooser(pickWallpaper,
1897 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001898 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1899 // Removed in Eclair MR1
1900// WallpaperManager wm = (WallpaperManager)
1901// getSystemService(Context.WALLPAPER_SERVICE);
1902// WallpaperInfo wi = wm.getWallpaperInfo();
1903// if (wi != null && wi.getSettingsActivity() != null) {
1904// LabeledIntent li = new LabeledIntent(getPackageName(),
1905// R.string.configure_wallpaper, 0);
1906// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1907// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1908// }
Mike Clerona0618e42009-10-22 13:55:21 -07001909 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 }
1911
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001912 /**
1913 * Registers various content observers. The current implementation registers
1914 * only a favorites observer to keep track of the favorites applications.
1915 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001916 private void registerContentObservers() {
1917 ContentResolver resolver = getContentResolver();
1918 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1919 true, mWidgetObserver);
1920 }
1921
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 @Override
1923 public boolean dispatchKeyEvent(KeyEvent event) {
1924 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1925 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001927 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001928 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001929 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001930 dumpState();
1931 return true;
1932 }
1933 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001934 }
1935 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1936 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001937 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 return true;
1939 }
1940 }
1941
1942 return super.dispatchKeyEvent(event);
1943 }
1944
Joe Onorato88ec0992009-11-19 13:16:06 -08001945 @Override
1946 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001947 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1948 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001949 } else {
1950 closeFolder();
1951 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001952 // Some launcher layouts don't have a previous and next view
1953 if (mPreviousView != null) {
1954 dismissPreview(mPreviousView);
1955 dismissPreview(mNextView);
1956 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001957 }
1958
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 private void closeFolder() {
1960 Folder folder = mWorkspace.getOpenFolder();
1961 if (folder != null) {
1962 closeFolder(folder);
1963 }
1964 }
1965
1966 void closeFolder(Folder folder) {
1967 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08001968 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001970 CellLayout cl = (CellLayout) parent;
1971 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001972 if (folder instanceof DropTarget) {
1973 // Live folders aren't DropTargets.
1974 mDragController.removeDropTarget((DropTarget)folder);
1975 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 }
1977 folder.onClose();
1978 }
1979
1980 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001981 * Re-listen when widgets are reset.
1982 */
1983 private void onAppWidgetReset() {
1984 mAppWidgetHost.startListening();
1985 }
1986
1987 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001988 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1989 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001991 private void unbindDesktopItems() {
1992 for (ItemInfo item: mDesktopItems) {
1993 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001995 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001997
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 /**
1999 * Launches the intent referred by the clicked shortcut.
2000 *
2001 * @param v The view representing the clicked shortcut.
2002 */
2003 public void onClick(View v) {
2004 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002005 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002007 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002008 int[] pos = new int[2];
2009 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002010 intent.setSourceBounds(new Rect(pos[0], pos[1],
2011 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07002012 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 } else if (tag instanceof FolderInfo) {
2014 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002015 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002016 if (mState == State.ALL_APPS) {
2017 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002018 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002019 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 }
2022 }
2023
Michael Jurka0e260592010-06-30 17:07:39 -07002024 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002025 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002026 // clicking anywhere on the workspace causes the customization drawer to slide down
2027 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002028 return false;
2029 }
2030
Michael Jurkaaf442092010-06-10 17:01:57 -07002031 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002032 * Event handler for the search button
2033 *
2034 * @param v The view that was clicked.
2035 */
2036 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002037 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002038 // Use a custom animation for launching search
2039 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002040 }
2041
2042 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002043 * Event handler for the voice button
2044 *
2045 * @param v The view that was clicked.
2046 */
2047 public void onClickVoiceButton(View v) {
2048 startVoiceSearch();
2049 }
2050
2051 private void startVoiceSearch() {
2052 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2053 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2054 startActivity(intent);
2055 }
2056
2057 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002058 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002059 * enters home screen customization mode.
2060 *
2061 * @param v The view that was clicked.
2062 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002063 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002064 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002065 }
2066
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002067 /**
2068 * Event handler for the "grid" button that appears on the home screen, which
2069 * enters all apps mode.
2070 *
2071 * @param v The view that was clicked.
2072 */
2073 public void onClickAllAppsButton(View v) {
2074 showAllApps(true);
2075 }
2076
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002077 public void onClickAppMarketButton(View v) {
2078 if (mAppMarketIntent != null) {
2079 startActivitySafely(mAppMarketIntent, "app market");
2080 }
2081 }
2082
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002083 void startApplicationDetailsActivity(ComponentName componentName) {
2084 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002085 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2086 Uri.fromParts("package", packageName, null));
2087 startActivity(intent);
2088 }
2089
Patrick Dubroy5539af72010-09-07 15:22:01 -07002090 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2091 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2092 // System applications cannot be installed. For now, show a toast explaining that.
2093 // We may give them the option of disabling apps this way.
2094 int messageId = R.string.uninstall_system_app_text;
2095 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2096 } else {
2097 String packageName = appInfo.componentName.getPackageName();
2098 String className = appInfo.componentName.getClassName();
2099 Intent intent = new Intent(
2100 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2101 startActivity(intent);
2102 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002103 }
2104
Joe Onoratof984e852010-03-25 09:47:45 -07002105 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2107 try {
2108 startActivity(intent);
2109 } catch (ActivityNotFoundException e) {
2110 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002111 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 } catch (SecurityException e) {
2113 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002114 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002116 "or use the exported attribute for this activity. "
2117 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 }
2119 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002120
Romain Guy8e633c52010-03-04 12:51:36 -08002121 void startActivityForResultSafely(Intent intent, int requestCode) {
2122 try {
2123 startActivityForResult(intent, requestCode);
2124 } catch (ActivityNotFoundException e) {
2125 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2126 } catch (SecurityException e) {
2127 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2128 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2129 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2130 "or use the exported attribute for this activity.", e);
2131 }
2132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133
2134 private void handleFolderClick(FolderInfo folderInfo) {
2135 if (!folderInfo.opened) {
2136 // Close any open folder
2137 closeFolder();
2138 // Open the requested folder
2139 openFolder(folderInfo);
2140 } else {
2141 // Find the open folder...
2142 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2143 int folderScreen;
2144 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002145 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146 // .. and close it
2147 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002148 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 // Close any folder open on the current screen
2150 closeFolder();
2151 // Pull the folder onto this screen
2152 openFolder(folderInfo);
2153 }
2154 }
2155 }
2156 }
2157
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002159 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 * is animated relative to the specified View. If the View is null, no animation
2161 * is played.
2162 *
2163 * @param folderInfo The FolderInfo describing the folder to open.
2164 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002165 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 Folder openFolder;
2167
2168 if (folderInfo instanceof UserFolderInfo) {
2169 openFolder = UserFolder.fromXml(this);
2170 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002171 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 } else {
2173 return;
2174 }
2175
Joe Onorato00acb122009-08-04 16:04:30 -04002176 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 openFolder.setLauncher(this);
2178
2179 openFolder.bind(folderInfo);
2180 folderInfo.opened = true;
2181
Winson Chungaafa03c2010-06-11 17:34:16 -07002182 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002184 openFolder.onOpen();
2185 }
2186
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002188 switch (v.getId()) {
2189 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002190 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002191 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2192 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002193 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002194 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002195 return true;
2196 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002197 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002198 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2199 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002200 showPreviews(v);
2201 }
2202 return true;
2203 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002204 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002205 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2206 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2207 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002208 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002209 return true;
2210 }
2211
Joe Onorato9c1289c2009-08-17 11:03:03 -04002212 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213 return false;
2214 }
2215
2216 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002217 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 }
2219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220
Michael Jurka0280c3b2010-09-17 15:00:07 -07002221 resetAddInfo();
2222 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002224 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225 return true;
2226 }
2227
Michael Jurka0280c3b2010-09-17 15:00:07 -07002228 final View itemUnderLongClick = longClickCellInfo.cell;
2229
Winson Chung304dcde2011-01-07 11:17:23 -08002230 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002231 if (itemUnderLongClick == null) {
2232 // User long pressed on empty space
2233 mWorkspace.setAllowLongPress(false);
2234 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2235 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002236 if (LauncherApplication.isScreenXLarge()) {
2237 addItems();
2238 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002239 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 }
2241 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002242 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002243 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002244 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2245 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002246 mAddIntersectCellX = longClickCellInfo.cellX;
2247 mAddIntersectCellY = longClickCellInfo.cellY;
2248 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 }
2250 }
2251 }
2252 return true;
2253 }
2254
Romain Guye6b8e2f2009-11-10 11:56:55 -08002255 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002256 private void dismissPreview(final View v) {
2257 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002258 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002259 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2260 public void onDismiss() {
2261 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2262 int count = group.getChildCount();
2263 for (int i = 0; i < count; i++) {
2264 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2265 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002266 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2267 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2268
2269 v.setTag(R.id.workspace, null);
2270 v.setTag(R.id.icon, null);
2271 window.setOnDismissListener(null);
2272 }
2273 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002274 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002275 }
2276 v.setTag(null);
2277 }
2278
Romain Guyf8e6a802009-12-07 17:48:02 -08002279 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002280 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002281 }
2282
Romain Guya6abce82009-11-10 02:54:41 -08002283 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002284 final Resources resources = getResources();
2285 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002286
Romain Guya6abce82009-11-10 02:54:41 -08002287 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002288
Romain Guy9d31e9f2009-11-11 18:54:28 -08002289 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002290
Romain Guyf8e6a802009-12-07 17:48:02 -08002291 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002292 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002293 int extraW = (int) ((r.left + r.right) * max);
2294 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002295
2296 int aW = cell.getWidth() - extraW;
2297 float w = aW / max;
2298
2299 int width = cell.getWidth();
2300 int height = cell.getHeight();
2301 int x = cell.getLeftPadding();
2302 int y = cell.getTopPadding();
2303 width -= (x + cell.getRightPadding());
2304 height -= (y + cell.getBottomPadding());
2305
2306 float scale = w / width;
2307
2308 int count = end - start;
2309
2310 final float sWidth = width * scale;
2311 float sHeight = height * scale;
2312
Romain Guye6b8e2f2009-11-10 11:56:55 -08002313 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002314
Romain Guye6b8e2f2009-11-10 11:56:55 -08002315 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2316 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002317
2318 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002319 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002320 cell = (CellLayout) workspace.getChildAt(i);
2321
Romain Guyf8e6a802009-12-07 17:48:02 -08002322 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002323 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002324
2325 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002326 c.scale(scale, scale);
2327 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002328 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002329
Romain Guy9d31e9f2009-11-11 18:54:28 -08002330 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002331 image.setImageBitmap(bitmap);
2332 image.setTag(i);
2333 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002334 image.setOnFocusChangeListener(handler);
2335 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002336 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002337
2338 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002339 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2340
Winson Chungaafa03c2010-06-11 17:34:16 -07002341 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002342 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002343
2344 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002345 p.setContentView(preview);
2346 p.setWidth((int) (sWidth * count + extraW));
2347 p.setHeight((int) (sHeight + extraH));
2348 p.setAnimationStyle(R.style.AnimationPreview);
2349 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002350 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002351 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002352 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002353
Romain Guye6b8e2f2009-11-10 11:56:55 -08002354 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2355 public void onDismiss() {
2356 dismissPreview(anchor);
2357 }
2358 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002359
2360 anchor.setTag(p);
2361 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002362 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002363 }
2364
Romain Guy9d31e9f2009-11-11 18:54:28 -08002365 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002366 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002367
Romain Guye6b8e2f2009-11-10 11:56:55 -08002368 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002369 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002370 }
2371
2372 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002373 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002374 v.post(this);
2375 }
2376
2377 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002378 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002379 }
2380
2381 public void onFocusChange(View v, boolean hasFocus) {
2382 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002383 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002384 }
Romain Guya6abce82009-11-10 02:54:41 -08002385 }
2386 }
2387
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388 Workspace getWorkspace() {
2389 return mWorkspace;
2390 }
2391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002392 @Override
2393 protected Dialog onCreateDialog(int id) {
2394 switch (id) {
2395 case DIALOG_CREATE_SHORTCUT:
2396 return new CreateShortcut().createDialog();
2397 case DIALOG_RENAME_FOLDER:
2398 return new RenameFolder().createDialog();
2399 }
2400
2401 return super.onCreateDialog(id);
2402 }
2403
2404 @Override
2405 protected void onPrepareDialog(int id, Dialog dialog) {
2406 switch (id) {
2407 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 break;
2409 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002410 if (mFolderInfo != null) {
2411 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2412 final CharSequence text = mFolderInfo.title;
2413 input.setText(text);
2414 input.setSelection(0, text.length());
2415 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 break;
2417 }
2418 }
2419
2420 void showRenameDialog(FolderInfo info) {
2421 mFolderInfo = info;
2422 mWaitingForResult = true;
2423 showDialog(DIALOG_RENAME_FOLDER);
2424 }
2425
Michael Jurka0280c3b2010-09-17 15:00:07 -07002426 private void showAddDialog(int intersectX, int intersectY) {
2427 resetAddInfo();
2428 mAddIntersectCellX = intersectX;
2429 mAddIntersectCellY = intersectY;
2430 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 mWaitingForResult = true;
2432 showDialog(DIALOG_CREATE_SHORTCUT);
2433 }
2434
Joe Onoratodeb98af2010-02-19 14:59:39 -08002435 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002436 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002437 Bundle bundle = new Bundle();
2438
2439 ArrayList<String> shortcutNames = new ArrayList<String>();
2440 shortcutNames.add(getString(R.string.group_applications));
2441 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2442
2443 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2444 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2445 R.drawable.ic_launcher_application));
2446 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2447
2448 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2449 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002450 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002451 pickIntent.putExtras(bundle);
2452
Joe Onoratodeb98af2010-02-19 14:59:39 -08002453 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002454 }
2455
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002456 private class RenameFolder {
2457 private EditText mInput;
2458
2459 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002460 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2461 mInput = (EditText) layout.findViewById(R.id.folder_name);
2462
2463 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2464 builder.setIcon(0);
2465 builder.setTitle(getString(R.string.rename_folder_title));
2466 builder.setCancelable(true);
2467 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2468 public void onCancel(DialogInterface dialog) {
2469 cleanup();
2470 }
2471 });
2472 builder.setNegativeButton(getString(R.string.cancel_action),
2473 new Dialog.OnClickListener() {
2474 public void onClick(DialogInterface dialog, int which) {
2475 cleanup();
2476 }
2477 }
2478 );
2479 builder.setPositiveButton(getString(R.string.rename_action),
2480 new Dialog.OnClickListener() {
2481 public void onClick(DialogInterface dialog, int which) {
2482 changeFolderName();
2483 }
2484 }
2485 );
2486 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002487
2488 final AlertDialog dialog = builder.create();
2489 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2490 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002491 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002492 mInput.requestFocus();
2493 InputMethodManager inputManager = (InputMethodManager)
2494 getSystemService(Context.INPUT_METHOD_SERVICE);
2495 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002496 }
2497 });
2498
2499 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 }
2501
2502 private void changeFolderName() {
2503 final String name = mInput.getText().toString();
2504 if (!TextUtils.isEmpty(name)) {
2505 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002506 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002507 mFolderInfo.title = name;
2508 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2509
Joe Onorato9c1289c2009-08-17 11:03:03 -04002510 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002511 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002512 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 } else {
2514 final FolderIcon folderIcon = (FolderIcon)
2515 mWorkspace.getViewForTag(mFolderInfo);
2516 if (folderIcon != null) {
2517 folderIcon.setText(name);
2518 getWorkspace().requestLayout();
2519 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002520 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002521 mWorkspaceLoading = true;
2522 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 }
2524 }
2525 }
2526 cleanup();
2527 }
2528
2529 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002530 dismissDialog(DIALOG_RENAME_FOLDER);
2531 mWaitingForResult = false;
2532 mFolderInfo = null;
2533 }
2534 }
2535
Daniel Sandler843e8602010-06-07 14:59:01 -04002536 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2537 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002538 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002539 }
2540
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002541 // AllAppsView.Watcher
2542 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002543 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2544 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002545 mWorkspace.setVisibility(View.GONE);
2546 }
2547 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002548
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002549 private void showToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002550 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);
Michael Jurkaea573482011-02-03 19:48:18 -08002596 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002597 } else {
2598 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002599 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002600 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002601 }
2602
2603 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002604 * Show/hide the appropriate toolbar buttons for newState.
2605 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002606 *
2607 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002608 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2609 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002610 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002611 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002612 switch (newState) {
2613 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002614 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2615 mDeleteZone.setOverlappingViews(
2616 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002617 mDeleteZone.setDragAndDropEnabled(true);
2618 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002619 break;
2620 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002621 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002622 mDeleteZone.setDragAndDropEnabled(false);
2623 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002624 break;
2625 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002626 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002627 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002628 break;
2629 }
2630 }
2631
2632 /**
2633 * Helper method for the cameraZoomIn/cameraZoomOut animations
2634 * @param view The view being animated
2635 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2636 * @param scaleFactor The scale factor used for the zoom
2637 */
2638 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2639 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002640
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002641 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002642 // Set pivotY so that at the starting zoom factor, the view is partially
2643 // visible. Modifying initialHeightFactor changes how much of the view is
2644 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002645 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002646 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002647 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002648 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002649 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002650 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002651 }
2652 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002653
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002654 /**
2655 * Zoom the camera out from the workspace to reveal 'toView'.
2656 * Assumes that the view to show is anchored at either the very top or very bottom
2657 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002658 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002659 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002660 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002661 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002662 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002663
2664 final int duration = toAllApps ?
2665 res.getInteger(R.integer.config_allAppsZoomInTime) :
2666 res.getInteger(R.integer.config_customizeZoomInTime);
2667
2668 final float scale = toAllApps ?
2669 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2670 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2671
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002672 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002673
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002674 setPivotsForZoom(toView, toState, scale);
2675
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002676 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002677 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002678 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002679 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002680 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002681
2682 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002683 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2684 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
2685 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2686 public void onAnimationUpdate(ValueAnimator animation) {
2687 final float b = (Float) animation.getAnimatedValue();
2688 final float a = 1f - b;
2689 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002690 toView.setFastScaleX(a * scale + b * 1f);
2691 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002692 }
2693 });
Adam Cohen61033d32010-11-15 18:29:44 -08002694
Adam Cohenf16e5712011-01-13 13:31:45 -08002695 if (toAllApps) {
Michael Jurkad99cf8b2011-01-16 16:53:18 -08002696 toView.setAlpha(0f);
Michael Jurka742574b2011-02-02 23:51:01 -08002697 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurkaea573482011-02-03 19:48:18 -08002698 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002699 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2700 public void onAnimationUpdate(ValueAnimator animation) {
2701 final float b = (Float) animation.getAnimatedValue();
2702 final float a = 1f - b;
2703 // don't need to invalidate because we do so above
2704 toView.setFastAlpha(a * 0f + b * 1f);
2705 }
2706 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002707 alphaAnim.start();
2708 }
2709
Michael Jurkabfadaad2011-01-31 21:35:39 -08002710 // Only use hardware layers in portrait mode, they don't give any gains in landscape
2711 if (mWorkspace.getWidth() < mWorkspace.getHeight()) {
2712 toView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
2713 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002714 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002715 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002716 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002717 // Prepare the position
2718 toView.setTranslationX(0.0f);
2719 toView.setTranslationY(0.0f);
2720 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002721 if (!toAllApps) {
2722 toView.setAlpha(1.0f);
2723 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002724 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002725 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002726 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002727 // If we don't set the final scale values here, if this animation is cancelled
2728 // it will have the wrong scale value and subsequent cameraPan animations will
2729 // not fix that
Michael Jurka3eeaea82011-01-31 15:02:36 -08002730 toView.setLayerType(View.LAYER_TYPE_NONE, null);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002731 toView.setScaleX(1.0f);
2732 toView.setScaleY(1.0f);
2733 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002734 });
2735
Chet Haaseb1254a62010-09-07 13:35:00 -07002736 AnimatorSet toolbarHideAnim = new AnimatorSet();
2737 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002738 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2739
2740 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002741 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002742
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002743 if (mStateAnimation != null) mStateAnimation.cancel();
2744 mStateAnimation = new AnimatorSet();
2745 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2746 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002747
2748 // Show the new toolbar buttons just as the main animation is ending
2749 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002750 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2751 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002752 } else {
2753 toView.setTranslationX(0.0f);
2754 toView.setTranslationY(0.0f);
2755 toView.setScaleX(1.0f);
2756 toView.setScaleY(1.0f);
2757 toView.setVisibility(View.VISIBLE);
2758 hideAndShowToolbarButtons(toState, null, null);
2759 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002760 }
2761
2762 /**
2763 * Zoom the camera back into the workspace, hiding 'fromView'.
2764 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002765 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2766 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002767 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002768 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002769 cameraZoomIn(fromState, animated, false);
2770 }
2771
2772 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002773 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002774 final boolean fromAllApps = (fromState == State.ALL_APPS);
2775
2776 int duration = fromAllApps ?
2777 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2778 res.getInteger(R.integer.config_customizeZoomOutTime);
2779
Michael Jurka742574b2011-02-02 23:51:01 -08002780 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002781 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2782 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2783
2784 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002785
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002786 mCustomizePagedView.endChoiceMode();
2787 mAllAppsPagedView.endChoiceMode();
2788
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002789 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002790
Michael Jurkad3ef3062010-11-23 16:23:58 -08002791 if (!springLoaded) {
2792 mWorkspace.unshrink(animated);
2793 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002794
2795 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002796 if (mStateAnimation != null) mStateAnimation.cancel();
2797 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002798
Michael Jurka742574b2011-02-02 23:51:01 -08002799 final float oldScaleX = fromView.getScaleX();
2800 final float oldScaleY = fromView.getScaleY();
2801
2802 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2803 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2804 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2805 public void onAnimationUpdate(ValueAnimator animation) {
2806 final float b = (Float) animation.getAnimatedValue();
2807 final float a = 1f - b;
2808 ((View)fromView.getParent()).fastInvalidate();
2809 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2810 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2811 }
2812 });
2813 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002814 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002815 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002816 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2817 public void onAnimationUpdate(ValueAnimator animation) {
2818 final float b = (Float) animation.getAnimatedValue();
2819 final float a = 1f - b;
2820 // don't need to invalidate because we do so above
2821 fromView.setFastAlpha(a * 1f + b * 0f);
2822 }
2823 });
2824
Michael Jurkacf6125c2011-01-28 15:20:01 -08002825 fromView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002826 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002827 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002828 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002829 fromView.setVisibility(View.GONE);
Michael Jurkacf6125c2011-01-28 15:20:01 -08002830 fromView.setLayerType(View.LAYER_TYPE_NONE, null);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002831 }
2832 });
2833
Chet Haaseb1254a62010-09-07 13:35:00 -07002834 AnimatorSet toolbarHideAnim = new AnimatorSet();
2835 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002836 if (!springLoaded) {
2837 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2838 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002839
Adam Cohen61033d32010-11-15 18:29:44 -08002840 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002841
2842 // Show the new toolbar buttons at the very end of the whole animation
2843 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2844 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002845 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2846 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002847 } else {
2848 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002849 if (!springLoaded) {
2850 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2851 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002852 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002853 }
2854
2855 /**
2856 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2857 * This is the transition used on xlarge screens to go between all apps and
2858 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002859 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2860 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2861 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002862 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002863 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002864 final Resources res = getResources();
2865 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002866 final int workspaceHeight = mWorkspace.getHeight();
2867
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002868 final boolean fromAllApps = (fromState == State.ALL_APPS);
2869 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2870 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002871
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002872 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2873 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2874 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2875 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002876
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002877 mCustomizePagedView.endChoiceMode();
2878 mAllAppsPagedView.endChoiceMode();
2879
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002880 if (toState == State.ALL_APPS) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002881 mWorkspace.shrink(Workspace.ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002882 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002883 mWorkspace.shrink(Workspace.ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002884 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002885
2886 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002887 if (mStateAnimation != null) mStateAnimation.cancel();
2888 mStateAnimation = new AnimatorSet();
Michael Jurka8edd75c2010-12-17 20:15:06 -08002889 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002890 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002891 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002892 toView.setVisibility(View.VISIBLE);
2893 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002894 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002895 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002896 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002897 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002898 fromView.setVisibility(View.GONE);
2899 }
2900 });
2901
Chet Haaseb1254a62010-09-07 13:35:00 -07002902 AnimatorSet toolbarHideAnim = new AnimatorSet();
2903 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002904 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2905
Chet Haase472b2812010-10-14 07:02:04 -07002906 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2907 fromViewStartY, fromViewEndY);
2908 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002909 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2910 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2911 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2912 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2913 );
Chet Haase472b2812010-10-14 07:02:04 -07002914 fromAnim.setDuration(duration);
2915 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002916
2917 // Show the new toolbar buttons just as the main animation is ending
2918 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002919 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2920 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002921 } else {
2922 fromView.setY(fromViewEndY);
2923 fromView.setVisibility(View.GONE);
2924 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002925 toView.setScaleX(1.0f);
2926 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002927 toView.setVisibility(View.VISIBLE);
2928 hideAndShowToolbarButtons(toState, null, null);
2929 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002930 }
2931
2932 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002933 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002934 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002935 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002936
Michael Jurka79212d82010-07-30 16:36:20 -07002937 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002938 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002939 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002940 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002941 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002942 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002943 } else {
2944 mAllAppsGrid.zoom(1.0f, animated);
2945 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002946
Romain Guyc16fea72010-03-12 17:17:56 -08002947 ((View) mAllAppsGrid).setFocusable(true);
2948 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002949
Joe Onorato7c312c12009-08-13 21:36:53 -07002950 // TODO: fade these two too
2951 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002952
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002953 // Change the state *after* we've called all the transition code
2954 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002955
Winson Chung5fb63472011-02-02 17:03:37 -08002956 // Pause the auto-advance of widgets until we are out of AllApps
2957 mUserPresent = false;
2958 updateRunning();
2959
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002960 // send an accessibility event to announce the context change
2961 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2962 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002963
2964 void showWorkspace(boolean animated) {
2965 showWorkspace(animated, null);
2966 }
2967
2968 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002969 if (layout != null) {
2970 // always animated, but that's ok since we never specify a layout and
2971 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002972 mWorkspace.unshrink(layout);
2973 } else {
2974 mWorkspace.unshrink(animated);
2975 }
2976 if (mState == State.ALL_APPS) {
2977 closeAllApps(animated);
2978 } else if (mState == State.CUSTOMIZE) {
2979 hideCustomizationDrawer(animated);
2980 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002981
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002982 // Change the state *after* we've called all the transition code
2983 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002984
Winson Chung5fb63472011-02-02 17:03:37 -08002985 // Resume the auto-advance of widgets
2986 mUserPresent = true;
2987 updateRunning();
2988
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002989 // send an accessibility event to announce the context change
2990 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002991 }
2992
Michael Jurkad3ef3062010-11-23 16:23:58 -08002993 void enterSpringLoadedDragMode(CellLayout layout) {
2994 mWorkspace.enterSpringLoadedDragMode(layout);
2995 if (mState == State.ALL_APPS) {
2996 cameraZoomIn(State.ALL_APPS, true, true);
2997 mState = State.ALL_APPS_SPRING_LOADED;
2998 } else if (mState == State.CUSTOMIZE) {
2999 cameraZoomIn(State.CUSTOMIZE, true, true);
3000 mState = State.CUSTOMIZE_SPRING_LOADED;
3001 }/* else {
3002 // we're already in spring loaded mode; don't do anything
3003 }*/
3004 }
3005
3006 void exitSpringLoadedDragMode() {
3007 if (mState == State.ALL_APPS_SPRING_LOADED) {
3008 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
3009 cameraZoomOut(State.ALL_APPS, true);
3010 mState = State.ALL_APPS;
3011 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
3012 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
3013 cameraZoomOut(State.CUSTOMIZE, true);
3014 mState = State.CUSTOMIZE;
3015 }/* else {
3016 // we're not in spring loaded mode; don't do anything
3017 }*/
3018 }
3019
Joe Onoratob2061212009-11-24 16:13:54 -05003020 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08003021 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05003022 * - Home from workspace
3023 * - from center screen
3024 * - from other screens
3025 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003026 * - from center screen
3027 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003028 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003029 * - from center screen
3030 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003031 * - Launch app from workspace and quit
3032 * - with back
3033 * - with home
3034 * - Launch app from all apps and quit
3035 * - with back
3036 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003037 * - Go to a screen that's not the default, then all
3038 * apps, and launch and app, and go back
3039 * - with back
3040 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003041 * - On workspace, long press power and go back
3042 * - with back
3043 * - with home
3044 * - On all apps, long press power and go back
3045 * - with back
3046 * - with home
3047 * - On workspace, power off
3048 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003049 * - Launch an app and turn off the screen while in that app
3050 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003051 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003052 * - From all apps
3053 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003054 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3055 * - From all apps
3056 * - From the center workspace
3057 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003058 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003059 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003060 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003061 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003062 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003063 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003064 } else {
3065 mAllAppsGrid.zoom(0.0f, animated);
3066 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003067 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003068 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003069 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003070 }
3071
Joe Onorato7c312c12009-08-13 21:36:53 -07003072 void lockAllApps() {
3073 // TODO
3074 }
3075
3076 void unlockAllApps() {
3077 // TODO
3078 }
3079
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003080 // Show the customization drawer (only exists in x-large configuration)
3081 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003082 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003083 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003084 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003085 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003086 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003087 // Change the state *after* we've called all the transition code
3088 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003089
3090 // Pause the auto-advance of widgets until we are out of Customization drawer
3091 mUserPresent = false;
3092 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003093 }
3094
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003095 // Hide the customization drawer (only exists in x-large configuration)
3096 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003097 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003098 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003099 }
3100 }
3101
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003102 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
3103 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3104 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07003105 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003106 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003107
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003108 void onWorkspaceClick(CellLayout layout) {
3109 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003110 }
3111
Michael Jurka4ef207b2010-11-29 17:05:45 -08003112 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003113 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003114 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3115 ImageView button = (ImageView) findViewById(buttonId);
3116 Drawable toolbarIcon = null;
3117 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003118 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003119 // Look for the toolbar icon specified in the activity meta-data
3120 Bundle metaData = packageManager.getActivityInfo(
3121 activityName, PackageManager.GET_META_DATA).metaData;
3122 if (metaData != null) {
3123 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3124 if (iconResId != 0) {
3125 Resources res = packageManager.getResourcesForActivity(activityName);
3126 toolbarIcon = res.getDrawable(iconResId);
3127 }
3128 }
3129 } catch (NameNotFoundException e) {
3130 // Do nothing
3131 }
3132 // If we were unable to find the icon via the meta-data, use a generic one
3133 if (toolbarIcon == null) {
3134 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003135 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003136 } else {
3137 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003138 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003139 }
3140 }
3141
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003142 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003143 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003144 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003145 }
3146
Michael Jurka0423dcf2010-10-05 14:56:18 -07003147 private void updateGlobalSearchIcon() {
3148 if (LauncherApplication.isScreenXLarge()) {
3149 final SearchManager searchManager =
3150 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3151 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003152 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003153 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003154 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003155 } else {
3156 findViewById(R.id.search_button).setVisibility(View.GONE);
3157 }
3158 }
3159 }
3160
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003161 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003162 updateButtonWithDrawable(R.id.search_button, d);
3163 }
3164
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003165 private void updateVoiceSearchIcon() {
3166 if (LauncherApplication.isScreenXLarge()) {
3167 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3168 ComponentName activityName = intent.resolveActivity(getPackageManager());
3169 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003170 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003171 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3172 } else {
3173 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003174 }
3175 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003176 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003177
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003178 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003179 updateButtonWithDrawable(R.id.voice_button, d);
3180 }
3181
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003182 /**
3183 * Sets the app market icon (shown when all apps is visible on x-large screens)
3184 */
3185 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003186 if (LauncherApplication.isScreenXLarge()) {
3187 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3188 // Find the app market activity by resolving an intent.
3189 // (If multiple app markets are installed, it will return the ResolverActivity.)
3190 ComponentName activityName = intent.resolveActivity(getPackageManager());
3191 if (activityName != null) {
3192 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003193 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003194 R.id.market_button, activityName, R.drawable.app_market_generic);
3195 }
3196 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003197 }
3198
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003199 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003200 updateButtonWithDrawable(R.id.market_button, d);
3201 }
3202
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003203 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003204 * Displays the shortcut creation dialog and launches, if necessary, the
3205 * appropriate activity.
3206 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003207 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003208 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3209 DialogInterface.OnShowListener {
3210
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003211 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003213 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003214 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003215
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003216 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3217 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003218 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003220 builder.setInverseBackgroundForced(true);
3221
3222 AlertDialog dialog = builder.create();
3223 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003224 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003225 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003227 return dialog;
3228 }
3229
3230 public void onCancel(DialogInterface dialog) {
3231 mWaitingForResult = false;
3232 cleanup();
3233 }
3234
Romain Guycbb89e42009-06-08 15:52:54 -07003235 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003236 }
3237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003238 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003239 try {
3240 dismissDialog(DIALOG_CREATE_SHORTCUT);
3241 } catch (Exception e) {
3242 // An exception is thrown if the dialog is not visible, which is fine
3243 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244 }
3245
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003246 /**
3247 * Handle the action clicked in the "Add to home" dialog.
3248 */
3249 public void onClick(DialogInterface dialog, int which) {
3250 Resources res = getResources();
3251 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003252
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003253 switch (which) {
3254 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003255 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003256 break;
3257 }
Romain Guycbb89e42009-06-08 15:52:54 -07003258
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003259 case AddAdapter.ITEM_APPWIDGET: {
3260 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003261
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003262 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3263 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003264 // start the pick activity
3265 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3266 break;
3267 }
Romain Guycbb89e42009-06-08 15:52:54 -07003268
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003269 case AddAdapter.ITEM_LIVE_FOLDER: {
3270 // Insert extra item to handle inserting folder
3271 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003272
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003273 ArrayList<String> shortcutNames = new ArrayList<String>();
3274 shortcutNames.add(res.getString(R.string.group_folder));
3275 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003276
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003277 ArrayList<ShortcutIconResource> shortcutIcons =
3278 new ArrayList<ShortcutIconResource>();
3279 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3280 R.drawable.ic_launcher_folder));
3281 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3282
3283 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3284 pickIntent.putExtra(Intent.EXTRA_INTENT,
3285 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3286 pickIntent.putExtra(Intent.EXTRA_TITLE,
3287 getText(R.string.title_select_live_folder));
3288 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003289
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003290 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3291 break;
3292 }
3293
3294 case AddAdapter.ITEM_WALLPAPER: {
3295 startWallpaper();
3296 break;
3297 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003298 }
3299 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003300
3301 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003302 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003303 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003304 }
3305
3306 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003307 * Receives notifications when applications are added/removed.
3308 */
3309 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3310 @Override
3311 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003312 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003313 String reason = intent.getStringExtra("reason");
3314 if (!"homekey".equals(reason)) {
3315 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003316 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003317 animate = false;
3318 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003319 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003320 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003321 }
3322 }
3323
3324 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003325 * Receives notifications whenever the appwidgets are reset.
3326 */
3327 private class AppWidgetResetObserver extends ContentObserver {
3328 public AppWidgetResetObserver() {
3329 super(new Handler());
3330 }
3331
3332 @Override
3333 public void onChange(boolean selfChange) {
3334 onAppWidgetReset();
3335 }
3336 }
3337
3338 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003339 * If the activity is currently paused, signal that we need to re-run the loader
3340 * in onResume.
3341 *
3342 * This needs to be called from incoming places where resources might have been loaded
3343 * while we are paused. That is becaues the Configuration might be wrong
3344 * when we're not running, and if it comes back to what it was when we
3345 * were paused, we are not restarted.
3346 *
3347 * Implementation of the method from LauncherModel.Callbacks.
3348 *
3349 * @return true if we are currently paused. The caller might be able to
3350 * skip some work in that case since we will come back again.
3351 */
3352 public boolean setLoadOnResume() {
3353 if (mPaused) {
3354 Log.i(TAG, "setLoadOnResume");
3355 mOnResumeNeedsLoad = true;
3356 return true;
3357 } else {
3358 return false;
3359 }
3360 }
3361
3362 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003363 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003364 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003365 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003366 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003367 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003368 } else {
3369 return SCREEN_COUNT / 2;
3370 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003371 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003372
Winson Chunga12a2502010-12-20 14:41:35 -08003373 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003374 mAllAppsPagedView = view;
3375 }
3376
Joe Onorato9c1289c2009-08-17 11:03:03 -04003377 /**
3378 * Refreshes the shortcuts shown on the workspace.
3379 *
3380 * Implementation of the method from LauncherModel.Callbacks.
3381 */
3382 public void startBinding() {
3383 final Workspace workspace = mWorkspace;
3384 int count = workspace.getChildCount();
3385 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003386 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003387 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3388 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003389 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003390
Joe Onorato9c1289c2009-08-17 11:03:03 -04003391 if (DEBUG_USER_INTERFACE) {
3392 android.widget.Button finishButton = new android.widget.Button(this);
3393 finishButton.setText("Finish");
3394 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3395
3396 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3397 public void onClick(View v) {
3398 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003399 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003400 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003401 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003402
3403 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3404 // mDesktopItems -> AppWidgetInfo -> hostView).
3405 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003406 }
3407
3408 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003409 * Bind the items start-end from the list.
3410 *
3411 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003412 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003413 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3414
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003415 setLoadOnResume();
3416
Joe Onorato9c1289c2009-08-17 11:03:03 -04003417 final Workspace workspace = mWorkspace;
3418
3419 for (int i=start; i<end; i++) {
3420 final ItemInfo item = shortcuts.get(i);
3421 mDesktopItems.add(item);
3422 switch (item.itemType) {
3423 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3424 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003425 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003426 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3427 false);
3428 break;
3429 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3430 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003431 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003432 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003433 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3434 false);
3435 break;
3436 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3437 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3438 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003439 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003440 (LiveFolderInfo) item);
3441 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3442 false);
3443 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003444 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003445 }
3446
Joe Onorato9c1289c2009-08-17 11:03:03 -04003447 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003448 }
3449
Joe Onorato9c1289c2009-08-17 11:03:03 -04003450 /**
3451 * Implementation of the method from LauncherModel.Callbacks.
3452 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003453 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003454 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003455 sFolders.clear();
3456 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003457 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003458
3459 /**
3460 * Add the views for a widget to the workspace.
3461 *
3462 * Implementation of the method from LauncherModel.Callbacks.
3463 */
3464 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003465 setLoadOnResume();
3466
Daniel Sandler843e8602010-06-07 14:59:01 -04003467 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3468 if (DEBUG_WIDGETS) {
3469 Log.d(TAG, "bindAppWidget: " + item);
3470 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003471 final Workspace workspace = mWorkspace;
3472
3473 final int appWidgetId = item.appWidgetId;
3474 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003475 if (DEBUG_WIDGETS) {
3476 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3477 }
3478
Joe Onorato9c1289c2009-08-17 11:03:03 -04003479 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3480
Joe Onorato9c1289c2009-08-17 11:03:03 -04003481 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3482 item.hostView.setTag(item);
3483
3484 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3485 item.cellY, item.spanX, item.spanY, false);
3486
Adam Cohended9f8d2010-11-03 13:25:16 -07003487 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3488
Joe Onorato9c1289c2009-08-17 11:03:03 -04003489 workspace.requestLayout();
3490
3491 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003492
3493 if (DEBUG_WIDGETS) {
3494 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3495 + (SystemClock.uptimeMillis()-start) + "ms");
3496 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003497 }
3498
3499 /**
3500 * Callback saying that there aren't any more items to bind.
3501 *
3502 * Implementation of the method from LauncherModel.Callbacks.
3503 */
3504 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003505 setLoadOnResume();
3506
Joe Onorato9c1289c2009-08-17 11:03:03 -04003507 if (mSavedState != null) {
3508 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003509 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003510 }
3511
3512 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3513 if (userFolders != null) {
3514 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003515 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003516 if (info != null) {
3517 openFolder(info);
3518 }
3519 }
3520 final Folder openFolder = mWorkspace.getOpenFolder();
3521 if (openFolder != null) {
3522 openFolder.requestFocus();
3523 }
3524 }
3525
Joe Onorato9c1289c2009-08-17 11:03:03 -04003526 mSavedState = null;
3527 }
3528
3529 if (mSavedInstanceState != null) {
3530 super.onRestoreInstanceState(mSavedInstanceState);
3531 mSavedInstanceState = null;
3532 }
3533
Winson Chung32bb5e52011-01-31 14:51:56 -08003534 // Workaround a bug that occurs when rotating the device while the customization mode is
3535 // open, we trigger a new layout on all the CellLayout children.
3536 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3537 final int childCount = mWorkspace.getChildCount();
3538 for (int i = 0; i < childCount; ++i) {
3539 mWorkspace.getChildAt(i).requestLayout();
3540 }
3541 }
3542
Joe Onorato9c1289c2009-08-17 11:03:03 -04003543 mWorkspaceLoading = false;
3544 }
3545
3546 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003547 * Updates the icons on the launcher that are affected by changes to the package list
3548 * on the device.
3549 */
3550 private void updateIconsAffectedByPackageManagerChanges() {
3551 updateAppMarketIcon();
3552 updateGlobalSearchIcon();
3553 updateVoiceSearchIcon();
3554 }
3555
3556 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003557 * Add the icons for all apps.
3558 *
3559 * Implementation of the method from LauncherModel.Callbacks.
3560 */
3561 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003562 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003563 if (mCustomizePagedView != null) {
3564 mCustomizePagedView.setApps(apps);
3565 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003566 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 }
3568
3569 /**
3570 * A package was installed.
3571 *
3572 * Implementation of the method from LauncherModel.Callbacks.
3573 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003574 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003575 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003576 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003577 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003578 if (mCustomizePagedView != null) {
3579 mCustomizePagedView.addApps(apps);
3580 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003581 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003582 }
3583
3584 /**
3585 * A package was updated.
3586 *
3587 * Implementation of the method from LauncherModel.Callbacks.
3588 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003589 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003590 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003591 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003592 mWorkspace.updateShortcuts(apps);
3593 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003594 if (mCustomizePagedView != null) {
3595 mCustomizePagedView.updateApps(apps);
3596 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003597 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598 }
3599
3600 /**
3601 * A package was uninstalled.
3602 *
3603 * Implementation of the method from LauncherModel.Callbacks.
3604 */
Joe Onorato36115782010-06-17 13:28:48 -04003605 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003607 if (permanent) {
3608 mWorkspace.removeItems(apps);
3609 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003610 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003611 if (mCustomizePagedView != null) {
3612 mCustomizePagedView.removeApps(apps);
3613 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003614 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003615 }
Joe Onoratobe386092009-11-17 17:32:16 -08003616
3617 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003618 * A number of packages were updated.
3619 */
3620 public void bindPackagesUpdated() {
3621 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003622 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003623 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003624 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003625 }
3626
Winson Chung400438b2011-01-16 17:53:48 -08003627 private int mapConfigurationOriActivityInfoOri(int configOri) {
3628 final Display d = getWindowManager().getDefaultDisplay();
3629 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3630 switch (d.getRotation()) {
3631 case Surface.ROTATION_0:
3632 case Surface.ROTATION_180:
3633 // We are currently in the same basic orientation as the natural orientation
3634 naturalOri = configOri;
3635 break;
3636 case Surface.ROTATION_90:
3637 case Surface.ROTATION_270:
3638 // We are currently in the other basic orientation to the natural orientation
3639 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3640 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3641 break;
3642 }
3643
3644 int[] oriMap = {
3645 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3646 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3647 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3648 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3649 };
3650 // Since the map starts at portrait, we need to offset if this device's natural orientation
3651 // is landscape.
3652 int indexOffset = 0;
3653 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3654 indexOffset = 1;
3655 }
3656 return oriMap[(d.getRotation() + indexOffset) % 4];
3657 }
3658 public void lockScreenOrientation() {
3659 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3660 .getConfiguration().orientation));
3661 }
3662 public void unlockScreenOrientation() {
3663 mHandler.postDelayed(new Runnable() {
3664 public void run() {
3665 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3666 }
3667 }, mRestoreScreenOrientationDelay);
3668 }
3669
Winson Chung80baf5a2010-08-09 16:03:15 -07003670 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003671 * Prints out out state for debugging.
3672 */
3673 public void dumpState() {
3674 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003675 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003676 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3677 Log.d(TAG, "mRestoring=" + mRestoring);
3678 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3679 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3680 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003681 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003682 mModel.dumpState();
3683 mAllAppsGrid.dumpState();
3684 Log.d(TAG, "END launcher2 dump state");
3685 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003686}