blob: 261569ef64625e9d98324f5b2f696d506f104ca9 [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
Romain Guy1fbc1c82009-11-09 20:43:08 -0800254 private ImageView mPreviousView;
255 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500256
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400257 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400258 private String[] mHotseatConfig = null;
259 private Intent[] mHotseats = null;
260 private Drawable[] mHotseatIcons = null;
261 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400262
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700263 private Intent mAppMarketIntent = null;
264
Adam Cohended9f8d2010-11-03 13:25:16 -0700265 // Related to the auto-advancing of widgets
266 private final int ADVANCE_MSG = 1;
267 private final int mAdvanceInterval = 20000;
268 private final int mAdvanceStagger = 250;
269 private long mAutoAdvanceSentTime;
270 private long mAutoAdvanceTimeLeft = -1;
271 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
272 new HashMap<View, AppWidgetProviderInfo>();
273
Winson Chung400438b2011-01-16 17:53:48 -0800274 // Determines how long to wait after a rotation before restoring the screen orientation to
275 // match the sensor state.
276 private final int mRestoreScreenOrientationDelay = 500;
277
Michael Jurka4ef207b2010-11-29 17:05:45 -0800278 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800279 private static Drawable.ConstantState sGlobalSearchIcon;
280 private static Drawable.ConstantState sVoiceSearchIcon;
281 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800282
Michael Jurkaddd62e92011-02-16 17:49:14 -0800283 private BubbleTextView mWaitingForResume;
284
Winson Chunga12a2502010-12-20 14:41:35 -0800285 private CustomizationType getCustomizeFilterForTabTag(String tag) {
286 if (tag.equals(WIDGETS_TAG)) {
287 return CustomizationType.WidgetCustomization;
288 } else if (tag.equals(APPLICATIONS_TAG)) {
289 return CustomizationType.ApplicationCustomization;
290 } else if (tag.equals(WALLPAPERS_TAG)) {
291 return CustomizePagedView.CustomizationType.WallpaperCustomization;
292 } else if (tag.equals(SHORTCUTS_TAG)) {
293 return CustomizePagedView.CustomizationType.ShortcutCustomization;
294 }
295 return CustomizationType.WidgetCustomization;
296 }
297
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 @Override
299 protected void onCreate(Bundle savedInstanceState) {
300 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700301
Winson Chungaafa03c2010-06-11 17:34:16 -0700302 if (LauncherApplication.isInPlaceRotationEnabled()) {
303 // hide the status bar (temporary until we get the status bar design figured out)
304 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
305 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
306 }
307
Joe Onorato0589f0f2010-02-08 13:44:00 -0800308 LauncherApplication app = ((LauncherApplication)getApplication());
309 mModel = app.setLauncher(this);
310 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400311 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700313
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700314 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700315 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
316 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700317
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200319 android.os.Debug.startMethodTracing(
320 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 }
322
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400323 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800324 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800326 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700327 if (mHomeCustomizationDrawer != null) {
328 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700329
Winson Chung80baf5a2010-08-09 16:03:15 -0700330 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700331 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
332 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700333 TabContentFactory contentFactory = new TabContentFactory() {
334 public View createTabContent(String tag) {
335 return mCustomizePagedView;
336 }
337 };
338
Winson Chung49767ae2010-11-29 14:48:30 -0800339
340 TextView tabView;
341 TabWidget tabWidget = (TabWidget)
342 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
343
344 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
345 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700346 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800347 .setIndicator(tabView).setContent(contentFactory));
348 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
349 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700350 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800351 .setIndicator(tabView).setContent(contentFactory));
352 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
353 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700354 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800355 .setIndicator(tabView).setContent(contentFactory));
356 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
357 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700358 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800359 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700360 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
361 public void onTabChanged(String tabId) {
Winson Chunga12a2502010-12-20 14:41:35 -0800362 final CustomizePagedView.CustomizationType newType =
363 getCustomizeFilterForTabTag(tabId);
364 if (newType != mCustomizePagedView.getCustomizationFilter()) {
365 // animate the changing of the tab content by fading pages in and out
366 final Resources res = getResources();
367 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
368 final float alpha = mCustomizePagedView.getAlpha();
369 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
370 "alpha", alpha, 0.0f);
371 alphaAnim.setDuration(duration);
372 alphaAnim.addListener(new AnimatorListenerAdapter() {
373 @Override
374 public void onAnimationEnd(Animator animation) {
Winson Chunga12a2502010-12-20 14:41:35 -0800375 mCustomizePagedView.setCustomizationFilter(newType);
Winson Chung80baf5a2010-08-09 16:03:15 -0700376
Winson Chunga12a2502010-12-20 14:41:35 -0800377 final float alpha = mCustomizePagedView.getAlpha();
378 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
379 mCustomizePagedView, "alpha", alpha, 1.0f);
380 alphaAnim.setDuration(duration);
381 alphaAnim.start();
382 }
383 });
384 alphaAnim.start();
385 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700386 }
387 });
Michael Jurka946ad472010-07-09 18:05:18 -0700388 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 setupViews();
390
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800391 registerContentObservers();
392
Joe Onorato7c312c12009-08-13 21:36:53 -0700393 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 mSavedState = savedInstanceState;
396 restoreState(mSavedState);
397
Winson Chunga12a2502010-12-20 14:41:35 -0800398 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800399 if (mCustomizePagedView != null) {
400 mCustomizePagedView.update();
401 }
Winson Chunga12a2502010-12-20 14:41:35 -0800402
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 if (PROFILE_STARTUP) {
404 android.os.Debug.stopMethodTracing();
405 }
406
407 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400408 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 }
410
411 // For handling default keys
412 mDefaultKeySsb = new SpannableStringBuilder();
413 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800414
415 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
416 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800417
418 // If we have a saved version of these external icons, we load them up immediately
419 if (LauncherApplication.isScreenXLarge()) {
420 if (sGlobalSearchIcon != null) {
421 updateGlobalSearchIcon(sGlobalSearchIcon);
422 }
423 if (sVoiceSearchIcon != null) {
424 updateVoiceSearchIcon(sVoiceSearchIcon);
425 }
426 if (sAppMarketIcon != null) {
427 updateAppMarketIcon(sAppMarketIcon);
428 }
429 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 }
Romain Guycbb89e42009-06-08 15:52:54 -0700431
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800432 @Override
433 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
434 super.dispatchPopulateAccessibilityEvent(event);
435
436 // we want to take over text population so it is context dependent
437 event.getText().clear();
438 if (mState == State.ALL_APPS) {
439 event.getText().add(getString(R.string.all_apps_button_label));
440 } else if (mState == State.WORKSPACE) {
441 event.getText().add(getString(R.string.all_apps_home_button_label));
442 }
443
444 return true;
445 }
446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700448 if (sLocaleConfiguration == null) {
449 new AsyncTask<Void, Void, LocaleConfiguration>() {
450 @Override
451 protected LocaleConfiguration doInBackground(Void... unused) {
452 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
453 readConfiguration(Launcher.this, localeConfiguration);
454 return localeConfiguration;
455 }
456
457 @Override
458 protected void onPostExecute(LocaleConfiguration result) {
459 sLocaleConfiguration = result;
460 checkForLocaleChange(); // recursive, but now with a locale configuration
461 }
462 }.execute();
463 return;
464 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700465
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 final Configuration configuration = getResources().getConfiguration();
467
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700468 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 final String locale = configuration.locale.toString();
470
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700471 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 final int mcc = configuration.mcc;
473
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700474 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 final int mnc = configuration.mnc;
476
Romain Guy84f296c2009-11-04 15:00:44 -0800477 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478
Romain Guy84f296c2009-11-04 15:00:44 -0800479 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700480 sLocaleConfiguration.locale = locale;
481 sLocaleConfiguration.mcc = mcc;
482 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700483
Joe Onorato0589f0f2010-02-08 13:44:00 -0800484 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400485 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700486
487 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
488 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700489 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700490 public void run() {
491 writeConfiguration(Launcher.this, localeConfiguration);
492 }
493 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700494 }
495 }
496
497 private static class LocaleConfiguration {
498 public String locale;
499 public int mcc = -1;
500 public int mnc = -1;
501 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700502
Romain Guy98d01652009-06-30 16:21:04 -0700503 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
504 DataInputStream in = null;
505 try {
506 in = new DataInputStream(context.openFileInput(PREFERENCES));
507 configuration.locale = in.readUTF();
508 configuration.mcc = in.readInt();
509 configuration.mnc = in.readInt();
510 } catch (FileNotFoundException e) {
511 // Ignore
512 } catch (IOException e) {
513 // Ignore
514 } finally {
515 if (in != null) {
516 try {
517 in.close();
518 } catch (IOException e) {
519 // Ignore
520 }
521 }
522 }
523 }
524
525 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
526 DataOutputStream out = null;
527 try {
528 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
529 out.writeUTF(configuration.locale);
530 out.writeInt(configuration.mcc);
531 out.writeInt(configuration.mnc);
532 out.flush();
533 } catch (FileNotFoundException e) {
534 // Ignore
535 } catch (IOException e) {
536 //noinspection ResultOfMethodCallIgnored
537 context.getFileStreamPath(PREFERENCES).delete();
538 } finally {
539 if (out != null) {
540 try {
541 out.close();
542 } catch (IOException e) {
543 // Ignore
544 }
545 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 }
547 }
548
549 static int getScreen() {
550 synchronized (sLock) {
551 return sScreen;
552 }
553 }
554
555 static void setScreen(int screen) {
556 synchronized (sLock) {
557 sScreen = screen;
558 }
559 }
560
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400561 // Note: This doesn't do all the client-id magic that BrowserProvider does
562 // in Browser. (http://b/2425179)
563 private Uri getDefaultBrowserUri() {
564 String url = getString(R.string.default_browser_url);
565 if (url.indexOf("{CID}") != -1) {
566 url = url.replace("{CID}", "android-google");
567 }
568 return Uri.parse(url);
569 }
570
571 // Load the Intent templates from arrays.xml to populate the hotseats. For
572 // each Intent, if it resolves to a single app, use that as the launch
573 // intent & use that app's label as the contentDescription. Otherwise,
574 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400575 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400576 if (mHotseatConfig == null) {
577 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
578 if (mHotseatConfig.length > 0) {
579 mHotseats = new Intent[mHotseatConfig.length];
580 mHotseatLabels = new CharSequence[mHotseatConfig.length];
581 mHotseatIcons = new Drawable[mHotseatConfig.length];
582 } else {
583 mHotseats = null;
584 mHotseatIcons = null;
585 mHotseatLabels = null;
586 }
587
588 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
589 for (int i=0; i<mHotseatConfig.length; i++) {
590 // load icon for this slot; currently unrelated to the actual activity
591 try {
592 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
593 } catch (ArrayIndexOutOfBoundsException ex) {
594 Log.w(TAG, "Missing hotseat_icons array item #" + i);
595 mHotseatIcons[i] = null;
596 }
597 }
598 hotseatIconDrawables.recycle();
599 }
600
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400601 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400602 for (int i=0; i<mHotseatConfig.length; i++) {
603 Intent intent = null;
604 if (mHotseatConfig[i].equals("*BROWSER*")) {
605 // magic value meaning "launch user's default web browser"
606 // replace it with a generic web request so we can see if there is indeed a default
607 String defaultUri = getString(R.string.default_browser_url);
608 intent = new Intent(
609 Intent.ACTION_VIEW,
610 ((defaultUri != null)
611 ? Uri.parse(defaultUri)
612 : getDefaultBrowserUri())
613 ).addCategory(Intent.CATEGORY_BROWSABLE);
614 // note: if the user launches this without a default set, she
615 // will always be taken to the default URL above; this is
616 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700617 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400618 // URL is unavoidably sent to the chosen app.
619 } else {
620 try {
621 intent = Intent.parseUri(mHotseatConfig[i], 0);
622 } catch (java.net.URISyntaxException ex) {
623 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
624 // bogus; leave intent=null
625 }
626 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700627
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400628 if (intent == null) {
629 mHotseats[i] = null;
630 mHotseatLabels[i] = getText(R.string.activity_not_found);
631 continue;
632 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400633
634 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700635 Log.d(TAG, "loadHotseats: hotseat " + i
636 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400637 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400638 + "]");
639 }
640
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400641 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
642 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700643 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400644 Log.d(TAG, "Best match for intent: " + bestMatch);
645 Log.d(TAG, "All matches: ");
646 for (ResolveInfo ri : allMatches) {
647 Log.d(TAG, " --> " + ri);
648 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400649 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400650 // did this resolve to a single app, or the resolver?
651 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700652 // can't find any activity to handle this. let's leave the
653 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400654 // to launch.
655 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400656
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400657 // set accessibility text to "Not installed"
658 mHotseatLabels[i] = getText(R.string.activity_not_found);
659 } else {
660 boolean found = false;
661 for (ResolveInfo ri : allMatches) {
662 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
663 && bestMatch.activityInfo.applicationInfo.packageName
664 .equals(ri.activityInfo.applicationInfo.packageName)) {
665 found = true;
666 break;
667 }
668 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700669
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400670 if (!found) {
671 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
672 // the bestMatch is probably the ResolveActivity, meaning the
673 // user has not yet selected a default
674 // so: we'll keep the original intent for now
675 mHotseats[i] = intent;
676
677 // set the accessibility text to "Select shortcut"
678 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
679 } else {
680 // we have an app!
681 // now reconstruct the intent to launch it through the front
682 // door
683 ComponentName com = new ComponentName(
684 bestMatch.activityInfo.applicationInfo.packageName,
685 bestMatch.activityInfo.name);
686 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
687
688 // load the app label for accessibility
689 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
690 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400691 }
692
693 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700694 Log.d(TAG, "loadHotseats: hotseat " + i
695 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400696 + ((mHotseats[i] == null)
697 ? "null"
698 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400699 + "] label=[" + mHotseatLabels[i]
700 + "]"
701 );
702 }
703 }
704 }
705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 @Override
707 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700708 mWaitingForResult = false;
709
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 // The pattern used here is that a user PICKs a specific application,
711 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700712
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
714 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700715
Michael Jurka0280c3b2010-09-17 15:00:07 -0700716 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 switch (requestCode) {
718 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700719 completeAddApplication(
720 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 break;
722 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800723 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 break;
725 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700726 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 break;
728 case REQUEST_PICK_LIVE_FOLDER:
729 addLiveFolder(data);
730 break;
731 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700732 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700734 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700735 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700737 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700738 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700739 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700741 case REQUEST_PICK_WALLPAPER:
742 // We just wanted the activity result here so we can clear mWaitingForResult
743 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 }
Romain Guy18042c82009-11-06 11:44:55 -0800745 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
746 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
747 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700748 // Clean up the appWidgetId if we canceled
749 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
750 if (appWidgetId != -1) {
751 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 }
755
756 @Override
757 protected void onResume() {
758 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800759 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700760 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400761 mWorkspaceLoading = true;
762 mModel.startLoader(this, true);
763 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700764 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800766 if (mWaitingForResume != null) {
767 mWaitingForResume.setStayPressed(false);
768 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700769 // When we resume Launcher, a different Activity might be responsible for the app
770 // market intent, so refresh the icon
771 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 }
773
774 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700775 protected void onPause() {
776 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700777 // Some launcher layouts don't have a previous and next view
778 if (mPreviousView != null) {
779 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700780 }
781 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700782 dismissPreview(mNextView);
783 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700784 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800785 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700786 }
Romain Guycbb89e42009-06-08 15:52:54 -0700787
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700788 @Override
789 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790 // Flag the loader to stop early before switching
791 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800792 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800793 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700794 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700795
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800796 // We can't hide the IME if it was forced open. So don't bother
797 /*
798 @Override
799 public void onWindowFocusChanged(boolean hasFocus) {
800 super.onWindowFocusChanged(hasFocus);
801
802 if (hasFocus) {
803 final InputMethodManager inputManager = (InputMethodManager)
804 getSystemService(Context.INPUT_METHOD_SERVICE);
805 WindowManager.LayoutParams lp = getWindow().getAttributes();
806 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
807 android.os.Handler()) {
808 protected void onReceiveResult(int resultCode, Bundle resultData) {
809 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
810 }
811 });
812 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
813 }
814 }
815 */
816
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 private boolean acceptFilter() {
818 final InputMethodManager inputManager = (InputMethodManager)
819 getSystemService(Context.INPUT_METHOD_SERVICE);
820 return !inputManager.isFullscreenMode();
821 }
822
823 @Override
824 public boolean onKeyDown(int keyCode, KeyEvent event) {
825 boolean handled = super.onKeyDown(keyCode, event);
826 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
827 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
828 keyCode, event);
829 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700830 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700831 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700832 // showSearchDialog()
833 // If there are multiple keystrokes before the search dialog takes focus,
834 // onSearchRequested() will be called for every keystroke,
835 // but it is idempotent, so it's fine.
836 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 }
838 }
839
Joe Onorato8a9625e2010-01-28 15:55:35 -0800840 // Eat the long press event so the keyboard doesn't come up.
841 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
842 return true;
843 }
844
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 return handled;
846 }
847
Karl Rosaen138a0412009-04-23 19:00:21 -0700848 private String getTypedText() {
849 return mDefaultKeySsb.toString();
850 }
851
852 private void clearTypedText() {
853 mDefaultKeySsb.clear();
854 mDefaultKeySsb.clearSpans();
855 Selection.setSelection(mDefaultKeySsb, 0);
856 }
857
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700859 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
860 * State
861 */
862 private static State intToState(int stateOrdinal) {
863 State state = State.WORKSPACE;
864 final State[] stateValues = State.values();
865 for (int i = 0; i < stateValues.length; i++) {
866 if (stateValues[i].ordinal() == stateOrdinal) {
867 state = stateValues[i];
868 break;
869 }
870 }
871 return state;
872 }
873
874 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 * Restores the previous state, if it exists.
876 *
877 * @param savedState The previous state.
878 */
879 private void restoreState(Bundle savedState) {
880 if (savedState == null) {
881 return;
882 }
883
Michael Jurka883f55b2010-10-21 15:47:14 -0700884 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
885
886 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800887 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700888 } else if (state == State.CUSTOMIZE) {
889 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800890 }
891
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
893 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700894 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 }
896
897 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700898
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700900 mAddScreen = addScreen;
901 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
902 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 mRestoring = true;
904 }
905
906 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
907 if (renameFolder) {
908 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700909 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 mRestoring = true;
911 }
Winson Chunga12a2502010-12-20 14:41:35 -0800912
913 // Restore the current AllApps drawer tab
914 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
915 String curTab = savedState.getString("allapps_currentTab");
916 if (curTab != null) {
917 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
918 tabhost.setCurrentTabByTag(curTab);
919 }
920 int curPage = savedState.getInt("allapps_currentPage", -1);
921 if (curPage > -1) {
922 mAllAppsPagedView.setRestorePage(curPage);
923 }
924 }
925
926 // Restore the current customization drawer tab
927 if (mHomeCustomizationDrawer != null) {
928 String curTab = savedState.getString("customize_currentTab");
929 if (curTab != null) {
930 // We set this directly so that there is no delay before the tab is set
931 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
932 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
933 }
934
935 // Note: currently we do not restore the page for the customization tray because unlike
936 // AllApps, the page content can change drastically
937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939
940 /**
941 * Finds all the views we need and configure them properly.
942 */
943 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700944 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400945
Romain Guyb1b69f52009-08-10 15:10:15 -0700946 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400947 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948
Joe Onorato7c312c12009-08-13 21:36:53 -0700949 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700950 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700951 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800952 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700953 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700954 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700955
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700956 if (LauncherApplication.isScreenXLarge()) {
957 // They need to be INVISIBLE initially so that they will be measured in the layout.
958 // Otherwise the animations are messed up when we show them for the first time.
959 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
960 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
961 }
962
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700964
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500966 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700967
Joe Onorato7c312c12009-08-13 21:36:53 -0700968 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
969 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700971 View handleView = findViewById(R.id.all_apps_button);
972 if (handleView != null && handleView instanceof HandleView) {
973 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700974 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700975 mHandleView.setLauncher(this);
976 mHandleView.setOnClickListener(this);
977 mHandleView.setOnLongClickListener(this);
978 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800979
Winson Chung80baf5a2010-08-09 16:03:15 -0700980 if (mCustomizePagedView != null) {
981 mCustomizePagedView.setLauncher(this);
982 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700983 } else {
984 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
985 hotseatLeft.setContentDescription(mHotseatLabels[0]);
986 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
987 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
988 hotseatRight.setContentDescription(mHotseatLabels[1]);
989 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400990
Michael Jurkaaf442092010-06-10 17:01:57 -0700991 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
992 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800993
Michael Jurkaaf442092010-06-10 17:01:57 -0700994 Drawable previous = mPreviousView.getDrawable();
995 Drawable next = mNextView.getDrawable();
996 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997
Michael Jurkaaf442092010-06-10 17:01:57 -0700998 mPreviousView.setHapticFeedbackEnabled(false);
999 mPreviousView.setOnLongClickListener(this);
1000 mNextView.setHapticFeedbackEnabled(false);
1001 mNextView.setOnLongClickListener(this);
1002 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001003
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001005 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -08001007 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008
1009 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001010 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -08001011
1012 final View allAppsButton = findViewById(R.id.all_apps_button);
1013 final View divider = findViewById(R.id.divider);
1014 final View configureButton = findViewById(R.id.configure_button);
1015
Michael Jurka54f7ac32010-08-02 13:56:46 -07001016 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001017 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -07001018 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001019 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001020 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001021 dragController.addDragListener(deleteZone);
1022
Adam Cohencdc30d52010-12-01 15:09:47 -08001023 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1024 if (allAppsDeleteZone != null) {
1025 allAppsDeleteZone.setLauncher(this);
1026 allAppsDeleteZone.setDragController(dragController);
1027 allAppsDeleteZone.setDragAndDropEnabled(false);
1028 dragController.addDragListener(allAppsDeleteZone);
1029 dragController.addDropTarget(allAppsDeleteZone);
1030 }
1031
1032 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1033 findViewById(R.id.all_apps_info_target);
1034 if (allAppsInfoTarget != null) {
1035 allAppsInfoTarget.setLauncher(this);
1036 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001037 allAppsInfoTarget.setDragAndDropEnabled(false);
1038 View marketButton = findViewById(R.id.market_button);
1039 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001040 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -08001041 }
1042 }
1043
Joe Onorato00acb122009-08-04 16:04:30 -04001044 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001045 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001046 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001047
Joe Onorato00acb122009-08-04 16:04:30 -04001048 // The order here is bottom to top.
1049 dragController.addDropTarget(workspace);
1050 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001051 if (allAppsInfoTarget != null) {
1052 dragController.addDropTarget(allAppsInfoTarget);
1053 }
1054 if (allAppsDeleteZone != null) {
1055 dragController.addDropTarget(allAppsDeleteZone);
1056 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001057 mButtonCluster = findViewById(R.id.all_apps_button_cluster);
1058
1059 mAllAppsButton = findViewById(R.id.all_apps_button);
1060 mDivider = findViewById(R.id.divider);
1061 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001062
1063 // We had previously set these click handlers in XML, but the first time we launched
1064 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1065 // found the right handler. Setting the handlers directly here eliminates that cost.
1066 if (mConfigureButton != null) {
1067 mConfigureButton.setOnClickListener(new OnClickListener() {
1068 public void onClick(View v) {
1069 onClickConfigureButton(v);
1070 }
1071 });
1072 }
1073 if (mDivider != null) {
1074 mDivider.setOnClickListener(new OnClickListener() {
1075 public void onClick(View v) {
1076 onClickAllAppsButton(v);
1077 }
1078 });
1079 }
1080 if (mAllAppsButton != null) {
1081 mAllAppsButton.setOnClickListener(new OnClickListener() {
1082 public void onClick(View v) {
1083 onClickAllAppsButton(v);
1084 }
1085 });
1086 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 }
1088
Romain Guy8a73c512009-11-09 19:19:59 -08001089 @SuppressWarnings({"UnusedDeclaration"})
1090 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001091 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001092 mWorkspace.scrollLeft();
1093 }
Romain Guy8a73c512009-11-09 19:19:59 -08001094 }
1095
1096 @SuppressWarnings({"UnusedDeclaration"})
1097 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001098 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001099 mWorkspace.scrollRight();
1100 }
Romain Guy8a73c512009-11-09 19:19:59 -08001101 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001102
1103 @SuppressWarnings({"UnusedDeclaration"})
1104 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001105 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001106
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001107 int index = -1;
1108 if (v.getId() == R.id.hotseat_left) {
1109 index = 0;
1110 } else if (v.getId() == R.id.hotseat_right) {
1111 index = 1;
1112 }
1113
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001114 // reload these every tap; you never know when they might change
1115 loadHotseats();
1116 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001117 startActivitySafely(
1118 mHotseats[index],
1119 "hotseat"
1120 );
1121 }
1122 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001123
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 /**
1125 * Creates a view representing a shortcut.
1126 *
1127 * @param info The data structure describing the shortcut.
1128 *
1129 * @return A View inflated from R.layout.application.
1130 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001131 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001133 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 }
1135
1136 /**
1137 * Creates a view representing a shortcut inflated from the specified resource.
1138 *
1139 * @param layoutResId The id of the XML layout used to create the shortcut.
1140 * @param parent The group the shortcut belongs to.
1141 * @param info The data structure describing the shortcut.
1142 *
1143 * @return A View inflated from layoutResId.
1144 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001145 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001146 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1147 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 return favorite;
1150 }
1151
1152 /**
1153 * Add an application shortcut to the workspace.
1154 *
1155 * @param data The intent describing the application.
1156 * @param cellInfo The position on screen where to create the shortcut.
1157 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001158 void completeAddApplication(Context context, Intent data, int screen,
1159 int intersectCellX, int intersectCellY) {
1160 final int[] cellXY = mTmpAddItemCellCoordinates;
1161 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1162
1163 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1164 showOutOfSpaceMessage();
1165 return;
1166 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167
Joe Onorato0589f0f2010-02-08 13:44:00 -08001168 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1169 data, context);
1170
Romain Guy73b979d2009-06-09 12:57:21 -07001171 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001172 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001174 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001175 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1176 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001177 } else {
1178 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 }
1180 }
Romain Guycbb89e42009-06-08 15:52:54 -07001181
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 /**
1183 * Add a shortcut to the workspace.
1184 *
1185 * @param data The intent describing the shortcut.
1186 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001188 private void completeAddShortcut(Intent data, int screen,
1189 int intersectCellX, int intersectCellY) {
1190 final int[] cellXY = mTmpAddItemCellCoordinates;
1191 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001192
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001193 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001194 boolean foundCellSpan = false;
1195 if (touchXY != null) {
1196 // when dragging and dropping, just find the closest free spot
1197 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1198 int[] result = screenLayout.findNearestVacantArea(
1199 touchXY[0], touchXY[1], 1, 1, cellXY);
1200 foundCellSpan = (result != null);
1201 } else {
1202 foundCellSpan = layout.findCellForSpanThatIntersects(
1203 cellXY, 1, 1, intersectCellX, intersectCellY);
1204 }
1205
1206 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001207 showOutOfSpaceMessage();
1208 return;
1209 }
1210
1211 final ShortcutInfo info = mModel.addShortcut(
1212 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213
1214 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001216 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 }
1218 }
1219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001221 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001223 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001224 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001226 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001227 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001228
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001229 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001230 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1231 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001234 // We have saved the position to which the widget was dragged-- this really only matters
1235 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001236 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001237
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001238 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001239 boolean foundCellSpan = false;
1240 if (touchXY != null) {
1241 // when dragging and dropping, just find the closest free spot
1242 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1243 int[] result = screenLayout.findNearestVacantArea(
1244 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001245 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001246 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001247 // if we long pressed on an empty cell to bring up a menu,
1248 // make sure we intersect the empty cell
1249 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1250 // findCellForSpanThatIntersects will just ignore them
1251 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1252 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001253 }
1254
Michael Jurka0280c3b2010-09-17 15:00:07 -07001255 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001256 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001257 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001258 return;
1259 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001261 // Build Launcher-specific widget info and save to database
1262 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001263 launcherInfo.spanX = spanXY[0];
1264 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001265
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 LauncherModel.addItemToDatabase(this, launcherInfo,
1267 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001268 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269
1270 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001271 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001272
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001274 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001275
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001276 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001278
Michael Jurka0280c3b2010-09-17 15:00:07 -07001279 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001280 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001281
1282 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
1284 }
Romain Guycbb89e42009-06-08 15:52:54 -07001285
Adam Cohended9f8d2010-11-03 13:25:16 -07001286 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1287 @Override
1288 public void onReceive(Context context, Intent intent) {
1289 final String action = intent.getAction();
1290 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1291 mUserPresent = false;
1292 updateRunning();
1293 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1294 mUserPresent = true;
1295 updateRunning();
1296 }
1297 }
1298 };
1299
1300 @Override
1301 public void onAttachedToWindow() {
1302 super.onAttachedToWindow();
1303
1304 // Listen for broadcasts related to user-presence
1305 final IntentFilter filter = new IntentFilter();
1306 filter.addAction(Intent.ACTION_SCREEN_OFF);
1307 filter.addAction(Intent.ACTION_USER_PRESENT);
1308 registerReceiver(mReceiver, filter);
1309
Adam Cohend113e0c2010-11-11 10:48:05 -08001310 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001311 mVisible = true;
1312 }
1313
1314 @Override
1315 public void onDetachedFromWindow() {
1316 super.onDetachedFromWindow();
1317 mVisible = false;
1318
Adam Cohend113e0c2010-11-11 10:48:05 -08001319 if (mAttached) {
1320 unregisterReceiver(mReceiver);
1321 mAttached = false;
1322 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001323 updateRunning();
1324 }
1325
1326 public void onWindowVisibilityChanged(int visibility) {
1327 mVisible = visibility == View.VISIBLE;
1328 updateRunning();
1329 }
1330
1331 private void sendAdvanceMessage(long delay) {
1332 mHandler.removeMessages(ADVANCE_MSG);
1333 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1334 mHandler.sendMessageDelayed(msg, delay);
1335 mAutoAdvanceSentTime = System.currentTimeMillis();
1336 }
1337
1338 private void updateRunning() {
1339 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1340 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1341 mAutoAdvanceRunning = autoAdvanceRunning;
1342 if (autoAdvanceRunning) {
1343 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1344 sendAdvanceMessage(delay);
1345 } else {
1346 if (!mWidgetsToAdvance.isEmpty()) {
1347 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1348 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1349 }
1350 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001351 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001352 }
1353 }
1354 }
1355
1356 private final Handler mHandler = new Handler() {
1357 @Override
1358 public void handleMessage(Message msg) {
1359 if (msg.what == ADVANCE_MSG) {
1360 int i = 0;
1361 for (View key: mWidgetsToAdvance.keySet()) {
1362 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1363 final int delay = mAdvanceStagger * i;
1364 if (v instanceof Advanceable) {
1365 postDelayed(new Runnable() {
1366 public void run() {
1367 ((Advanceable) v).advance();
1368 }
1369 }, delay);
1370 }
1371 i++;
1372 }
1373 sendAdvanceMessage(mAdvanceInterval);
1374 }
1375 }
1376 };
1377
1378 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1379 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1380 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1381 if (v instanceof Advanceable) {
1382 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001383 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001384 updateRunning();
1385 }
1386 }
1387
1388 void removeWidgetToAutoAdvance(View hostView) {
1389 if (mWidgetsToAdvance.containsKey(hostView)) {
1390 mWidgetsToAdvance.remove(hostView);
1391 updateRunning();
1392 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001393 }
1394
Joe Onorato9c1289c2009-08-17 11:03:03 -04001395 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1396 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001397 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001398 launcherInfo.hostView = null;
1399 }
1400
Adam Cohended9f8d2010-11-03 13:25:16 -07001401 void showOutOfSpaceMessage() {
1402 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1403 }
1404
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001405 public LauncherAppWidgetHost getAppWidgetHost() {
1406 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 }
Romain Guycbb89e42009-06-08 15:52:54 -07001408
Winson Chunga9abd0e2010-10-27 17:18:37 -07001409 public LauncherModel getModel() {
1410 return mModel;
1411 }
1412
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001413 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001414 getWindow().closeAllPanels();
1415
1416 try {
1417 dismissDialog(DIALOG_CREATE_SHORTCUT);
1418 // Unlock the workspace if the dialog was showing
1419 } catch (Exception e) {
1420 // An exception is thrown if the dialog is not visible, which is fine
1421 }
1422
1423 try {
1424 dismissDialog(DIALOG_RENAME_FOLDER);
1425 // Unlock the workspace if the dialog was showing
1426 } catch (Exception e) {
1427 // An exception is thrown if the dialog is not visible, which is fine
1428 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001429
1430 // Whatever we were doing is hereby canceled.
1431 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001432 }
1433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 @Override
1435 protected void onNewIntent(Intent intent) {
1436 super.onNewIntent(intent);
1437
1438 // Close the menu
1439 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001440 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001441 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001442
Joe Onorato14f122b2009-11-19 14:06:36 -08001443 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1444 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001445
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001446 // In all these cases, only animate if we're already on home
1447
Michael Jurka4cb37242010-08-09 21:05:32 -07001448 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001449 mWorkspace.unshrink(alreadyOnHome);
1450 }
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001451
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001452 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001453 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001454 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001455 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001456
Joe Onorato3a8820b2009-11-10 15:06:42 -08001457 final View v = getWindow().peekDecorView();
1458 if (v != null && v.getWindowToken() != null) {
1459 InputMethodManager imm = (InputMethodManager)getSystemService(
1460 INPUT_METHOD_SERVICE);
1461 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 }
1463 }
1464 }
1465
1466 @Override
1467 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1468 // Do not call super here
1469 mSavedInstanceState = savedInstanceState;
1470 }
1471
1472 @Override
1473 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001474 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475
1476 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1477 if (folders.size() > 0) {
1478 final int count = folders.size();
1479 long[] ids = new long[count];
1480 for (int i = 0; i < count; i++) {
1481 final FolderInfo info = folders.get(i).getInfo();
1482 ids[i] = info.id;
1483 }
1484 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1485 } else {
1486 super.onSaveInstanceState(outState);
1487 }
1488
Michael Jurka883f55b2010-10-21 15:47:14 -07001489 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490
Michael Jurka0280c3b2010-09-17 15:00:07 -07001491 if (mAddScreen > -1 && mWaitingForResult) {
1492 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1493 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1494 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 }
1496
1497 if (mFolderInfo != null && mWaitingForResult) {
1498 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1499 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1500 }
Michael Jurka946ad472010-07-09 18:05:18 -07001501
Winson Chung43b119d2010-12-06 16:45:05 -08001502 // Save the current AllApps drawer tab
1503 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1504 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1505 String currentTabTag = tabhost.getCurrentTabTag();
1506 if (currentTabTag != null) {
1507 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001508 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001509 }
1510 }
1511
1512 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001513 if (mHomeCustomizationDrawer != null) {
1514 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1515 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001516 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001517 }
1518 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 }
1520
1521 @Override
1522 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001524
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001526 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001528 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001530 mAppWidgetHost = null;
1531
1532 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533
1534 TextKeyListener.getInstance().release();
1535
Joe Onorato9c1289c2009-08-17 11:03:03 -04001536 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537
Joe Onorato9c1289c2009-08-17 11:03:03 -04001538 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001539
1540 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001541
Patrick Dubroyab962b72010-07-26 12:10:10 -07001542 // Some launcher layouts don't have a previous and next view
1543 if (mPreviousView != null) {
1544 dismissPreview(mPreviousView);
1545 }
1546 if (mNextView != null) {
1547 dismissPreview(mNextView);
1548 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001549
1550 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001551
1552 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1553 mWorkspace.removeAllViews();
1554 mWorkspace = null;
1555 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001556
1557 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 }
1559
1560 @Override
1561 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001562 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 super.startActivityForResult(intent, requestCode);
1564 }
1565
1566 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001567 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001569
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001570 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001571
Karl Rosaen138a0412009-04-23 19:00:21 -07001572 if (initialQuery == null) {
1573 // Use any text typed in the launcher as the initial query
1574 initialQuery = getTypedText();
1575 clearTypedText();
1576 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 if (appSearchData == null) {
1578 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001579 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
Romain Guycbb89e42009-06-08 15:52:54 -07001581
Karl Rosaen138a0412009-04-23 19:00:21 -07001582 final SearchManager searchManager =
1583 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001584 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001585 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 }
1587
1588 @Override
1589 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001590 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001591 return false;
1592 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593
1594 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001595
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1597 .setIcon(android.R.drawable.ic_menu_add)
1598 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001599 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1600 .setIcon(android.R.drawable.ic_menu_manage)
1601 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001602 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 .setIcon(android.R.drawable.ic_menu_gallery)
1604 .setAlphabeticShortcut('W');
1605 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1606 .setIcon(android.R.drawable.ic_search_category_default)
1607 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1608 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1609 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1610 .setAlphabeticShortcut('N');
1611
1612 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001613 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1614 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615
1616 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1617 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1618 .setIntent(settings);
1619
1620 return true;
1621 }
1622
1623 @Override
1624 public boolean onPrepareOptionsMenu(Menu menu) {
1625 super.onPrepareOptionsMenu(menu);
1626
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001627 // If all apps is animating, don't show the menu, because we don't know
1628 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001629 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001630 return false;
1631 }
1632
1633 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001634 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001635 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1636 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1637
1638 // Disable add if the workspace is full.
1639 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001640 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1641 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001642 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643
1644 return true;
1645 }
1646
1647 @Override
1648 public boolean onOptionsItemSelected(MenuItem item) {
1649 switch (item.getItemId()) {
1650 case MENU_ADD:
1651 addItems();
1652 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001653 case MENU_MANAGE_APPS:
1654 manageApps();
1655 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 case MENU_WALLPAPER_SETTINGS:
1657 startWallpaper();
1658 return true;
1659 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001660 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 return true;
1662 case MENU_NOTIFICATIONS:
1663 showNotifications();
1664 return true;
1665 }
1666
1667 return super.onOptionsItemSelected(item);
1668 }
Romain Guycbb89e42009-06-08 15:52:54 -07001669
Karl Rosaen138a0412009-04-23 19:00:21 -07001670 /**
1671 * Indicates that we want global search for this activity by setting the globalSearch
1672 * argument for {@link #startSearch} to true.
1673 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001675 @Override
1676 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001677 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001678 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001679 }
1680
Joe Onorato9c1289c2009-08-17 11:03:03 -04001681 public boolean isWorkspaceLocked() {
1682 return mWorkspaceLoading || mWaitingForResult;
1683 }
1684
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001686 if (LauncherApplication.isScreenXLarge()) {
1687 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001688 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001689 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001690 }
1691 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001692 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001693 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001694 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 }
1696
Michael Jurka0280c3b2010-09-17 15:00:07 -07001697 private void resetAddInfo() {
1698 mAddScreen = -1;
1699 mAddIntersectCellX = -1;
1700 mAddIntersectCellY = -1;
1701 mAddDropPosition = null;
1702 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001703
Winson Chung55cef262010-10-28 14:14:18 -07001704 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001705 resetAddInfo();
1706 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001707 mAddDropPosition = position;
1708
Michael Jurkaaf442092010-06-10 17:01:57 -07001709 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001710 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1711 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001712 }
1713
Winson Chung7ad01412010-09-27 11:33:03 -07001714 private void manageApps() {
1715 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1716 }
1717
Michael Jurkaaf442092010-06-10 17:01:57 -07001718 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001719 // TODO: catch bad widget exception when sent
1720 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001721 // TODO: Is this log message meaningful?
1722 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001723 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001724 }
1725
Winson Chung55cef262010-10-28 14:14:18 -07001726 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001727 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728
Bjorn Bringert7984c942009-12-09 15:38:25 +00001729 if (appWidget.configure != null) {
1730 // Launch over to configure widget, if needed
1731 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1732 intent.setComponent(appWidget.configure);
1733 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001734 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001735 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1736 intent.putExtra(
1737 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1738 info.mimeType);
1739
1740 final String mimeType = info.mimeType;
1741 final ClipData clipData = (ClipData) info.configurationData;
1742 final ClipDescription clipDesc = clipData.getDescription();
1743 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1744 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001745 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001746 final CharSequence stringData = item.getText();
1747 final Uri uriData = item.getUri();
1748 final Intent intentData = item.getIntent();
1749 final String key =
1750 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1751 if (uriData != null) {
1752 intent.putExtra(key, uriData);
1753 } else if (intentData != null) {
1754 intent.putExtra(key, intentData);
1755 } else if (stringData != null) {
1756 intent.putExtra(key, stringData);
1757 }
1758 break;
1759 }
1760 }
1761 }
Winson Chung55cef262010-10-28 14:14:18 -07001762 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001763
Romain Guy8e633c52010-03-04 12:51:36 -08001764 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001766 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001767 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 }
1769 }
Romain Guycbb89e42009-06-08 15:52:54 -07001770
Michael Jurka0280c3b2010-09-17 15:00:07 -07001771 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1772 resetAddInfo();
1773 mAddScreen = screen;
1774 mAddDropPosition = position;
1775
1776 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1777 createShortcutIntent.setComponent(componentName);
1778 processShortcut(createShortcutIntent);
1779 }
1780
Joe Onoratodeb98af2010-02-19 14:59:39 -08001781 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001782 // Handle case where user selected "Applications"
1783 String applicationName = getResources().getString(R.string.group_applications);
1784 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001785
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001786 if (applicationName != null && applicationName.equals(shortcutName)) {
1787 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1788 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001789
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001790 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1791 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001792 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001793 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001794 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001795 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001796 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 }
1798
Winson Chung24ab2f12010-09-16 14:10:47 -07001799 void processWallpaper(Intent intent) {
1800 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1801 }
1802
Michael Jurka0280c3b2010-09-17 15:00:07 -07001803 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1804 resetAddInfo();
1805 mAddScreen = screen;
1806 mAddDropPosition = position;
1807
1808 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1809 createFolderIntent.setComponent(componentName);
1810
1811 addLiveFolder(createFolderIntent);
1812 }
1813
1814 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001815 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001816 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001817 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001818
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001819 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001820 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001821 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001822 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001823 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 }
1825
Michael Jurka0280c3b2010-09-17 15:00:07 -07001826 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 UserFolderInfo folderInfo = new UserFolderInfo();
1828 folderInfo.title = getText(R.string.folder_name);
1829
Michael Jurka0280c3b2010-09-17 15:00:07 -07001830 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1831 final int[] cellXY = mTmpAddItemCellCoordinates;
1832 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1833 showOutOfSpaceMessage();
1834 return;
1835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836
1837 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001838 LauncherModel.addItemToDatabase(this, folderInfo,
1839 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001840 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001841 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842
1843 // Create the view
1844 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001845 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1846 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001847 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 }
Romain Guycbb89e42009-06-08 15:52:54 -07001849
Joe Onorato9c1289c2009-08-17 11:03:03 -04001850 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001851 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001852 }
1853
Michael Jurka28750fb2010-09-24 17:43:49 -07001854 private void completeAddLiveFolder(
1855 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001856 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1857 final int[] cellXY = mTmpAddItemCellCoordinates;
1858 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1859 showOutOfSpaceMessage();
1860 return;
1861 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862
Michael Jurka0280c3b2010-09-17 15:00:07 -07001863 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864
1865 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001867 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001868 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 }
1870 }
1871
1872 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001873 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874
1875 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1876 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1877
1878 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 Intent.ShortcutIconResource iconResource = null;
1880
1881 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1882 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1883 try {
1884 iconResource = (Intent.ShortcutIconResource) extra;
1885 final PackageManager packageManager = context.getPackageManager();
1886 Resources resources = packageManager.getResourcesForApplication(
1887 iconResource.packageName);
1888 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1889 icon = resources.getDrawable(id);
1890 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001891 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 }
1893 }
1894
1895 if (icon == null) {
1896 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1897 }
1898
1899 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001900 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 info.title = name;
1902 info.iconResource = iconResource;
1903 info.uri = data.getData();
1904 info.baseIntent = baseIntent;
1905 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1906 LiveFolders.DISPLAY_MODE_GRID);
1907
1908 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001909 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001910 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911
1912 return info;
1913 }
1914
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 private void showNotifications() {
1916 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1917 if (statusBar != null) {
1918 statusBar.expand();
1919 }
1920 }
1921
1922 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001923 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001925 Intent chooser = Intent.createChooser(pickWallpaper,
1926 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001927 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1928 // Removed in Eclair MR1
1929// WallpaperManager wm = (WallpaperManager)
1930// getSystemService(Context.WALLPAPER_SERVICE);
1931// WallpaperInfo wi = wm.getWallpaperInfo();
1932// if (wi != null && wi.getSettingsActivity() != null) {
1933// LabeledIntent li = new LabeledIntent(getPackageName(),
1934// R.string.configure_wallpaper, 0);
1935// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1936// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1937// }
Mike Clerona0618e42009-10-22 13:55:21 -07001938 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 }
1940
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001941 /**
1942 * Registers various content observers. The current implementation registers
1943 * only a favorites observer to keep track of the favorites applications.
1944 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001945 private void registerContentObservers() {
1946 ContentResolver resolver = getContentResolver();
1947 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1948 true, mWidgetObserver);
1949 }
1950
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 @Override
1952 public boolean dispatchKeyEvent(KeyEvent event) {
1953 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1954 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001956 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001957 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001958 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001959 dumpState();
1960 return true;
1961 }
1962 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001963 }
1964 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1965 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001966 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 return true;
1968 }
1969 }
1970
1971 return super.dispatchKeyEvent(event);
1972 }
1973
Joe Onorato88ec0992009-11-19 13:16:06 -08001974 @Override
1975 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001976 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1977 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001978 } else {
1979 closeFolder();
1980 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001981 // Some launcher layouts don't have a previous and next view
1982 if (mPreviousView != null) {
1983 dismissPreview(mPreviousView);
1984 dismissPreview(mNextView);
1985 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001986 }
1987
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 private void closeFolder() {
1989 Folder folder = mWorkspace.getOpenFolder();
1990 if (folder != null) {
1991 closeFolder(folder);
1992 }
1993 }
1994
1995 void closeFolder(Folder folder) {
1996 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08001997 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001999 CellLayout cl = (CellLayout) parent;
2000 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05002001 if (folder instanceof DropTarget) {
2002 // Live folders aren't DropTargets.
2003 mDragController.removeDropTarget((DropTarget)folder);
2004 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 }
2006 folder.onClose();
2007 }
2008
2009 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002010 * Re-listen when widgets are reset.
2011 */
2012 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002013 if (mAppWidgetHost != null) {
2014 mAppWidgetHost.startListening();
2015 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002016 }
2017
2018 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002019 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2020 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002022 private void unbindDesktopItems() {
2023 for (ItemInfo item: mDesktopItems) {
2024 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002026 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002028
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 /**
2030 * Launches the intent referred by the clicked shortcut.
2031 *
2032 * @param v The view representing the clicked shortcut.
2033 */
2034 public void onClick(View v) {
2035 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002036 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002038 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002039 int[] pos = new int[2];
2040 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002041 intent.setSourceBounds(new Rect(pos[0], pos[1],
2042 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08002043 boolean success = startActivitySafely(intent, tag);
2044
2045 if (success && v instanceof BubbleTextView) {
2046 mWaitingForResume = (BubbleTextView) v;
2047 mWaitingForResume.setStayPressed(true);
2048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 } else if (tag instanceof FolderInfo) {
2050 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002051 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002052 if (mState == State.ALL_APPS) {
2053 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002054 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002055 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002056 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 }
2058 }
2059
Michael Jurka0e260592010-06-30 17:07:39 -07002060 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002061 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002062 // clicking anywhere on the workspace causes the customization drawer to slide down
2063 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002064 return false;
2065 }
2066
Michael Jurkaaf442092010-06-10 17:01:57 -07002067 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002068 * Event handler for the search button
2069 *
2070 * @param v The view that was clicked.
2071 */
2072 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002073 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002074 // Use a custom animation for launching search
2075 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002076 }
2077
2078 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002079 * Event handler for the voice button
2080 *
2081 * @param v The view that was clicked.
2082 */
2083 public void onClickVoiceButton(View v) {
2084 startVoiceSearch();
2085 }
2086
2087 private void startVoiceSearch() {
2088 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2089 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2090 startActivity(intent);
2091 }
2092
2093 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002094 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002095 * enters home screen customization mode.
2096 *
2097 * @param v The view that was clicked.
2098 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002099 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002100 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002101 }
2102
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002103 /**
2104 * Event handler for the "grid" button that appears on the home screen, which
2105 * enters all apps mode.
2106 *
2107 * @param v The view that was clicked.
2108 */
2109 public void onClickAllAppsButton(View v) {
2110 showAllApps(true);
2111 }
2112
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002113 public void onClickAppMarketButton(View v) {
2114 if (mAppMarketIntent != null) {
2115 startActivitySafely(mAppMarketIntent, "app market");
2116 }
2117 }
2118
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002119 void startApplicationDetailsActivity(ComponentName componentName) {
2120 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002121 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2122 Uri.fromParts("package", packageName, null));
2123 startActivity(intent);
2124 }
2125
Patrick Dubroy5539af72010-09-07 15:22:01 -07002126 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2127 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2128 // System applications cannot be installed. For now, show a toast explaining that.
2129 // We may give them the option of disabling apps this way.
2130 int messageId = R.string.uninstall_system_app_text;
2131 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2132 } else {
2133 String packageName = appInfo.componentName.getPackageName();
2134 String className = appInfo.componentName.getClassName();
2135 Intent intent = new Intent(
2136 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2137 startActivity(intent);
2138 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002139 }
2140
Michael Jurkaddd62e92011-02-16 17:49:14 -08002141 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2143 try {
2144 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002145 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146 } catch (ActivityNotFoundException e) {
2147 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002148 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 } catch (SecurityException e) {
2150 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002151 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002152 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002153 "or use the exported attribute for this activity. "
2154 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002156 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002157 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002158
Romain Guy8e633c52010-03-04 12:51:36 -08002159 void startActivityForResultSafely(Intent intent, int requestCode) {
2160 try {
2161 startActivityForResult(intent, requestCode);
2162 } catch (ActivityNotFoundException e) {
2163 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2164 } catch (SecurityException e) {
2165 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2166 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2167 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2168 "or use the exported attribute for this activity.", e);
2169 }
2170 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171
2172 private void handleFolderClick(FolderInfo folderInfo) {
2173 if (!folderInfo.opened) {
2174 // Close any open folder
2175 closeFolder();
2176 // Open the requested folder
2177 openFolder(folderInfo);
2178 } else {
2179 // Find the open folder...
2180 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2181 int folderScreen;
2182 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002183 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002184 // .. and close it
2185 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002186 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 // Close any folder open on the current screen
2188 closeFolder();
2189 // Pull the folder onto this screen
2190 openFolder(folderInfo);
2191 }
2192 }
2193 }
2194 }
2195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002197 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 * is animated relative to the specified View. If the View is null, no animation
2199 * is played.
2200 *
2201 * @param folderInfo The FolderInfo describing the folder to open.
2202 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002203 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 Folder openFolder;
2205
2206 if (folderInfo instanceof UserFolderInfo) {
2207 openFolder = UserFolder.fromXml(this);
2208 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002209 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 } else {
2211 return;
2212 }
2213
Joe Onorato00acb122009-08-04 16:04:30 -04002214 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 openFolder.setLauncher(this);
2216
2217 openFolder.bind(folderInfo);
2218 folderInfo.opened = true;
2219
Winson Chungaafa03c2010-06-11 17:34:16 -07002220 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 openFolder.onOpen();
2223 }
2224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002226 switch (v.getId()) {
2227 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002228 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002229 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2230 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002231 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002232 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002233 return true;
2234 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002235 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002236 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2237 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002238 showPreviews(v);
2239 }
2240 return true;
2241 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002242 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002243 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2244 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2245 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002246 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002247 return true;
2248 }
2249
Joe Onorato9c1289c2009-08-17 11:03:03 -04002250 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 return false;
2252 }
2253
2254 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002255 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 }
2257
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258
Michael Jurka0280c3b2010-09-17 15:00:07 -07002259 resetAddInfo();
2260 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002261 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002262 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002263 return true;
2264 }
2265
Michael Jurka0280c3b2010-09-17 15:00:07 -07002266 final View itemUnderLongClick = longClickCellInfo.cell;
2267
Winson Chung304dcde2011-01-07 11:17:23 -08002268 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002269 if (itemUnderLongClick == null) {
2270 // User long pressed on empty space
2271 mWorkspace.setAllowLongPress(false);
2272 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2273 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002274 if (LauncherApplication.isScreenXLarge()) {
2275 addItems();
2276 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002277 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 }
2279 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002280 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002281 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002282 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2283 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002284 mAddIntersectCellX = longClickCellInfo.cellX;
2285 mAddIntersectCellY = longClickCellInfo.cellY;
2286 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002287 }
2288 }
2289 }
2290 return true;
2291 }
2292
Romain Guye6b8e2f2009-11-10 11:56:55 -08002293 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002294 private void dismissPreview(final View v) {
2295 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002296 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002297 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2298 public void onDismiss() {
2299 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2300 int count = group.getChildCount();
2301 for (int i = 0; i < count; i++) {
2302 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2303 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002304 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2305 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2306
2307 v.setTag(R.id.workspace, null);
2308 v.setTag(R.id.icon, null);
2309 window.setOnDismissListener(null);
2310 }
2311 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002312 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002313 }
2314 v.setTag(null);
2315 }
2316
Romain Guyf8e6a802009-12-07 17:48:02 -08002317 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002318 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002319 }
2320
Romain Guya6abce82009-11-10 02:54:41 -08002321 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002322 final Resources resources = getResources();
2323 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002324
Romain Guya6abce82009-11-10 02:54:41 -08002325 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002326
Romain Guy9d31e9f2009-11-11 18:54:28 -08002327 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002328
Romain Guyf8e6a802009-12-07 17:48:02 -08002329 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002330 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002331 int extraW = (int) ((r.left + r.right) * max);
2332 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002333
2334 int aW = cell.getWidth() - extraW;
2335 float w = aW / max;
2336
2337 int width = cell.getWidth();
2338 int height = cell.getHeight();
2339 int x = cell.getLeftPadding();
2340 int y = cell.getTopPadding();
2341 width -= (x + cell.getRightPadding());
2342 height -= (y + cell.getBottomPadding());
2343
2344 float scale = w / width;
2345
2346 int count = end - start;
2347
2348 final float sWidth = width * scale;
2349 float sHeight = height * scale;
2350
Romain Guye6b8e2f2009-11-10 11:56:55 -08002351 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002352
Romain Guye6b8e2f2009-11-10 11:56:55 -08002353 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2354 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002355
2356 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002357 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002358 cell = (CellLayout) workspace.getChildAt(i);
2359
Romain Guyf8e6a802009-12-07 17:48:02 -08002360 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002361 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002362
2363 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002364 c.scale(scale, scale);
2365 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002366 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002367
Romain Guy9d31e9f2009-11-11 18:54:28 -08002368 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002369 image.setImageBitmap(bitmap);
2370 image.setTag(i);
2371 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002372 image.setOnFocusChangeListener(handler);
2373 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002374 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002375
2376 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002377 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2378
Winson Chungaafa03c2010-06-11 17:34:16 -07002379 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002380 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002381
2382 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002383 p.setContentView(preview);
2384 p.setWidth((int) (sWidth * count + extraW));
2385 p.setHeight((int) (sHeight + extraH));
2386 p.setAnimationStyle(R.style.AnimationPreview);
2387 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002388 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002389 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002390 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002391
Romain Guye6b8e2f2009-11-10 11:56:55 -08002392 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2393 public void onDismiss() {
2394 dismissPreview(anchor);
2395 }
2396 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002397
2398 anchor.setTag(p);
2399 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002400 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002401 }
2402
Romain Guy9d31e9f2009-11-11 18:54:28 -08002403 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002404 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002405
Romain Guye6b8e2f2009-11-10 11:56:55 -08002406 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002407 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002408 }
2409
2410 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002411 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002412 v.post(this);
2413 }
2414
2415 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002416 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002417 }
2418
2419 public void onFocusChange(View v, boolean hasFocus) {
2420 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002421 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002422 }
Romain Guya6abce82009-11-10 02:54:41 -08002423 }
2424 }
2425
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 Workspace getWorkspace() {
2427 return mWorkspace;
2428 }
2429
Michael Jurka3c38d562011-02-18 19:22:12 -08002430 TabHost getCustomizationDrawer() {
2431 return mHomeCustomizationDrawer;
2432 }
2433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 @Override
2435 protected Dialog onCreateDialog(int id) {
2436 switch (id) {
2437 case DIALOG_CREATE_SHORTCUT:
2438 return new CreateShortcut().createDialog();
2439 case DIALOG_RENAME_FOLDER:
2440 return new RenameFolder().createDialog();
2441 }
2442
2443 return super.onCreateDialog(id);
2444 }
2445
2446 @Override
2447 protected void onPrepareDialog(int id, Dialog dialog) {
2448 switch (id) {
2449 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 break;
2451 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002452 if (mFolderInfo != null) {
2453 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2454 final CharSequence text = mFolderInfo.title;
2455 input.setText(text);
2456 input.setSelection(0, text.length());
2457 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002458 break;
2459 }
2460 }
2461
2462 void showRenameDialog(FolderInfo info) {
2463 mFolderInfo = info;
2464 mWaitingForResult = true;
2465 showDialog(DIALOG_RENAME_FOLDER);
2466 }
2467
Michael Jurka0280c3b2010-09-17 15:00:07 -07002468 private void showAddDialog(int intersectX, int intersectY) {
2469 resetAddInfo();
2470 mAddIntersectCellX = intersectX;
2471 mAddIntersectCellY = intersectY;
2472 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002473 mWaitingForResult = true;
2474 showDialog(DIALOG_CREATE_SHORTCUT);
2475 }
2476
Joe Onoratodeb98af2010-02-19 14:59:39 -08002477 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002478 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002479 Bundle bundle = new Bundle();
2480
2481 ArrayList<String> shortcutNames = new ArrayList<String>();
2482 shortcutNames.add(getString(R.string.group_applications));
2483 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2484
2485 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2486 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2487 R.drawable.ic_launcher_application));
2488 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2489
2490 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2491 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002492 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002493 pickIntent.putExtras(bundle);
2494
Joe Onoratodeb98af2010-02-19 14:59:39 -08002495 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002496 }
2497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 private class RenameFolder {
2499 private EditText mInput;
2500
2501 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002502 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2503 mInput = (EditText) layout.findViewById(R.id.folder_name);
2504
2505 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2506 builder.setIcon(0);
2507 builder.setTitle(getString(R.string.rename_folder_title));
2508 builder.setCancelable(true);
2509 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2510 public void onCancel(DialogInterface dialog) {
2511 cleanup();
2512 }
2513 });
2514 builder.setNegativeButton(getString(R.string.cancel_action),
2515 new Dialog.OnClickListener() {
2516 public void onClick(DialogInterface dialog, int which) {
2517 cleanup();
2518 }
2519 }
2520 );
2521 builder.setPositiveButton(getString(R.string.rename_action),
2522 new Dialog.OnClickListener() {
2523 public void onClick(DialogInterface dialog, int which) {
2524 changeFolderName();
2525 }
2526 }
2527 );
2528 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002529
2530 final AlertDialog dialog = builder.create();
2531 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2532 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002533 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002534 mInput.requestFocus();
2535 InputMethodManager inputManager = (InputMethodManager)
2536 getSystemService(Context.INPUT_METHOD_SERVICE);
2537 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002538 }
2539 });
2540
2541 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002542 }
2543
2544 private void changeFolderName() {
2545 final String name = mInput.getText().toString();
2546 if (!TextUtils.isEmpty(name)) {
2547 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002548 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002549 mFolderInfo.title = name;
2550 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2551
Joe Onorato9c1289c2009-08-17 11:03:03 -04002552 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002553 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002554 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002555 } else {
2556 final FolderIcon folderIcon = (FolderIcon)
2557 mWorkspace.getViewForTag(mFolderInfo);
2558 if (folderIcon != null) {
2559 folderIcon.setText(name);
2560 getWorkspace().requestLayout();
2561 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002562 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002563 mWorkspaceLoading = true;
2564 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002565 }
2566 }
2567 }
2568 cleanup();
2569 }
2570
2571 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002572 dismissDialog(DIALOG_RENAME_FOLDER);
2573 mWaitingForResult = false;
2574 mFolderInfo = null;
2575 }
2576 }
2577
Daniel Sandler843e8602010-06-07 14:59:01 -04002578 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2579 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002580 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002581 }
2582
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002583 // AllAppsView.Watcher
2584 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002585 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2586 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002587 mWorkspace.setVisibility(View.GONE);
2588 }
2589 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002590
Michael Jurkaafca9532011-02-16 14:50:35 -08002591 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002592 button.setVisibility(View.VISIBLE);
2593 button.setFocusable(true);
2594 button.setClickable(true);
2595 }
2596
2597 private void hideToolbarButton(View button) {
2598 button.setAlpha(0.0f);
2599 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2600 button.setVisibility(View.INVISIBLE);
Michael Jurkaafca9532011-02-16 14:50:35 -08002601 }
2602
2603 private void disableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002604 button.setFocusable(false);
2605 button.setClickable(false);
2606 }
2607
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002608 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002609 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002610 *
2611 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2612 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002613 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002614 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002615 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002616 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002617 final boolean showing = show;
2618 final boolean hiding = !show;
2619
2620 final int duration = show ?
2621 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2622 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2623
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002624 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002625 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002626 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002627 anim.addUpdateListener(new AnimatorUpdateListener() {
2628 public void onAnimationUpdate(ValueAnimator animation) {
2629 view.setAlpha((Float) animation.getAnimatedValue());
2630 }
2631 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002632 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002633 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002634 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002635 if (showing) showAndEnableToolbarButton(view);
2636 if (hiding) disableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002637 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002638 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002639 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002640 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002641 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002642 });
2643 seq.play(anim);
2644 } else {
2645 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002646 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002647 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002648 } else {
Michael Jurkaafca9532011-02-16 14:50:35 -08002649 disableToolbarButton(view);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002650 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002651 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002652 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002653 }
2654
2655 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002656 * Show/hide the appropriate toolbar buttons for newState.
2657 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002658 *
2659 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002660 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2661 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002662 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002663 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002664 switch (newState) {
2665 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002666 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2667 mDeleteZone.setOverlappingViews(
2668 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002669 mDeleteZone.setDragAndDropEnabled(true);
2670 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002671 break;
2672 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002673 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002674 mDeleteZone.setDragAndDropEnabled(false);
2675 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002676 break;
2677 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002678 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002679 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002680 break;
2681 }
2682 }
2683
2684 /**
2685 * Helper method for the cameraZoomIn/cameraZoomOut animations
2686 * @param view The view being animated
2687 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2688 * @param scaleFactor The scale factor used for the zoom
2689 */
2690 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2691 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002692
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002693 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002694 // Set pivotY so that at the starting zoom factor, the view is partially
2695 // visible. Modifying initialHeightFactor changes how much of the view is
2696 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002697 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002698 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002699 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002700 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002701 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002702 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002703 }
2704 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002705
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002706 /**
2707 * Zoom the camera out from the workspace to reveal 'toView'.
2708 * Assumes that the view to show is anchored at either the very top or very bottom
2709 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002710 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002711 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002712 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002713 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002714 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002715
2716 final int duration = toAllApps ?
2717 res.getInteger(R.integer.config_allAppsZoomInTime) :
2718 res.getInteger(R.integer.config_customizeZoomInTime);
2719
2720 final float scale = toAllApps ?
2721 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2722 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2723
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002724 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002725
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002726 setPivotsForZoom(toView, toState, scale);
2727
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002728 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002729 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002730 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002731 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002732 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002733
2734 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002735 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2736 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
2737 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2738 public void onAnimationUpdate(ValueAnimator animation) {
2739 final float b = (Float) animation.getAnimatedValue();
2740 final float a = 1f - b;
2741 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002742 toView.setFastScaleX(a * scale + b * 1f);
2743 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002744 }
2745 });
Adam Cohen61033d32010-11-15 18:29:44 -08002746
Adam Cohenf16e5712011-01-13 13:31:45 -08002747 if (toAllApps) {
Michael Jurkad99cf8b2011-01-16 16:53:18 -08002748 toView.setAlpha(0f);
Michael Jurka742574b2011-02-02 23:51:01 -08002749 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurkaea573482011-02-03 19:48:18 -08002750 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002751 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2752 public void onAnimationUpdate(ValueAnimator animation) {
2753 final float b = (Float) animation.getAnimatedValue();
2754 final float a = 1f - b;
2755 // don't need to invalidate because we do so above
2756 toView.setFastAlpha(a * 0f + b * 1f);
2757 }
2758 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002759 alphaAnim.start();
2760 }
2761
Michael Jurkabfadaad2011-01-31 21:35:39 -08002762 // Only use hardware layers in portrait mode, they don't give any gains in landscape
2763 if (mWorkspace.getWidth() < mWorkspace.getHeight()) {
2764 toView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
2765 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002766 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002767 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002768 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002769 // Prepare the position
2770 toView.setTranslationX(0.0f);
2771 toView.setTranslationY(0.0f);
2772 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002773 if (!toAllApps) {
2774 toView.setAlpha(1.0f);
2775 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002776 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002777 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002778 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002779 // If we don't set the final scale values here, if this animation is cancelled
2780 // it will have the wrong scale value and subsequent cameraPan animations will
2781 // not fix that
Michael Jurka3eeaea82011-01-31 15:02:36 -08002782 toView.setLayerType(View.LAYER_TYPE_NONE, null);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002783 toView.setScaleX(1.0f);
2784 toView.setScaleY(1.0f);
2785 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002786 });
2787
Chet Haaseb1254a62010-09-07 13:35:00 -07002788 AnimatorSet toolbarHideAnim = new AnimatorSet();
2789 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002790 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2791
2792 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002793 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002794
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002795 if (mStateAnimation != null) mStateAnimation.cancel();
2796 mStateAnimation = new AnimatorSet();
2797 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2798 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002799
2800 // Show the new toolbar buttons just as the main animation is ending
2801 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002802 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2803 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002804 } else {
2805 toView.setTranslationX(0.0f);
2806 toView.setTranslationY(0.0f);
2807 toView.setScaleX(1.0f);
2808 toView.setScaleY(1.0f);
2809 toView.setVisibility(View.VISIBLE);
2810 hideAndShowToolbarButtons(toState, null, null);
2811 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002812 }
2813
2814 /**
2815 * Zoom the camera back into the workspace, hiding 'fromView'.
2816 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002817 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2818 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002819 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002820 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002821 cameraZoomIn(fromState, animated, false);
2822 }
2823
2824 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002825 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002826 final boolean fromAllApps = (fromState == State.ALL_APPS);
2827
2828 int duration = fromAllApps ?
2829 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2830 res.getInteger(R.integer.config_customizeZoomOutTime);
2831
Michael Jurka742574b2011-02-02 23:51:01 -08002832 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002833 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2834 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2835
2836 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002837
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002838 mCustomizePagedView.endChoiceMode();
2839 mAllAppsPagedView.endChoiceMode();
2840
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002841 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002842
Michael Jurkad3ef3062010-11-23 16:23:58 -08002843 if (!springLoaded) {
2844 mWorkspace.unshrink(animated);
2845 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002846
2847 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002848 if (mStateAnimation != null) mStateAnimation.cancel();
2849 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002850
Michael Jurka742574b2011-02-02 23:51:01 -08002851 final float oldScaleX = fromView.getScaleX();
2852 final float oldScaleY = fromView.getScaleY();
2853
2854 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2855 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2856 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2857 public void onAnimationUpdate(ValueAnimator animation) {
2858 final float b = (Float) animation.getAnimatedValue();
2859 final float a = 1f - b;
2860 ((View)fromView.getParent()).fastInvalidate();
2861 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2862 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2863 }
2864 });
2865 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002866 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002867 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002868 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2869 public void onAnimationUpdate(ValueAnimator animation) {
2870 final float b = (Float) animation.getAnimatedValue();
2871 final float a = 1f - b;
2872 // don't need to invalidate because we do so above
2873 fromView.setFastAlpha(a * 1f + b * 0f);
2874 }
2875 });
2876
Michael Jurkacf6125c2011-01-28 15:20:01 -08002877 fromView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002878 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002879 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002880 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002881 fromView.setVisibility(View.GONE);
Michael Jurkacf6125c2011-01-28 15:20:01 -08002882 fromView.setLayerType(View.LAYER_TYPE_NONE, null);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002883 }
2884 });
2885
Chet Haaseb1254a62010-09-07 13:35:00 -07002886 AnimatorSet toolbarHideAnim = new AnimatorSet();
2887 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002888 if (!springLoaded) {
2889 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2890 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002891
Adam Cohen61033d32010-11-15 18:29:44 -08002892 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002893
2894 // Show the new toolbar buttons at the very end of the whole animation
2895 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2896 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002897 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2898 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002899 } else {
2900 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002901 if (!springLoaded) {
2902 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2903 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002904 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002905 }
2906
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002907 void showAllApps(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002908 if (mState != State.WORKSPACE) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002909 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002910 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002911
Michael Jurka79212d82010-07-30 16:36:20 -07002912 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002913 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002914 } else {
2915 mAllAppsGrid.zoom(1.0f, animated);
2916 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002917
Romain Guyc16fea72010-03-12 17:17:56 -08002918 ((View) mAllAppsGrid).setFocusable(true);
2919 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002920
Joe Onorato7c312c12009-08-13 21:36:53 -07002921 // TODO: fade these two too
2922 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002923
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002924 // Change the state *after* we've called all the transition code
2925 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002926
Winson Chung5fb63472011-02-02 17:03:37 -08002927 // Pause the auto-advance of widgets until we are out of AllApps
2928 mUserPresent = false;
2929 updateRunning();
2930
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002931 // send an accessibility event to announce the context change
2932 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2933 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002934
2935 void showWorkspace(boolean animated) {
2936 showWorkspace(animated, null);
2937 }
2938
2939 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002940 if (layout != null) {
2941 // always animated, but that's ok since we never specify a layout and
2942 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002943 mWorkspace.unshrink(layout);
2944 } else {
2945 mWorkspace.unshrink(animated);
2946 }
2947 if (mState == State.ALL_APPS) {
2948 closeAllApps(animated);
2949 } else if (mState == State.CUSTOMIZE) {
2950 hideCustomizationDrawer(animated);
2951 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002952
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002953 // Change the state *after* we've called all the transition code
2954 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002955
Winson Chung5fb63472011-02-02 17:03:37 -08002956 // Resume the auto-advance of widgets
2957 mUserPresent = true;
2958 updateRunning();
2959
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002960 // send an accessibility event to announce the context change
2961 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002962 }
2963
Michael Jurkad3ef3062010-11-23 16:23:58 -08002964 void enterSpringLoadedDragMode(CellLayout layout) {
2965 mWorkspace.enterSpringLoadedDragMode(layout);
2966 if (mState == State.ALL_APPS) {
2967 cameraZoomIn(State.ALL_APPS, true, true);
2968 mState = State.ALL_APPS_SPRING_LOADED;
2969 } else if (mState == State.CUSTOMIZE) {
2970 cameraZoomIn(State.CUSTOMIZE, true, true);
2971 mState = State.CUSTOMIZE_SPRING_LOADED;
2972 }/* else {
2973 // we're already in spring loaded mode; don't do anything
2974 }*/
2975 }
2976
2977 void exitSpringLoadedDragMode() {
2978 if (mState == State.ALL_APPS_SPRING_LOADED) {
2979 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2980 cameraZoomOut(State.ALL_APPS, true);
2981 mState = State.ALL_APPS;
2982 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2983 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2984 cameraZoomOut(State.CUSTOMIZE, true);
2985 mState = State.CUSTOMIZE;
2986 }/* else {
2987 // we're not in spring loaded mode; don't do anything
2988 }*/
2989 }
2990
Joe Onoratob2061212009-11-24 16:13:54 -05002991 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002992 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002993 * - Home from workspace
2994 * - from center screen
2995 * - from other screens
2996 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002997 * - from center screen
2998 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002999 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003000 * - from center screen
3001 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003002 * - Launch app from workspace and quit
3003 * - with back
3004 * - with home
3005 * - Launch app from all apps and quit
3006 * - with back
3007 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003008 * - Go to a screen that's not the default, then all
3009 * apps, and launch and app, and go back
3010 * - with back
3011 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003012 * - On workspace, long press power and go back
3013 * - with back
3014 * - with home
3015 * - On all apps, long press power and go back
3016 * - with back
3017 * - with home
3018 * - On workspace, power off
3019 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003020 * - Launch an app and turn off the screen while in that app
3021 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003022 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003023 * - From all apps
3024 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003025 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3026 * - From all apps
3027 * - From the center workspace
3028 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003029 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003030 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003031 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003032 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003033 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003034 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003035 } else {
3036 mAllAppsGrid.zoom(0.0f, animated);
3037 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003038 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003039 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003040 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003041 }
3042
Joe Onorato7c312c12009-08-13 21:36:53 -07003043 void lockAllApps() {
3044 // TODO
3045 }
3046
3047 void unlockAllApps() {
3048 // TODO
3049 }
3050
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003051 // Show the customization drawer (only exists in x-large configuration)
3052 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003053 if (mState != State.WORKSPACE) {
3054 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003055 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003056
3057 cameraZoomOut(State.CUSTOMIZE, animated);
3058
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003059 // Change the state *after* we've called all the transition code
3060 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003061
3062 // Pause the auto-advance of widgets until we are out of Customization drawer
3063 mUserPresent = false;
3064 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003065 }
3066
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003067 // Hide the customization drawer (only exists in x-large configuration)
3068 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003069 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003070 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003071 }
3072 }
3073
Patrick Dubroy5f445422011-02-18 14:35:21 -08003074 /**
3075 * Add an item from all apps or customize onto the given workspace screen.
3076 * If layout is null, add to the current screen.
3077 */
3078 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003079 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3080 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003081 } else {
3082 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003083 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003084 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003085
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003086 void onWorkspaceClick(CellLayout layout) {
3087 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003088 }
3089
Michael Jurka4ef207b2010-11-29 17:05:45 -08003090 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003091 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003092 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3093 ImageView button = (ImageView) findViewById(buttonId);
3094 Drawable toolbarIcon = null;
3095 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003096 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003097 // Look for the toolbar icon specified in the activity meta-data
3098 Bundle metaData = packageManager.getActivityInfo(
3099 activityName, PackageManager.GET_META_DATA).metaData;
3100 if (metaData != null) {
3101 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3102 if (iconResId != 0) {
3103 Resources res = packageManager.getResourcesForActivity(activityName);
3104 toolbarIcon = res.getDrawable(iconResId);
3105 }
3106 }
3107 } catch (NameNotFoundException e) {
3108 // Do nothing
3109 }
3110 // If we were unable to find the icon via the meta-data, use a generic one
3111 if (toolbarIcon == null) {
3112 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003113 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003114 } else {
3115 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003116 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003117 }
3118 }
3119
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003120 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003121 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003122 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003123 }
3124
Michael Jurka0423dcf2010-10-05 14:56:18 -07003125 private void updateGlobalSearchIcon() {
3126 if (LauncherApplication.isScreenXLarge()) {
3127 final SearchManager searchManager =
3128 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3129 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003130 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003131 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003132 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003133 } else {
3134 findViewById(R.id.search_button).setVisibility(View.GONE);
3135 }
3136 }
3137 }
3138
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003139 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003140 updateButtonWithDrawable(R.id.search_button, d);
3141 }
3142
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003143 private void updateVoiceSearchIcon() {
3144 if (LauncherApplication.isScreenXLarge()) {
3145 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3146 ComponentName activityName = intent.resolveActivity(getPackageManager());
3147 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003148 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003149 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3150 } else {
3151 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003152 }
3153 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003154 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003155
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003156 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003157 updateButtonWithDrawable(R.id.voice_button, d);
3158 }
3159
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003160 /**
3161 * Sets the app market icon (shown when all apps is visible on x-large screens)
3162 */
3163 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003164 if (LauncherApplication.isScreenXLarge()) {
3165 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3166 // Find the app market activity by resolving an intent.
3167 // (If multiple app markets are installed, it will return the ResolverActivity.)
3168 ComponentName activityName = intent.resolveActivity(getPackageManager());
3169 if (activityName != null) {
3170 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003171 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003172 R.id.market_button, activityName, R.drawable.app_market_generic);
3173 }
3174 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003175 }
3176
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003177 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003178 updateButtonWithDrawable(R.id.market_button, d);
3179 }
3180
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003181 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003182 * Displays the shortcut creation dialog and launches, if necessary, the
3183 * appropriate activity.
3184 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003185 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003186 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3187 DialogInterface.OnShowListener {
3188
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003189 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003190
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003191 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003192 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003193
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003194 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3195 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003196 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003197
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003198 builder.setInverseBackgroundForced(true);
3199
3200 AlertDialog dialog = builder.create();
3201 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003202 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003203 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003205 return dialog;
3206 }
3207
3208 public void onCancel(DialogInterface dialog) {
3209 mWaitingForResult = false;
3210 cleanup();
3211 }
3212
Romain Guycbb89e42009-06-08 15:52:54 -07003213 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003214 }
3215
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003216 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003217 try {
3218 dismissDialog(DIALOG_CREATE_SHORTCUT);
3219 } catch (Exception e) {
3220 // An exception is thrown if the dialog is not visible, which is fine
3221 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003222 }
3223
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003224 /**
3225 * Handle the action clicked in the "Add to home" dialog.
3226 */
3227 public void onClick(DialogInterface dialog, int which) {
3228 Resources res = getResources();
3229 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003230
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003231 switch (which) {
3232 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003233 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003234 break;
3235 }
Romain Guycbb89e42009-06-08 15:52:54 -07003236
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003237 case AddAdapter.ITEM_APPWIDGET: {
3238 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003239
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003240 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3241 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003242 // start the pick activity
3243 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3244 break;
3245 }
Romain Guycbb89e42009-06-08 15:52:54 -07003246
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003247 case AddAdapter.ITEM_LIVE_FOLDER: {
3248 // Insert extra item to handle inserting folder
3249 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003250
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003251 ArrayList<String> shortcutNames = new ArrayList<String>();
3252 shortcutNames.add(res.getString(R.string.group_folder));
3253 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003254
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003255 ArrayList<ShortcutIconResource> shortcutIcons =
3256 new ArrayList<ShortcutIconResource>();
3257 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3258 R.drawable.ic_launcher_folder));
3259 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3260
3261 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3262 pickIntent.putExtra(Intent.EXTRA_INTENT,
3263 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3264 pickIntent.putExtra(Intent.EXTRA_TITLE,
3265 getText(R.string.title_select_live_folder));
3266 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003267
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003268 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3269 break;
3270 }
3271
3272 case AddAdapter.ITEM_WALLPAPER: {
3273 startWallpaper();
3274 break;
3275 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003276 }
3277 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003278
3279 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003280 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003281 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003282 }
3283
3284 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003285 * Receives notifications when applications are added/removed.
3286 */
3287 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3288 @Override
3289 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003290 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003291 String reason = intent.getStringExtra("reason");
3292 if (!"homekey".equals(reason)) {
3293 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003294 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003295 animate = false;
3296 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003297 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003298 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003299 }
3300 }
3301
3302 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003303 * Receives notifications whenever the appwidgets are reset.
3304 */
3305 private class AppWidgetResetObserver extends ContentObserver {
3306 public AppWidgetResetObserver() {
3307 super(new Handler());
3308 }
3309
3310 @Override
3311 public void onChange(boolean selfChange) {
3312 onAppWidgetReset();
3313 }
3314 }
3315
3316 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003317 * If the activity is currently paused, signal that we need to re-run the loader
3318 * in onResume.
3319 *
3320 * This needs to be called from incoming places where resources might have been loaded
3321 * while we are paused. That is becaues the Configuration might be wrong
3322 * when we're not running, and if it comes back to what it was when we
3323 * were paused, we are not restarted.
3324 *
3325 * Implementation of the method from LauncherModel.Callbacks.
3326 *
3327 * @return true if we are currently paused. The caller might be able to
3328 * skip some work in that case since we will come back again.
3329 */
3330 public boolean setLoadOnResume() {
3331 if (mPaused) {
3332 Log.i(TAG, "setLoadOnResume");
3333 mOnResumeNeedsLoad = true;
3334 return true;
3335 } else {
3336 return false;
3337 }
3338 }
3339
3340 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003341 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003342 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003343 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003344 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003345 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003346 } else {
3347 return SCREEN_COUNT / 2;
3348 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003349 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003350
Winson Chunga12a2502010-12-20 14:41:35 -08003351 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003352 mAllAppsPagedView = view;
3353 }
3354
Joe Onorato9c1289c2009-08-17 11:03:03 -04003355 /**
3356 * Refreshes the shortcuts shown on the workspace.
3357 *
3358 * Implementation of the method from LauncherModel.Callbacks.
3359 */
3360 public void startBinding() {
3361 final Workspace workspace = mWorkspace;
3362 int count = workspace.getChildCount();
3363 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003364 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003365 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3366 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003367 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003368
Joe Onorato9c1289c2009-08-17 11:03:03 -04003369 if (DEBUG_USER_INTERFACE) {
3370 android.widget.Button finishButton = new android.widget.Button(this);
3371 finishButton.setText("Finish");
3372 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3373
3374 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3375 public void onClick(View v) {
3376 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003377 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003378 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003379 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003380
3381 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3382 // mDesktopItems -> AppWidgetInfo -> hostView).
3383 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003384 }
3385
3386 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003387 * Bind the items start-end from the list.
3388 *
3389 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003390 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003391 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3392
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003393 setLoadOnResume();
3394
Joe Onorato9c1289c2009-08-17 11:03:03 -04003395 final Workspace workspace = mWorkspace;
3396
3397 for (int i=start; i<end; i++) {
3398 final ItemInfo item = shortcuts.get(i);
3399 mDesktopItems.add(item);
3400 switch (item.itemType) {
3401 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3402 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003403 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003404 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3405 false);
3406 break;
3407 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3408 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003409 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003410 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003411 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3412 false);
3413 break;
3414 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3415 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3416 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003417 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003418 (LiveFolderInfo) item);
3419 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3420 false);
3421 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003423 }
3424
Joe Onorato9c1289c2009-08-17 11:03:03 -04003425 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003426 }
3427
Joe Onorato9c1289c2009-08-17 11:03:03 -04003428 /**
3429 * Implementation of the method from LauncherModel.Callbacks.
3430 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003431 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003432 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003433 sFolders.clear();
3434 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003435 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003436
3437 /**
3438 * Add the views for a widget to the workspace.
3439 *
3440 * Implementation of the method from LauncherModel.Callbacks.
3441 */
3442 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003443 setLoadOnResume();
3444
Daniel Sandler843e8602010-06-07 14:59:01 -04003445 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3446 if (DEBUG_WIDGETS) {
3447 Log.d(TAG, "bindAppWidget: " + item);
3448 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003449 final Workspace workspace = mWorkspace;
3450
3451 final int appWidgetId = item.appWidgetId;
3452 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003453 if (DEBUG_WIDGETS) {
3454 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3455 }
3456
Joe Onorato9c1289c2009-08-17 11:03:03 -04003457 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3458
Joe Onorato9c1289c2009-08-17 11:03:03 -04003459 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3460 item.hostView.setTag(item);
3461
3462 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3463 item.cellY, item.spanX, item.spanY, false);
3464
Adam Cohended9f8d2010-11-03 13:25:16 -07003465 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3466
Joe Onorato9c1289c2009-08-17 11:03:03 -04003467 workspace.requestLayout();
3468
3469 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003470
3471 if (DEBUG_WIDGETS) {
3472 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3473 + (SystemClock.uptimeMillis()-start) + "ms");
3474 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003475 }
3476
3477 /**
3478 * Callback saying that there aren't any more items to bind.
3479 *
3480 * Implementation of the method from LauncherModel.Callbacks.
3481 */
3482 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003483 setLoadOnResume();
3484
Joe Onorato9c1289c2009-08-17 11:03:03 -04003485 if (mSavedState != null) {
3486 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003487 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003488 }
3489
3490 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3491 if (userFolders != null) {
3492 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003493 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003494 if (info != null) {
3495 openFolder(info);
3496 }
3497 }
3498 final Folder openFolder = mWorkspace.getOpenFolder();
3499 if (openFolder != null) {
3500 openFolder.requestFocus();
3501 }
3502 }
3503
Joe Onorato9c1289c2009-08-17 11:03:03 -04003504 mSavedState = null;
3505 }
3506
3507 if (mSavedInstanceState != null) {
3508 super.onRestoreInstanceState(mSavedInstanceState);
3509 mSavedInstanceState = null;
3510 }
3511
Winson Chung32bb5e52011-01-31 14:51:56 -08003512 // Workaround a bug that occurs when rotating the device while the customization mode is
3513 // open, we trigger a new layout on all the CellLayout children.
3514 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3515 final int childCount = mWorkspace.getChildCount();
3516 for (int i = 0; i < childCount; ++i) {
3517 mWorkspace.getChildAt(i).requestLayout();
3518 }
3519 }
3520
Joe Onorato9c1289c2009-08-17 11:03:03 -04003521 mWorkspaceLoading = false;
3522 }
3523
3524 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003525 * Updates the icons on the launcher that are affected by changes to the package list
3526 * on the device.
3527 */
3528 private void updateIconsAffectedByPackageManagerChanges() {
3529 updateAppMarketIcon();
3530 updateGlobalSearchIcon();
3531 updateVoiceSearchIcon();
3532 }
3533
3534 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003535 * Add the icons for all apps.
3536 *
3537 * Implementation of the method from LauncherModel.Callbacks.
3538 */
3539 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003540 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003541 if (mCustomizePagedView != null) {
3542 mCustomizePagedView.setApps(apps);
3543 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003544 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003545 }
3546
3547 /**
3548 * A package was installed.
3549 *
3550 * Implementation of the method from LauncherModel.Callbacks.
3551 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003552 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003553 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003554 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003555 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003556 if (mCustomizePagedView != null) {
3557 mCustomizePagedView.addApps(apps);
3558 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003559 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003560 }
3561
3562 /**
3563 * A package was updated.
3564 *
3565 * Implementation of the method from LauncherModel.Callbacks.
3566 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003567 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003568 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003570 if (mWorkspace != null) {
3571 mWorkspace.updateShortcuts(apps);
3572 }
3573 if (mAllAppsGrid != null) {
3574 mAllAppsGrid.updateApps(apps);
3575 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003576 if (mCustomizePagedView != null) {
3577 mCustomizePagedView.updateApps(apps);
3578 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003579 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003580 }
3581
3582 /**
3583 * A package was uninstalled.
3584 *
3585 * Implementation of the method from LauncherModel.Callbacks.
3586 */
Joe Onorato36115782010-06-17 13:28:48 -04003587 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003588 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003589 if (permanent) {
3590 mWorkspace.removeItems(apps);
3591 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003592 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003593 if (mCustomizePagedView != null) {
3594 mCustomizePagedView.removeApps(apps);
3595 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003596 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003597 }
Joe Onoratobe386092009-11-17 17:32:16 -08003598
3599 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003600 * A number of packages were updated.
3601 */
3602 public void bindPackagesUpdated() {
3603 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003604 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003605 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003606 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003607 }
3608
Winson Chung400438b2011-01-16 17:53:48 -08003609 private int mapConfigurationOriActivityInfoOri(int configOri) {
3610 final Display d = getWindowManager().getDefaultDisplay();
3611 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3612 switch (d.getRotation()) {
3613 case Surface.ROTATION_0:
3614 case Surface.ROTATION_180:
3615 // We are currently in the same basic orientation as the natural orientation
3616 naturalOri = configOri;
3617 break;
3618 case Surface.ROTATION_90:
3619 case Surface.ROTATION_270:
3620 // We are currently in the other basic orientation to the natural orientation
3621 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3622 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3623 break;
3624 }
3625
3626 int[] oriMap = {
3627 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3628 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3629 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3630 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3631 };
3632 // Since the map starts at portrait, we need to offset if this device's natural orientation
3633 // is landscape.
3634 int indexOffset = 0;
3635 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3636 indexOffset = 1;
3637 }
3638 return oriMap[(d.getRotation() + indexOffset) % 4];
3639 }
3640 public void lockScreenOrientation() {
3641 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3642 .getConfiguration().orientation));
3643 }
3644 public void unlockScreenOrientation() {
3645 mHandler.postDelayed(new Runnable() {
3646 public void run() {
3647 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3648 }
3649 }, mRestoreScreenOrientationDelay);
3650 }
3651
Winson Chung80baf5a2010-08-09 16:03:15 -07003652 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003653 * Prints out out state for debugging.
3654 */
3655 public void dumpState() {
3656 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003657 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003658 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3659 Log.d(TAG, "mRestoring=" + mRestoring);
3660 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3661 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3662 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003663 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003664 mModel.dumpState();
3665 mAllAppsGrid.dumpState();
3666 Log.d(TAG, "END launcher2 dump state");
3667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003668}