blob: 9cea69b493385fb1a362dcc5304e738728eb6db9 [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;
Michael Jurkae0f5a612011-02-07 16:45:41 -080091import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080092import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080093import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080094import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070096import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070098import android.widget.ImageView;
99import android.widget.LinearLayout;
100import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700101import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -0800102import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -0700103import android.widget.TextView;
104import android.widget.Toast;
Adam Cohen860f4c52011-01-27 20:16:13 -0800105import android.widget.TabHost.OnTabChangeListener;
106import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700107
Michael Jurka742574b2011-02-02 23:51:01 -0800108import java.io.DataInputStream;
109import java.io.DataOutputStream;
110import java.io.FileNotFoundException;
111import java.io.IOException;
112import java.util.ArrayList;
113import java.util.HashMap;
114import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800115
Adam Cohended9f8d2010-11-03 13:25:16 -0700116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117/**
118 * Default launcher application.
119 */
Michael Jurka946ad472010-07-09 18:05:18 -0700120public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700121 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
122 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800123 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Joe Onorato9c1289c2009-08-17 11:03:03 -0400126 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400127 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400128 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800131 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
132
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700134 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
135 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
137 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700138 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
140 private static final int REQUEST_CREATE_SHORTCUT = 1;
141 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700142 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 private static final int REQUEST_PICK_APPLICATION = 6;
144 private static final int REQUEST_PICK_SHORTCUT = 7;
145 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700146 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700147 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
149 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
150
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800151 static final int SCREEN_COUNT = 5;
152 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Joe Onorato7c312c12009-08-13 21:36:53 -0700154 static final int DIALOG_CREATE_SHORTCUT = 1;
155 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Romain Guy98d01652009-06-30 16:21:04 -0700157 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
159 // Type: int
160 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700161 // Type: int
162 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: long
164 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
169 // Type: int
170 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 // Type: boolean
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
173 // Type: long
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
175
Winson Chung80baf5a2010-08-09 16:03:15 -0700176 // tags for the customization tabs
177 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700178 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700179 private static final String SHORTCUTS_TAG = "shortcuts";
180 private static final String WALLPAPERS_TAG = "wallpapers";
181
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700182 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
183
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700184 /** The different states that Launcher can be in. */
Michael Jurkaafca9532011-02-16 14:50:35 -0800185 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800186 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700187 private State mState = State.WORKSPACE;
188 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700189
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800193 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800195 private final BroadcastReceiver mCloseSystemDialogsReceiver
196 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800197 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 private LayoutInflater mInflater;
200
Joe Onorato00acb122009-08-04 16:04:30 -0400201 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700203
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700204 private AppWidgetManager mAppWidgetManager;
205 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700206
Michael Jurka0280c3b2010-09-17 15:00:07 -0700207 private int mAddScreen = -1;
208 private int mAddIntersectCellX = -1;
209 private int mAddIntersectCellY = -1;
210 private int[] mAddDropPosition;
211 private int[] mTmpAddItemCellCoordinates = new int[2];
212
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 private FolderInfo mFolderInfo;
214
Joe Onorato7c312c12009-08-13 21:36:53 -0700215 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700216 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700217 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700218 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700219 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700220
Michael Jurka838a4ca2011-02-07 13:33:06 -0800221 private View mButtonCluster;
222 private View mAllAppsButton;
223 private View mDivider;
224 private View mConfigureButton;
225
Winson Chunga12a2502010-12-20 14:41:35 -0800226 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700227 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 private Bundle mSavedState;
230
231 private SpannableStringBuilder mDefaultKeySsb = null;
232
Joe Onorato9c1289c2009-08-17 11:03:03 -0400233 private boolean mWorkspaceLoading = true;
234
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800235 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 private boolean mRestoring;
237 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700238 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239
240 private Bundle mSavedInstanceState;
241
Joe Onorato9c1289c2009-08-17 11:03:03 -0400242 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800243 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800244 private boolean mUserPresent = true;
245 private boolean mVisible = false;
246 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400247
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700248 private static LocaleConfiguration sLocaleConfiguration = null;
249
Romain Guy84f296c2009-11-04 15:00:44 -0800250 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700251
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700252 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700253
Patrick Dubroy94f78a52011-02-28 17:39:16 -0800254 // The "signpost" images along the bottom of the screen (only in some layouts)
Romain Guy1fbc1c82009-11-09 20:43:08 -0800255 private ImageView mPreviousView;
256 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500257
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400258 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400259 private String[] mHotseatConfig = null;
260 private Intent[] mHotseats = null;
261 private Drawable[] mHotseatIcons = null;
262 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400263
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700264 private Intent mAppMarketIntent = null;
265
Adam Cohended9f8d2010-11-03 13:25:16 -0700266 // Related to the auto-advancing of widgets
267 private final int ADVANCE_MSG = 1;
268 private final int mAdvanceInterval = 20000;
269 private final int mAdvanceStagger = 250;
270 private long mAutoAdvanceSentTime;
271 private long mAutoAdvanceTimeLeft = -1;
272 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
273 new HashMap<View, AppWidgetProviderInfo>();
274
Winson Chung400438b2011-01-16 17:53:48 -0800275 // Determines how long to wait after a rotation before restoring the screen orientation to
276 // match the sensor state.
277 private final int mRestoreScreenOrientationDelay = 500;
278
Michael Jurka4ef207b2010-11-29 17:05:45 -0800279 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800280 private static Drawable.ConstantState sGlobalSearchIcon;
281 private static Drawable.ConstantState sVoiceSearchIcon;
282 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800283
Michael Jurkaddd62e92011-02-16 17:49:14 -0800284 private BubbleTextView mWaitingForResume;
285
Winson Chunga12a2502010-12-20 14:41:35 -0800286 private CustomizationType getCustomizeFilterForTabTag(String tag) {
287 if (tag.equals(WIDGETS_TAG)) {
288 return CustomizationType.WidgetCustomization;
289 } else if (tag.equals(APPLICATIONS_TAG)) {
290 return CustomizationType.ApplicationCustomization;
291 } else if (tag.equals(WALLPAPERS_TAG)) {
292 return CustomizePagedView.CustomizationType.WallpaperCustomization;
293 } else if (tag.equals(SHORTCUTS_TAG)) {
294 return CustomizePagedView.CustomizationType.ShortcutCustomization;
295 }
296 return CustomizationType.WidgetCustomization;
297 }
298
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 @Override
300 protected void onCreate(Bundle savedInstanceState) {
301 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700302
Winson Chungaafa03c2010-06-11 17:34:16 -0700303 if (LauncherApplication.isInPlaceRotationEnabled()) {
304 // hide the status bar (temporary until we get the status bar design figured out)
305 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
306 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
307 }
308
Joe Onorato0589f0f2010-02-08 13:44:00 -0800309 LauncherApplication app = ((LauncherApplication)getApplication());
310 mModel = app.setLauncher(this);
311 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400312 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800313 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700314
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700315 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700316 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
317 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700318
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200320 android.os.Debug.startMethodTracing(
321 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 }
323
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400324 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800327 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700328 if (mHomeCustomizationDrawer != null) {
329 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700330
Winson Chung80baf5a2010-08-09 16:03:15 -0700331 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700332 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
333 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700334 TabContentFactory contentFactory = new TabContentFactory() {
335 public View createTabContent(String tag) {
336 return mCustomizePagedView;
337 }
338 };
339
Winson Chung49767ae2010-11-29 14:48:30 -0800340
341 TextView tabView;
342 TabWidget tabWidget = (TabWidget)
343 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
344
345 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
346 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700347 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_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.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700351 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_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.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700355 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800356 .setIndicator(tabView).setContent(contentFactory));
357 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
358 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700359 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800360 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700361 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
362 public void onTabChanged(String tabId) {
Winson Chunga12a2502010-12-20 14:41:35 -0800363 final CustomizePagedView.CustomizationType newType =
364 getCustomizeFilterForTabTag(tabId);
365 if (newType != mCustomizePagedView.getCustomizationFilter()) {
366 // animate the changing of the tab content by fading pages in and out
367 final Resources res = getResources();
368 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
369 final float alpha = mCustomizePagedView.getAlpha();
370 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
371 "alpha", alpha, 0.0f);
372 alphaAnim.setDuration(duration);
373 alphaAnim.addListener(new AnimatorListenerAdapter() {
374 @Override
375 public void onAnimationEnd(Animator animation) {
Winson Chunga12a2502010-12-20 14:41:35 -0800376 mCustomizePagedView.setCustomizationFilter(newType);
Winson Chung80baf5a2010-08-09 16:03:15 -0700377
Winson Chunga12a2502010-12-20 14:41:35 -0800378 final float alpha = mCustomizePagedView.getAlpha();
379 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
380 mCustomizePagedView, "alpha", alpha, 1.0f);
381 alphaAnim.setDuration(duration);
382 alphaAnim.start();
383 }
384 });
385 alphaAnim.start();
386 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700387 }
388 });
Michael Jurka946ad472010-07-09 18:05:18 -0700389 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 setupViews();
391
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800392 registerContentObservers();
393
Joe Onorato7c312c12009-08-13 21:36:53 -0700394 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700395
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 mSavedState = savedInstanceState;
397 restoreState(mSavedState);
398
Winson Chunga12a2502010-12-20 14:41:35 -0800399 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800400 if (mCustomizePagedView != null) {
401 mCustomizePagedView.update();
402 }
Winson Chunga12a2502010-12-20 14:41:35 -0800403
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 if (PROFILE_STARTUP) {
405 android.os.Debug.stopMethodTracing();
406 }
407
408 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400409 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 }
411
412 // For handling default keys
413 mDefaultKeySsb = new SpannableStringBuilder();
414 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800415
416 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
417 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800418
419 // If we have a saved version of these external icons, we load them up immediately
420 if (LauncherApplication.isScreenXLarge()) {
421 if (sGlobalSearchIcon != null) {
422 updateGlobalSearchIcon(sGlobalSearchIcon);
423 }
424 if (sVoiceSearchIcon != null) {
425 updateVoiceSearchIcon(sVoiceSearchIcon);
426 }
427 if (sAppMarketIcon != null) {
428 updateAppMarketIcon(sAppMarketIcon);
429 }
430 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 }
Romain Guycbb89e42009-06-08 15:52:54 -0700432
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800433 @Override
434 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
435 super.dispatchPopulateAccessibilityEvent(event);
436
437 // we want to take over text population so it is context dependent
438 event.getText().clear();
439 if (mState == State.ALL_APPS) {
440 event.getText().add(getString(R.string.all_apps_button_label));
441 } else if (mState == State.WORKSPACE) {
442 event.getText().add(getString(R.string.all_apps_home_button_label));
443 }
444
445 return true;
446 }
447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700449 if (sLocaleConfiguration == null) {
450 new AsyncTask<Void, Void, LocaleConfiguration>() {
451 @Override
452 protected LocaleConfiguration doInBackground(Void... unused) {
453 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
454 readConfiguration(Launcher.this, localeConfiguration);
455 return localeConfiguration;
456 }
457
458 @Override
459 protected void onPostExecute(LocaleConfiguration result) {
460 sLocaleConfiguration = result;
461 checkForLocaleChange(); // recursive, but now with a locale configuration
462 }
463 }.execute();
464 return;
465 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700466
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 final Configuration configuration = getResources().getConfiguration();
468
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700469 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 final String locale = configuration.locale.toString();
471
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700472 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 final int mcc = configuration.mcc;
474
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700475 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476 final int mnc = configuration.mnc;
477
Romain Guy84f296c2009-11-04 15:00:44 -0800478 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479
Romain Guy84f296c2009-11-04 15:00:44 -0800480 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700481 sLocaleConfiguration.locale = locale;
482 sLocaleConfiguration.mcc = mcc;
483 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700484
Joe Onorato0589f0f2010-02-08 13:44:00 -0800485 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400486 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700487
488 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
489 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700490 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700491 public void run() {
492 writeConfiguration(Launcher.this, localeConfiguration);
493 }
494 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700495 }
496 }
497
498 private static class LocaleConfiguration {
499 public String locale;
500 public int mcc = -1;
501 public int mnc = -1;
502 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700503
Romain Guy98d01652009-06-30 16:21:04 -0700504 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
505 DataInputStream in = null;
506 try {
507 in = new DataInputStream(context.openFileInput(PREFERENCES));
508 configuration.locale = in.readUTF();
509 configuration.mcc = in.readInt();
510 configuration.mnc = in.readInt();
511 } catch (FileNotFoundException e) {
512 // Ignore
513 } catch (IOException e) {
514 // Ignore
515 } finally {
516 if (in != null) {
517 try {
518 in.close();
519 } catch (IOException e) {
520 // Ignore
521 }
522 }
523 }
524 }
525
526 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
527 DataOutputStream out = null;
528 try {
529 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
530 out.writeUTF(configuration.locale);
531 out.writeInt(configuration.mcc);
532 out.writeInt(configuration.mnc);
533 out.flush();
534 } catch (FileNotFoundException e) {
535 // Ignore
536 } catch (IOException e) {
537 //noinspection ResultOfMethodCallIgnored
538 context.getFileStreamPath(PREFERENCES).delete();
539 } finally {
540 if (out != null) {
541 try {
542 out.close();
543 } catch (IOException e) {
544 // Ignore
545 }
546 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 }
548 }
549
550 static int getScreen() {
551 synchronized (sLock) {
552 return sScreen;
553 }
554 }
555
556 static void setScreen(int screen) {
557 synchronized (sLock) {
558 sScreen = screen;
559 }
560 }
561
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400562 // Note: This doesn't do all the client-id magic that BrowserProvider does
563 // in Browser. (http://b/2425179)
564 private Uri getDefaultBrowserUri() {
565 String url = getString(R.string.default_browser_url);
566 if (url.indexOf("{CID}") != -1) {
567 url = url.replace("{CID}", "android-google");
568 }
569 return Uri.parse(url);
570 }
571
572 // Load the Intent templates from arrays.xml to populate the hotseats. For
573 // each Intent, if it resolves to a single app, use that as the launch
574 // intent & use that app's label as the contentDescription. Otherwise,
575 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400576 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400577 if (mHotseatConfig == null) {
578 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
579 if (mHotseatConfig.length > 0) {
580 mHotseats = new Intent[mHotseatConfig.length];
581 mHotseatLabels = new CharSequence[mHotseatConfig.length];
582 mHotseatIcons = new Drawable[mHotseatConfig.length];
583 } else {
584 mHotseats = null;
585 mHotseatIcons = null;
586 mHotseatLabels = null;
587 }
588
589 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
590 for (int i=0; i<mHotseatConfig.length; i++) {
591 // load icon for this slot; currently unrelated to the actual activity
592 try {
593 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
594 } catch (ArrayIndexOutOfBoundsException ex) {
595 Log.w(TAG, "Missing hotseat_icons array item #" + i);
596 mHotseatIcons[i] = null;
597 }
598 }
599 hotseatIconDrawables.recycle();
600 }
601
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400602 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400603 for (int i=0; i<mHotseatConfig.length; i++) {
604 Intent intent = null;
605 if (mHotseatConfig[i].equals("*BROWSER*")) {
606 // magic value meaning "launch user's default web browser"
607 // replace it with a generic web request so we can see if there is indeed a default
608 String defaultUri = getString(R.string.default_browser_url);
609 intent = new Intent(
610 Intent.ACTION_VIEW,
611 ((defaultUri != null)
612 ? Uri.parse(defaultUri)
613 : getDefaultBrowserUri())
614 ).addCategory(Intent.CATEGORY_BROWSABLE);
615 // note: if the user launches this without a default set, she
616 // will always be taken to the default URL above; this is
617 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700618 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400619 // URL is unavoidably sent to the chosen app.
620 } else {
621 try {
622 intent = Intent.parseUri(mHotseatConfig[i], 0);
623 } catch (java.net.URISyntaxException ex) {
624 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
625 // bogus; leave intent=null
626 }
627 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700628
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400629 if (intent == null) {
630 mHotseats[i] = null;
631 mHotseatLabels[i] = getText(R.string.activity_not_found);
632 continue;
633 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400634
635 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700636 Log.d(TAG, "loadHotseats: hotseat " + i
637 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400638 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400639 + "]");
640 }
641
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400642 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
643 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700644 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400645 Log.d(TAG, "Best match for intent: " + bestMatch);
646 Log.d(TAG, "All matches: ");
647 for (ResolveInfo ri : allMatches) {
648 Log.d(TAG, " --> " + ri);
649 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400650 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400651 // did this resolve to a single app, or the resolver?
652 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700653 // can't find any activity to handle this. let's leave the
654 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400655 // to launch.
656 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400657
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400658 // set accessibility text to "Not installed"
659 mHotseatLabels[i] = getText(R.string.activity_not_found);
660 } else {
661 boolean found = false;
662 for (ResolveInfo ri : allMatches) {
663 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
664 && bestMatch.activityInfo.applicationInfo.packageName
665 .equals(ri.activityInfo.applicationInfo.packageName)) {
666 found = true;
667 break;
668 }
669 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700670
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400671 if (!found) {
672 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
673 // the bestMatch is probably the ResolveActivity, meaning the
674 // user has not yet selected a default
675 // so: we'll keep the original intent for now
676 mHotseats[i] = intent;
677
678 // set the accessibility text to "Select shortcut"
679 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
680 } else {
681 // we have an app!
682 // now reconstruct the intent to launch it through the front
683 // door
684 ComponentName com = new ComponentName(
685 bestMatch.activityInfo.applicationInfo.packageName,
686 bestMatch.activityInfo.name);
687 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
688
689 // load the app label for accessibility
690 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
691 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400692 }
693
694 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700695 Log.d(TAG, "loadHotseats: hotseat " + i
696 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400697 + ((mHotseats[i] == null)
698 ? "null"
699 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400700 + "] label=[" + mHotseatLabels[i]
701 + "]"
702 );
703 }
704 }
705 }
706
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 @Override
708 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700709 mWaitingForResult = false;
710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 // The pattern used here is that a user PICKs a specific application,
712 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700713
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
715 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700716
Michael Jurka0280c3b2010-09-17 15:00:07 -0700717 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 switch (requestCode) {
719 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700720 completeAddApplication(
721 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 break;
723 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800724 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 break;
726 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700727 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 break;
729 case REQUEST_PICK_LIVE_FOLDER:
730 addLiveFolder(data);
731 break;
732 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700733 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700735 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700736 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700738 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700739 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700740 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700742 case REQUEST_PICK_WALLPAPER:
743 // We just wanted the activity result here so we can clear mWaitingForResult
744 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 }
Romain Guy18042c82009-11-06 11:44:55 -0800746 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
747 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
748 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700749 // Clean up the appWidgetId if we canceled
750 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
751 if (appWidgetId != -1) {
752 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 }
754 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 }
756
757 @Override
758 protected void onResume() {
759 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800760 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700761 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400762 mWorkspaceLoading = true;
763 mModel.startLoader(this, true);
764 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700765 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800767 if (mWaitingForResume != null) {
768 mWaitingForResume.setStayPressed(false);
769 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700770 // When we resume Launcher, a different Activity might be responsible for the app
771 // market intent, so refresh the icon
772 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 }
774
775 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700776 protected void onPause() {
777 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700778 // Some launcher layouts don't have a previous and next view
779 if (mPreviousView != null) {
780 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700781 }
782 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700783 dismissPreview(mNextView);
784 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700785 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800786 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700787 }
Romain Guycbb89e42009-06-08 15:52:54 -0700788
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700789 @Override
790 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400791 // Flag the loader to stop early before switching
792 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800793 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800794 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700795 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700796
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800797 // We can't hide the IME if it was forced open. So don't bother
798 /*
799 @Override
800 public void onWindowFocusChanged(boolean hasFocus) {
801 super.onWindowFocusChanged(hasFocus);
802
803 if (hasFocus) {
804 final InputMethodManager inputManager = (InputMethodManager)
805 getSystemService(Context.INPUT_METHOD_SERVICE);
806 WindowManager.LayoutParams lp = getWindow().getAttributes();
807 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
808 android.os.Handler()) {
809 protected void onReceiveResult(int resultCode, Bundle resultData) {
810 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
811 }
812 });
813 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
814 }
815 }
816 */
817
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 private boolean acceptFilter() {
819 final InputMethodManager inputManager = (InputMethodManager)
820 getSystemService(Context.INPUT_METHOD_SERVICE);
821 return !inputManager.isFullscreenMode();
822 }
823
824 @Override
825 public boolean onKeyDown(int keyCode, KeyEvent event) {
826 boolean handled = super.onKeyDown(keyCode, event);
827 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
828 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
829 keyCode, event);
830 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700831 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700832 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700833 // showSearchDialog()
834 // If there are multiple keystrokes before the search dialog takes focus,
835 // onSearchRequested() will be called for every keystroke,
836 // but it is idempotent, so it's fine.
837 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 }
839 }
840
Joe Onorato8a9625e2010-01-28 15:55:35 -0800841 // Eat the long press event so the keyboard doesn't come up.
842 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
843 return true;
844 }
845
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 return handled;
847 }
848
Karl Rosaen138a0412009-04-23 19:00:21 -0700849 private String getTypedText() {
850 return mDefaultKeySsb.toString();
851 }
852
853 private void clearTypedText() {
854 mDefaultKeySsb.clear();
855 mDefaultKeySsb.clearSpans();
856 Selection.setSelection(mDefaultKeySsb, 0);
857 }
858
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700860 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
861 * State
862 */
863 private static State intToState(int stateOrdinal) {
864 State state = State.WORKSPACE;
865 final State[] stateValues = State.values();
866 for (int i = 0; i < stateValues.length; i++) {
867 if (stateValues[i].ordinal() == stateOrdinal) {
868 state = stateValues[i];
869 break;
870 }
871 }
872 return state;
873 }
874
875 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 * Restores the previous state, if it exists.
877 *
878 * @param savedState The previous state.
879 */
880 private void restoreState(Bundle savedState) {
881 if (savedState == null) {
882 return;
883 }
884
Michael Jurka883f55b2010-10-21 15:47:14 -0700885 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
886
887 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800888 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700889 } else if (state == State.CUSTOMIZE) {
890 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800891 }
892
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
894 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700895 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 }
897
898 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700899
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700901 mAddScreen = addScreen;
902 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
903 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 mRestoring = true;
905 }
906
907 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
908 if (renameFolder) {
909 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700910 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 mRestoring = true;
912 }
Winson Chunga12a2502010-12-20 14:41:35 -0800913
914 // Restore the current AllApps drawer tab
915 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
916 String curTab = savedState.getString("allapps_currentTab");
917 if (curTab != null) {
918 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
919 tabhost.setCurrentTabByTag(curTab);
920 }
921 int curPage = savedState.getInt("allapps_currentPage", -1);
922 if (curPage > -1) {
923 mAllAppsPagedView.setRestorePage(curPage);
924 }
925 }
926
927 // Restore the current customization drawer tab
928 if (mHomeCustomizationDrawer != null) {
929 String curTab = savedState.getString("customize_currentTab");
930 if (curTab != null) {
931 // We set this directly so that there is no delay before the tab is set
932 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
933 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
934 }
935
936 // Note: currently we do not restore the page for the customization tray because unlike
937 // AllApps, the page content can change drastically
938 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 }
940
941 /**
942 * Finds all the views we need and configure them properly.
943 */
944 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700945 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400946
Romain Guyb1b69f52009-08-10 15:10:15 -0700947 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400948 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949
Joe Onorato7c312c12009-08-13 21:36:53 -0700950 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700951 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700952 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800953 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700954 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700955 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700956
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700957 if (LauncherApplication.isScreenXLarge()) {
958 // They need to be INVISIBLE initially so that they will be measured in the layout.
959 // Otherwise the animations are messed up when we show them for the first time.
960 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
961 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
962 }
963
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700965
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500967 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700968
Joe Onorato7c312c12009-08-13 21:36:53 -0700969 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
970 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700972 View handleView = findViewById(R.id.all_apps_button);
973 if (handleView != null && handleView instanceof HandleView) {
974 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700975 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700976 mHandleView.setLauncher(this);
977 mHandleView.setOnClickListener(this);
978 mHandleView.setOnLongClickListener(this);
979 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800980
Winson Chung80baf5a2010-08-09 16:03:15 -0700981 if (mCustomizePagedView != null) {
982 mCustomizePagedView.setLauncher(this);
983 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700984 } else {
985 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
986 hotseatLeft.setContentDescription(mHotseatLabels[0]);
987 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
988 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
989 hotseatRight.setContentDescription(mHotseatLabels[1]);
990 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400991
Michael Jurkaaf442092010-06-10 17:01:57 -0700992 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
993 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800994
Michael Jurkaaf442092010-06-10 17:01:57 -0700995 Drawable previous = mPreviousView.getDrawable();
996 Drawable next = mNextView.getDrawable();
997 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998
Michael Jurkaaf442092010-06-10 17:01:57 -0700999 mPreviousView.setHapticFeedbackEnabled(false);
1000 mPreviousView.setOnLongClickListener(this);
1001 mNextView.setHapticFeedbackEnabled(false);
1002 mNextView.setOnLongClickListener(this);
1003 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001004
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001006 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -08001008 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009
1010 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001011 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -08001012
1013 final View allAppsButton = findViewById(R.id.all_apps_button);
1014 final View divider = findViewById(R.id.divider);
1015 final View configureButton = findViewById(R.id.configure_button);
1016
Michael Jurka54f7ac32010-08-02 13:56:46 -07001017 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001018 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -07001019 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001020 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001021 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001022 dragController.addDragListener(deleteZone);
1023
Adam Cohencdc30d52010-12-01 15:09:47 -08001024 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1025 if (allAppsDeleteZone != null) {
1026 allAppsDeleteZone.setLauncher(this);
1027 allAppsDeleteZone.setDragController(dragController);
1028 allAppsDeleteZone.setDragAndDropEnabled(false);
1029 dragController.addDragListener(allAppsDeleteZone);
1030 dragController.addDropTarget(allAppsDeleteZone);
1031 }
1032
1033 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1034 findViewById(R.id.all_apps_info_target);
1035 if (allAppsInfoTarget != null) {
1036 allAppsInfoTarget.setLauncher(this);
1037 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001038 allAppsInfoTarget.setDragAndDropEnabled(false);
1039 View marketButton = findViewById(R.id.market_button);
1040 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001041 allAppsInfoTarget.setOverlappingView(marketButton);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08001042 marketButton.setOnClickListener(new OnClickListener() {
1043 public void onClick(View v) {
1044 onClickAppMarketButton(v);
1045 }
1046 });
Adam Cohencdc30d52010-12-01 15:09:47 -08001047 }
1048 }
1049
Joe Onorato00acb122009-08-04 16:04:30 -04001050 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001051 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001052 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001053
Joe Onorato00acb122009-08-04 16:04:30 -04001054 // The order here is bottom to top.
1055 dragController.addDropTarget(workspace);
1056 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001057 if (allAppsInfoTarget != null) {
1058 dragController.addDropTarget(allAppsInfoTarget);
1059 }
1060 if (allAppsDeleteZone != null) {
1061 dragController.addDropTarget(allAppsDeleteZone);
1062 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001063 mButtonCluster = findViewById(R.id.all_apps_button_cluster);
1064
1065 mAllAppsButton = findViewById(R.id.all_apps_button);
1066 mDivider = findViewById(R.id.divider);
1067 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001068
1069 // We had previously set these click handlers in XML, but the first time we launched
1070 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1071 // found the right handler. Setting the handlers directly here eliminates that cost.
1072 if (mConfigureButton != null) {
1073 mConfigureButton.setOnClickListener(new OnClickListener() {
1074 public void onClick(View v) {
1075 onClickConfigureButton(v);
1076 }
1077 });
1078 }
1079 if (mDivider != null) {
1080 mDivider.setOnClickListener(new OnClickListener() {
1081 public void onClick(View v) {
1082 onClickAllAppsButton(v);
1083 }
1084 });
1085 }
1086 if (mAllAppsButton != null) {
1087 mAllAppsButton.setOnClickListener(new OnClickListener() {
1088 public void onClick(View v) {
1089 onClickAllAppsButton(v);
1090 }
1091 });
1092 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 }
1094
Romain Guy8a73c512009-11-09 19:19:59 -08001095 @SuppressWarnings({"UnusedDeclaration"})
1096 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001097 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001098 mWorkspace.scrollLeft();
1099 }
Romain Guy8a73c512009-11-09 19:19:59 -08001100 }
1101
1102 @SuppressWarnings({"UnusedDeclaration"})
1103 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001104 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001105 mWorkspace.scrollRight();
1106 }
Romain Guy8a73c512009-11-09 19:19:59 -08001107 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001108
1109 @SuppressWarnings({"UnusedDeclaration"})
1110 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001111 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001112
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001113 int index = -1;
1114 if (v.getId() == R.id.hotseat_left) {
1115 index = 0;
1116 } else if (v.getId() == R.id.hotseat_right) {
1117 index = 1;
1118 }
1119
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001120 // reload these every tap; you never know when they might change
1121 loadHotseats();
1122 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001123 startActivitySafely(
1124 mHotseats[index],
1125 "hotseat"
1126 );
1127 }
1128 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001129
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 /**
1131 * Creates a view representing a shortcut.
1132 *
1133 * @param info The data structure describing the shortcut.
1134 *
1135 * @return A View inflated from R.layout.application.
1136 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001137 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001139 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
1141
1142 /**
1143 * Creates a view representing a shortcut inflated from the specified resource.
1144 *
1145 * @param layoutResId The id of the XML layout used to create the shortcut.
1146 * @param parent The group the shortcut belongs to.
1147 * @param info The data structure describing the shortcut.
1148 *
1149 * @return A View inflated from layoutResId.
1150 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001151 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001152 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1153 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 return favorite;
1156 }
1157
1158 /**
1159 * Add an application shortcut to the workspace.
1160 *
1161 * @param data The intent describing the application.
1162 * @param cellInfo The position on screen where to create the shortcut.
1163 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001164 void completeAddApplication(Context context, Intent data, int screen,
1165 int intersectCellX, int intersectCellY) {
1166 final int[] cellXY = mTmpAddItemCellCoordinates;
1167 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1168
1169 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1170 showOutOfSpaceMessage();
1171 return;
1172 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173
Joe Onorato0589f0f2010-02-08 13:44:00 -08001174 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1175 data, context);
1176
Romain Guy73b979d2009-06-09 12:57:21 -07001177 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001178 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001180 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001181 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1182 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001183 } else {
1184 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 }
1186 }
Romain Guycbb89e42009-06-08 15:52:54 -07001187
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 /**
1189 * Add a shortcut to the workspace.
1190 *
1191 * @param data The intent describing the shortcut.
1192 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001194 private void completeAddShortcut(Intent data, int screen,
1195 int intersectCellX, int intersectCellY) {
1196 final int[] cellXY = mTmpAddItemCellCoordinates;
1197 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001198
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001199 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001200 boolean foundCellSpan = false;
1201 if (touchXY != null) {
1202 // when dragging and dropping, just find the closest free spot
1203 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1204 int[] result = screenLayout.findNearestVacantArea(
1205 touchXY[0], touchXY[1], 1, 1, cellXY);
1206 foundCellSpan = (result != null);
1207 } else {
1208 foundCellSpan = layout.findCellForSpanThatIntersects(
1209 cellXY, 1, 1, intersectCellX, intersectCellY);
1210 }
1211
1212 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001213 showOutOfSpaceMessage();
1214 return;
1215 }
1216
1217 final ShortcutInfo info = mModel.addShortcut(
1218 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219
1220 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001222 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 }
1224 }
1225
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001227 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001229 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001230 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 */
Winson Chungf7640c82011-02-28 13:47:29 -08001232 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001233 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001234
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001235 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001236 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1237 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001238
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001240 // We have saved the position to which the widget was dragged-- this really only matters
1241 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001242 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001243
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001244 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001245 boolean foundCellSpan = false;
1246 if (touchXY != null) {
1247 // when dragging and dropping, just find the closest free spot
1248 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1249 int[] result = screenLayout.findNearestVacantArea(
1250 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001251 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001252 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001253 // if we long pressed on an empty cell to bring up a menu,
1254 // make sure we intersect the empty cell
1255 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1256 // findCellForSpanThatIntersects will just ignore them
1257 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1258 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001259 }
1260
Michael Jurka0280c3b2010-09-17 15:00:07 -07001261 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001262 if (appWidgetId != -1) {
1263 // Deleting an app widget ID is a void call but writes to disk before returning
1264 // to the caller...
1265 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1266 new Thread("deleteAppWidgetId") {
1267 public void run() {
1268 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1269 }
1270 }.start();
1271 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001272 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001273 return;
1274 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001276 // Build Launcher-specific widget info and save to database
1277 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001278 launcherInfo.spanX = spanXY[0];
1279 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001280
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 LauncherModel.addItemToDatabase(this, launcherInfo,
1282 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001283 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284
1285 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001286 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001289 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001290
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001291 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001293
Michael Jurka0280c3b2010-09-17 15:00:07 -07001294 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001295 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001296
1297 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 }
1299 }
Romain Guycbb89e42009-06-08 15:52:54 -07001300
Adam Cohended9f8d2010-11-03 13:25:16 -07001301 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1302 @Override
1303 public void onReceive(Context context, Intent intent) {
1304 final String action = intent.getAction();
1305 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1306 mUserPresent = false;
1307 updateRunning();
1308 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1309 mUserPresent = true;
1310 updateRunning();
1311 }
1312 }
1313 };
1314
1315 @Override
1316 public void onAttachedToWindow() {
1317 super.onAttachedToWindow();
1318
1319 // Listen for broadcasts related to user-presence
1320 final IntentFilter filter = new IntentFilter();
1321 filter.addAction(Intent.ACTION_SCREEN_OFF);
1322 filter.addAction(Intent.ACTION_USER_PRESENT);
1323 registerReceiver(mReceiver, filter);
1324
Adam Cohend113e0c2010-11-11 10:48:05 -08001325 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001326 mVisible = true;
1327 }
1328
1329 @Override
1330 public void onDetachedFromWindow() {
1331 super.onDetachedFromWindow();
1332 mVisible = false;
1333
Adam Cohend113e0c2010-11-11 10:48:05 -08001334 if (mAttached) {
1335 unregisterReceiver(mReceiver);
1336 mAttached = false;
1337 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001338 updateRunning();
1339 }
1340
1341 public void onWindowVisibilityChanged(int visibility) {
1342 mVisible = visibility == View.VISIBLE;
1343 updateRunning();
1344 }
1345
1346 private void sendAdvanceMessage(long delay) {
1347 mHandler.removeMessages(ADVANCE_MSG);
1348 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1349 mHandler.sendMessageDelayed(msg, delay);
1350 mAutoAdvanceSentTime = System.currentTimeMillis();
1351 }
1352
1353 private void updateRunning() {
1354 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1355 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1356 mAutoAdvanceRunning = autoAdvanceRunning;
1357 if (autoAdvanceRunning) {
1358 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1359 sendAdvanceMessage(delay);
1360 } else {
1361 if (!mWidgetsToAdvance.isEmpty()) {
1362 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1363 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1364 }
1365 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001366 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001367 }
1368 }
1369 }
1370
1371 private final Handler mHandler = new Handler() {
1372 @Override
1373 public void handleMessage(Message msg) {
1374 if (msg.what == ADVANCE_MSG) {
1375 int i = 0;
1376 for (View key: mWidgetsToAdvance.keySet()) {
1377 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1378 final int delay = mAdvanceStagger * i;
1379 if (v instanceof Advanceable) {
1380 postDelayed(new Runnable() {
1381 public void run() {
1382 ((Advanceable) v).advance();
1383 }
1384 }, delay);
1385 }
1386 i++;
1387 }
1388 sendAdvanceMessage(mAdvanceInterval);
1389 }
1390 }
1391 };
1392
1393 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1394 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1395 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1396 if (v instanceof Advanceable) {
1397 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001398 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001399 updateRunning();
1400 }
1401 }
1402
1403 void removeWidgetToAutoAdvance(View hostView) {
1404 if (mWidgetsToAdvance.containsKey(hostView)) {
1405 mWidgetsToAdvance.remove(hostView);
1406 updateRunning();
1407 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001408 }
1409
Joe Onorato9c1289c2009-08-17 11:03:03 -04001410 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1411 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001412 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001413 launcherInfo.hostView = null;
1414 }
1415
Adam Cohended9f8d2010-11-03 13:25:16 -07001416 void showOutOfSpaceMessage() {
1417 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1418 }
1419
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001420 public LauncherAppWidgetHost getAppWidgetHost() {
1421 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 }
Romain Guycbb89e42009-06-08 15:52:54 -07001423
Winson Chunga9abd0e2010-10-27 17:18:37 -07001424 public LauncherModel getModel() {
1425 return mModel;
1426 }
1427
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001428 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001429 getWindow().closeAllPanels();
1430
1431 try {
1432 dismissDialog(DIALOG_CREATE_SHORTCUT);
1433 // Unlock the workspace if the dialog was showing
1434 } catch (Exception e) {
1435 // An exception is thrown if the dialog is not visible, which is fine
1436 }
1437
1438 try {
1439 dismissDialog(DIALOG_RENAME_FOLDER);
1440 // Unlock the workspace if the dialog was showing
1441 } catch (Exception e) {
1442 // An exception is thrown if the dialog is not visible, which is fine
1443 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001444
1445 // Whatever we were doing is hereby canceled.
1446 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001447 }
1448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 @Override
1450 protected void onNewIntent(Intent intent) {
1451 super.onNewIntent(intent);
1452
1453 // Close the menu
1454 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001455 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001456 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001457
Joe Onorato14f122b2009-11-19 14:06:36 -08001458 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1459 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001460
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001461 // In all these cases, only animate if we're already on home
1462
Michael Jurka4cb37242010-08-09 21:05:32 -07001463 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001464 mWorkspace.unshrink(alreadyOnHome);
1465 }
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001466
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001467 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001468 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001469 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001470 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001471
Joe Onorato3a8820b2009-11-10 15:06:42 -08001472 final View v = getWindow().peekDecorView();
1473 if (v != null && v.getWindowToken() != null) {
1474 InputMethodManager imm = (InputMethodManager)getSystemService(
1475 INPUT_METHOD_SERVICE);
1476 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 }
1478 }
1479 }
1480
1481 @Override
1482 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1483 // Do not call super here
1484 mSavedInstanceState = savedInstanceState;
1485 }
1486
1487 @Override
1488 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001489 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490
1491 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1492 if (folders.size() > 0) {
1493 final int count = folders.size();
1494 long[] ids = new long[count];
1495 for (int i = 0; i < count; i++) {
1496 final FolderInfo info = folders.get(i).getInfo();
1497 ids[i] = info.id;
1498 }
1499 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1500 } else {
1501 super.onSaveInstanceState(outState);
1502 }
1503
Michael Jurka883f55b2010-10-21 15:47:14 -07001504 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505
Michael Jurka0280c3b2010-09-17 15:00:07 -07001506 if (mAddScreen > -1 && mWaitingForResult) {
1507 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1508 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1509 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 }
1511
1512 if (mFolderInfo != null && mWaitingForResult) {
1513 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1514 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1515 }
Michael Jurka946ad472010-07-09 18:05:18 -07001516
Winson Chung43b119d2010-12-06 16:45:05 -08001517 // Save the current AllApps drawer tab
1518 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1519 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1520 String currentTabTag = tabhost.getCurrentTabTag();
1521 if (currentTabTag != null) {
1522 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001523 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001524 }
1525 }
1526
1527 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001528 if (mHomeCustomizationDrawer != null) {
1529 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1530 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001531 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001532 }
1533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
1535
1536 @Override
1537 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001539
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001541 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001543 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001545 mAppWidgetHost = null;
1546
1547 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548
1549 TextKeyListener.getInstance().release();
1550
Joe Onorato9c1289c2009-08-17 11:03:03 -04001551 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552
Joe Onorato9c1289c2009-08-17 11:03:03 -04001553 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001554
1555 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001556
Patrick Dubroyab962b72010-07-26 12:10:10 -07001557 // Some launcher layouts don't have a previous and next view
1558 if (mPreviousView != null) {
1559 dismissPreview(mPreviousView);
1560 }
1561 if (mNextView != null) {
1562 dismissPreview(mNextView);
1563 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001564
1565 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001566
1567 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1568 mWorkspace.removeAllViews();
1569 mWorkspace = null;
1570 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001571
1572 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 }
1574
1575 @Override
1576 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001577 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 super.startActivityForResult(intent, requestCode);
1579 }
1580
1581 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001582 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001584
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001585 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001586
Karl Rosaen138a0412009-04-23 19:00:21 -07001587 if (initialQuery == null) {
1588 // Use any text typed in the launcher as the initial query
1589 initialQuery = getTypedText();
1590 clearTypedText();
1591 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 if (appSearchData == null) {
1593 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001594 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 }
Romain Guycbb89e42009-06-08 15:52:54 -07001596
Karl Rosaen138a0412009-04-23 19:00:21 -07001597 final SearchManager searchManager =
1598 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001599 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001600 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 }
1602
1603 @Override
1604 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001605 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001606 return false;
1607 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608
1609 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001610
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1612 .setIcon(android.R.drawable.ic_menu_add)
1613 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001614 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1615 .setIcon(android.R.drawable.ic_menu_manage)
1616 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001617 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 .setIcon(android.R.drawable.ic_menu_gallery)
1619 .setAlphabeticShortcut('W');
1620 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1621 .setIcon(android.R.drawable.ic_search_category_default)
1622 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1623 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1624 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1625 .setAlphabeticShortcut('N');
1626
1627 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001628 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1629 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630
1631 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1632 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1633 .setIntent(settings);
1634
1635 return true;
1636 }
1637
1638 @Override
1639 public boolean onPrepareOptionsMenu(Menu menu) {
1640 super.onPrepareOptionsMenu(menu);
1641
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001642 // If all apps is animating, don't show the menu, because we don't know
1643 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001644 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001645 return false;
1646 }
1647
1648 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001649 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001650 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1651 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1652
1653 // Disable add if the workspace is full.
1654 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001655 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1656 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001657 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658
1659 return true;
1660 }
1661
1662 @Override
1663 public boolean onOptionsItemSelected(MenuItem item) {
1664 switch (item.getItemId()) {
1665 case MENU_ADD:
1666 addItems();
1667 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001668 case MENU_MANAGE_APPS:
1669 manageApps();
1670 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 case MENU_WALLPAPER_SETTINGS:
1672 startWallpaper();
1673 return true;
1674 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001675 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 return true;
1677 case MENU_NOTIFICATIONS:
1678 showNotifications();
1679 return true;
1680 }
1681
1682 return super.onOptionsItemSelected(item);
1683 }
Romain Guycbb89e42009-06-08 15:52:54 -07001684
Karl Rosaen138a0412009-04-23 19:00:21 -07001685 /**
1686 * Indicates that we want global search for this activity by setting the globalSearch
1687 * argument for {@link #startSearch} to true.
1688 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001690 @Override
1691 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001692 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001693 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001694 }
1695
Joe Onorato9c1289c2009-08-17 11:03:03 -04001696 public boolean isWorkspaceLocked() {
1697 return mWorkspaceLoading || mWaitingForResult;
1698 }
1699
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001700 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1701 private boolean isPreviewVisible() {
1702 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1703 (mNextView != null && mNextView.getTag() != null);
1704 }
1705
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001707 if (LauncherApplication.isScreenXLarge()) {
1708 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001709 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001710 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001711 }
1712 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001713 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001714 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001715 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001716 }
1717
Michael Jurka0280c3b2010-09-17 15:00:07 -07001718 private void resetAddInfo() {
1719 mAddScreen = -1;
1720 mAddIntersectCellX = -1;
1721 mAddIntersectCellY = -1;
1722 mAddDropPosition = null;
1723 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001724
Winson Chung55cef262010-10-28 14:14:18 -07001725 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001726 resetAddInfo();
1727 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001728 mAddDropPosition = position;
1729
Michael Jurkaaf442092010-06-10 17:01:57 -07001730 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001731 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1732 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001733 }
1734
Winson Chung7ad01412010-09-27 11:33:03 -07001735 private void manageApps() {
1736 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1737 }
1738
Michael Jurkaaf442092010-06-10 17:01:57 -07001739 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001740 // TODO: catch bad widget exception when sent
1741 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001742 // TODO: Is this log message meaningful?
1743 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001744 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001745 }
1746
Winson Chung55cef262010-10-28 14:14:18 -07001747 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001748 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749
Bjorn Bringert7984c942009-12-09 15:38:25 +00001750 if (appWidget.configure != null) {
1751 // Launch over to configure widget, if needed
1752 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1753 intent.setComponent(appWidget.configure);
1754 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001755 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001756 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1757 intent.putExtra(
1758 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1759 info.mimeType);
1760
1761 final String mimeType = info.mimeType;
1762 final ClipData clipData = (ClipData) info.configurationData;
1763 final ClipDescription clipDesc = clipData.getDescription();
1764 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1765 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001766 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001767 final CharSequence stringData = item.getText();
1768 final Uri uriData = item.getUri();
1769 final Intent intentData = item.getIntent();
1770 final String key =
1771 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1772 if (uriData != null) {
1773 intent.putExtra(key, uriData);
1774 } else if (intentData != null) {
1775 intent.putExtra(key, intentData);
1776 } else if (stringData != null) {
1777 intent.putExtra(key, stringData);
1778 }
1779 break;
1780 }
1781 }
1782 }
Winson Chung55cef262010-10-28 14:14:18 -07001783 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001784
Romain Guy8e633c52010-03-04 12:51:36 -08001785 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001787 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001788 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 }
1790 }
Romain Guycbb89e42009-06-08 15:52:54 -07001791
Michael Jurka0280c3b2010-09-17 15:00:07 -07001792 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1793 resetAddInfo();
1794 mAddScreen = screen;
1795 mAddDropPosition = position;
1796
1797 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1798 createShortcutIntent.setComponent(componentName);
1799 processShortcut(createShortcutIntent);
1800 }
1801
Joe Onoratodeb98af2010-02-19 14:59:39 -08001802 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001803 // Handle case where user selected "Applications"
1804 String applicationName = getResources().getString(R.string.group_applications);
1805 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001806
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001807 if (applicationName != null && applicationName.equals(shortcutName)) {
1808 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1809 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001810
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001811 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1812 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001813 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001814 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001815 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001816 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 }
1819
Winson Chung24ab2f12010-09-16 14:10:47 -07001820 void processWallpaper(Intent intent) {
1821 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1822 }
1823
Michael Jurka0280c3b2010-09-17 15:00:07 -07001824 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1825 resetAddInfo();
1826 mAddScreen = screen;
1827 mAddDropPosition = position;
1828
1829 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1830 createFolderIntent.setComponent(componentName);
1831
1832 addLiveFolder(createFolderIntent);
1833 }
1834
1835 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001836 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001837 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001838 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001839
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001840 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001841 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001842 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001843 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 }
1846
Michael Jurka0280c3b2010-09-17 15:00:07 -07001847 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 UserFolderInfo folderInfo = new UserFolderInfo();
1849 folderInfo.title = getText(R.string.folder_name);
1850
Michael Jurka0280c3b2010-09-17 15:00:07 -07001851 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1852 final int[] cellXY = mTmpAddItemCellCoordinates;
1853 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1854 showOutOfSpaceMessage();
1855 return;
1856 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857
1858 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001859 LauncherModel.addItemToDatabase(this, folderInfo,
1860 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001861 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001862 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863
1864 // Create the view
1865 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001866 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1867 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001868 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 }
Romain Guycbb89e42009-06-08 15:52:54 -07001870
Joe Onorato9c1289c2009-08-17 11:03:03 -04001871 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001872 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001873 }
1874
Michael Jurka28750fb2010-09-24 17:43:49 -07001875 private void completeAddLiveFolder(
1876 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001877 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1878 final int[] cellXY = mTmpAddItemCellCoordinates;
1879 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1880 showOutOfSpaceMessage();
1881 return;
1882 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883
Michael Jurka0280c3b2010-09-17 15:00:07 -07001884 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885
1886 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001888 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001889 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 }
1891 }
1892
1893 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001894 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895
1896 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1897 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1898
1899 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 Intent.ShortcutIconResource iconResource = null;
1901
1902 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1903 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1904 try {
1905 iconResource = (Intent.ShortcutIconResource) extra;
1906 final PackageManager packageManager = context.getPackageManager();
1907 Resources resources = packageManager.getResourcesForApplication(
1908 iconResource.packageName);
1909 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1910 icon = resources.getDrawable(id);
1911 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001912 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 }
1914 }
1915
1916 if (icon == null) {
1917 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1918 }
1919
1920 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001921 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 info.title = name;
1923 info.iconResource = iconResource;
1924 info.uri = data.getData();
1925 info.baseIntent = baseIntent;
1926 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1927 LiveFolders.DISPLAY_MODE_GRID);
1928
1929 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001930 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001931 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932
1933 return info;
1934 }
1935
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 private void showNotifications() {
1937 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1938 if (statusBar != null) {
1939 statusBar.expand();
1940 }
1941 }
1942
1943 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001944 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001946 Intent chooser = Intent.createChooser(pickWallpaper,
1947 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001948 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1949 // Removed in Eclair MR1
1950// WallpaperManager wm = (WallpaperManager)
1951// getSystemService(Context.WALLPAPER_SERVICE);
1952// WallpaperInfo wi = wm.getWallpaperInfo();
1953// if (wi != null && wi.getSettingsActivity() != null) {
1954// LabeledIntent li = new LabeledIntent(getPackageName(),
1955// R.string.configure_wallpaper, 0);
1956// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1957// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1958// }
Mike Clerona0618e42009-10-22 13:55:21 -07001959 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 }
1961
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001962 /**
1963 * Registers various content observers. The current implementation registers
1964 * only a favorites observer to keep track of the favorites applications.
1965 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001966 private void registerContentObservers() {
1967 ContentResolver resolver = getContentResolver();
1968 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1969 true, mWidgetObserver);
1970 }
1971
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 @Override
1973 public boolean dispatchKeyEvent(KeyEvent event) {
1974 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1975 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001977 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001978 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001979 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001980 dumpState();
1981 return true;
1982 }
1983 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001984 }
1985 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1986 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001987 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 return true;
1989 }
1990 }
1991
1992 return super.dispatchKeyEvent(event);
1993 }
1994
Joe Onorato88ec0992009-11-19 13:16:06 -08001995 @Override
1996 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001997 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1998 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001999 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08002000 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002001 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07002002 dismissPreview(mPreviousView);
2003 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002004 } else {
2005 // Back button is a no-op here, but give at least some feedback for the button press
2006 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002007 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002008 }
2009
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 private void closeFolder() {
2011 Folder folder = mWorkspace.getOpenFolder();
2012 if (folder != null) {
2013 closeFolder(folder);
2014 }
2015 }
2016
2017 void closeFolder(Folder folder) {
2018 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08002019 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07002021 CellLayout cl = (CellLayout) parent;
2022 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05002023 if (folder instanceof DropTarget) {
2024 // Live folders aren't DropTargets.
2025 mDragController.removeDropTarget((DropTarget)folder);
2026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 }
2028 folder.onClose();
2029 }
2030
2031 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002032 * Re-listen when widgets are reset.
2033 */
2034 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002035 if (mAppWidgetHost != null) {
2036 mAppWidgetHost.startListening();
2037 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002038 }
2039
2040 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002041 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2042 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002044 private void unbindDesktopItems() {
2045 for (ItemInfo item: mDesktopItems) {
2046 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002048 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002050
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 /**
2052 * Launches the intent referred by the clicked shortcut.
2053 *
2054 * @param v The view representing the clicked shortcut.
2055 */
2056 public void onClick(View v) {
2057 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002058 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002060 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002061 int[] pos = new int[2];
2062 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002063 intent.setSourceBounds(new Rect(pos[0], pos[1],
2064 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08002065 boolean success = startActivitySafely(intent, tag);
2066
2067 if (success && v instanceof BubbleTextView) {
2068 mWaitingForResume = (BubbleTextView) v;
2069 mWaitingForResume.setStayPressed(true);
2070 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 } else if (tag instanceof FolderInfo) {
2072 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002073 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002074 if (mState == State.ALL_APPS) {
2075 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002076 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002077 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002078 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 }
2080 }
2081
Michael Jurka0e260592010-06-30 17:07:39 -07002082 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002083 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002084 // clicking anywhere on the workspace causes the customization drawer to slide down
2085 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002086 return false;
2087 }
2088
Michael Jurkaaf442092010-06-10 17:01:57 -07002089 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002090 * Event handler for the search button
2091 *
2092 * @param v The view that was clicked.
2093 */
2094 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002095 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002096 // Use a custom animation for launching search
2097 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002098 }
2099
2100 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002101 * Event handler for the voice button
2102 *
2103 * @param v The view that was clicked.
2104 */
2105 public void onClickVoiceButton(View v) {
2106 startVoiceSearch();
2107 }
2108
2109 private void startVoiceSearch() {
2110 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2111 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2112 startActivity(intent);
2113 }
2114
2115 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002116 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002117 * enters home screen customization mode.
2118 *
2119 * @param v The view that was clicked.
2120 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002121 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002122 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002123 }
2124
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002125 /**
2126 * Event handler for the "grid" button that appears on the home screen, which
2127 * enters all apps mode.
2128 *
2129 * @param v The view that was clicked.
2130 */
2131 public void onClickAllAppsButton(View v) {
2132 showAllApps(true);
2133 }
2134
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002135 public void onClickAppMarketButton(View v) {
2136 if (mAppMarketIntent != null) {
2137 startActivitySafely(mAppMarketIntent, "app market");
2138 }
2139 }
2140
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002141 void startApplicationDetailsActivity(ComponentName componentName) {
2142 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002143 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2144 Uri.fromParts("package", packageName, null));
2145 startActivity(intent);
2146 }
2147
Patrick Dubroy5539af72010-09-07 15:22:01 -07002148 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2149 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2150 // System applications cannot be installed. For now, show a toast explaining that.
2151 // We may give them the option of disabling apps this way.
2152 int messageId = R.string.uninstall_system_app_text;
2153 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2154 } else {
2155 String packageName = appInfo.componentName.getPackageName();
2156 String className = appInfo.componentName.getClassName();
2157 Intent intent = new Intent(
2158 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2159 startActivity(intent);
2160 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002161 }
2162
Michael Jurkaddd62e92011-02-16 17:49:14 -08002163 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2165 try {
2166 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002167 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002168 } catch (ActivityNotFoundException e) {
2169 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002170 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 } catch (SecurityException e) {
2172 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002173 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002175 "or use the exported attribute for this activity. "
2176 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002178 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002180
Romain Guy8e633c52010-03-04 12:51:36 -08002181 void startActivityForResultSafely(Intent intent, int requestCode) {
2182 try {
2183 startActivityForResult(intent, requestCode);
2184 } catch (ActivityNotFoundException e) {
2185 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2186 } catch (SecurityException e) {
2187 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2188 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2189 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2190 "or use the exported attribute for this activity.", e);
2191 }
2192 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193
2194 private void handleFolderClick(FolderInfo folderInfo) {
2195 if (!folderInfo.opened) {
2196 // Close any open folder
2197 closeFolder();
2198 // Open the requested folder
2199 openFolder(folderInfo);
2200 } else {
2201 // Find the open folder...
2202 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2203 int folderScreen;
2204 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002205 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 // .. and close it
2207 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002208 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 // Close any folder open on the current screen
2210 closeFolder();
2211 // Pull the folder onto this screen
2212 openFolder(folderInfo);
2213 }
2214 }
2215 }
2216 }
2217
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002219 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 * is animated relative to the specified View. If the View is null, no animation
2221 * is played.
2222 *
2223 * @param folderInfo The FolderInfo describing the folder to open.
2224 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002225 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002226 Folder openFolder;
2227
2228 if (folderInfo instanceof UserFolderInfo) {
2229 openFolder = UserFolder.fromXml(this);
2230 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002231 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 } else {
2233 return;
2234 }
2235
Joe Onorato00acb122009-08-04 16:04:30 -04002236 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 openFolder.setLauncher(this);
2238
2239 openFolder.bind(folderInfo);
2240 folderInfo.opened = true;
2241
Winson Chungaafa03c2010-06-11 17:34:16 -07002242 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 openFolder.onOpen();
2245 }
2246
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002247 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002248 if (mState != State.WORKSPACE) {
2249 return false;
2250 }
2251
Romain Guy1fbc1c82009-11-09 20:43:08 -08002252 switch (v.getId()) {
2253 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002254 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002255 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2256 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002257 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002258 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002259 return true;
2260 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002261 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002262 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2263 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002264 showPreviews(v);
2265 }
2266 return true;
2267 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002268 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002269 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2270 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2271 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002272 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002273 return true;
2274 }
2275
Joe Onorato9c1289c2009-08-17 11:03:03 -04002276 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002277 return false;
2278 }
2279
2280 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002281 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002282 }
2283
Michael Jurka0280c3b2010-09-17 15:00:07 -07002284 resetAddInfo();
2285 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002287 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002288 return true;
2289 }
2290
Michael Jurka0280c3b2010-09-17 15:00:07 -07002291 final View itemUnderLongClick = longClickCellInfo.cell;
2292
Winson Chung304dcde2011-01-07 11:17:23 -08002293 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002294 if (itemUnderLongClick == null) {
2295 // User long pressed on empty space
2296 mWorkspace.setAllowLongPress(false);
2297 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2298 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002299 if (LauncherApplication.isScreenXLarge()) {
2300 addItems();
2301 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002302 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303 }
2304 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002305 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002306 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002307 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2308 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002309 mAddIntersectCellX = longClickCellInfo.cellX;
2310 mAddIntersectCellY = longClickCellInfo.cellY;
2311 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002312 }
2313 }
2314 }
2315 return true;
2316 }
2317
Romain Guye6b8e2f2009-11-10 11:56:55 -08002318 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002319 private void dismissPreview(final View v) {
2320 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002321 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002322 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2323 public void onDismiss() {
2324 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2325 int count = group.getChildCount();
2326 for (int i = 0; i < count; i++) {
2327 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2328 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002329 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2330 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2331
2332 v.setTag(R.id.workspace, null);
2333 v.setTag(R.id.icon, null);
2334 window.setOnDismissListener(null);
2335 }
2336 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002337 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002338 }
2339 v.setTag(null);
2340 }
2341
Romain Guyf8e6a802009-12-07 17:48:02 -08002342 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002343 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002344 }
2345
Romain Guya6abce82009-11-10 02:54:41 -08002346 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002347 final Resources resources = getResources();
2348 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002349
Romain Guya6abce82009-11-10 02:54:41 -08002350 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002351
Romain Guy9d31e9f2009-11-11 18:54:28 -08002352 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002353
Romain Guyf8e6a802009-12-07 17:48:02 -08002354 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002355 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002356 int extraW = (int) ((r.left + r.right) * max);
2357 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002358
2359 int aW = cell.getWidth() - extraW;
2360 float w = aW / max;
2361
2362 int width = cell.getWidth();
2363 int height = cell.getHeight();
2364 int x = cell.getLeftPadding();
2365 int y = cell.getTopPadding();
2366 width -= (x + cell.getRightPadding());
2367 height -= (y + cell.getBottomPadding());
2368
2369 float scale = w / width;
2370
2371 int count = end - start;
2372
2373 final float sWidth = width * scale;
2374 float sHeight = height * scale;
2375
Romain Guye6b8e2f2009-11-10 11:56:55 -08002376 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002377
Romain Guye6b8e2f2009-11-10 11:56:55 -08002378 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2379 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002380
2381 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002382 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002383 cell = (CellLayout) workspace.getChildAt(i);
2384
Romain Guyf8e6a802009-12-07 17:48:02 -08002385 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002386 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002387
2388 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002389 c.scale(scale, scale);
2390 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002391 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002392
Romain Guy9d31e9f2009-11-11 18:54:28 -08002393 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002394 image.setImageBitmap(bitmap);
2395 image.setTag(i);
2396 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002397 image.setOnFocusChangeListener(handler);
2398 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002399 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002400
2401 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002402 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2403
Winson Chungaafa03c2010-06-11 17:34:16 -07002404 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002405 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002406
2407 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002408 p.setContentView(preview);
2409 p.setWidth((int) (sWidth * count + extraW));
2410 p.setHeight((int) (sHeight + extraH));
2411 p.setAnimationStyle(R.style.AnimationPreview);
2412 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002413 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002414 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002415 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002416
Romain Guye6b8e2f2009-11-10 11:56:55 -08002417 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2418 public void onDismiss() {
2419 dismissPreview(anchor);
2420 }
2421 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002422
2423 anchor.setTag(p);
2424 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002425 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002426 }
2427
Romain Guy9d31e9f2009-11-11 18:54:28 -08002428 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002429 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002430
Romain Guye6b8e2f2009-11-10 11:56:55 -08002431 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002432 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002433 }
2434
2435 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002436 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002437 v.post(this);
2438 }
2439
2440 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002441 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002442 }
2443
2444 public void onFocusChange(View v, boolean hasFocus) {
2445 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002446 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002447 }
Romain Guya6abce82009-11-10 02:54:41 -08002448 }
2449 }
2450
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002451 Workspace getWorkspace() {
2452 return mWorkspace;
2453 }
2454
Michael Jurka3c38d562011-02-18 19:22:12 -08002455 TabHost getCustomizationDrawer() {
2456 return mHomeCustomizationDrawer;
2457 }
2458
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 @Override
2460 protected Dialog onCreateDialog(int id) {
2461 switch (id) {
2462 case DIALOG_CREATE_SHORTCUT:
2463 return new CreateShortcut().createDialog();
2464 case DIALOG_RENAME_FOLDER:
2465 return new RenameFolder().createDialog();
2466 }
2467
2468 return super.onCreateDialog(id);
2469 }
2470
2471 @Override
2472 protected void onPrepareDialog(int id, Dialog dialog) {
2473 switch (id) {
2474 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002475 break;
2476 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002477 if (mFolderInfo != null) {
2478 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2479 final CharSequence text = mFolderInfo.title;
2480 input.setText(text);
2481 input.setSelection(0, text.length());
2482 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002483 break;
2484 }
2485 }
2486
2487 void showRenameDialog(FolderInfo info) {
2488 mFolderInfo = info;
2489 mWaitingForResult = true;
2490 showDialog(DIALOG_RENAME_FOLDER);
2491 }
2492
Michael Jurka0280c3b2010-09-17 15:00:07 -07002493 private void showAddDialog(int intersectX, int intersectY) {
2494 resetAddInfo();
2495 mAddIntersectCellX = intersectX;
2496 mAddIntersectCellY = intersectY;
2497 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 mWaitingForResult = true;
2499 showDialog(DIALOG_CREATE_SHORTCUT);
2500 }
2501
Joe Onoratodeb98af2010-02-19 14:59:39 -08002502 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002503 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002504 Bundle bundle = new Bundle();
2505
2506 ArrayList<String> shortcutNames = new ArrayList<String>();
2507 shortcutNames.add(getString(R.string.group_applications));
2508 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2509
2510 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2511 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2512 R.drawable.ic_launcher_application));
2513 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2514
2515 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2516 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002517 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002518 pickIntent.putExtras(bundle);
2519
Joe Onoratodeb98af2010-02-19 14:59:39 -08002520 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002521 }
2522
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 private class RenameFolder {
2524 private EditText mInput;
2525
2526 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002527 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2528 mInput = (EditText) layout.findViewById(R.id.folder_name);
2529
2530 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2531 builder.setIcon(0);
2532 builder.setTitle(getString(R.string.rename_folder_title));
2533 builder.setCancelable(true);
2534 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2535 public void onCancel(DialogInterface dialog) {
2536 cleanup();
2537 }
2538 });
2539 builder.setNegativeButton(getString(R.string.cancel_action),
2540 new Dialog.OnClickListener() {
2541 public void onClick(DialogInterface dialog, int which) {
2542 cleanup();
2543 }
2544 }
2545 );
2546 builder.setPositiveButton(getString(R.string.rename_action),
2547 new Dialog.OnClickListener() {
2548 public void onClick(DialogInterface dialog, int which) {
2549 changeFolderName();
2550 }
2551 }
2552 );
2553 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002554
2555 final AlertDialog dialog = builder.create();
2556 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2557 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002558 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002559 mInput.requestFocus();
2560 InputMethodManager inputManager = (InputMethodManager)
2561 getSystemService(Context.INPUT_METHOD_SERVICE);
2562 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002563 }
2564 });
2565
2566 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002567 }
2568
2569 private void changeFolderName() {
2570 final String name = mInput.getText().toString();
2571 if (!TextUtils.isEmpty(name)) {
2572 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002573 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002574 mFolderInfo.title = name;
2575 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2576
Joe Onorato9c1289c2009-08-17 11:03:03 -04002577 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002578 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002579 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002580 } else {
2581 final FolderIcon folderIcon = (FolderIcon)
2582 mWorkspace.getViewForTag(mFolderInfo);
2583 if (folderIcon != null) {
2584 folderIcon.setText(name);
2585 getWorkspace().requestLayout();
2586 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002587 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002588 mWorkspaceLoading = true;
2589 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002590 }
2591 }
2592 }
2593 cleanup();
2594 }
2595
2596 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002597 dismissDialog(DIALOG_RENAME_FOLDER);
2598 mWaitingForResult = false;
2599 mFolderInfo = null;
2600 }
2601 }
2602
Daniel Sandler843e8602010-06-07 14:59:01 -04002603 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2604 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002605 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002606 }
2607
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002608 // AllAppsView.Watcher
2609 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002610 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2611 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002612 mWorkspace.setVisibility(View.GONE);
2613 }
2614 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002615
Michael Jurkaafca9532011-02-16 14:50:35 -08002616 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002617 button.setVisibility(View.VISIBLE);
2618 button.setFocusable(true);
2619 button.setClickable(true);
2620 }
2621
2622 private void hideToolbarButton(View button) {
2623 button.setAlpha(0.0f);
2624 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2625 button.setVisibility(View.INVISIBLE);
Michael Jurkaafca9532011-02-16 14:50:35 -08002626 }
2627
2628 private void disableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002629 button.setFocusable(false);
2630 button.setClickable(false);
2631 }
2632
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002633 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002634 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002635 *
2636 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2637 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002638 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002639 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002640 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002641 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002642 final boolean showing = show;
2643 final boolean hiding = !show;
2644
2645 final int duration = show ?
2646 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2647 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2648
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002649 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002650 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002651 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002652 anim.addUpdateListener(new AnimatorUpdateListener() {
2653 public void onAnimationUpdate(ValueAnimator animation) {
2654 view.setAlpha((Float) animation.getAnimatedValue());
2655 }
2656 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002657 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002658 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002659 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002660 if (showing) showAndEnableToolbarButton(view);
2661 if (hiding) disableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002662 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002663 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002664 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002665 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002666 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002667 });
2668 seq.play(anim);
2669 } else {
2670 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002671 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002672 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002673 } else {
Michael Jurkaafca9532011-02-16 14:50:35 -08002674 disableToolbarButton(view);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002675 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002676 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002677 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002678 }
2679
2680 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002681 * Show/hide the appropriate toolbar buttons for newState.
2682 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002683 *
2684 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002685 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2686 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002687 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002688 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002689 switch (newState) {
2690 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002691 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2692 mDeleteZone.setOverlappingViews(
2693 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002694 mDeleteZone.setDragAndDropEnabled(true);
2695 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002696 break;
2697 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002698 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002699 mDeleteZone.setDragAndDropEnabled(false);
2700 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002701 break;
2702 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002703 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002704 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002705 break;
2706 }
2707 }
2708
2709 /**
2710 * Helper method for the cameraZoomIn/cameraZoomOut animations
2711 * @param view The view being animated
2712 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2713 * @param scaleFactor The scale factor used for the zoom
2714 */
2715 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2716 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002717
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002718 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002719 // Set pivotY so that at the starting zoom factor, the view is partially
2720 // visible. Modifying initialHeightFactor changes how much of the view is
2721 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002722 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002723 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002724 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002725 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002726 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002727 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002728 }
2729 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002730
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002731 /**
2732 * Zoom the camera out from the workspace to reveal 'toView'.
2733 * Assumes that the view to show is anchored at either the very top or very bottom
2734 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002735 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002736 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002737 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002738 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002739 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002740
2741 final int duration = toAllApps ?
2742 res.getInteger(R.integer.config_allAppsZoomInTime) :
2743 res.getInteger(R.integer.config_customizeZoomInTime);
2744
2745 final float scale = toAllApps ?
2746 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2747 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2748
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002749 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002750
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002751 setPivotsForZoom(toView, toState, scale);
2752
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002753 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002754 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002755 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002756 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002757 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002758
2759 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002760 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2761 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
2762 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2763 public void onAnimationUpdate(ValueAnimator animation) {
2764 final float b = (Float) animation.getAnimatedValue();
2765 final float a = 1f - b;
2766 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002767 toView.setFastScaleX(a * scale + b * 1f);
2768 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002769 }
2770 });
Adam Cohen61033d32010-11-15 18:29:44 -08002771
Adam Cohenf16e5712011-01-13 13:31:45 -08002772 if (toAllApps) {
Michael Jurkad99cf8b2011-01-16 16:53:18 -08002773 toView.setAlpha(0f);
Michael Jurka742574b2011-02-02 23:51:01 -08002774 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurkaea573482011-02-03 19:48:18 -08002775 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002776 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2777 public void onAnimationUpdate(ValueAnimator animation) {
2778 final float b = (Float) animation.getAnimatedValue();
2779 final float a = 1f - b;
2780 // don't need to invalidate because we do so above
2781 toView.setFastAlpha(a * 0f + b * 1f);
2782 }
2783 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002784 alphaAnim.start();
2785 }
2786
Michael Jurkabfadaad2011-01-31 21:35:39 -08002787 // Only use hardware layers in portrait mode, they don't give any gains in landscape
2788 if (mWorkspace.getWidth() < mWorkspace.getHeight()) {
2789 toView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
2790 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002791 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002792 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002793 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002794 // Prepare the position
2795 toView.setTranslationX(0.0f);
2796 toView.setTranslationY(0.0f);
2797 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002798 if (!toAllApps) {
2799 toView.setAlpha(1.0f);
2800 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002801 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002802 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002803 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002804 // If we don't set the final scale values here, if this animation is cancelled
2805 // it will have the wrong scale value and subsequent cameraPan animations will
2806 // not fix that
Michael Jurka3eeaea82011-01-31 15:02:36 -08002807 toView.setLayerType(View.LAYER_TYPE_NONE, null);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002808 toView.setScaleX(1.0f);
2809 toView.setScaleY(1.0f);
2810 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002811 });
2812
Chet Haaseb1254a62010-09-07 13:35:00 -07002813 AnimatorSet toolbarHideAnim = new AnimatorSet();
2814 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002815 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2816
2817 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002818 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002819
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002820 if (mStateAnimation != null) mStateAnimation.cancel();
2821 mStateAnimation = new AnimatorSet();
2822 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2823 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002824
2825 // Show the new toolbar buttons just as the main animation is ending
2826 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002827 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2828 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002829 } else {
2830 toView.setTranslationX(0.0f);
2831 toView.setTranslationY(0.0f);
2832 toView.setScaleX(1.0f);
2833 toView.setScaleY(1.0f);
2834 toView.setVisibility(View.VISIBLE);
2835 hideAndShowToolbarButtons(toState, null, null);
2836 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002837 }
2838
2839 /**
2840 * Zoom the camera back into the workspace, hiding 'fromView'.
2841 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002842 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2843 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002844 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002845 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002846 cameraZoomIn(fromState, animated, false);
2847 }
2848
2849 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002850 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002851 final boolean fromAllApps = (fromState == State.ALL_APPS);
2852
2853 int duration = fromAllApps ?
2854 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2855 res.getInteger(R.integer.config_customizeZoomOutTime);
2856
Michael Jurka742574b2011-02-02 23:51:01 -08002857 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002858 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2859 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2860
2861 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002862
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002863 mCustomizePagedView.endChoiceMode();
2864 mAllAppsPagedView.endChoiceMode();
2865
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002866 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002867
Michael Jurkad3ef3062010-11-23 16:23:58 -08002868 if (!springLoaded) {
2869 mWorkspace.unshrink(animated);
2870 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002871
2872 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002873 if (mStateAnimation != null) mStateAnimation.cancel();
2874 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002875
Michael Jurka742574b2011-02-02 23:51:01 -08002876 final float oldScaleX = fromView.getScaleX();
2877 final float oldScaleY = fromView.getScaleY();
2878
2879 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2880 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2881 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2882 public void onAnimationUpdate(ValueAnimator animation) {
2883 final float b = (Float) animation.getAnimatedValue();
2884 final float a = 1f - b;
2885 ((View)fromView.getParent()).fastInvalidate();
2886 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2887 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2888 }
2889 });
2890 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002891 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002892 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002893 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2894 public void onAnimationUpdate(ValueAnimator animation) {
2895 final float b = (Float) animation.getAnimatedValue();
2896 final float a = 1f - b;
2897 // don't need to invalidate because we do so above
2898 fromView.setFastAlpha(a * 1f + b * 0f);
2899 }
2900 });
2901
Michael Jurkacf6125c2011-01-28 15:20:01 -08002902 fromView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002903 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002904 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002905 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002906 fromView.setVisibility(View.GONE);
Michael Jurkacf6125c2011-01-28 15:20:01 -08002907 fromView.setLayerType(View.LAYER_TYPE_NONE, null);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002908 }
2909 });
2910
Chet Haaseb1254a62010-09-07 13:35:00 -07002911 AnimatorSet toolbarHideAnim = new AnimatorSet();
2912 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002913 if (!springLoaded) {
2914 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2915 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002916
Adam Cohen61033d32010-11-15 18:29:44 -08002917 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002918
2919 // Show the new toolbar buttons at the very end of the whole animation
2920 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2921 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002922 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2923 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002924 } else {
2925 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002926 if (!springLoaded) {
2927 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2928 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002929 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002930 }
2931
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002932 void showAllApps(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002933 if (mState != State.WORKSPACE) {
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 Jurkaafca9532011-02-16 14:50:35 -08002938 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002939 } else {
2940 mAllAppsGrid.zoom(1.0f, animated);
2941 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002942
Romain Guyc16fea72010-03-12 17:17:56 -08002943 ((View) mAllAppsGrid).setFocusable(true);
2944 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002945
Joe Onorato7c312c12009-08-13 21:36:53 -07002946 // TODO: fade these two too
2947 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002948
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002949 // Change the state *after* we've called all the transition code
2950 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002951
Winson Chung5fb63472011-02-02 17:03:37 -08002952 // Pause the auto-advance of widgets until we are out of AllApps
2953 mUserPresent = false;
2954 updateRunning();
2955
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002956 // send an accessibility event to announce the context change
2957 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2958 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002959
2960 void showWorkspace(boolean animated) {
2961 showWorkspace(animated, null);
2962 }
2963
2964 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002965 if (layout != null) {
2966 // always animated, but that's ok since we never specify a layout and
2967 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002968 mWorkspace.unshrink(layout);
2969 } else {
2970 mWorkspace.unshrink(animated);
2971 }
2972 if (mState == State.ALL_APPS) {
2973 closeAllApps(animated);
2974 } else if (mState == State.CUSTOMIZE) {
2975 hideCustomizationDrawer(animated);
2976 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002977
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002978 // Change the state *after* we've called all the transition code
2979 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002980
Winson Chung5fb63472011-02-02 17:03:37 -08002981 // Resume the auto-advance of widgets
2982 mUserPresent = true;
2983 updateRunning();
2984
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002985 // send an accessibility event to announce the context change
2986 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002987 }
2988
Michael Jurkad3ef3062010-11-23 16:23:58 -08002989 void enterSpringLoadedDragMode(CellLayout layout) {
2990 mWorkspace.enterSpringLoadedDragMode(layout);
2991 if (mState == State.ALL_APPS) {
2992 cameraZoomIn(State.ALL_APPS, true, true);
2993 mState = State.ALL_APPS_SPRING_LOADED;
2994 } else if (mState == State.CUSTOMIZE) {
2995 cameraZoomIn(State.CUSTOMIZE, true, true);
2996 mState = State.CUSTOMIZE_SPRING_LOADED;
2997 }/* else {
2998 // we're already in spring loaded mode; don't do anything
2999 }*/
3000 }
3001
3002 void exitSpringLoadedDragMode() {
3003 if (mState == State.ALL_APPS_SPRING_LOADED) {
3004 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
3005 cameraZoomOut(State.ALL_APPS, true);
3006 mState = State.ALL_APPS;
3007 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
3008 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
3009 cameraZoomOut(State.CUSTOMIZE, true);
3010 mState = State.CUSTOMIZE;
3011 }/* else {
3012 // we're not in spring loaded mode; don't do anything
3013 }*/
3014 }
3015
Joe Onoratob2061212009-11-24 16:13:54 -05003016 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08003017 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05003018 * - Home from workspace
3019 * - from center screen
3020 * - from other screens
3021 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003022 * - from center screen
3023 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003024 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003025 * - from center screen
3026 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003027 * - Launch app from workspace and quit
3028 * - with back
3029 * - with home
3030 * - Launch app from all apps and quit
3031 * - with back
3032 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003033 * - Go to a screen that's not the default, then all
3034 * apps, and launch and app, and go back
3035 * - with back
3036 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003037 * - On workspace, long press power and go back
3038 * - with back
3039 * - with home
3040 * - On all apps, long press power and go back
3041 * - with back
3042 * - with home
3043 * - On workspace, power off
3044 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003045 * - Launch an app and turn off the screen while in that app
3046 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003047 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003048 * - From all apps
3049 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003050 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3051 * - From all apps
3052 * - From the center workspace
3053 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003054 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003055 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003056 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003057 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003058 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003059 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003060 } else {
3061 mAllAppsGrid.zoom(0.0f, animated);
3062 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003063 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003064 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003065 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003066 }
3067
Joe Onorato7c312c12009-08-13 21:36:53 -07003068 void lockAllApps() {
3069 // TODO
3070 }
3071
3072 void unlockAllApps() {
3073 // TODO
3074 }
3075
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003076 // Show the customization drawer (only exists in x-large configuration)
3077 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003078 if (mState != State.WORKSPACE) {
3079 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003080 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003081
3082 cameraZoomOut(State.CUSTOMIZE, animated);
3083
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003084 // Change the state *after* we've called all the transition code
3085 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003086
3087 // Pause the auto-advance of widgets until we are out of Customization drawer
3088 mUserPresent = false;
3089 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003090 }
3091
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003092 // Hide the customization drawer (only exists in x-large configuration)
3093 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003094 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003095 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003096 }
3097 }
3098
Patrick Dubroy5f445422011-02-18 14:35:21 -08003099 /**
3100 * Add an item from all apps or customize onto the given workspace screen.
3101 * If layout is null, add to the current screen.
3102 */
3103 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003104 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3105 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003106 } else {
3107 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003108 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003109 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003110
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003111 void onWorkspaceClick(CellLayout layout) {
3112 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003113 }
3114
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003115 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003116 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003117 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003118 // Look for the toolbar icon specified in the activity meta-data
3119 Bundle metaData = packageManager.getActivityInfo(
3120 activityName, PackageManager.GET_META_DATA).metaData;
3121 if (metaData != null) {
3122 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3123 if (iconResId != 0) {
3124 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003125 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003126 }
3127 }
3128 } catch (NameNotFoundException e) {
3129 // Do nothing
3130 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003131 return null;
3132 }
3133
3134 // if successful in getting icon, return it; otherwise, set button to use default drawable
3135 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3136 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3137 TextView button = (TextView) findViewById(buttonId);
3138 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3139
3140 // If we were unable to find the icon via the meta-data, use a generic one
3141 if (toolbarIcon == null) {
3142 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3143 return null;
3144 } else {
3145 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3146 return toolbarIcon.getConstantState();
3147 }
3148 }
3149
3150 // if successful in getting icon, return it; otherwise, set button to use default drawable
3151 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3152 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3153 ImageView button = (ImageView) findViewById(buttonId);
3154 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3155
Michael Jurka0423dcf2010-10-05 14:56:18 -07003156 // If we were unable to find the icon via the meta-data, use a generic one
3157 if (toolbarIcon == null) {
3158 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003159 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003160 } else {
3161 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003162 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003163 }
3164 }
3165
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003166 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3167 TextView button = (TextView) findViewById(buttonId);
3168 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3169 }
3170
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003171 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003172 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003173 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003174 }
3175
Michael Jurka0423dcf2010-10-05 14:56:18 -07003176 private void updateGlobalSearchIcon() {
3177 if (LauncherApplication.isScreenXLarge()) {
3178 final SearchManager searchManager =
3179 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3180 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003181 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003182 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003183 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003184 } else {
3185 findViewById(R.id.search_button).setVisibility(View.GONE);
3186 }
3187 }
3188 }
3189
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003190 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003191 updateButtonWithDrawable(R.id.search_button, d);
3192 }
3193
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003194 private void updateVoiceSearchIcon() {
3195 if (LauncherApplication.isScreenXLarge()) {
3196 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3197 ComponentName activityName = intent.resolveActivity(getPackageManager());
3198 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003199 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003200 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3201 } else {
3202 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003203 }
3204 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003205 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003206
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003207 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003208 updateButtonWithDrawable(R.id.voice_button, d);
3209 }
3210
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003211 /**
3212 * Sets the app market icon (shown when all apps is visible on x-large screens)
3213 */
3214 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003215 if (LauncherApplication.isScreenXLarge()) {
3216 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3217 // Find the app market activity by resolving an intent.
3218 // (If multiple app markets are installed, it will return the ResolverActivity.)
3219 ComponentName activityName = intent.resolveActivity(getPackageManager());
3220 if (activityName != null) {
3221 mAppMarketIntent = intent;
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003222 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003223 R.id.market_button, activityName, R.drawable.app_market_generic);
3224 }
3225 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003226 }
3227
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003228 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003229 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003230 }
3231
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003232 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003233 * Displays the shortcut creation dialog and launches, if necessary, the
3234 * appropriate activity.
3235 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003236 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003237 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3238 DialogInterface.OnShowListener {
3239
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003240 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003242 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003243 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003244
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003245 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3246 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003247 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003248
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003249 builder.setInverseBackgroundForced(true);
3250
3251 AlertDialog dialog = builder.create();
3252 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003253 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003254 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003255
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003256 return dialog;
3257 }
3258
3259 public void onCancel(DialogInterface dialog) {
3260 mWaitingForResult = false;
3261 cleanup();
3262 }
3263
Romain Guycbb89e42009-06-08 15:52:54 -07003264 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003265 }
3266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003267 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003268 try {
3269 dismissDialog(DIALOG_CREATE_SHORTCUT);
3270 } catch (Exception e) {
3271 // An exception is thrown if the dialog is not visible, which is fine
3272 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003273 }
3274
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003275 /**
3276 * Handle the action clicked in the "Add to home" dialog.
3277 */
3278 public void onClick(DialogInterface dialog, int which) {
3279 Resources res = getResources();
3280 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003281
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003282 switch (which) {
3283 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003284 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003285 break;
3286 }
Romain Guycbb89e42009-06-08 15:52:54 -07003287
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003288 case AddAdapter.ITEM_APPWIDGET: {
3289 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003290
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003291 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3292 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003293 // start the pick activity
3294 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3295 break;
3296 }
Romain Guycbb89e42009-06-08 15:52:54 -07003297
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003298 case AddAdapter.ITEM_LIVE_FOLDER: {
3299 // Insert extra item to handle inserting folder
3300 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003301
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003302 ArrayList<String> shortcutNames = new ArrayList<String>();
3303 shortcutNames.add(res.getString(R.string.group_folder));
3304 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003305
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003306 ArrayList<ShortcutIconResource> shortcutIcons =
3307 new ArrayList<ShortcutIconResource>();
3308 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3309 R.drawable.ic_launcher_folder));
3310 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3311
3312 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3313 pickIntent.putExtra(Intent.EXTRA_INTENT,
3314 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3315 pickIntent.putExtra(Intent.EXTRA_TITLE,
3316 getText(R.string.title_select_live_folder));
3317 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003318
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003319 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3320 break;
3321 }
3322
3323 case AddAdapter.ITEM_WALLPAPER: {
3324 startWallpaper();
3325 break;
3326 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003327 }
3328 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003329
3330 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003331 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003332 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003333 }
3334
3335 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003336 * Receives notifications when applications are added/removed.
3337 */
3338 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3339 @Override
3340 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003341 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003342 String reason = intent.getStringExtra("reason");
3343 if (!"homekey".equals(reason)) {
3344 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003345 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003346 animate = false;
3347 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003348 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003349 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003350 }
3351 }
3352
3353 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003354 * Receives notifications whenever the appwidgets are reset.
3355 */
3356 private class AppWidgetResetObserver extends ContentObserver {
3357 public AppWidgetResetObserver() {
3358 super(new Handler());
3359 }
3360
3361 @Override
3362 public void onChange(boolean selfChange) {
3363 onAppWidgetReset();
3364 }
3365 }
3366
3367 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003368 * If the activity is currently paused, signal that we need to re-run the loader
3369 * in onResume.
3370 *
3371 * This needs to be called from incoming places where resources might have been loaded
3372 * while we are paused. That is becaues the Configuration might be wrong
3373 * when we're not running, and if it comes back to what it was when we
3374 * were paused, we are not restarted.
3375 *
3376 * Implementation of the method from LauncherModel.Callbacks.
3377 *
3378 * @return true if we are currently paused. The caller might be able to
3379 * skip some work in that case since we will come back again.
3380 */
3381 public boolean setLoadOnResume() {
3382 if (mPaused) {
3383 Log.i(TAG, "setLoadOnResume");
3384 mOnResumeNeedsLoad = true;
3385 return true;
3386 } else {
3387 return false;
3388 }
3389 }
3390
3391 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003392 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003393 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003394 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003395 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003396 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003397 } else {
3398 return SCREEN_COUNT / 2;
3399 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003400 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003401
Winson Chunga12a2502010-12-20 14:41:35 -08003402 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003403 mAllAppsPagedView = view;
3404 }
3405
Joe Onorato9c1289c2009-08-17 11:03:03 -04003406 /**
3407 * Refreshes the shortcuts shown on the workspace.
3408 *
3409 * Implementation of the method from LauncherModel.Callbacks.
3410 */
3411 public void startBinding() {
3412 final Workspace workspace = mWorkspace;
3413 int count = workspace.getChildCount();
3414 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003415 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003416 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3417 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003418 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003419
Joe Onorato9c1289c2009-08-17 11:03:03 -04003420 if (DEBUG_USER_INTERFACE) {
3421 android.widget.Button finishButton = new android.widget.Button(this);
3422 finishButton.setText("Finish");
3423 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3424
3425 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3426 public void onClick(View v) {
3427 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003428 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003429 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003430 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003431
3432 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3433 // mDesktopItems -> AppWidgetInfo -> hostView).
3434 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003435 }
3436
3437 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003438 * Bind the items start-end from the list.
3439 *
3440 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003441 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003442 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3443
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003444 setLoadOnResume();
3445
Joe Onorato9c1289c2009-08-17 11:03:03 -04003446 final Workspace workspace = mWorkspace;
3447
3448 for (int i=start; i<end; i++) {
3449 final ItemInfo item = shortcuts.get(i);
3450 mDesktopItems.add(item);
3451 switch (item.itemType) {
3452 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3453 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003454 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003455 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3456 false);
3457 break;
3458 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3459 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003460 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003461 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003462 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3463 false);
3464 break;
3465 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3466 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3467 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003468 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003469 (LiveFolderInfo) item);
3470 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3471 false);
3472 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003474 }
3475
Joe Onorato9c1289c2009-08-17 11:03:03 -04003476 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003477 }
3478
Joe Onorato9c1289c2009-08-17 11:03:03 -04003479 /**
3480 * Implementation of the method from LauncherModel.Callbacks.
3481 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003482 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003483 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003484 sFolders.clear();
3485 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003486 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003487
3488 /**
3489 * Add the views for a widget to the workspace.
3490 *
3491 * Implementation of the method from LauncherModel.Callbacks.
3492 */
3493 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003494 setLoadOnResume();
3495
Daniel Sandler843e8602010-06-07 14:59:01 -04003496 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3497 if (DEBUG_WIDGETS) {
3498 Log.d(TAG, "bindAppWidget: " + item);
3499 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003500 final Workspace workspace = mWorkspace;
3501
3502 final int appWidgetId = item.appWidgetId;
3503 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003504 if (DEBUG_WIDGETS) {
3505 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3506 }
3507
Joe Onorato9c1289c2009-08-17 11:03:03 -04003508 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3509
Joe Onorato9c1289c2009-08-17 11:03:03 -04003510 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3511 item.hostView.setTag(item);
3512
3513 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3514 item.cellY, item.spanX, item.spanY, false);
3515
Adam Cohended9f8d2010-11-03 13:25:16 -07003516 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3517
Joe Onorato9c1289c2009-08-17 11:03:03 -04003518 workspace.requestLayout();
3519
3520 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003521
3522 if (DEBUG_WIDGETS) {
3523 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3524 + (SystemClock.uptimeMillis()-start) + "ms");
3525 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003526 }
3527
3528 /**
3529 * Callback saying that there aren't any more items to bind.
3530 *
3531 * Implementation of the method from LauncherModel.Callbacks.
3532 */
3533 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003534 setLoadOnResume();
3535
Joe Onorato9c1289c2009-08-17 11:03:03 -04003536 if (mSavedState != null) {
3537 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003538 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003539 }
3540
3541 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3542 if (userFolders != null) {
3543 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003544 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003545 if (info != null) {
3546 openFolder(info);
3547 }
3548 }
3549 final Folder openFolder = mWorkspace.getOpenFolder();
3550 if (openFolder != null) {
3551 openFolder.requestFocus();
3552 }
3553 }
3554
Joe Onorato9c1289c2009-08-17 11:03:03 -04003555 mSavedState = null;
3556 }
3557
3558 if (mSavedInstanceState != null) {
3559 super.onRestoreInstanceState(mSavedInstanceState);
3560 mSavedInstanceState = null;
3561 }
3562
Winson Chung32bb5e52011-01-31 14:51:56 -08003563 // Workaround a bug that occurs when rotating the device while the customization mode is
3564 // open, we trigger a new layout on all the CellLayout children.
3565 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3566 final int childCount = mWorkspace.getChildCount();
3567 for (int i = 0; i < childCount; ++i) {
3568 mWorkspace.getChildAt(i).requestLayout();
3569 }
3570 }
3571
Joe Onorato9c1289c2009-08-17 11:03:03 -04003572 mWorkspaceLoading = false;
3573 }
3574
3575 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003576 * Updates the icons on the launcher that are affected by changes to the package list
3577 * on the device.
3578 */
3579 private void updateIconsAffectedByPackageManagerChanges() {
3580 updateAppMarketIcon();
3581 updateGlobalSearchIcon();
3582 updateVoiceSearchIcon();
3583 }
3584
3585 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003586 * Add the icons for all apps.
3587 *
3588 * Implementation of the method from LauncherModel.Callbacks.
3589 */
3590 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003591 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003592 if (mCustomizePagedView != null) {
3593 mCustomizePagedView.setApps(apps);
3594 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003595 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003596 }
3597
3598 /**
3599 * A package was installed.
3600 *
3601 * Implementation of the method from LauncherModel.Callbacks.
3602 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003603 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003604 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003605 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003606 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003607 if (mCustomizePagedView != null) {
3608 mCustomizePagedView.addApps(apps);
3609 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003610 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 }
3612
3613 /**
3614 * A package was updated.
3615 *
3616 * Implementation of the method from LauncherModel.Callbacks.
3617 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003618 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003619 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003620 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003621 if (mWorkspace != null) {
3622 mWorkspace.updateShortcuts(apps);
3623 }
3624 if (mAllAppsGrid != null) {
3625 mAllAppsGrid.updateApps(apps);
3626 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003627 if (mCustomizePagedView != null) {
3628 mCustomizePagedView.updateApps(apps);
3629 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003630 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003631 }
3632
3633 /**
3634 * A package was uninstalled.
3635 *
3636 * Implementation of the method from LauncherModel.Callbacks.
3637 */
Joe Onorato36115782010-06-17 13:28:48 -04003638 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003639 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003640 if (permanent) {
3641 mWorkspace.removeItems(apps);
3642 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003643 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003644 if (mCustomizePagedView != null) {
3645 mCustomizePagedView.removeApps(apps);
3646 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003647 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003648 }
Joe Onoratobe386092009-11-17 17:32:16 -08003649
3650 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003651 * A number of packages were updated.
3652 */
3653 public void bindPackagesUpdated() {
3654 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003655 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003656 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003657 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003658 }
3659
Winson Chung400438b2011-01-16 17:53:48 -08003660 private int mapConfigurationOriActivityInfoOri(int configOri) {
3661 final Display d = getWindowManager().getDefaultDisplay();
3662 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3663 switch (d.getRotation()) {
3664 case Surface.ROTATION_0:
3665 case Surface.ROTATION_180:
3666 // We are currently in the same basic orientation as the natural orientation
3667 naturalOri = configOri;
3668 break;
3669 case Surface.ROTATION_90:
3670 case Surface.ROTATION_270:
3671 // We are currently in the other basic orientation to the natural orientation
3672 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3673 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3674 break;
3675 }
3676
3677 int[] oriMap = {
3678 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3679 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3680 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3681 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3682 };
3683 // Since the map starts at portrait, we need to offset if this device's natural orientation
3684 // is landscape.
3685 int indexOffset = 0;
3686 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3687 indexOffset = 1;
3688 }
3689 return oriMap[(d.getRotation() + indexOffset) % 4];
3690 }
3691 public void lockScreenOrientation() {
3692 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3693 .getConfiguration().orientation));
3694 }
3695 public void unlockScreenOrientation() {
3696 mHandler.postDelayed(new Runnable() {
3697 public void run() {
3698 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3699 }
3700 }, mRestoreScreenOrientationDelay);
3701 }
3702
Winson Chung80baf5a2010-08-09 16:03:15 -07003703 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003704 * Prints out out state for debugging.
3705 */
3706 public void dumpState() {
3707 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003708 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003709 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3710 Log.d(TAG, "mRestoring=" + mRestoring);
3711 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3712 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3713 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003714 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003715 mModel.dumpState();
3716 mAllAppsGrid.dumpState();
3717 Log.d(TAG, "END launcher2 dump state");
3718 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003719}