blob: 63982ba309a8a0bd55e505ec7d618b25c9af1896 [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()) {
Winson Chungc6d20002011-03-02 11:57:29 -0800421 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
422 updateIconsAffectedByPackageManagerChanges();
Michael Jurka4ef207b2010-11-29 17:05:45 -0800423 }
Winson Chungc6d20002011-03-02 11:57:29 -0800424 updateGlobalSearchIcon(sGlobalSearchIcon);
425 updateVoiceSearchIcon(sVoiceSearchIcon);
426 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800427 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 }
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800430 @Override
431 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
432 super.dispatchPopulateAccessibilityEvent(event);
433
434 // we want to take over text population so it is context dependent
435 event.getText().clear();
436 if (mState == State.ALL_APPS) {
437 event.getText().add(getString(R.string.all_apps_button_label));
438 } else if (mState == State.WORKSPACE) {
439 event.getText().add(getString(R.string.all_apps_home_button_label));
440 }
441
442 return true;
443 }
444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700446 if (sLocaleConfiguration == null) {
447 new AsyncTask<Void, Void, LocaleConfiguration>() {
448 @Override
449 protected LocaleConfiguration doInBackground(Void... unused) {
450 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
451 readConfiguration(Launcher.this, localeConfiguration);
452 return localeConfiguration;
453 }
454
455 @Override
456 protected void onPostExecute(LocaleConfiguration result) {
457 sLocaleConfiguration = result;
458 checkForLocaleChange(); // recursive, but now with a locale configuration
459 }
460 }.execute();
461 return;
462 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700463
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 final Configuration configuration = getResources().getConfiguration();
465
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700466 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 final String locale = configuration.locale.toString();
468
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700469 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 final int mcc = configuration.mcc;
471
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700472 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 final int mnc = configuration.mnc;
474
Romain Guy84f296c2009-11-04 15:00:44 -0800475 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476
Romain Guy84f296c2009-11-04 15:00:44 -0800477 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700478 sLocaleConfiguration.locale = locale;
479 sLocaleConfiguration.mcc = mcc;
480 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700481
Joe Onorato0589f0f2010-02-08 13:44:00 -0800482 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400483 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700484
485 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
486 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700487 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700488 public void run() {
489 writeConfiguration(Launcher.this, localeConfiguration);
490 }
491 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700492 }
493 }
494
495 private static class LocaleConfiguration {
496 public String locale;
497 public int mcc = -1;
498 public int mnc = -1;
499 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700500
Romain Guy98d01652009-06-30 16:21:04 -0700501 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
502 DataInputStream in = null;
503 try {
504 in = new DataInputStream(context.openFileInput(PREFERENCES));
505 configuration.locale = in.readUTF();
506 configuration.mcc = in.readInt();
507 configuration.mnc = in.readInt();
508 } catch (FileNotFoundException e) {
509 // Ignore
510 } catch (IOException e) {
511 // Ignore
512 } finally {
513 if (in != null) {
514 try {
515 in.close();
516 } catch (IOException e) {
517 // Ignore
518 }
519 }
520 }
521 }
522
523 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
524 DataOutputStream out = null;
525 try {
526 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
527 out.writeUTF(configuration.locale);
528 out.writeInt(configuration.mcc);
529 out.writeInt(configuration.mnc);
530 out.flush();
531 } catch (FileNotFoundException e) {
532 // Ignore
533 } catch (IOException e) {
534 //noinspection ResultOfMethodCallIgnored
535 context.getFileStreamPath(PREFERENCES).delete();
536 } finally {
537 if (out != null) {
538 try {
539 out.close();
540 } catch (IOException e) {
541 // Ignore
542 }
543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 }
545 }
546
547 static int getScreen() {
548 synchronized (sLock) {
549 return sScreen;
550 }
551 }
552
553 static void setScreen(int screen) {
554 synchronized (sLock) {
555 sScreen = screen;
556 }
557 }
558
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400559 // Note: This doesn't do all the client-id magic that BrowserProvider does
560 // in Browser. (http://b/2425179)
561 private Uri getDefaultBrowserUri() {
562 String url = getString(R.string.default_browser_url);
563 if (url.indexOf("{CID}") != -1) {
564 url = url.replace("{CID}", "android-google");
565 }
566 return Uri.parse(url);
567 }
568
569 // Load the Intent templates from arrays.xml to populate the hotseats. For
570 // each Intent, if it resolves to a single app, use that as the launch
571 // intent & use that app's label as the contentDescription. Otherwise,
572 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400573 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400574 if (mHotseatConfig == null) {
575 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
576 if (mHotseatConfig.length > 0) {
577 mHotseats = new Intent[mHotseatConfig.length];
578 mHotseatLabels = new CharSequence[mHotseatConfig.length];
579 mHotseatIcons = new Drawable[mHotseatConfig.length];
580 } else {
581 mHotseats = null;
582 mHotseatIcons = null;
583 mHotseatLabels = null;
584 }
585
586 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
587 for (int i=0; i<mHotseatConfig.length; i++) {
588 // load icon for this slot; currently unrelated to the actual activity
589 try {
590 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
591 } catch (ArrayIndexOutOfBoundsException ex) {
592 Log.w(TAG, "Missing hotseat_icons array item #" + i);
593 mHotseatIcons[i] = null;
594 }
595 }
596 hotseatIconDrawables.recycle();
597 }
598
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400599 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400600 for (int i=0; i<mHotseatConfig.length; i++) {
601 Intent intent = null;
602 if (mHotseatConfig[i].equals("*BROWSER*")) {
603 // magic value meaning "launch user's default web browser"
604 // replace it with a generic web request so we can see if there is indeed a default
605 String defaultUri = getString(R.string.default_browser_url);
606 intent = new Intent(
607 Intent.ACTION_VIEW,
608 ((defaultUri != null)
609 ? Uri.parse(defaultUri)
610 : getDefaultBrowserUri())
611 ).addCategory(Intent.CATEGORY_BROWSABLE);
612 // note: if the user launches this without a default set, she
613 // will always be taken to the default URL above; this is
614 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700615 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400616 // URL is unavoidably sent to the chosen app.
617 } else {
618 try {
619 intent = Intent.parseUri(mHotseatConfig[i], 0);
620 } catch (java.net.URISyntaxException ex) {
621 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
622 // bogus; leave intent=null
623 }
624 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700625
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400626 if (intent == null) {
627 mHotseats[i] = null;
628 mHotseatLabels[i] = getText(R.string.activity_not_found);
629 continue;
630 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400631
632 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700633 Log.d(TAG, "loadHotseats: hotseat " + i
634 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400635 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400636 + "]");
637 }
638
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400639 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
640 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700641 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400642 Log.d(TAG, "Best match for intent: " + bestMatch);
643 Log.d(TAG, "All matches: ");
644 for (ResolveInfo ri : allMatches) {
645 Log.d(TAG, " --> " + ri);
646 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400647 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400648 // did this resolve to a single app, or the resolver?
649 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700650 // can't find any activity to handle this. let's leave the
651 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400652 // to launch.
653 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400654
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400655 // set accessibility text to "Not installed"
656 mHotseatLabels[i] = getText(R.string.activity_not_found);
657 } else {
658 boolean found = false;
659 for (ResolveInfo ri : allMatches) {
660 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
661 && bestMatch.activityInfo.applicationInfo.packageName
662 .equals(ri.activityInfo.applicationInfo.packageName)) {
663 found = true;
664 break;
665 }
666 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700667
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400668 if (!found) {
669 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
670 // the bestMatch is probably the ResolveActivity, meaning the
671 // user has not yet selected a default
672 // so: we'll keep the original intent for now
673 mHotseats[i] = intent;
674
675 // set the accessibility text to "Select shortcut"
676 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
677 } else {
678 // we have an app!
679 // now reconstruct the intent to launch it through the front
680 // door
681 ComponentName com = new ComponentName(
682 bestMatch.activityInfo.applicationInfo.packageName,
683 bestMatch.activityInfo.name);
684 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
685
686 // load the app label for accessibility
687 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
688 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400689 }
690
691 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700692 Log.d(TAG, "loadHotseats: hotseat " + i
693 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400694 + ((mHotseats[i] == null)
695 ? "null"
696 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400697 + "] label=[" + mHotseatLabels[i]
698 + "]"
699 );
700 }
701 }
702 }
703
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 @Override
705 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700706 mWaitingForResult = false;
707
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 // The pattern used here is that a user PICKs a specific application,
709 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
712 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700713
Michael Jurka0280c3b2010-09-17 15:00:07 -0700714 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 switch (requestCode) {
716 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700717 completeAddApplication(
718 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 break;
720 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800721 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 break;
723 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700724 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 break;
726 case REQUEST_PICK_LIVE_FOLDER:
727 addLiveFolder(data);
728 break;
729 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700730 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700732 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700733 addAppWidgetFromPick(data);
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_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700736 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700737 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700739 case REQUEST_PICK_WALLPAPER:
740 // We just wanted the activity result here so we can clear mWaitingForResult
741 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 }
Romain Guy18042c82009-11-06 11:44:55 -0800743 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
744 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
745 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700746 // Clean up the appWidgetId if we canceled
747 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
748 if (appWidgetId != -1) {
749 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 }
751 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753
754 @Override
755 protected void onResume() {
756 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800757 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700758 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759 mWorkspaceLoading = true;
760 mModel.startLoader(this, true);
761 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700762 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800764 if (mWaitingForResume != null) {
765 mWaitingForResume.setStayPressed(false);
766 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700767 // When we resume Launcher, a different Activity might be responsible for the app
768 // market intent, so refresh the icon
769 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 }
771
772 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700773 protected void onPause() {
774 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700775 // Some launcher layouts don't have a previous and next view
776 if (mPreviousView != null) {
777 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700778 }
779 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700780 dismissPreview(mNextView);
781 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700782 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800783 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700784 }
Romain Guycbb89e42009-06-08 15:52:54 -0700785
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700786 @Override
787 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400788 // Flag the loader to stop early before switching
789 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800790 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800791 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700792 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700793
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800794 // We can't hide the IME if it was forced open. So don't bother
795 /*
796 @Override
797 public void onWindowFocusChanged(boolean hasFocus) {
798 super.onWindowFocusChanged(hasFocus);
799
800 if (hasFocus) {
801 final InputMethodManager inputManager = (InputMethodManager)
802 getSystemService(Context.INPUT_METHOD_SERVICE);
803 WindowManager.LayoutParams lp = getWindow().getAttributes();
804 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
805 android.os.Handler()) {
806 protected void onReceiveResult(int resultCode, Bundle resultData) {
807 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
808 }
809 });
810 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
811 }
812 }
813 */
814
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 private boolean acceptFilter() {
816 final InputMethodManager inputManager = (InputMethodManager)
817 getSystemService(Context.INPUT_METHOD_SERVICE);
818 return !inputManager.isFullscreenMode();
819 }
820
821 @Override
822 public boolean onKeyDown(int keyCode, KeyEvent event) {
823 boolean handled = super.onKeyDown(keyCode, event);
824 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
825 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
826 keyCode, event);
827 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700828 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700829 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700830 // showSearchDialog()
831 // If there are multiple keystrokes before the search dialog takes focus,
832 // onSearchRequested() will be called for every keystroke,
833 // but it is idempotent, so it's fine.
834 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
836 }
837
Joe Onorato8a9625e2010-01-28 15:55:35 -0800838 // Eat the long press event so the keyboard doesn't come up.
839 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
840 return true;
841 }
842
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 return handled;
844 }
845
Karl Rosaen138a0412009-04-23 19:00:21 -0700846 private String getTypedText() {
847 return mDefaultKeySsb.toString();
848 }
849
850 private void clearTypedText() {
851 mDefaultKeySsb.clear();
852 mDefaultKeySsb.clearSpans();
853 Selection.setSelection(mDefaultKeySsb, 0);
854 }
855
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700857 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
858 * State
859 */
860 private static State intToState(int stateOrdinal) {
861 State state = State.WORKSPACE;
862 final State[] stateValues = State.values();
863 for (int i = 0; i < stateValues.length; i++) {
864 if (stateValues[i].ordinal() == stateOrdinal) {
865 state = stateValues[i];
866 break;
867 }
868 }
869 return state;
870 }
871
872 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 * Restores the previous state, if it exists.
874 *
875 * @param savedState The previous state.
876 */
877 private void restoreState(Bundle savedState) {
878 if (savedState == null) {
879 return;
880 }
881
Michael Jurka883f55b2010-10-21 15:47:14 -0700882 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
883
884 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800885 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700886 } else if (state == State.CUSTOMIZE) {
887 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800888 }
889
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
891 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700892 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 }
894
895 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700896
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700898 mAddScreen = addScreen;
899 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
900 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 mRestoring = true;
902 }
903
904 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
905 if (renameFolder) {
906 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700907 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 mRestoring = true;
909 }
Winson Chunga12a2502010-12-20 14:41:35 -0800910
911 // Restore the current AllApps drawer tab
912 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
913 String curTab = savedState.getString("allapps_currentTab");
914 if (curTab != null) {
915 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
916 tabhost.setCurrentTabByTag(curTab);
917 }
918 int curPage = savedState.getInt("allapps_currentPage", -1);
919 if (curPage > -1) {
920 mAllAppsPagedView.setRestorePage(curPage);
921 }
922 }
923
924 // Restore the current customization drawer tab
925 if (mHomeCustomizationDrawer != null) {
926 String curTab = savedState.getString("customize_currentTab");
927 if (curTab != null) {
928 // We set this directly so that there is no delay before the tab is set
929 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
930 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
931 }
932
933 // Note: currently we do not restore the page for the customization tray because unlike
934 // AllApps, the page content can change drastically
935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 }
937
938 /**
939 * Finds all the views we need and configure them properly.
940 */
941 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700942 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400943
Romain Guyb1b69f52009-08-10 15:10:15 -0700944 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400945 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946
Joe Onorato7c312c12009-08-13 21:36:53 -0700947 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700948 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700949 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800950 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700951 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700952 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700953
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700954 if (LauncherApplication.isScreenXLarge()) {
955 // They need to be INVISIBLE initially so that they will be measured in the layout.
956 // Otherwise the animations are messed up when we show them for the first time.
957 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
958 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
959 }
960
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700962
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500964 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700965
Joe Onorato7c312c12009-08-13 21:36:53 -0700966 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
967 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700969 View handleView = findViewById(R.id.all_apps_button);
970 if (handleView != null && handleView instanceof HandleView) {
971 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700972 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700973 mHandleView.setLauncher(this);
974 mHandleView.setOnClickListener(this);
975 mHandleView.setOnLongClickListener(this);
976 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800977
Winson Chung80baf5a2010-08-09 16:03:15 -0700978 if (mCustomizePagedView != null) {
979 mCustomizePagedView.setLauncher(this);
980 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700981 } else {
982 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
983 hotseatLeft.setContentDescription(mHotseatLabels[0]);
984 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
985 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
986 hotseatRight.setContentDescription(mHotseatLabels[1]);
987 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400988
Michael Jurkaaf442092010-06-10 17:01:57 -0700989 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
990 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800991
Michael Jurkaaf442092010-06-10 17:01:57 -0700992 Drawable previous = mPreviousView.getDrawable();
993 Drawable next = mNextView.getDrawable();
994 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995
Michael Jurkaaf442092010-06-10 17:01:57 -0700996 mPreviousView.setHapticFeedbackEnabled(false);
997 mPreviousView.setOnLongClickListener(this);
998 mNextView.setHapticFeedbackEnabled(false);
999 mNextView.setOnLongClickListener(this);
1000 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001003 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -08001005 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006
1007 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001008 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -08001009
1010 final View allAppsButton = findViewById(R.id.all_apps_button);
1011 final View divider = findViewById(R.id.divider);
1012 final View configureButton = findViewById(R.id.configure_button);
1013
Michael Jurka54f7ac32010-08-02 13:56:46 -07001014 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001015 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -07001016 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001017 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001018 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001019 dragController.addDragListener(deleteZone);
1020
Adam Cohencdc30d52010-12-01 15:09:47 -08001021 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1022 if (allAppsDeleteZone != null) {
1023 allAppsDeleteZone.setLauncher(this);
1024 allAppsDeleteZone.setDragController(dragController);
1025 allAppsDeleteZone.setDragAndDropEnabled(false);
1026 dragController.addDragListener(allAppsDeleteZone);
1027 dragController.addDropTarget(allAppsDeleteZone);
1028 }
1029
1030 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1031 findViewById(R.id.all_apps_info_target);
1032 if (allAppsInfoTarget != null) {
1033 allAppsInfoTarget.setLauncher(this);
1034 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001035 allAppsInfoTarget.setDragAndDropEnabled(false);
1036 View marketButton = findViewById(R.id.market_button);
1037 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001038 allAppsInfoTarget.setOverlappingView(marketButton);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08001039 marketButton.setOnClickListener(new OnClickListener() {
1040 public void onClick(View v) {
1041 onClickAppMarketButton(v);
1042 }
1043 });
Adam Cohencdc30d52010-12-01 15:09:47 -08001044 }
1045 }
1046
Joe Onorato00acb122009-08-04 16:04:30 -04001047 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001048 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001049 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001050
Joe Onorato00acb122009-08-04 16:04:30 -04001051 // The order here is bottom to top.
1052 dragController.addDropTarget(workspace);
1053 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001054 if (allAppsInfoTarget != null) {
1055 dragController.addDropTarget(allAppsInfoTarget);
1056 }
1057 if (allAppsDeleteZone != null) {
1058 dragController.addDropTarget(allAppsDeleteZone);
1059 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001060 mButtonCluster = findViewById(R.id.all_apps_button_cluster);
1061
1062 mAllAppsButton = findViewById(R.id.all_apps_button);
1063 mDivider = findViewById(R.id.divider);
1064 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001065
1066 // We had previously set these click handlers in XML, but the first time we launched
1067 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1068 // found the right handler. Setting the handlers directly here eliminates that cost.
1069 if (mConfigureButton != null) {
1070 mConfigureButton.setOnClickListener(new OnClickListener() {
1071 public void onClick(View v) {
1072 onClickConfigureButton(v);
1073 }
1074 });
1075 }
1076 if (mDivider != null) {
1077 mDivider.setOnClickListener(new OnClickListener() {
1078 public void onClick(View v) {
1079 onClickAllAppsButton(v);
1080 }
1081 });
1082 }
1083 if (mAllAppsButton != null) {
1084 mAllAppsButton.setOnClickListener(new OnClickListener() {
1085 public void onClick(View v) {
1086 onClickAllAppsButton(v);
1087 }
1088 });
1089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 }
1091
Romain Guy8a73c512009-11-09 19:19:59 -08001092 @SuppressWarnings({"UnusedDeclaration"})
1093 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001094 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001095 mWorkspace.scrollLeft();
1096 }
Romain Guy8a73c512009-11-09 19:19:59 -08001097 }
1098
1099 @SuppressWarnings({"UnusedDeclaration"})
1100 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001101 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001102 mWorkspace.scrollRight();
1103 }
Romain Guy8a73c512009-11-09 19:19:59 -08001104 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001105
1106 @SuppressWarnings({"UnusedDeclaration"})
1107 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001108 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001109
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001110 int index = -1;
1111 if (v.getId() == R.id.hotseat_left) {
1112 index = 0;
1113 } else if (v.getId() == R.id.hotseat_right) {
1114 index = 1;
1115 }
1116
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001117 // reload these every tap; you never know when they might change
1118 loadHotseats();
1119 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001120 startActivitySafely(
1121 mHotseats[index],
1122 "hotseat"
1123 );
1124 }
1125 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001126
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 /**
1128 * Creates a view representing a shortcut.
1129 *
1130 * @param info The data structure describing the shortcut.
1131 *
1132 * @return A View inflated from R.layout.application.
1133 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001134 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001136 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 }
1138
1139 /**
1140 * Creates a view representing a shortcut inflated from the specified resource.
1141 *
1142 * @param layoutResId The id of the XML layout used to create the shortcut.
1143 * @param parent The group the shortcut belongs to.
1144 * @param info The data structure describing the shortcut.
1145 *
1146 * @return A View inflated from layoutResId.
1147 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001148 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001149 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1150 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 return favorite;
1153 }
1154
1155 /**
1156 * Add an application shortcut to the workspace.
1157 *
1158 * @param data The intent describing the application.
1159 * @param cellInfo The position on screen where to create the shortcut.
1160 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001161 void completeAddApplication(Context context, Intent data, int screen,
1162 int intersectCellX, int intersectCellY) {
1163 final int[] cellXY = mTmpAddItemCellCoordinates;
1164 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1165
1166 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1167 showOutOfSpaceMessage();
1168 return;
1169 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170
Joe Onorato0589f0f2010-02-08 13:44:00 -08001171 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1172 data, context);
1173
Romain Guy73b979d2009-06-09 12:57:21 -07001174 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001175 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001177 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001178 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1179 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001180 } else {
1181 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 }
1183 }
Romain Guycbb89e42009-06-08 15:52:54 -07001184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 /**
1186 * Add a shortcut to the workspace.
1187 *
1188 * @param data The intent describing the shortcut.
1189 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001191 private void completeAddShortcut(Intent data, int screen,
1192 int intersectCellX, int intersectCellY) {
1193 final int[] cellXY = mTmpAddItemCellCoordinates;
1194 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001195
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001196 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001197 boolean foundCellSpan = false;
1198 if (touchXY != null) {
1199 // when dragging and dropping, just find the closest free spot
1200 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1201 int[] result = screenLayout.findNearestVacantArea(
1202 touchXY[0], touchXY[1], 1, 1, cellXY);
1203 foundCellSpan = (result != null);
1204 } else {
1205 foundCellSpan = layout.findCellForSpanThatIntersects(
1206 cellXY, 1, 1, intersectCellX, intersectCellY);
1207 }
1208
1209 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001210 showOutOfSpaceMessage();
1211 return;
1212 }
1213
1214 final ShortcutInfo info = mModel.addShortcut(
1215 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216
1217 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001219 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 }
1221 }
1222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001224 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001226 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001227 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 */
Winson Chungf7640c82011-02-28 13:47:29 -08001229 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001230 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001231
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001232 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001233 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1234 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001237 // We have saved the position to which the widget was dragged-- this really only matters
1238 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001239 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001240
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001241 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001242 boolean foundCellSpan = false;
1243 if (touchXY != null) {
1244 // when dragging and dropping, just find the closest free spot
1245 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1246 int[] result = screenLayout.findNearestVacantArea(
1247 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001248 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001249 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001250 // if we long pressed on an empty cell to bring up a menu,
1251 // make sure we intersect the empty cell
1252 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1253 // findCellForSpanThatIntersects will just ignore them
1254 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1255 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001256 }
1257
Michael Jurka0280c3b2010-09-17 15:00:07 -07001258 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001259 if (appWidgetId != -1) {
1260 // Deleting an app widget ID is a void call but writes to disk before returning
1261 // to the caller...
1262 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1263 new Thread("deleteAppWidgetId") {
1264 public void run() {
1265 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1266 }
1267 }.start();
1268 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001269 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001270 return;
1271 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001273 // Build Launcher-specific widget info and save to database
1274 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001275 launcherInfo.spanX = spanXY[0];
1276 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001277
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 LauncherModel.addItemToDatabase(this, launcherInfo,
1279 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001280 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281
1282 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001283 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001284
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001286 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001287
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001288 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001290
Michael Jurka0280c3b2010-09-17 15:00:07 -07001291 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001292 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001293
1294 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 }
1296 }
Romain Guycbb89e42009-06-08 15:52:54 -07001297
Adam Cohended9f8d2010-11-03 13:25:16 -07001298 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1299 @Override
1300 public void onReceive(Context context, Intent intent) {
1301 final String action = intent.getAction();
1302 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1303 mUserPresent = false;
1304 updateRunning();
1305 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1306 mUserPresent = true;
1307 updateRunning();
1308 }
1309 }
1310 };
1311
1312 @Override
1313 public void onAttachedToWindow() {
1314 super.onAttachedToWindow();
1315
1316 // Listen for broadcasts related to user-presence
1317 final IntentFilter filter = new IntentFilter();
1318 filter.addAction(Intent.ACTION_SCREEN_OFF);
1319 filter.addAction(Intent.ACTION_USER_PRESENT);
1320 registerReceiver(mReceiver, filter);
1321
Adam Cohend113e0c2010-11-11 10:48:05 -08001322 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001323 mVisible = true;
1324 }
1325
1326 @Override
1327 public void onDetachedFromWindow() {
1328 super.onDetachedFromWindow();
1329 mVisible = false;
1330
Adam Cohend113e0c2010-11-11 10:48:05 -08001331 if (mAttached) {
1332 unregisterReceiver(mReceiver);
1333 mAttached = false;
1334 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001335 updateRunning();
1336 }
1337
1338 public void onWindowVisibilityChanged(int visibility) {
1339 mVisible = visibility == View.VISIBLE;
1340 updateRunning();
1341 }
1342
1343 private void sendAdvanceMessage(long delay) {
1344 mHandler.removeMessages(ADVANCE_MSG);
1345 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1346 mHandler.sendMessageDelayed(msg, delay);
1347 mAutoAdvanceSentTime = System.currentTimeMillis();
1348 }
1349
1350 private void updateRunning() {
1351 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1352 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1353 mAutoAdvanceRunning = autoAdvanceRunning;
1354 if (autoAdvanceRunning) {
1355 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1356 sendAdvanceMessage(delay);
1357 } else {
1358 if (!mWidgetsToAdvance.isEmpty()) {
1359 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1360 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1361 }
1362 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001363 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001364 }
1365 }
1366 }
1367
1368 private final Handler mHandler = new Handler() {
1369 @Override
1370 public void handleMessage(Message msg) {
1371 if (msg.what == ADVANCE_MSG) {
1372 int i = 0;
1373 for (View key: mWidgetsToAdvance.keySet()) {
1374 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1375 final int delay = mAdvanceStagger * i;
1376 if (v instanceof Advanceable) {
1377 postDelayed(new Runnable() {
1378 public void run() {
1379 ((Advanceable) v).advance();
1380 }
1381 }, delay);
1382 }
1383 i++;
1384 }
1385 sendAdvanceMessage(mAdvanceInterval);
1386 }
1387 }
1388 };
1389
1390 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1391 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1392 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1393 if (v instanceof Advanceable) {
1394 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001395 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001396 updateRunning();
1397 }
1398 }
1399
1400 void removeWidgetToAutoAdvance(View hostView) {
1401 if (mWidgetsToAdvance.containsKey(hostView)) {
1402 mWidgetsToAdvance.remove(hostView);
1403 updateRunning();
1404 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001405 }
1406
Joe Onorato9c1289c2009-08-17 11:03:03 -04001407 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1408 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001409 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001410 launcherInfo.hostView = null;
1411 }
1412
Adam Cohended9f8d2010-11-03 13:25:16 -07001413 void showOutOfSpaceMessage() {
1414 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1415 }
1416
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001417 public LauncherAppWidgetHost getAppWidgetHost() {
1418 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 }
Romain Guycbb89e42009-06-08 15:52:54 -07001420
Winson Chunga9abd0e2010-10-27 17:18:37 -07001421 public LauncherModel getModel() {
1422 return mModel;
1423 }
1424
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001425 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001426 getWindow().closeAllPanels();
1427
1428 try {
1429 dismissDialog(DIALOG_CREATE_SHORTCUT);
1430 // Unlock the workspace if the dialog was showing
1431 } catch (Exception e) {
1432 // An exception is thrown if the dialog is not visible, which is fine
1433 }
1434
1435 try {
1436 dismissDialog(DIALOG_RENAME_FOLDER);
1437 // Unlock the workspace if the dialog was showing
1438 } catch (Exception e) {
1439 // An exception is thrown if the dialog is not visible, which is fine
1440 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001441
1442 // Whatever we were doing is hereby canceled.
1443 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001444 }
1445
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 @Override
1447 protected void onNewIntent(Intent intent) {
1448 super.onNewIntent(intent);
1449
1450 // Close the menu
1451 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001452 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001453 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001454
Joe Onorato14f122b2009-11-19 14:06:36 -08001455 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1456 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001457
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001458 // In all these cases, only animate if we're already on home
1459
Michael Jurka4cb37242010-08-09 21:05:32 -07001460 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001461 mWorkspace.unshrink(alreadyOnHome);
1462 }
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001463
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001464 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001465 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001466 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001467 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001468
Joe Onorato3a8820b2009-11-10 15:06:42 -08001469 final View v = getWindow().peekDecorView();
1470 if (v != null && v.getWindowToken() != null) {
1471 InputMethodManager imm = (InputMethodManager)getSystemService(
1472 INPUT_METHOD_SERVICE);
1473 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 }
1475 }
1476 }
1477
1478 @Override
1479 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1480 // Do not call super here
1481 mSavedInstanceState = savedInstanceState;
1482 }
1483
1484 @Override
1485 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001486 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487
1488 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1489 if (folders.size() > 0) {
1490 final int count = folders.size();
1491 long[] ids = new long[count];
1492 for (int i = 0; i < count; i++) {
1493 final FolderInfo info = folders.get(i).getInfo();
1494 ids[i] = info.id;
1495 }
1496 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1497 } else {
1498 super.onSaveInstanceState(outState);
1499 }
1500
Michael Jurka883f55b2010-10-21 15:47:14 -07001501 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502
Michael Jurka0280c3b2010-09-17 15:00:07 -07001503 if (mAddScreen > -1 && mWaitingForResult) {
1504 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1505 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1506 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
1508
1509 if (mFolderInfo != null && mWaitingForResult) {
1510 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1511 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1512 }
Michael Jurka946ad472010-07-09 18:05:18 -07001513
Winson Chung43b119d2010-12-06 16:45:05 -08001514 // Save the current AllApps drawer tab
1515 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1516 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1517 String currentTabTag = tabhost.getCurrentTabTag();
1518 if (currentTabTag != null) {
1519 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001520 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001521 }
1522 }
1523
1524 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001525 if (mHomeCustomizationDrawer != null) {
1526 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1527 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001528 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001529 }
1530 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
1532
1533 @Override
1534 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001536
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001538 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001540 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001542 mAppWidgetHost = null;
1543
1544 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545
1546 TextKeyListener.getInstance().release();
1547
Joe Onorato9c1289c2009-08-17 11:03:03 -04001548 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549
Joe Onorato9c1289c2009-08-17 11:03:03 -04001550 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001551
1552 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001553
Patrick Dubroyab962b72010-07-26 12:10:10 -07001554 // Some launcher layouts don't have a previous and next view
1555 if (mPreviousView != null) {
1556 dismissPreview(mPreviousView);
1557 }
1558 if (mNextView != null) {
1559 dismissPreview(mNextView);
1560 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001561
1562 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001563
1564 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1565 mWorkspace.removeAllViews();
1566 mWorkspace = null;
1567 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001568
1569 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
1571
1572 @Override
1573 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001574 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 super.startActivityForResult(intent, requestCode);
1576 }
1577
1578 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001579 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001581
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001582 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001583
Karl Rosaen138a0412009-04-23 19:00:21 -07001584 if (initialQuery == null) {
1585 // Use any text typed in the launcher as the initial query
1586 initialQuery = getTypedText();
1587 clearTypedText();
1588 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 if (appSearchData == null) {
1590 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001591 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 }
Romain Guycbb89e42009-06-08 15:52:54 -07001593
Karl Rosaen138a0412009-04-23 19:00:21 -07001594 final SearchManager searchManager =
1595 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001596 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001597 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 }
1599
1600 @Override
1601 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001602 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001603 return false;
1604 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605
1606 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001607
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1609 .setIcon(android.R.drawable.ic_menu_add)
1610 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001611 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1612 .setIcon(android.R.drawable.ic_menu_manage)
1613 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001614 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 .setIcon(android.R.drawable.ic_menu_gallery)
1616 .setAlphabeticShortcut('W');
1617 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1618 .setIcon(android.R.drawable.ic_search_category_default)
1619 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1620 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1621 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1622 .setAlphabeticShortcut('N');
1623
1624 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001625 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1626 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627
1628 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1629 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1630 .setIntent(settings);
1631
1632 return true;
1633 }
1634
1635 @Override
1636 public boolean onPrepareOptionsMenu(Menu menu) {
1637 super.onPrepareOptionsMenu(menu);
1638
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001639 // If all apps is animating, don't show the menu, because we don't know
1640 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001641 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001642 return false;
1643 }
1644
1645 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001646 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001647 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1648 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1649
1650 // Disable add if the workspace is full.
1651 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001652 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1653 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001654 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655
1656 return true;
1657 }
1658
1659 @Override
1660 public boolean onOptionsItemSelected(MenuItem item) {
1661 switch (item.getItemId()) {
1662 case MENU_ADD:
1663 addItems();
1664 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001665 case MENU_MANAGE_APPS:
1666 manageApps();
1667 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 case MENU_WALLPAPER_SETTINGS:
1669 startWallpaper();
1670 return true;
1671 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001672 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 return true;
1674 case MENU_NOTIFICATIONS:
1675 showNotifications();
1676 return true;
1677 }
1678
1679 return super.onOptionsItemSelected(item);
1680 }
Romain Guycbb89e42009-06-08 15:52:54 -07001681
Karl Rosaen138a0412009-04-23 19:00:21 -07001682 /**
1683 * Indicates that we want global search for this activity by setting the globalSearch
1684 * argument for {@link #startSearch} to true.
1685 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001687 @Override
1688 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001689 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001690 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001691 }
1692
Joe Onorato9c1289c2009-08-17 11:03:03 -04001693 public boolean isWorkspaceLocked() {
1694 return mWorkspaceLoading || mWaitingForResult;
1695 }
1696
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001697 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1698 private boolean isPreviewVisible() {
1699 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1700 (mNextView != null && mNextView.getTag() != null);
1701 }
1702
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001704 if (LauncherApplication.isScreenXLarge()) {
1705 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001706 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001707 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001708 }
1709 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001710 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001711 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 }
1714
Michael Jurka0280c3b2010-09-17 15:00:07 -07001715 private void resetAddInfo() {
1716 mAddScreen = -1;
1717 mAddIntersectCellX = -1;
1718 mAddIntersectCellY = -1;
1719 mAddDropPosition = null;
1720 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001721
Winson Chung55cef262010-10-28 14:14:18 -07001722 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001723 resetAddInfo();
1724 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001725 mAddDropPosition = position;
1726
Michael Jurkaaf442092010-06-10 17:01:57 -07001727 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001728 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1729 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001730 }
1731
Winson Chung7ad01412010-09-27 11:33:03 -07001732 private void manageApps() {
1733 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1734 }
1735
Michael Jurkaaf442092010-06-10 17:01:57 -07001736 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001737 // TODO: catch bad widget exception when sent
1738 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001739 // TODO: Is this log message meaningful?
1740 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001741 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001742 }
1743
Winson Chung55cef262010-10-28 14:14:18 -07001744 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001745 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746
Bjorn Bringert7984c942009-12-09 15:38:25 +00001747 if (appWidget.configure != null) {
1748 // Launch over to configure widget, if needed
1749 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1750 intent.setComponent(appWidget.configure);
1751 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001752 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001753 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1754 intent.putExtra(
1755 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1756 info.mimeType);
1757
1758 final String mimeType = info.mimeType;
1759 final ClipData clipData = (ClipData) info.configurationData;
1760 final ClipDescription clipDesc = clipData.getDescription();
1761 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1762 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001763 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001764 final CharSequence stringData = item.getText();
1765 final Uri uriData = item.getUri();
1766 final Intent intentData = item.getIntent();
1767 final String key =
1768 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1769 if (uriData != null) {
1770 intent.putExtra(key, uriData);
1771 } else if (intentData != null) {
1772 intent.putExtra(key, intentData);
1773 } else if (stringData != null) {
1774 intent.putExtra(key, stringData);
1775 }
1776 break;
1777 }
1778 }
1779 }
Winson Chung55cef262010-10-28 14:14:18 -07001780 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001781
Romain Guy8e633c52010-03-04 12:51:36 -08001782 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001784 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001785 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 }
1787 }
Romain Guycbb89e42009-06-08 15:52:54 -07001788
Michael Jurka0280c3b2010-09-17 15:00:07 -07001789 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1790 resetAddInfo();
1791 mAddScreen = screen;
1792 mAddDropPosition = position;
1793
1794 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1795 createShortcutIntent.setComponent(componentName);
1796 processShortcut(createShortcutIntent);
1797 }
1798
Joe Onoratodeb98af2010-02-19 14:59:39 -08001799 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001800 // Handle case where user selected "Applications"
1801 String applicationName = getResources().getString(R.string.group_applications);
1802 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001803
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001804 if (applicationName != null && applicationName.equals(shortcutName)) {
1805 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1806 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001807
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001808 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1809 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001810 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001811 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001812 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001813 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001814 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 }
1816
Winson Chung24ab2f12010-09-16 14:10:47 -07001817 void processWallpaper(Intent intent) {
1818 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1819 }
1820
Michael Jurka0280c3b2010-09-17 15:00:07 -07001821 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1822 resetAddInfo();
1823 mAddScreen = screen;
1824 mAddDropPosition = position;
1825
1826 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1827 createFolderIntent.setComponent(componentName);
1828
1829 addLiveFolder(createFolderIntent);
1830 }
1831
1832 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001833 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001834 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001835 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001836
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001837 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001838 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001839 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001840 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001841 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 }
1843
Michael Jurka0280c3b2010-09-17 15:00:07 -07001844 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 UserFolderInfo folderInfo = new UserFolderInfo();
1846 folderInfo.title = getText(R.string.folder_name);
1847
Michael Jurka0280c3b2010-09-17 15:00:07 -07001848 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1849 final int[] cellXY = mTmpAddItemCellCoordinates;
1850 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1851 showOutOfSpaceMessage();
1852 return;
1853 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854
1855 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001856 LauncherModel.addItemToDatabase(this, folderInfo,
1857 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001858 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001859 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860
1861 // Create the view
1862 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001863 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1864 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001865 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 }
Romain Guycbb89e42009-06-08 15:52:54 -07001867
Joe Onorato9c1289c2009-08-17 11:03:03 -04001868 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001869 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001870 }
1871
Michael Jurka28750fb2010-09-24 17:43:49 -07001872 private void completeAddLiveFolder(
1873 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001874 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1875 final int[] cellXY = mTmpAddItemCellCoordinates;
1876 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1877 showOutOfSpaceMessage();
1878 return;
1879 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880
Michael Jurka0280c3b2010-09-17 15:00:07 -07001881 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882
1883 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001885 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001886 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 }
1888 }
1889
1890 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001891 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892
1893 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1894 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1895
1896 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 Intent.ShortcutIconResource iconResource = null;
1898
1899 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1900 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1901 try {
1902 iconResource = (Intent.ShortcutIconResource) extra;
1903 final PackageManager packageManager = context.getPackageManager();
1904 Resources resources = packageManager.getResourcesForApplication(
1905 iconResource.packageName);
1906 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1907 icon = resources.getDrawable(id);
1908 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001909 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 }
1911 }
1912
1913 if (icon == null) {
1914 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1915 }
1916
1917 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001918 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 info.title = name;
1920 info.iconResource = iconResource;
1921 info.uri = data.getData();
1922 info.baseIntent = baseIntent;
1923 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1924 LiveFolders.DISPLAY_MODE_GRID);
1925
1926 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001927 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001928 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929
1930 return info;
1931 }
1932
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 private void showNotifications() {
1934 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1935 if (statusBar != null) {
1936 statusBar.expand();
1937 }
1938 }
1939
1940 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001941 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001943 Intent chooser = Intent.createChooser(pickWallpaper,
1944 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001945 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1946 // Removed in Eclair MR1
1947// WallpaperManager wm = (WallpaperManager)
1948// getSystemService(Context.WALLPAPER_SERVICE);
1949// WallpaperInfo wi = wm.getWallpaperInfo();
1950// if (wi != null && wi.getSettingsActivity() != null) {
1951// LabeledIntent li = new LabeledIntent(getPackageName(),
1952// R.string.configure_wallpaper, 0);
1953// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1954// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1955// }
Mike Clerona0618e42009-10-22 13:55:21 -07001956 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 }
1958
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001959 /**
1960 * Registers various content observers. The current implementation registers
1961 * only a favorites observer to keep track of the favorites applications.
1962 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001963 private void registerContentObservers() {
1964 ContentResolver resolver = getContentResolver();
1965 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1966 true, mWidgetObserver);
1967 }
1968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 @Override
1970 public boolean dispatchKeyEvent(KeyEvent event) {
1971 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1972 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001974 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001975 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001976 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001977 dumpState();
1978 return true;
1979 }
1980 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001981 }
1982 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1983 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001984 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 return true;
1986 }
1987 }
1988
1989 return super.dispatchKeyEvent(event);
1990 }
1991
Joe Onorato88ec0992009-11-19 13:16:06 -08001992 @Override
1993 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001994 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1995 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001996 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08001997 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001998 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001999 dismissPreview(mPreviousView);
2000 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002001 } else {
2002 // Back button is a no-op here, but give at least some feedback for the button press
2003 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002004 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002005 }
2006
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 private void closeFolder() {
2008 Folder folder = mWorkspace.getOpenFolder();
2009 if (folder != null) {
2010 closeFolder(folder);
2011 }
2012 }
2013
2014 void closeFolder(Folder folder) {
2015 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08002016 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07002018 CellLayout cl = (CellLayout) parent;
2019 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05002020 if (folder instanceof DropTarget) {
2021 // Live folders aren't DropTargets.
2022 mDragController.removeDropTarget((DropTarget)folder);
2023 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 }
2025 folder.onClose();
2026 }
2027
2028 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002029 * Re-listen when widgets are reset.
2030 */
2031 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002032 if (mAppWidgetHost != null) {
2033 mAppWidgetHost.startListening();
2034 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002035 }
2036
2037 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002038 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2039 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002041 private void unbindDesktopItems() {
2042 for (ItemInfo item: mDesktopItems) {
2043 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002045 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002047
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 /**
2049 * Launches the intent referred by the clicked shortcut.
2050 *
2051 * @param v The view representing the clicked shortcut.
2052 */
2053 public void onClick(View v) {
2054 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002055 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002057 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002058 int[] pos = new int[2];
2059 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002060 intent.setSourceBounds(new Rect(pos[0], pos[1],
2061 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08002062 boolean success = startActivitySafely(intent, tag);
2063
2064 if (success && v instanceof BubbleTextView) {
2065 mWaitingForResume = (BubbleTextView) v;
2066 mWaitingForResume.setStayPressed(true);
2067 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 } else if (tag instanceof FolderInfo) {
2069 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002070 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002071 if (mState == State.ALL_APPS) {
2072 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002073 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002074 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002075 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 }
2077 }
2078
Michael Jurka0e260592010-06-30 17:07:39 -07002079 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002080 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002081 // clicking anywhere on the workspace causes the customization drawer to slide down
2082 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002083 return false;
2084 }
2085
Michael Jurkaaf442092010-06-10 17:01:57 -07002086 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002087 * Event handler for the search button
2088 *
2089 * @param v The view that was clicked.
2090 */
2091 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002092 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002093 // Use a custom animation for launching search
2094 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002095 }
2096
2097 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002098 * Event handler for the voice button
2099 *
2100 * @param v The view that was clicked.
2101 */
2102 public void onClickVoiceButton(View v) {
2103 startVoiceSearch();
2104 }
2105
2106 private void startVoiceSearch() {
2107 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2108 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2109 startActivity(intent);
2110 }
2111
2112 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002113 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002114 * enters home screen customization mode.
2115 *
2116 * @param v The view that was clicked.
2117 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002118 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002119 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002120 }
2121
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002122 /**
2123 * Event handler for the "grid" button that appears on the home screen, which
2124 * enters all apps mode.
2125 *
2126 * @param v The view that was clicked.
2127 */
2128 public void onClickAllAppsButton(View v) {
2129 showAllApps(true);
2130 }
2131
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002132 public void onClickAppMarketButton(View v) {
2133 if (mAppMarketIntent != null) {
2134 startActivitySafely(mAppMarketIntent, "app market");
2135 }
2136 }
2137
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002138 void startApplicationDetailsActivity(ComponentName componentName) {
2139 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002140 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2141 Uri.fromParts("package", packageName, null));
2142 startActivity(intent);
2143 }
2144
Patrick Dubroy5539af72010-09-07 15:22:01 -07002145 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2146 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2147 // System applications cannot be installed. For now, show a toast explaining that.
2148 // We may give them the option of disabling apps this way.
2149 int messageId = R.string.uninstall_system_app_text;
2150 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2151 } else {
2152 String packageName = appInfo.componentName.getPackageName();
2153 String className = appInfo.componentName.getClassName();
2154 Intent intent = new Intent(
2155 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2156 startActivity(intent);
2157 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002158 }
2159
Michael Jurkaddd62e92011-02-16 17:49:14 -08002160 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2162 try {
2163 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002164 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002165 } catch (ActivityNotFoundException e) {
2166 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002167 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002168 } catch (SecurityException e) {
2169 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002170 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002172 "or use the exported attribute for this activity. "
2173 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002175 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002177
Romain Guy8e633c52010-03-04 12:51:36 -08002178 void startActivityForResultSafely(Intent intent, int requestCode) {
2179 try {
2180 startActivityForResult(intent, requestCode);
2181 } catch (ActivityNotFoundException e) {
2182 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2183 } catch (SecurityException e) {
2184 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2185 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2186 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2187 "or use the exported attribute for this activity.", e);
2188 }
2189 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190
2191 private void handleFolderClick(FolderInfo folderInfo) {
2192 if (!folderInfo.opened) {
2193 // Close any open folder
2194 closeFolder();
2195 // Open the requested folder
2196 openFolder(folderInfo);
2197 } else {
2198 // Find the open folder...
2199 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2200 int folderScreen;
2201 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002202 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 // .. and close it
2204 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002205 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 // Close any folder open on the current screen
2207 closeFolder();
2208 // Pull the folder onto this screen
2209 openFolder(folderInfo);
2210 }
2211 }
2212 }
2213 }
2214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002216 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 * is animated relative to the specified View. If the View is null, no animation
2218 * is played.
2219 *
2220 * @param folderInfo The FolderInfo describing the folder to open.
2221 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002222 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 Folder openFolder;
2224
2225 if (folderInfo instanceof UserFolderInfo) {
2226 openFolder = UserFolder.fromXml(this);
2227 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002228 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229 } else {
2230 return;
2231 }
2232
Joe Onorato00acb122009-08-04 16:04:30 -04002233 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 openFolder.setLauncher(this);
2235
2236 openFolder.bind(folderInfo);
2237 folderInfo.opened = true;
2238
Winson Chungaafa03c2010-06-11 17:34:16 -07002239 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 openFolder.onOpen();
2242 }
2243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002245 if (mState != State.WORKSPACE) {
2246 return false;
2247 }
2248
Romain Guy1fbc1c82009-11-09 20:43:08 -08002249 switch (v.getId()) {
2250 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002251 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002252 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2253 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002254 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002255 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002256 return true;
2257 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002258 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002259 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2260 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002261 showPreviews(v);
2262 }
2263 return true;
2264 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002265 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002266 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2267 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2268 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002269 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002270 return true;
2271 }
2272
Joe Onorato9c1289c2009-08-17 11:03:03 -04002273 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002274 return false;
2275 }
2276
2277 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002278 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002279 }
2280
Michael Jurka0280c3b2010-09-17 15:00:07 -07002281 resetAddInfo();
2282 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002283 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002284 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002285 return true;
2286 }
2287
Michael Jurka0280c3b2010-09-17 15:00:07 -07002288 final View itemUnderLongClick = longClickCellInfo.cell;
2289
Winson Chung304dcde2011-01-07 11:17:23 -08002290 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002291 if (itemUnderLongClick == null) {
2292 // User long pressed on empty space
2293 mWorkspace.setAllowLongPress(false);
2294 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2295 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002296 if (LauncherApplication.isScreenXLarge()) {
2297 addItems();
2298 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002299 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002300 }
2301 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002302 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002304 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2305 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002306 mAddIntersectCellX = longClickCellInfo.cellX;
2307 mAddIntersectCellY = longClickCellInfo.cellY;
2308 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 }
2310 }
2311 }
2312 return true;
2313 }
2314
Romain Guye6b8e2f2009-11-10 11:56:55 -08002315 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002316 private void dismissPreview(final View v) {
2317 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002318 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002319 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2320 public void onDismiss() {
2321 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2322 int count = group.getChildCount();
2323 for (int i = 0; i < count; i++) {
2324 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2325 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002326 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2327 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2328
2329 v.setTag(R.id.workspace, null);
2330 v.setTag(R.id.icon, null);
2331 window.setOnDismissListener(null);
2332 }
2333 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002334 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002335 }
2336 v.setTag(null);
2337 }
2338
Romain Guyf8e6a802009-12-07 17:48:02 -08002339 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002340 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002341 }
2342
Romain Guya6abce82009-11-10 02:54:41 -08002343 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002344 final Resources resources = getResources();
2345 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002346
Romain Guya6abce82009-11-10 02:54:41 -08002347 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002348
Romain Guy9d31e9f2009-11-11 18:54:28 -08002349 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002350
Romain Guyf8e6a802009-12-07 17:48:02 -08002351 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002352 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002353 int extraW = (int) ((r.left + r.right) * max);
2354 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002355
2356 int aW = cell.getWidth() - extraW;
2357 float w = aW / max;
2358
2359 int width = cell.getWidth();
2360 int height = cell.getHeight();
2361 int x = cell.getLeftPadding();
2362 int y = cell.getTopPadding();
2363 width -= (x + cell.getRightPadding());
2364 height -= (y + cell.getBottomPadding());
2365
2366 float scale = w / width;
2367
2368 int count = end - start;
2369
2370 final float sWidth = width * scale;
2371 float sHeight = height * scale;
2372
Romain Guye6b8e2f2009-11-10 11:56:55 -08002373 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002374
Romain Guye6b8e2f2009-11-10 11:56:55 -08002375 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2376 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002377
2378 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002379 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002380 cell = (CellLayout) workspace.getChildAt(i);
2381
Romain Guyf8e6a802009-12-07 17:48:02 -08002382 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002383 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002384
2385 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002386 c.scale(scale, scale);
2387 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002388 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002389
Romain Guy9d31e9f2009-11-11 18:54:28 -08002390 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002391 image.setImageBitmap(bitmap);
2392 image.setTag(i);
2393 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002394 image.setOnFocusChangeListener(handler);
2395 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002396 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002397
2398 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002399 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2400
Winson Chungaafa03c2010-06-11 17:34:16 -07002401 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002402 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002403
2404 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002405 p.setContentView(preview);
2406 p.setWidth((int) (sWidth * count + extraW));
2407 p.setHeight((int) (sHeight + extraH));
2408 p.setAnimationStyle(R.style.AnimationPreview);
2409 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002410 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002411 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002412 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002413
Romain Guye6b8e2f2009-11-10 11:56:55 -08002414 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2415 public void onDismiss() {
2416 dismissPreview(anchor);
2417 }
2418 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002419
2420 anchor.setTag(p);
2421 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002422 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002423 }
2424
Romain Guy9d31e9f2009-11-11 18:54:28 -08002425 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002426 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002427
Romain Guye6b8e2f2009-11-10 11:56:55 -08002428 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002429 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002430 }
2431
2432 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002433 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002434 v.post(this);
2435 }
2436
2437 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002438 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002439 }
2440
2441 public void onFocusChange(View v, boolean hasFocus) {
2442 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002443 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002444 }
Romain Guya6abce82009-11-10 02:54:41 -08002445 }
2446 }
2447
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002448 Workspace getWorkspace() {
2449 return mWorkspace;
2450 }
2451
Michael Jurka3c38d562011-02-18 19:22:12 -08002452 TabHost getCustomizationDrawer() {
2453 return mHomeCustomizationDrawer;
2454 }
2455
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002456 @Override
2457 protected Dialog onCreateDialog(int id) {
2458 switch (id) {
2459 case DIALOG_CREATE_SHORTCUT:
2460 return new CreateShortcut().createDialog();
2461 case DIALOG_RENAME_FOLDER:
2462 return new RenameFolder().createDialog();
2463 }
2464
2465 return super.onCreateDialog(id);
2466 }
2467
2468 @Override
2469 protected void onPrepareDialog(int id, Dialog dialog) {
2470 switch (id) {
2471 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002472 break;
2473 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002474 if (mFolderInfo != null) {
2475 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2476 final CharSequence text = mFolderInfo.title;
2477 input.setText(text);
2478 input.setSelection(0, text.length());
2479 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 break;
2481 }
2482 }
2483
2484 void showRenameDialog(FolderInfo info) {
2485 mFolderInfo = info;
2486 mWaitingForResult = true;
2487 showDialog(DIALOG_RENAME_FOLDER);
2488 }
2489
Michael Jurka0280c3b2010-09-17 15:00:07 -07002490 private void showAddDialog(int intersectX, int intersectY) {
2491 resetAddInfo();
2492 mAddIntersectCellX = intersectX;
2493 mAddIntersectCellY = intersectY;
2494 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 mWaitingForResult = true;
2496 showDialog(DIALOG_CREATE_SHORTCUT);
2497 }
2498
Joe Onoratodeb98af2010-02-19 14:59:39 -08002499 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002500 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002501 Bundle bundle = new Bundle();
2502
2503 ArrayList<String> shortcutNames = new ArrayList<String>();
2504 shortcutNames.add(getString(R.string.group_applications));
2505 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2506
2507 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2508 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2509 R.drawable.ic_launcher_application));
2510 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2511
2512 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2513 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002514 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002515 pickIntent.putExtras(bundle);
2516
Joe Onoratodeb98af2010-02-19 14:59:39 -08002517 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002518 }
2519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 private class RenameFolder {
2521 private EditText mInput;
2522
2523 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002524 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2525 mInput = (EditText) layout.findViewById(R.id.folder_name);
2526
2527 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2528 builder.setIcon(0);
2529 builder.setTitle(getString(R.string.rename_folder_title));
2530 builder.setCancelable(true);
2531 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2532 public void onCancel(DialogInterface dialog) {
2533 cleanup();
2534 }
2535 });
2536 builder.setNegativeButton(getString(R.string.cancel_action),
2537 new Dialog.OnClickListener() {
2538 public void onClick(DialogInterface dialog, int which) {
2539 cleanup();
2540 }
2541 }
2542 );
2543 builder.setPositiveButton(getString(R.string.rename_action),
2544 new Dialog.OnClickListener() {
2545 public void onClick(DialogInterface dialog, int which) {
2546 changeFolderName();
2547 }
2548 }
2549 );
2550 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002551
2552 final AlertDialog dialog = builder.create();
2553 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2554 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002555 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002556 mInput.requestFocus();
2557 InputMethodManager inputManager = (InputMethodManager)
2558 getSystemService(Context.INPUT_METHOD_SERVICE);
2559 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002560 }
2561 });
2562
2563 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002564 }
2565
2566 private void changeFolderName() {
2567 final String name = mInput.getText().toString();
2568 if (!TextUtils.isEmpty(name)) {
2569 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002570 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002571 mFolderInfo.title = name;
2572 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2573
Joe Onorato9c1289c2009-08-17 11:03:03 -04002574 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002575 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002576 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 } else {
2578 final FolderIcon folderIcon = (FolderIcon)
2579 mWorkspace.getViewForTag(mFolderInfo);
2580 if (folderIcon != null) {
2581 folderIcon.setText(name);
2582 getWorkspace().requestLayout();
2583 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002584 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002585 mWorkspaceLoading = true;
2586 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 }
2588 }
2589 }
2590 cleanup();
2591 }
2592
2593 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002594 dismissDialog(DIALOG_RENAME_FOLDER);
2595 mWaitingForResult = false;
2596 mFolderInfo = null;
2597 }
2598 }
2599
Daniel Sandler843e8602010-06-07 14:59:01 -04002600 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2601 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002602 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002603 }
2604
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002605 // AllAppsView.Watcher
2606 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002607 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2608 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002609 mWorkspace.setVisibility(View.GONE);
2610 }
2611 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002612
Michael Jurkaafca9532011-02-16 14:50:35 -08002613 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002614 button.setVisibility(View.VISIBLE);
2615 button.setFocusable(true);
2616 button.setClickable(true);
2617 }
2618
2619 private void hideToolbarButton(View button) {
2620 button.setAlpha(0.0f);
2621 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2622 button.setVisibility(View.INVISIBLE);
Michael Jurkaafca9532011-02-16 14:50:35 -08002623 }
2624
2625 private void disableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002626 button.setFocusable(false);
2627 button.setClickable(false);
2628 }
2629
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002630 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002631 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002632 *
2633 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2634 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002635 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002636 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002637 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002638 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002639 final boolean showing = show;
2640 final boolean hiding = !show;
2641
2642 final int duration = show ?
2643 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2644 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2645
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002646 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002647 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002648 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002649 anim.addUpdateListener(new AnimatorUpdateListener() {
2650 public void onAnimationUpdate(ValueAnimator animation) {
2651 view.setAlpha((Float) animation.getAnimatedValue());
2652 }
2653 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002654 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002655 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002656 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002657 if (showing) showAndEnableToolbarButton(view);
2658 if (hiding) disableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002659 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002660 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002661 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002662 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002663 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002664 });
2665 seq.play(anim);
2666 } else {
2667 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002668 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002669 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002670 } else {
Michael Jurkaafca9532011-02-16 14:50:35 -08002671 disableToolbarButton(view);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002672 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002673 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002674 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002675 }
2676
2677 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002678 * Show/hide the appropriate toolbar buttons for newState.
2679 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002680 *
2681 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002682 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2683 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002684 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002685 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002686 switch (newState) {
2687 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002688 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2689 mDeleteZone.setOverlappingViews(
2690 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002691 mDeleteZone.setDragAndDropEnabled(true);
2692 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002693 break;
2694 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002695 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002696 mDeleteZone.setDragAndDropEnabled(false);
2697 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002698 break;
2699 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002700 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002701 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002702 break;
2703 }
2704 }
2705
2706 /**
2707 * Helper method for the cameraZoomIn/cameraZoomOut animations
2708 * @param view The view being animated
2709 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2710 * @param scaleFactor The scale factor used for the zoom
2711 */
2712 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2713 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002714
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002715 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002716 // Set pivotY so that at the starting zoom factor, the view is partially
2717 // visible. Modifying initialHeightFactor changes how much of the view is
2718 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002719 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002720 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002721 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002722 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002723 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002724 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002725 }
2726 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002727
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002728 /**
2729 * Zoom the camera out from the workspace to reveal 'toView'.
2730 * Assumes that the view to show is anchored at either the very top or very bottom
2731 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002732 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002733 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002734 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002735 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002736 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002737
2738 final int duration = toAllApps ?
2739 res.getInteger(R.integer.config_allAppsZoomInTime) :
2740 res.getInteger(R.integer.config_customizeZoomInTime);
2741
2742 final float scale = toAllApps ?
2743 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2744 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2745
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002746 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002747
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002748 setPivotsForZoom(toView, toState, scale);
2749
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002750 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002751 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002752 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002753 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002754 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002755
2756 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002757 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2758 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
2759 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2760 public void onAnimationUpdate(ValueAnimator animation) {
2761 final float b = (Float) animation.getAnimatedValue();
2762 final float a = 1f - b;
2763 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002764 toView.setFastScaleX(a * scale + b * 1f);
2765 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002766 }
2767 });
Adam Cohen61033d32010-11-15 18:29:44 -08002768
Adam Cohenf16e5712011-01-13 13:31:45 -08002769 if (toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002770 toView.setFastAlpha(0f);
Michael Jurka742574b2011-02-02 23:51:01 -08002771 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurkaea573482011-02-03 19:48:18 -08002772 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002773 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2774 public void onAnimationUpdate(ValueAnimator animation) {
2775 final float b = (Float) animation.getAnimatedValue();
2776 final float a = 1f - b;
2777 // don't need to invalidate because we do so above
2778 toView.setFastAlpha(a * 0f + b * 1f);
2779 }
2780 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002781 alphaAnim.start();
2782 }
2783
Michael Jurka2763be32011-02-24 11:19:57 -08002784 if (toView instanceof LauncherAnimatable) {
2785 ((LauncherAnimatable) toView).onLauncherAnimationStart();
Michael Jurkabfadaad2011-01-31 21:35:39 -08002786 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002787 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002788 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002789 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002790 // Prepare the position
2791 toView.setTranslationX(0.0f);
2792 toView.setTranslationY(0.0f);
2793 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002794 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002795 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002796 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002797 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002798 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002799 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002800 // If we don't set the final scale values here, if this animation is cancelled
2801 // it will have the wrong scale value and subsequent cameraPan animations will
2802 // not fix that
2803 toView.setScaleX(1.0f);
2804 toView.setScaleY(1.0f);
Michael Jurka2763be32011-02-24 11:19:57 -08002805 if (toView instanceof LauncherAnimatable) {
2806 ((LauncherAnimatable) toView).onLauncherAnimationEnd();
2807 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002808 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002809 });
2810
Chet Haaseb1254a62010-09-07 13:35:00 -07002811 AnimatorSet toolbarHideAnim = new AnimatorSet();
2812 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002813 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2814
2815 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002816 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002817
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002818 if (mStateAnimation != null) mStateAnimation.cancel();
2819 mStateAnimation = new AnimatorSet();
2820 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2821 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002822
2823 // Show the new toolbar buttons just as the main animation is ending
2824 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002825 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2826 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002827 } else {
2828 toView.setTranslationX(0.0f);
2829 toView.setTranslationY(0.0f);
2830 toView.setScaleX(1.0f);
2831 toView.setScaleY(1.0f);
2832 toView.setVisibility(View.VISIBLE);
2833 hideAndShowToolbarButtons(toState, null, null);
2834 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002835 }
2836
2837 /**
2838 * Zoom the camera back into the workspace, hiding 'fromView'.
2839 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002840 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2841 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002842 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002843 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002844 cameraZoomIn(fromState, animated, false);
2845 }
2846
2847 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002848 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002849 final boolean fromAllApps = (fromState == State.ALL_APPS);
2850
2851 int duration = fromAllApps ?
2852 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2853 res.getInteger(R.integer.config_customizeZoomOutTime);
2854
Michael Jurka742574b2011-02-02 23:51:01 -08002855 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002856 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2857 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2858
2859 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002860
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002861 mCustomizePagedView.endChoiceMode();
2862 mAllAppsPagedView.endChoiceMode();
2863
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002864 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002865
Michael Jurkad3ef3062010-11-23 16:23:58 -08002866 if (!springLoaded) {
2867 mWorkspace.unshrink(animated);
2868 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002869
2870 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002871 if (mStateAnimation != null) mStateAnimation.cancel();
2872 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002873
Michael Jurka742574b2011-02-02 23:51:01 -08002874 final float oldScaleX = fromView.getScaleX();
2875 final float oldScaleY = fromView.getScaleY();
2876
2877 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2878 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2879 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2880 public void onAnimationUpdate(ValueAnimator animation) {
2881 final float b = (Float) animation.getAnimatedValue();
2882 final float a = 1f - b;
2883 ((View)fromView.getParent()).fastInvalidate();
2884 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2885 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2886 }
2887 });
2888 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002889 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002890 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002891 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2892 public void onAnimationUpdate(ValueAnimator animation) {
2893 final float b = (Float) animation.getAnimatedValue();
2894 final float a = 1f - b;
2895 // don't need to invalidate because we do so above
2896 fromView.setFastAlpha(a * 1f + b * 0f);
2897 }
2898 });
Michael Jurka2763be32011-02-24 11:19:57 -08002899 if (fromView instanceof LauncherAnimatable) {
2900 ((LauncherAnimatable) fromView).onLauncherAnimationStart();
2901 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002902 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002903 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002904 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002905 fromView.setVisibility(View.GONE);
Michael Jurka2763be32011-02-24 11:19:57 -08002906 if (fromView instanceof LauncherAnimatable) {
2907 ((LauncherAnimatable) fromView).onLauncherAnimationEnd();
2908 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002909 }
2910 });
2911
Chet Haaseb1254a62010-09-07 13:35:00 -07002912 AnimatorSet toolbarHideAnim = new AnimatorSet();
2913 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002914 if (!springLoaded) {
2915 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2916 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002917
Adam Cohen61033d32010-11-15 18:29:44 -08002918 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002919
2920 // Show the new toolbar buttons at the very end of the whole animation
2921 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2922 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002923 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2924 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002925 } else {
2926 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002927 if (!springLoaded) {
2928 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2929 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002930 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002931 }
2932
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002933 void showAllApps(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002934 if (mState != State.WORKSPACE) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002935 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002936 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002937
Michael Jurka79212d82010-07-30 16:36:20 -07002938 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002939 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002940 } else {
2941 mAllAppsGrid.zoom(1.0f, animated);
2942 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002943
Romain Guyc16fea72010-03-12 17:17:56 -08002944 ((View) mAllAppsGrid).setFocusable(true);
2945 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002946
Joe Onorato7c312c12009-08-13 21:36:53 -07002947 // TODO: fade these two too
2948 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002949
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002950 // Change the state *after* we've called all the transition code
2951 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002952
Winson Chung5fb63472011-02-02 17:03:37 -08002953 // Pause the auto-advance of widgets until we are out of AllApps
2954 mUserPresent = false;
2955 updateRunning();
2956
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002957 // send an accessibility event to announce the context change
2958 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2959 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002960
2961 void showWorkspace(boolean animated) {
2962 showWorkspace(animated, null);
2963 }
2964
2965 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002966 if (layout != null) {
2967 // always animated, but that's ok since we never specify a layout and
2968 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002969 mWorkspace.unshrink(layout);
2970 } else {
2971 mWorkspace.unshrink(animated);
2972 }
2973 if (mState == State.ALL_APPS) {
2974 closeAllApps(animated);
2975 } else if (mState == State.CUSTOMIZE) {
2976 hideCustomizationDrawer(animated);
2977 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002978
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002979 // Change the state *after* we've called all the transition code
2980 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002981
Winson Chung5fb63472011-02-02 17:03:37 -08002982 // Resume the auto-advance of widgets
2983 mUserPresent = true;
2984 updateRunning();
2985
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002986 // send an accessibility event to announce the context change
2987 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002988 }
2989
Michael Jurkad3ef3062010-11-23 16:23:58 -08002990 void enterSpringLoadedDragMode(CellLayout layout) {
2991 mWorkspace.enterSpringLoadedDragMode(layout);
2992 if (mState == State.ALL_APPS) {
2993 cameraZoomIn(State.ALL_APPS, true, true);
2994 mState = State.ALL_APPS_SPRING_LOADED;
2995 } else if (mState == State.CUSTOMIZE) {
2996 cameraZoomIn(State.CUSTOMIZE, true, true);
2997 mState = State.CUSTOMIZE_SPRING_LOADED;
2998 }/* else {
2999 // we're already in spring loaded mode; don't do anything
3000 }*/
3001 }
3002
3003 void exitSpringLoadedDragMode() {
3004 if (mState == State.ALL_APPS_SPRING_LOADED) {
3005 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
3006 cameraZoomOut(State.ALL_APPS, true);
3007 mState = State.ALL_APPS;
3008 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
3009 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
3010 cameraZoomOut(State.CUSTOMIZE, true);
3011 mState = State.CUSTOMIZE;
3012 }/* else {
3013 // we're not in spring loaded mode; don't do anything
3014 }*/
3015 }
3016
Joe Onoratob2061212009-11-24 16:13:54 -05003017 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08003018 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05003019 * - Home from workspace
3020 * - from center screen
3021 * - from other screens
3022 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003023 * - from center screen
3024 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003025 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003026 * - from center screen
3027 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003028 * - Launch app from workspace and quit
3029 * - with back
3030 * - with home
3031 * - Launch app from all apps and quit
3032 * - with back
3033 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003034 * - Go to a screen that's not the default, then all
3035 * apps, and launch and app, and go back
3036 * - with back
3037 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003038 * - On workspace, long press power and go back
3039 * - with back
3040 * - with home
3041 * - On all apps, long press power and go back
3042 * - with back
3043 * - with home
3044 * - On workspace, power off
3045 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003046 * - Launch an app and turn off the screen while in that app
3047 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003048 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003049 * - From all apps
3050 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003051 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3052 * - From all apps
3053 * - From the center workspace
3054 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003055 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003056 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003057 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003058 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003059 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003060 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003061 } else {
3062 mAllAppsGrid.zoom(0.0f, animated);
3063 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003064 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003065 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003066 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003067 }
3068
Joe Onorato7c312c12009-08-13 21:36:53 -07003069 void lockAllApps() {
3070 // TODO
3071 }
3072
3073 void unlockAllApps() {
3074 // TODO
3075 }
3076
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003077 // Show the customization drawer (only exists in x-large configuration)
3078 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003079 if (mState != State.WORKSPACE) {
3080 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003081 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003082
3083 cameraZoomOut(State.CUSTOMIZE, animated);
3084
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003085 // Change the state *after* we've called all the transition code
3086 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003087
3088 // Pause the auto-advance of widgets until we are out of Customization drawer
3089 mUserPresent = false;
3090 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003091 }
3092
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003093 // Hide the customization drawer (only exists in x-large configuration)
3094 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003095 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003096 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003097 }
3098 }
3099
Patrick Dubroy5f445422011-02-18 14:35:21 -08003100 /**
3101 * Add an item from all apps or customize onto the given workspace screen.
3102 * If layout is null, add to the current screen.
3103 */
3104 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003105 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3106 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003107 } else {
3108 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003109 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003110 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003111
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003112 void onWorkspaceClick(CellLayout layout) {
3113 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003114 }
3115
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003116 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003117 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003118 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003119 // Look for the toolbar icon specified in the activity meta-data
3120 Bundle metaData = packageManager.getActivityInfo(
3121 activityName, PackageManager.GET_META_DATA).metaData;
3122 if (metaData != null) {
3123 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3124 if (iconResId != 0) {
3125 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003126 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003127 }
3128 }
3129 } catch (NameNotFoundException e) {
3130 // Do nothing
3131 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003132 return null;
3133 }
3134
3135 // if successful in getting icon, return it; otherwise, set button to use default drawable
3136 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3137 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3138 TextView button = (TextView) findViewById(buttonId);
3139 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3140
3141 // If we were unable to find the icon via the meta-data, use a generic one
3142 if (toolbarIcon == null) {
3143 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3144 return null;
3145 } else {
3146 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3147 return toolbarIcon.getConstantState();
3148 }
3149 }
3150
3151 // if successful in getting icon, return it; otherwise, set button to use default drawable
3152 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3153 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3154 ImageView button = (ImageView) findViewById(buttonId);
3155 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3156
Michael Jurka0423dcf2010-10-05 14:56:18 -07003157 // If we were unable to find the icon via the meta-data, use a generic one
3158 if (toolbarIcon == null) {
3159 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003160 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003161 } else {
3162 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003163 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003164 }
3165 }
3166
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003167 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3168 TextView button = (TextView) findViewById(buttonId);
3169 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3170 }
3171
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003172 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003173 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003174 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003175 }
3176
Michael Jurka0423dcf2010-10-05 14:56:18 -07003177 private void updateGlobalSearchIcon() {
3178 if (LauncherApplication.isScreenXLarge()) {
3179 final SearchManager searchManager =
3180 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3181 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003182 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003183 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003184 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003185 } else {
3186 findViewById(R.id.search_button).setVisibility(View.GONE);
3187 }
3188 }
3189 }
3190
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003191 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003192 updateButtonWithDrawable(R.id.search_button, d);
3193 }
3194
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003195 private void updateVoiceSearchIcon() {
3196 if (LauncherApplication.isScreenXLarge()) {
3197 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3198 ComponentName activityName = intent.resolveActivity(getPackageManager());
3199 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003200 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003201 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3202 } else {
3203 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003204 }
3205 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003206 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003207
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003208 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003209 updateButtonWithDrawable(R.id.voice_button, d);
3210 }
3211
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003212 /**
3213 * Sets the app market icon (shown when all apps is visible on x-large screens)
3214 */
3215 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003216 if (LauncherApplication.isScreenXLarge()) {
3217 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3218 // Find the app market activity by resolving an intent.
3219 // (If multiple app markets are installed, it will return the ResolverActivity.)
3220 ComponentName activityName = intent.resolveActivity(getPackageManager());
3221 if (activityName != null) {
3222 mAppMarketIntent = intent;
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003223 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003224 R.id.market_button, activityName, R.drawable.app_market_generic);
3225 }
3226 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003227 }
3228
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003229 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003230 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003231 }
3232
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003233 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003234 * Displays the shortcut creation dialog and launches, if necessary, the
3235 * appropriate activity.
3236 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003237 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003238 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3239 DialogInterface.OnShowListener {
3240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003241 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003243 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003246 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3247 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003248 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003250 builder.setInverseBackgroundForced(true);
3251
3252 AlertDialog dialog = builder.create();
3253 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003254 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003255 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003256
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003257 return dialog;
3258 }
3259
3260 public void onCancel(DialogInterface dialog) {
3261 mWaitingForResult = false;
3262 cleanup();
3263 }
3264
Romain Guycbb89e42009-06-08 15:52:54 -07003265 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003266 }
3267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003268 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003269 try {
3270 dismissDialog(DIALOG_CREATE_SHORTCUT);
3271 } catch (Exception e) {
3272 // An exception is thrown if the dialog is not visible, which is fine
3273 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003274 }
3275
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003276 /**
3277 * Handle the action clicked in the "Add to home" dialog.
3278 */
3279 public void onClick(DialogInterface dialog, int which) {
3280 Resources res = getResources();
3281 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003282
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003283 switch (which) {
3284 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003285 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003286 break;
3287 }
Romain Guycbb89e42009-06-08 15:52:54 -07003288
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003289 case AddAdapter.ITEM_APPWIDGET: {
3290 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003291
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003292 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3293 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003294 // start the pick activity
3295 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3296 break;
3297 }
Romain Guycbb89e42009-06-08 15:52:54 -07003298
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003299 case AddAdapter.ITEM_LIVE_FOLDER: {
3300 // Insert extra item to handle inserting folder
3301 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003302
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003303 ArrayList<String> shortcutNames = new ArrayList<String>();
3304 shortcutNames.add(res.getString(R.string.group_folder));
3305 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003306
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003307 ArrayList<ShortcutIconResource> shortcutIcons =
3308 new ArrayList<ShortcutIconResource>();
3309 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3310 R.drawable.ic_launcher_folder));
3311 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3312
3313 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3314 pickIntent.putExtra(Intent.EXTRA_INTENT,
3315 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3316 pickIntent.putExtra(Intent.EXTRA_TITLE,
3317 getText(R.string.title_select_live_folder));
3318 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003319
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003320 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3321 break;
3322 }
3323
3324 case AddAdapter.ITEM_WALLPAPER: {
3325 startWallpaper();
3326 break;
3327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003328 }
3329 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003330
3331 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003332 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003333 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003334 }
3335
3336 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003337 * Receives notifications when applications are added/removed.
3338 */
3339 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3340 @Override
3341 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003342 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003343 String reason = intent.getStringExtra("reason");
3344 if (!"homekey".equals(reason)) {
3345 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003346 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003347 animate = false;
3348 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003349 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003350 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003351 }
3352 }
3353
3354 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003355 * Receives notifications whenever the appwidgets are reset.
3356 */
3357 private class AppWidgetResetObserver extends ContentObserver {
3358 public AppWidgetResetObserver() {
3359 super(new Handler());
3360 }
3361
3362 @Override
3363 public void onChange(boolean selfChange) {
3364 onAppWidgetReset();
3365 }
3366 }
3367
3368 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003369 * If the activity is currently paused, signal that we need to re-run the loader
3370 * in onResume.
3371 *
3372 * This needs to be called from incoming places where resources might have been loaded
3373 * while we are paused. That is becaues the Configuration might be wrong
3374 * when we're not running, and if it comes back to what it was when we
3375 * were paused, we are not restarted.
3376 *
3377 * Implementation of the method from LauncherModel.Callbacks.
3378 *
3379 * @return true if we are currently paused. The caller might be able to
3380 * skip some work in that case since we will come back again.
3381 */
3382 public boolean setLoadOnResume() {
3383 if (mPaused) {
3384 Log.i(TAG, "setLoadOnResume");
3385 mOnResumeNeedsLoad = true;
3386 return true;
3387 } else {
3388 return false;
3389 }
3390 }
3391
3392 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003393 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003394 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003395 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003396 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003397 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003398 } else {
3399 return SCREEN_COUNT / 2;
3400 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003401 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003402
Winson Chunga12a2502010-12-20 14:41:35 -08003403 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003404 mAllAppsPagedView = view;
3405 }
3406
Joe Onorato9c1289c2009-08-17 11:03:03 -04003407 /**
3408 * Refreshes the shortcuts shown on the workspace.
3409 *
3410 * Implementation of the method from LauncherModel.Callbacks.
3411 */
3412 public void startBinding() {
3413 final Workspace workspace = mWorkspace;
3414 int count = workspace.getChildCount();
3415 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003416 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003417 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3418 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003420
Joe Onorato9c1289c2009-08-17 11:03:03 -04003421 if (DEBUG_USER_INTERFACE) {
3422 android.widget.Button finishButton = new android.widget.Button(this);
3423 finishButton.setText("Finish");
3424 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3425
3426 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3427 public void onClick(View v) {
3428 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003429 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003430 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003431 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003432
3433 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3434 // mDesktopItems -> AppWidgetInfo -> hostView).
3435 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003436 }
3437
3438 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003439 * Bind the items start-end from the list.
3440 *
3441 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003442 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003443 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3444
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003445 setLoadOnResume();
3446
Joe Onorato9c1289c2009-08-17 11:03:03 -04003447 final Workspace workspace = mWorkspace;
3448
3449 for (int i=start; i<end; i++) {
3450 final ItemInfo item = shortcuts.get(i);
3451 mDesktopItems.add(item);
3452 switch (item.itemType) {
3453 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3454 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003455 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003456 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3457 false);
3458 break;
3459 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3460 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003461 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003462 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003463 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3464 false);
3465 break;
3466 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3467 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3468 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003469 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003470 (LiveFolderInfo) item);
3471 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3472 false);
3473 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003474 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003475 }
3476
Joe Onorato9c1289c2009-08-17 11:03:03 -04003477 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003478 }
3479
Joe Onorato9c1289c2009-08-17 11:03:03 -04003480 /**
3481 * Implementation of the method from LauncherModel.Callbacks.
3482 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003483 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003484 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003485 sFolders.clear();
3486 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003487 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003488
3489 /**
3490 * Add the views for a widget to the workspace.
3491 *
3492 * Implementation of the method from LauncherModel.Callbacks.
3493 */
3494 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003495 setLoadOnResume();
3496
Daniel Sandler843e8602010-06-07 14:59:01 -04003497 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3498 if (DEBUG_WIDGETS) {
3499 Log.d(TAG, "bindAppWidget: " + item);
3500 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003501 final Workspace workspace = mWorkspace;
3502
3503 final int appWidgetId = item.appWidgetId;
3504 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003505 if (DEBUG_WIDGETS) {
3506 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3507 }
3508
Joe Onorato9c1289c2009-08-17 11:03:03 -04003509 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3510
Joe Onorato9c1289c2009-08-17 11:03:03 -04003511 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3512 item.hostView.setTag(item);
3513
3514 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3515 item.cellY, item.spanX, item.spanY, false);
3516
Adam Cohended9f8d2010-11-03 13:25:16 -07003517 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3518
Joe Onorato9c1289c2009-08-17 11:03:03 -04003519 workspace.requestLayout();
3520
3521 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003522
3523 if (DEBUG_WIDGETS) {
3524 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3525 + (SystemClock.uptimeMillis()-start) + "ms");
3526 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 }
3528
3529 /**
3530 * Callback saying that there aren't any more items to bind.
3531 *
3532 * Implementation of the method from LauncherModel.Callbacks.
3533 */
3534 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003535 setLoadOnResume();
3536
Joe Onorato9c1289c2009-08-17 11:03:03 -04003537 if (mSavedState != null) {
3538 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003539 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003540 }
3541
3542 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3543 if (userFolders != null) {
3544 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003545 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003546 if (info != null) {
3547 openFolder(info);
3548 }
3549 }
3550 final Folder openFolder = mWorkspace.getOpenFolder();
3551 if (openFolder != null) {
3552 openFolder.requestFocus();
3553 }
3554 }
3555
Joe Onorato9c1289c2009-08-17 11:03:03 -04003556 mSavedState = null;
3557 }
3558
3559 if (mSavedInstanceState != null) {
3560 super.onRestoreInstanceState(mSavedInstanceState);
3561 mSavedInstanceState = null;
3562 }
3563
Winson Chung32bb5e52011-01-31 14:51:56 -08003564 // Workaround a bug that occurs when rotating the device while the customization mode is
3565 // open, we trigger a new layout on all the CellLayout children.
3566 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3567 final int childCount = mWorkspace.getChildCount();
3568 for (int i = 0; i < childCount; ++i) {
3569 mWorkspace.getChildAt(i).requestLayout();
3570 }
3571 }
3572
Joe Onorato9c1289c2009-08-17 11:03:03 -04003573 mWorkspaceLoading = false;
3574 }
3575
3576 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003577 * Updates the icons on the launcher that are affected by changes to the package list
3578 * on the device.
3579 */
3580 private void updateIconsAffectedByPackageManagerChanges() {
3581 updateAppMarketIcon();
3582 updateGlobalSearchIcon();
3583 updateVoiceSearchIcon();
3584 }
3585
3586 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003587 * Add the icons for all apps.
3588 *
3589 * Implementation of the method from LauncherModel.Callbacks.
3590 */
3591 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003592 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003593 if (mCustomizePagedView != null) {
3594 mCustomizePagedView.setApps(apps);
3595 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003596 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003597 }
3598
3599 /**
3600 * A package was installed.
3601 *
3602 * Implementation of the method from LauncherModel.Callbacks.
3603 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003604 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003605 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003607 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003608 if (mCustomizePagedView != null) {
3609 mCustomizePagedView.addApps(apps);
3610 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003611 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003612 }
3613
3614 /**
3615 * A package was updated.
3616 *
3617 * Implementation of the method from LauncherModel.Callbacks.
3618 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003619 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003620 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003621 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003622 if (mWorkspace != null) {
3623 mWorkspace.updateShortcuts(apps);
3624 }
3625 if (mAllAppsGrid != null) {
3626 mAllAppsGrid.updateApps(apps);
3627 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003628 if (mCustomizePagedView != null) {
3629 mCustomizePagedView.updateApps(apps);
3630 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003631 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003632 }
3633
3634 /**
3635 * A package was uninstalled.
3636 *
3637 * Implementation of the method from LauncherModel.Callbacks.
3638 */
Joe Onorato36115782010-06-17 13:28:48 -04003639 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003640 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003641 if (permanent) {
3642 mWorkspace.removeItems(apps);
3643 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003644 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003645 if (mCustomizePagedView != null) {
3646 mCustomizePagedView.removeApps(apps);
3647 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003648 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003649 }
Joe Onoratobe386092009-11-17 17:32:16 -08003650
3651 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003652 * A number of packages were updated.
3653 */
3654 public void bindPackagesUpdated() {
3655 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003656 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003657 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003658 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003659 }
3660
Winson Chung400438b2011-01-16 17:53:48 -08003661 private int mapConfigurationOriActivityInfoOri(int configOri) {
3662 final Display d = getWindowManager().getDefaultDisplay();
3663 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3664 switch (d.getRotation()) {
3665 case Surface.ROTATION_0:
3666 case Surface.ROTATION_180:
3667 // We are currently in the same basic orientation as the natural orientation
3668 naturalOri = configOri;
3669 break;
3670 case Surface.ROTATION_90:
3671 case Surface.ROTATION_270:
3672 // We are currently in the other basic orientation to the natural orientation
3673 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3674 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3675 break;
3676 }
3677
3678 int[] oriMap = {
3679 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3680 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3681 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3682 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3683 };
3684 // Since the map starts at portrait, we need to offset if this device's natural orientation
3685 // is landscape.
3686 int indexOffset = 0;
3687 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3688 indexOffset = 1;
3689 }
3690 return oriMap[(d.getRotation() + indexOffset) % 4];
3691 }
3692 public void lockScreenOrientation() {
3693 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3694 .getConfiguration().orientation));
3695 }
3696 public void unlockScreenOrientation() {
3697 mHandler.postDelayed(new Runnable() {
3698 public void run() {
3699 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3700 }
3701 }, mRestoreScreenOrientationDelay);
3702 }
3703
Winson Chung80baf5a2010-08-09 16:03:15 -07003704 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003705 * Prints out out state for debugging.
3706 */
3707 public void dumpState() {
3708 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003709 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003710 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3711 Log.d(TAG, "mRestoring=" + mRestoring);
3712 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3713 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3714 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003715 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003716 mModel.dumpState();
3717 mAllAppsGrid.dumpState();
3718 Log.d(TAG, "END launcher2 dump state");
3719 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003720}
Michael Jurka2763be32011-02-24 11:19:57 -08003721
3722interface LauncherAnimatable {
3723 void onLauncherAnimationStart();
3724 void onLauncherAnimationEnd();
3725}