blob: b95feaf5e6459dee5c71308bea02789f7515f0f0 [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 */
Winson Chungf7640c82011-02-28 13:47:29 -08001226 private void completeAddAppWidget(final 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) {
Winson Chungf7640c82011-02-28 13:47:29 -08001256 if (appWidgetId != -1) {
1257 // Deleting an app widget ID is a void call but writes to disk before returning
1258 // to the caller...
1259 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1260 new Thread("deleteAppWidgetId") {
1261 public void run() {
1262 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1263 }
1264 }.start();
1265 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001266 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001267 return;
1268 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001270 // Build Launcher-specific widget info and save to database
1271 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001272 launcherInfo.spanX = spanXY[0];
1273 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 LauncherModel.addItemToDatabase(this, launcherInfo,
1276 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001277 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278
1279 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001280 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001281
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001283 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001284
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001285 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001287
Michael Jurka0280c3b2010-09-17 15:00:07 -07001288 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001289 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001290
1291 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 }
1293 }
Romain Guycbb89e42009-06-08 15:52:54 -07001294
Adam Cohended9f8d2010-11-03 13:25:16 -07001295 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1296 @Override
1297 public void onReceive(Context context, Intent intent) {
1298 final String action = intent.getAction();
1299 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1300 mUserPresent = false;
1301 updateRunning();
1302 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1303 mUserPresent = true;
1304 updateRunning();
1305 }
1306 }
1307 };
1308
1309 @Override
1310 public void onAttachedToWindow() {
1311 super.onAttachedToWindow();
1312
1313 // Listen for broadcasts related to user-presence
1314 final IntentFilter filter = new IntentFilter();
1315 filter.addAction(Intent.ACTION_SCREEN_OFF);
1316 filter.addAction(Intent.ACTION_USER_PRESENT);
1317 registerReceiver(mReceiver, filter);
1318
Adam Cohend113e0c2010-11-11 10:48:05 -08001319 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001320 mVisible = true;
1321 }
1322
1323 @Override
1324 public void onDetachedFromWindow() {
1325 super.onDetachedFromWindow();
1326 mVisible = false;
1327
Adam Cohend113e0c2010-11-11 10:48:05 -08001328 if (mAttached) {
1329 unregisterReceiver(mReceiver);
1330 mAttached = false;
1331 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001332 updateRunning();
1333 }
1334
1335 public void onWindowVisibilityChanged(int visibility) {
1336 mVisible = visibility == View.VISIBLE;
1337 updateRunning();
1338 }
1339
1340 private void sendAdvanceMessage(long delay) {
1341 mHandler.removeMessages(ADVANCE_MSG);
1342 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1343 mHandler.sendMessageDelayed(msg, delay);
1344 mAutoAdvanceSentTime = System.currentTimeMillis();
1345 }
1346
1347 private void updateRunning() {
1348 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1349 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1350 mAutoAdvanceRunning = autoAdvanceRunning;
1351 if (autoAdvanceRunning) {
1352 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1353 sendAdvanceMessage(delay);
1354 } else {
1355 if (!mWidgetsToAdvance.isEmpty()) {
1356 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1357 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1358 }
1359 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001360 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001361 }
1362 }
1363 }
1364
1365 private final Handler mHandler = new Handler() {
1366 @Override
1367 public void handleMessage(Message msg) {
1368 if (msg.what == ADVANCE_MSG) {
1369 int i = 0;
1370 for (View key: mWidgetsToAdvance.keySet()) {
1371 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1372 final int delay = mAdvanceStagger * i;
1373 if (v instanceof Advanceable) {
1374 postDelayed(new Runnable() {
1375 public void run() {
1376 ((Advanceable) v).advance();
1377 }
1378 }, delay);
1379 }
1380 i++;
1381 }
1382 sendAdvanceMessage(mAdvanceInterval);
1383 }
1384 }
1385 };
1386
1387 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1388 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1389 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1390 if (v instanceof Advanceable) {
1391 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001392 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001393 updateRunning();
1394 }
1395 }
1396
1397 void removeWidgetToAutoAdvance(View hostView) {
1398 if (mWidgetsToAdvance.containsKey(hostView)) {
1399 mWidgetsToAdvance.remove(hostView);
1400 updateRunning();
1401 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001402 }
1403
Joe Onorato9c1289c2009-08-17 11:03:03 -04001404 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1405 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001406 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001407 launcherInfo.hostView = null;
1408 }
1409
Adam Cohended9f8d2010-11-03 13:25:16 -07001410 void showOutOfSpaceMessage() {
1411 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1412 }
1413
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001414 public LauncherAppWidgetHost getAppWidgetHost() {
1415 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 }
Romain Guycbb89e42009-06-08 15:52:54 -07001417
Winson Chunga9abd0e2010-10-27 17:18:37 -07001418 public LauncherModel getModel() {
1419 return mModel;
1420 }
1421
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001422 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001423 getWindow().closeAllPanels();
1424
1425 try {
1426 dismissDialog(DIALOG_CREATE_SHORTCUT);
1427 // Unlock the workspace if the dialog was showing
1428 } catch (Exception e) {
1429 // An exception is thrown if the dialog is not visible, which is fine
1430 }
1431
1432 try {
1433 dismissDialog(DIALOG_RENAME_FOLDER);
1434 // Unlock the workspace if the dialog was showing
1435 } catch (Exception e) {
1436 // An exception is thrown if the dialog is not visible, which is fine
1437 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001438
1439 // Whatever we were doing is hereby canceled.
1440 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001441 }
1442
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 @Override
1444 protected void onNewIntent(Intent intent) {
1445 super.onNewIntent(intent);
1446
1447 // Close the menu
1448 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001449 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001450 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001451
Joe Onorato14f122b2009-11-19 14:06:36 -08001452 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1453 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001454
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001455 // In all these cases, only animate if we're already on home
1456
Michael Jurka4cb37242010-08-09 21:05:32 -07001457 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001458 mWorkspace.unshrink(alreadyOnHome);
1459 }
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001460
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001461 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001462 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001463 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001464 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001465
Joe Onorato3a8820b2009-11-10 15:06:42 -08001466 final View v = getWindow().peekDecorView();
1467 if (v != null && v.getWindowToken() != null) {
1468 InputMethodManager imm = (InputMethodManager)getSystemService(
1469 INPUT_METHOD_SERVICE);
1470 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 }
1472 }
1473 }
1474
1475 @Override
1476 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1477 // Do not call super here
1478 mSavedInstanceState = savedInstanceState;
1479 }
1480
1481 @Override
1482 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001483 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484
1485 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1486 if (folders.size() > 0) {
1487 final int count = folders.size();
1488 long[] ids = new long[count];
1489 for (int i = 0; i < count; i++) {
1490 final FolderInfo info = folders.get(i).getInfo();
1491 ids[i] = info.id;
1492 }
1493 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1494 } else {
1495 super.onSaveInstanceState(outState);
1496 }
1497
Michael Jurka883f55b2010-10-21 15:47:14 -07001498 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499
Michael Jurka0280c3b2010-09-17 15:00:07 -07001500 if (mAddScreen > -1 && mWaitingForResult) {
1501 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1502 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1503 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 }
1505
1506 if (mFolderInfo != null && mWaitingForResult) {
1507 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1508 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1509 }
Michael Jurka946ad472010-07-09 18:05:18 -07001510
Winson Chung43b119d2010-12-06 16:45:05 -08001511 // Save the current AllApps drawer tab
1512 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1513 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1514 String currentTabTag = tabhost.getCurrentTabTag();
1515 if (currentTabTag != null) {
1516 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001517 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001518 }
1519 }
1520
1521 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001522 if (mHomeCustomizationDrawer != null) {
1523 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1524 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001525 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001526 }
1527 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 }
1529
1530 @Override
1531 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001533
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001535 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001537 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001539 mAppWidgetHost = null;
1540
1541 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542
1543 TextKeyListener.getInstance().release();
1544
Joe Onorato9c1289c2009-08-17 11:03:03 -04001545 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546
Joe Onorato9c1289c2009-08-17 11:03:03 -04001547 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001548
1549 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001550
Patrick Dubroyab962b72010-07-26 12:10:10 -07001551 // Some launcher layouts don't have a previous and next view
1552 if (mPreviousView != null) {
1553 dismissPreview(mPreviousView);
1554 }
1555 if (mNextView != null) {
1556 dismissPreview(mNextView);
1557 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001558
1559 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001560
1561 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1562 mWorkspace.removeAllViews();
1563 mWorkspace = null;
1564 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001565
1566 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
1568
1569 @Override
1570 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001571 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 super.startActivityForResult(intent, requestCode);
1573 }
1574
1575 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001576 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001578
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001579 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001580
Karl Rosaen138a0412009-04-23 19:00:21 -07001581 if (initialQuery == null) {
1582 // Use any text typed in the launcher as the initial query
1583 initialQuery = getTypedText();
1584 clearTypedText();
1585 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 if (appSearchData == null) {
1587 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001588 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 }
Romain Guycbb89e42009-06-08 15:52:54 -07001590
Karl Rosaen138a0412009-04-23 19:00:21 -07001591 final SearchManager searchManager =
1592 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001593 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001594 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 }
1596
1597 @Override
1598 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001599 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001600 return false;
1601 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602
1603 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001604
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1606 .setIcon(android.R.drawable.ic_menu_add)
1607 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001608 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1609 .setIcon(android.R.drawable.ic_menu_manage)
1610 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001611 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 .setIcon(android.R.drawable.ic_menu_gallery)
1613 .setAlphabeticShortcut('W');
1614 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1615 .setIcon(android.R.drawable.ic_search_category_default)
1616 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1617 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1618 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1619 .setAlphabeticShortcut('N');
1620
1621 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001622 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1623 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624
1625 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1626 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1627 .setIntent(settings);
1628
1629 return true;
1630 }
1631
1632 @Override
1633 public boolean onPrepareOptionsMenu(Menu menu) {
1634 super.onPrepareOptionsMenu(menu);
1635
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001636 // If all apps is animating, don't show the menu, because we don't know
1637 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001638 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001639 return false;
1640 }
1641
1642 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001643 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001644 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1645 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1646
1647 // Disable add if the workspace is full.
1648 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001649 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1650 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001651 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652
1653 return true;
1654 }
1655
1656 @Override
1657 public boolean onOptionsItemSelected(MenuItem item) {
1658 switch (item.getItemId()) {
1659 case MENU_ADD:
1660 addItems();
1661 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001662 case MENU_MANAGE_APPS:
1663 manageApps();
1664 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 case MENU_WALLPAPER_SETTINGS:
1666 startWallpaper();
1667 return true;
1668 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001669 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 return true;
1671 case MENU_NOTIFICATIONS:
1672 showNotifications();
1673 return true;
1674 }
1675
1676 return super.onOptionsItemSelected(item);
1677 }
Romain Guycbb89e42009-06-08 15:52:54 -07001678
Karl Rosaen138a0412009-04-23 19:00:21 -07001679 /**
1680 * Indicates that we want global search for this activity by setting the globalSearch
1681 * argument for {@link #startSearch} to true.
1682 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001684 @Override
1685 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001686 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001687 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001688 }
1689
Joe Onorato9c1289c2009-08-17 11:03:03 -04001690 public boolean isWorkspaceLocked() {
1691 return mWorkspaceLoading || mWaitingForResult;
1692 }
1693
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001695 if (LauncherApplication.isScreenXLarge()) {
1696 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001697 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001698 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001699 }
1700 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001701 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001702 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001703 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 }
1705
Michael Jurka0280c3b2010-09-17 15:00:07 -07001706 private void resetAddInfo() {
1707 mAddScreen = -1;
1708 mAddIntersectCellX = -1;
1709 mAddIntersectCellY = -1;
1710 mAddDropPosition = null;
1711 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001712
Winson Chung55cef262010-10-28 14:14:18 -07001713 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001714 resetAddInfo();
1715 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001716 mAddDropPosition = position;
1717
Michael Jurkaaf442092010-06-10 17:01:57 -07001718 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001719 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1720 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001721 }
1722
Winson Chung7ad01412010-09-27 11:33:03 -07001723 private void manageApps() {
1724 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1725 }
1726
Michael Jurkaaf442092010-06-10 17:01:57 -07001727 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001728 // TODO: catch bad widget exception when sent
1729 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001730 // TODO: Is this log message meaningful?
1731 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001732 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001733 }
1734
Winson Chung55cef262010-10-28 14:14:18 -07001735 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001736 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737
Bjorn Bringert7984c942009-12-09 15:38:25 +00001738 if (appWidget.configure != null) {
1739 // Launch over to configure widget, if needed
1740 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1741 intent.setComponent(appWidget.configure);
1742 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001743 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001744 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1745 intent.putExtra(
1746 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1747 info.mimeType);
1748
1749 final String mimeType = info.mimeType;
1750 final ClipData clipData = (ClipData) info.configurationData;
1751 final ClipDescription clipDesc = clipData.getDescription();
1752 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1753 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001754 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001755 final CharSequence stringData = item.getText();
1756 final Uri uriData = item.getUri();
1757 final Intent intentData = item.getIntent();
1758 final String key =
1759 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1760 if (uriData != null) {
1761 intent.putExtra(key, uriData);
1762 } else if (intentData != null) {
1763 intent.putExtra(key, intentData);
1764 } else if (stringData != null) {
1765 intent.putExtra(key, stringData);
1766 }
1767 break;
1768 }
1769 }
1770 }
Winson Chung55cef262010-10-28 14:14:18 -07001771 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001772
Romain Guy8e633c52010-03-04 12:51:36 -08001773 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001775 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001776 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 }
1778 }
Romain Guycbb89e42009-06-08 15:52:54 -07001779
Michael Jurka0280c3b2010-09-17 15:00:07 -07001780 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1781 resetAddInfo();
1782 mAddScreen = screen;
1783 mAddDropPosition = position;
1784
1785 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1786 createShortcutIntent.setComponent(componentName);
1787 processShortcut(createShortcutIntent);
1788 }
1789
Joe Onoratodeb98af2010-02-19 14:59:39 -08001790 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001791 // Handle case where user selected "Applications"
1792 String applicationName = getResources().getString(R.string.group_applications);
1793 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001794
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001795 if (applicationName != null && applicationName.equals(shortcutName)) {
1796 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1797 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001798
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001799 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1800 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001801 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001802 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001803 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001804 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001805 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 }
1807
Winson Chung24ab2f12010-09-16 14:10:47 -07001808 void processWallpaper(Intent intent) {
1809 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1810 }
1811
Michael Jurka0280c3b2010-09-17 15:00:07 -07001812 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1813 resetAddInfo();
1814 mAddScreen = screen;
1815 mAddDropPosition = position;
1816
1817 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1818 createFolderIntent.setComponent(componentName);
1819
1820 addLiveFolder(createFolderIntent);
1821 }
1822
1823 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001824 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001825 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001826 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001827
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001828 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001829 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001830 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001831 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 }
1834
Michael Jurka0280c3b2010-09-17 15:00:07 -07001835 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 UserFolderInfo folderInfo = new UserFolderInfo();
1837 folderInfo.title = getText(R.string.folder_name);
1838
Michael Jurka0280c3b2010-09-17 15:00:07 -07001839 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1840 final int[] cellXY = mTmpAddItemCellCoordinates;
1841 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1842 showOutOfSpaceMessage();
1843 return;
1844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845
1846 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001847 LauncherModel.addItemToDatabase(this, folderInfo,
1848 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001849 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001850 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851
1852 // Create the view
1853 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001854 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1855 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001856 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 }
Romain Guycbb89e42009-06-08 15:52:54 -07001858
Joe Onorato9c1289c2009-08-17 11:03:03 -04001859 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001860 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001861 }
1862
Michael Jurka28750fb2010-09-24 17:43:49 -07001863 private void completeAddLiveFolder(
1864 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001865 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1866 final int[] cellXY = mTmpAddItemCellCoordinates;
1867 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1868 showOutOfSpaceMessage();
1869 return;
1870 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871
Michael Jurka0280c3b2010-09-17 15:00:07 -07001872 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873
1874 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001876 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001877 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 }
1879 }
1880
1881 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001882 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883
1884 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1885 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1886
1887 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 Intent.ShortcutIconResource iconResource = null;
1889
1890 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1891 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1892 try {
1893 iconResource = (Intent.ShortcutIconResource) extra;
1894 final PackageManager packageManager = context.getPackageManager();
1895 Resources resources = packageManager.getResourcesForApplication(
1896 iconResource.packageName);
1897 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1898 icon = resources.getDrawable(id);
1899 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001900 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 }
1902 }
1903
1904 if (icon == null) {
1905 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1906 }
1907
1908 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001909 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 info.title = name;
1911 info.iconResource = iconResource;
1912 info.uri = data.getData();
1913 info.baseIntent = baseIntent;
1914 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1915 LiveFolders.DISPLAY_MODE_GRID);
1916
1917 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001918 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001919 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920
1921 return info;
1922 }
1923
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 private void showNotifications() {
1925 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1926 if (statusBar != null) {
1927 statusBar.expand();
1928 }
1929 }
1930
1931 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001932 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001934 Intent chooser = Intent.createChooser(pickWallpaper,
1935 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001936 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1937 // Removed in Eclair MR1
1938// WallpaperManager wm = (WallpaperManager)
1939// getSystemService(Context.WALLPAPER_SERVICE);
1940// WallpaperInfo wi = wm.getWallpaperInfo();
1941// if (wi != null && wi.getSettingsActivity() != null) {
1942// LabeledIntent li = new LabeledIntent(getPackageName(),
1943// R.string.configure_wallpaper, 0);
1944// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1945// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1946// }
Mike Clerona0618e42009-10-22 13:55:21 -07001947 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
1949
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001950 /**
1951 * Registers various content observers. The current implementation registers
1952 * only a favorites observer to keep track of the favorites applications.
1953 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001954 private void registerContentObservers() {
1955 ContentResolver resolver = getContentResolver();
1956 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1957 true, mWidgetObserver);
1958 }
1959
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 @Override
1961 public boolean dispatchKeyEvent(KeyEvent event) {
1962 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1963 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001965 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001966 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001967 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001968 dumpState();
1969 return true;
1970 }
1971 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001972 }
1973 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1974 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001975 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 return true;
1977 }
1978 }
1979
1980 return super.dispatchKeyEvent(event);
1981 }
1982
Joe Onorato88ec0992009-11-19 13:16:06 -08001983 @Override
1984 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001985 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1986 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001987 } else {
1988 closeFolder();
1989 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001990 // Some launcher layouts don't have a previous and next view
1991 if (mPreviousView != null) {
1992 dismissPreview(mPreviousView);
1993 dismissPreview(mNextView);
1994 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001995 }
1996
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 private void closeFolder() {
1998 Folder folder = mWorkspace.getOpenFolder();
1999 if (folder != null) {
2000 closeFolder(folder);
2001 }
2002 }
2003
2004 void closeFolder(Folder folder) {
2005 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08002006 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07002008 CellLayout cl = (CellLayout) parent;
2009 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05002010 if (folder instanceof DropTarget) {
2011 // Live folders aren't DropTargets.
2012 mDragController.removeDropTarget((DropTarget)folder);
2013 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 }
2015 folder.onClose();
2016 }
2017
2018 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002019 * Re-listen when widgets are reset.
2020 */
2021 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002022 if (mAppWidgetHost != null) {
2023 mAppWidgetHost.startListening();
2024 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002025 }
2026
2027 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002028 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2029 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002031 private void unbindDesktopItems() {
2032 for (ItemInfo item: mDesktopItems) {
2033 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002035 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 /**
2039 * Launches the intent referred by the clicked shortcut.
2040 *
2041 * @param v The view representing the clicked shortcut.
2042 */
2043 public void onClick(View v) {
2044 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002045 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002047 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002048 int[] pos = new int[2];
2049 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002050 intent.setSourceBounds(new Rect(pos[0], pos[1],
2051 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08002052 boolean success = startActivitySafely(intent, tag);
2053
2054 if (success && v instanceof BubbleTextView) {
2055 mWaitingForResume = (BubbleTextView) v;
2056 mWaitingForResume.setStayPressed(true);
2057 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 } else if (tag instanceof FolderInfo) {
2059 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002060 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002061 if (mState == State.ALL_APPS) {
2062 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002063 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002064 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002065 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 }
2067 }
2068
Michael Jurka0e260592010-06-30 17:07:39 -07002069 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002070 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002071 // clicking anywhere on the workspace causes the customization drawer to slide down
2072 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002073 return false;
2074 }
2075
Michael Jurkaaf442092010-06-10 17:01:57 -07002076 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002077 * Event handler for the search button
2078 *
2079 * @param v The view that was clicked.
2080 */
2081 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002082 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002083 // Use a custom animation for launching search
2084 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002085 }
2086
2087 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002088 * Event handler for the voice button
2089 *
2090 * @param v The view that was clicked.
2091 */
2092 public void onClickVoiceButton(View v) {
2093 startVoiceSearch();
2094 }
2095
2096 private void startVoiceSearch() {
2097 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2098 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2099 startActivity(intent);
2100 }
2101
2102 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002103 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002104 * enters home screen customization mode.
2105 *
2106 * @param v The view that was clicked.
2107 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002108 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002109 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002110 }
2111
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002112 /**
2113 * Event handler for the "grid" button that appears on the home screen, which
2114 * enters all apps mode.
2115 *
2116 * @param v The view that was clicked.
2117 */
2118 public void onClickAllAppsButton(View v) {
2119 showAllApps(true);
2120 }
2121
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002122 public void onClickAppMarketButton(View v) {
2123 if (mAppMarketIntent != null) {
2124 startActivitySafely(mAppMarketIntent, "app market");
2125 }
2126 }
2127
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002128 void startApplicationDetailsActivity(ComponentName componentName) {
2129 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002130 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2131 Uri.fromParts("package", packageName, null));
2132 startActivity(intent);
2133 }
2134
Patrick Dubroy5539af72010-09-07 15:22:01 -07002135 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2136 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2137 // System applications cannot be installed. For now, show a toast explaining that.
2138 // We may give them the option of disabling apps this way.
2139 int messageId = R.string.uninstall_system_app_text;
2140 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2141 } else {
2142 String packageName = appInfo.componentName.getPackageName();
2143 String className = appInfo.componentName.getClassName();
2144 Intent intent = new Intent(
2145 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2146 startActivity(intent);
2147 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002148 }
2149
Michael Jurkaddd62e92011-02-16 17:49:14 -08002150 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002151 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2152 try {
2153 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002154 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155 } catch (ActivityNotFoundException e) {
2156 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002157 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 } catch (SecurityException e) {
2159 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002160 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002162 "or use the exported attribute for this activity. "
2163 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002165 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002167
Romain Guy8e633c52010-03-04 12:51:36 -08002168 void startActivityForResultSafely(Intent intent, int requestCode) {
2169 try {
2170 startActivityForResult(intent, requestCode);
2171 } catch (ActivityNotFoundException e) {
2172 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2173 } catch (SecurityException e) {
2174 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2175 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2176 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2177 "or use the exported attribute for this activity.", e);
2178 }
2179 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180
2181 private void handleFolderClick(FolderInfo folderInfo) {
2182 if (!folderInfo.opened) {
2183 // Close any open folder
2184 closeFolder();
2185 // Open the requested folder
2186 openFolder(folderInfo);
2187 } else {
2188 // Find the open folder...
2189 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2190 int folderScreen;
2191 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002192 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 // .. and close it
2194 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002195 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 // Close any folder open on the current screen
2197 closeFolder();
2198 // Pull the folder onto this screen
2199 openFolder(folderInfo);
2200 }
2201 }
2202 }
2203 }
2204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002206 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 * is animated relative to the specified View. If the View is null, no animation
2208 * is played.
2209 *
2210 * @param folderInfo The FolderInfo describing the folder to open.
2211 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002212 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213 Folder openFolder;
2214
2215 if (folderInfo instanceof UserFolderInfo) {
2216 openFolder = UserFolder.fromXml(this);
2217 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002218 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 } else {
2220 return;
2221 }
2222
Joe Onorato00acb122009-08-04 16:04:30 -04002223 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 openFolder.setLauncher(this);
2225
2226 openFolder.bind(folderInfo);
2227 folderInfo.opened = true;
2228
Winson Chungaafa03c2010-06-11 17:34:16 -07002229 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 openFolder.onOpen();
2232 }
2233
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002235 switch (v.getId()) {
2236 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002237 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002238 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2239 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002240 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002241 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002242 return true;
2243 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002244 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002245 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2246 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002247 showPreviews(v);
2248 }
2249 return true;
2250 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002251 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002252 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2253 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2254 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002255 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002256 return true;
2257 }
2258
Joe Onorato9c1289c2009-08-17 11:03:03 -04002259 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002260 return false;
2261 }
2262
2263 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002264 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002265 }
2266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267
Michael Jurka0280c3b2010-09-17 15:00:07 -07002268 resetAddInfo();
2269 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002270 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002271 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002272 return true;
2273 }
2274
Michael Jurka0280c3b2010-09-17 15:00:07 -07002275 final View itemUnderLongClick = longClickCellInfo.cell;
2276
Winson Chung304dcde2011-01-07 11:17:23 -08002277 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002278 if (itemUnderLongClick == null) {
2279 // User long pressed on empty space
2280 mWorkspace.setAllowLongPress(false);
2281 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2282 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002283 if (LauncherApplication.isScreenXLarge()) {
2284 addItems();
2285 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002286 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002287 }
2288 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002289 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002290 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002291 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2292 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002293 mAddIntersectCellX = longClickCellInfo.cellX;
2294 mAddIntersectCellY = longClickCellInfo.cellY;
2295 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002296 }
2297 }
2298 }
2299 return true;
2300 }
2301
Romain Guye6b8e2f2009-11-10 11:56:55 -08002302 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002303 private void dismissPreview(final View v) {
2304 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002305 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002306 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2307 public void onDismiss() {
2308 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2309 int count = group.getChildCount();
2310 for (int i = 0; i < count; i++) {
2311 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2312 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002313 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2314 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2315
2316 v.setTag(R.id.workspace, null);
2317 v.setTag(R.id.icon, null);
2318 window.setOnDismissListener(null);
2319 }
2320 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002321 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002322 }
2323 v.setTag(null);
2324 }
2325
Romain Guyf8e6a802009-12-07 17:48:02 -08002326 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002327 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002328 }
2329
Romain Guya6abce82009-11-10 02:54:41 -08002330 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002331 final Resources resources = getResources();
2332 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002333
Romain Guya6abce82009-11-10 02:54:41 -08002334 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002335
Romain Guy9d31e9f2009-11-11 18:54:28 -08002336 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002337
Romain Guyf8e6a802009-12-07 17:48:02 -08002338 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002339 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002340 int extraW = (int) ((r.left + r.right) * max);
2341 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002342
2343 int aW = cell.getWidth() - extraW;
2344 float w = aW / max;
2345
2346 int width = cell.getWidth();
2347 int height = cell.getHeight();
2348 int x = cell.getLeftPadding();
2349 int y = cell.getTopPadding();
2350 width -= (x + cell.getRightPadding());
2351 height -= (y + cell.getBottomPadding());
2352
2353 float scale = w / width;
2354
2355 int count = end - start;
2356
2357 final float sWidth = width * scale;
2358 float sHeight = height * scale;
2359
Romain Guye6b8e2f2009-11-10 11:56:55 -08002360 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002361
Romain Guye6b8e2f2009-11-10 11:56:55 -08002362 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2363 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002364
2365 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002366 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002367 cell = (CellLayout) workspace.getChildAt(i);
2368
Romain Guyf8e6a802009-12-07 17:48:02 -08002369 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002370 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002371
2372 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002373 c.scale(scale, scale);
2374 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002375 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002376
Romain Guy9d31e9f2009-11-11 18:54:28 -08002377 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002378 image.setImageBitmap(bitmap);
2379 image.setTag(i);
2380 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002381 image.setOnFocusChangeListener(handler);
2382 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002383 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002384
2385 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002386 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2387
Winson Chungaafa03c2010-06-11 17:34:16 -07002388 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002389 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002390
2391 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002392 p.setContentView(preview);
2393 p.setWidth((int) (sWidth * count + extraW));
2394 p.setHeight((int) (sHeight + extraH));
2395 p.setAnimationStyle(R.style.AnimationPreview);
2396 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002397 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002398 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002399 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002400
Romain Guye6b8e2f2009-11-10 11:56:55 -08002401 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2402 public void onDismiss() {
2403 dismissPreview(anchor);
2404 }
2405 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002406
2407 anchor.setTag(p);
2408 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002409 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002410 }
2411
Romain Guy9d31e9f2009-11-11 18:54:28 -08002412 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002413 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002414
Romain Guye6b8e2f2009-11-10 11:56:55 -08002415 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002416 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002417 }
2418
2419 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002420 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002421 v.post(this);
2422 }
2423
2424 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002425 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002426 }
2427
2428 public void onFocusChange(View v, boolean hasFocus) {
2429 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002430 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002431 }
Romain Guya6abce82009-11-10 02:54:41 -08002432 }
2433 }
2434
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 Workspace getWorkspace() {
2436 return mWorkspace;
2437 }
2438
Michael Jurka3c38d562011-02-18 19:22:12 -08002439 TabHost getCustomizationDrawer() {
2440 return mHomeCustomizationDrawer;
2441 }
2442
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 @Override
2444 protected Dialog onCreateDialog(int id) {
2445 switch (id) {
2446 case DIALOG_CREATE_SHORTCUT:
2447 return new CreateShortcut().createDialog();
2448 case DIALOG_RENAME_FOLDER:
2449 return new RenameFolder().createDialog();
2450 }
2451
2452 return super.onCreateDialog(id);
2453 }
2454
2455 @Override
2456 protected void onPrepareDialog(int id, Dialog dialog) {
2457 switch (id) {
2458 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 break;
2460 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002461 if (mFolderInfo != null) {
2462 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2463 final CharSequence text = mFolderInfo.title;
2464 input.setText(text);
2465 input.setSelection(0, text.length());
2466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002467 break;
2468 }
2469 }
2470
2471 void showRenameDialog(FolderInfo info) {
2472 mFolderInfo = info;
2473 mWaitingForResult = true;
2474 showDialog(DIALOG_RENAME_FOLDER);
2475 }
2476
Michael Jurka0280c3b2010-09-17 15:00:07 -07002477 private void showAddDialog(int intersectX, int intersectY) {
2478 resetAddInfo();
2479 mAddIntersectCellX = intersectX;
2480 mAddIntersectCellY = intersectY;
2481 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 mWaitingForResult = true;
2483 showDialog(DIALOG_CREATE_SHORTCUT);
2484 }
2485
Joe Onoratodeb98af2010-02-19 14:59:39 -08002486 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002487 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002488 Bundle bundle = new Bundle();
2489
2490 ArrayList<String> shortcutNames = new ArrayList<String>();
2491 shortcutNames.add(getString(R.string.group_applications));
2492 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2493
2494 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2495 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2496 R.drawable.ic_launcher_application));
2497 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2498
2499 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2500 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002501 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002502 pickIntent.putExtras(bundle);
2503
Joe Onoratodeb98af2010-02-19 14:59:39 -08002504 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002505 }
2506
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002507 private class RenameFolder {
2508 private EditText mInput;
2509
2510 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2512 mInput = (EditText) layout.findViewById(R.id.folder_name);
2513
2514 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2515 builder.setIcon(0);
2516 builder.setTitle(getString(R.string.rename_folder_title));
2517 builder.setCancelable(true);
2518 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2519 public void onCancel(DialogInterface dialog) {
2520 cleanup();
2521 }
2522 });
2523 builder.setNegativeButton(getString(R.string.cancel_action),
2524 new Dialog.OnClickListener() {
2525 public void onClick(DialogInterface dialog, int which) {
2526 cleanup();
2527 }
2528 }
2529 );
2530 builder.setPositiveButton(getString(R.string.rename_action),
2531 new Dialog.OnClickListener() {
2532 public void onClick(DialogInterface dialog, int which) {
2533 changeFolderName();
2534 }
2535 }
2536 );
2537 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002538
2539 final AlertDialog dialog = builder.create();
2540 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2541 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002542 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002543 mInput.requestFocus();
2544 InputMethodManager inputManager = (InputMethodManager)
2545 getSystemService(Context.INPUT_METHOD_SERVICE);
2546 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002547 }
2548 });
2549
2550 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 }
2552
2553 private void changeFolderName() {
2554 final String name = mInput.getText().toString();
2555 if (!TextUtils.isEmpty(name)) {
2556 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002557 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 mFolderInfo.title = name;
2559 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2560
Joe Onorato9c1289c2009-08-17 11:03:03 -04002561 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002562 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002563 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002564 } else {
2565 final FolderIcon folderIcon = (FolderIcon)
2566 mWorkspace.getViewForTag(mFolderInfo);
2567 if (folderIcon != null) {
2568 folderIcon.setText(name);
2569 getWorkspace().requestLayout();
2570 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002571 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002572 mWorkspaceLoading = true;
2573 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002574 }
2575 }
2576 }
2577 cleanup();
2578 }
2579
2580 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002581 dismissDialog(DIALOG_RENAME_FOLDER);
2582 mWaitingForResult = false;
2583 mFolderInfo = null;
2584 }
2585 }
2586
Daniel Sandler843e8602010-06-07 14:59:01 -04002587 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2588 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002589 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002590 }
2591
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002592 // AllAppsView.Watcher
2593 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002594 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2595 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002596 mWorkspace.setVisibility(View.GONE);
2597 }
2598 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002599
Michael Jurkaafca9532011-02-16 14:50:35 -08002600 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002601 button.setVisibility(View.VISIBLE);
2602 button.setFocusable(true);
2603 button.setClickable(true);
2604 }
2605
2606 private void hideToolbarButton(View button) {
2607 button.setAlpha(0.0f);
2608 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2609 button.setVisibility(View.INVISIBLE);
Michael Jurkaafca9532011-02-16 14:50:35 -08002610 }
2611
2612 private void disableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002613 button.setFocusable(false);
2614 button.setClickable(false);
2615 }
2616
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002617 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002618 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002619 *
2620 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2621 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002622 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002623 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002624 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002625 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002626 final boolean showing = show;
2627 final boolean hiding = !show;
2628
2629 final int duration = show ?
2630 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2631 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2632
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002633 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002634 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002635 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002636 anim.addUpdateListener(new AnimatorUpdateListener() {
2637 public void onAnimationUpdate(ValueAnimator animation) {
2638 view.setAlpha((Float) animation.getAnimatedValue());
2639 }
2640 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002641 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002642 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002643 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002644 if (showing) showAndEnableToolbarButton(view);
2645 if (hiding) disableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002646 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002647 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002648 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002649 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002650 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002651 });
2652 seq.play(anim);
2653 } else {
2654 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002655 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002656 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002657 } else {
Michael Jurkaafca9532011-02-16 14:50:35 -08002658 disableToolbarButton(view);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002659 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002660 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002661 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002662 }
2663
2664 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002665 * Show/hide the appropriate toolbar buttons for newState.
2666 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002667 *
2668 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002669 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2670 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002671 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002672 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002673 switch (newState) {
2674 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002675 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2676 mDeleteZone.setOverlappingViews(
2677 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002678 mDeleteZone.setDragAndDropEnabled(true);
2679 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002680 break;
2681 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002682 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002683 mDeleteZone.setDragAndDropEnabled(false);
2684 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002685 break;
2686 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002687 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002688 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002689 break;
2690 }
2691 }
2692
2693 /**
2694 * Helper method for the cameraZoomIn/cameraZoomOut animations
2695 * @param view The view being animated
2696 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2697 * @param scaleFactor The scale factor used for the zoom
2698 */
2699 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2700 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002701
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002702 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002703 // Set pivotY so that at the starting zoom factor, the view is partially
2704 // visible. Modifying initialHeightFactor changes how much of the view is
2705 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002706 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002707 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002708 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002709 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002710 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002711 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002712 }
2713 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002714
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002715 /**
2716 * Zoom the camera out from the workspace to reveal 'toView'.
2717 * Assumes that the view to show is anchored at either the very top or very bottom
2718 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002719 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002720 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002721 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002722 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002723 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002724
2725 final int duration = toAllApps ?
2726 res.getInteger(R.integer.config_allAppsZoomInTime) :
2727 res.getInteger(R.integer.config_customizeZoomInTime);
2728
2729 final float scale = toAllApps ?
2730 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2731 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2732
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002733 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002734
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002735 setPivotsForZoom(toView, toState, scale);
2736
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002737 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002738 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002739 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002740 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002741 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002742
2743 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002744 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2745 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
2746 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2747 public void onAnimationUpdate(ValueAnimator animation) {
2748 final float b = (Float) animation.getAnimatedValue();
2749 final float a = 1f - b;
2750 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002751 toView.setFastScaleX(a * scale + b * 1f);
2752 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002753 }
2754 });
Adam Cohen61033d32010-11-15 18:29:44 -08002755
Adam Cohenf16e5712011-01-13 13:31:45 -08002756 if (toAllApps) {
Michael Jurkad99cf8b2011-01-16 16:53:18 -08002757 toView.setAlpha(0f);
Michael Jurka742574b2011-02-02 23:51:01 -08002758 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurkaea573482011-02-03 19:48:18 -08002759 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002760 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2761 public void onAnimationUpdate(ValueAnimator animation) {
2762 final float b = (Float) animation.getAnimatedValue();
2763 final float a = 1f - b;
2764 // don't need to invalidate because we do so above
2765 toView.setFastAlpha(a * 0f + b * 1f);
2766 }
2767 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002768 alphaAnim.start();
2769 }
2770
Michael Jurkabfadaad2011-01-31 21:35:39 -08002771 // Only use hardware layers in portrait mode, they don't give any gains in landscape
2772 if (mWorkspace.getWidth() < mWorkspace.getHeight()) {
2773 toView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
2774 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002775 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002776 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002777 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002778 // Prepare the position
2779 toView.setTranslationX(0.0f);
2780 toView.setTranslationY(0.0f);
2781 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002782 if (!toAllApps) {
2783 toView.setAlpha(1.0f);
2784 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002785 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002786 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002787 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002788 // If we don't set the final scale values here, if this animation is cancelled
2789 // it will have the wrong scale value and subsequent cameraPan animations will
2790 // not fix that
Michael Jurka3eeaea82011-01-31 15:02:36 -08002791 toView.setLayerType(View.LAYER_TYPE_NONE, null);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002792 toView.setScaleX(1.0f);
2793 toView.setScaleY(1.0f);
2794 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002795 });
2796
Chet Haaseb1254a62010-09-07 13:35:00 -07002797 AnimatorSet toolbarHideAnim = new AnimatorSet();
2798 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002799 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2800
2801 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002802 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002803
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002804 if (mStateAnimation != null) mStateAnimation.cancel();
2805 mStateAnimation = new AnimatorSet();
2806 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2807 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002808
2809 // Show the new toolbar buttons just as the main animation is ending
2810 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002811 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2812 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002813 } else {
2814 toView.setTranslationX(0.0f);
2815 toView.setTranslationY(0.0f);
2816 toView.setScaleX(1.0f);
2817 toView.setScaleY(1.0f);
2818 toView.setVisibility(View.VISIBLE);
2819 hideAndShowToolbarButtons(toState, null, null);
2820 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002821 }
2822
2823 /**
2824 * Zoom the camera back into the workspace, hiding 'fromView'.
2825 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002826 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2827 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002828 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002829 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002830 cameraZoomIn(fromState, animated, false);
2831 }
2832
2833 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002834 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002835 final boolean fromAllApps = (fromState == State.ALL_APPS);
2836
2837 int duration = fromAllApps ?
2838 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2839 res.getInteger(R.integer.config_customizeZoomOutTime);
2840
Michael Jurka742574b2011-02-02 23:51:01 -08002841 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002842 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2843 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2844
2845 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002846
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002847 mCustomizePagedView.endChoiceMode();
2848 mAllAppsPagedView.endChoiceMode();
2849
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002850 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002851
Michael Jurkad3ef3062010-11-23 16:23:58 -08002852 if (!springLoaded) {
2853 mWorkspace.unshrink(animated);
2854 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002855
2856 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002857 if (mStateAnimation != null) mStateAnimation.cancel();
2858 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002859
Michael Jurka742574b2011-02-02 23:51:01 -08002860 final float oldScaleX = fromView.getScaleX();
2861 final float oldScaleY = fromView.getScaleY();
2862
2863 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2864 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2865 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2866 public void onAnimationUpdate(ValueAnimator animation) {
2867 final float b = (Float) animation.getAnimatedValue();
2868 final float a = 1f - b;
2869 ((View)fromView.getParent()).fastInvalidate();
2870 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2871 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2872 }
2873 });
2874 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002875 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002876 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002877 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2878 public void onAnimationUpdate(ValueAnimator animation) {
2879 final float b = (Float) animation.getAnimatedValue();
2880 final float a = 1f - b;
2881 // don't need to invalidate because we do so above
2882 fromView.setFastAlpha(a * 1f + b * 0f);
2883 }
2884 });
2885
Michael Jurkacf6125c2011-01-28 15:20:01 -08002886 fromView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002887 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002888 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002889 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002890 fromView.setVisibility(View.GONE);
Michael Jurkacf6125c2011-01-28 15:20:01 -08002891 fromView.setLayerType(View.LAYER_TYPE_NONE, null);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002892 }
2893 });
2894
Chet Haaseb1254a62010-09-07 13:35:00 -07002895 AnimatorSet toolbarHideAnim = new AnimatorSet();
2896 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002897 if (!springLoaded) {
2898 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2899 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002900
Adam Cohen61033d32010-11-15 18:29:44 -08002901 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002902
2903 // Show the new toolbar buttons at the very end of the whole animation
2904 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2905 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002906 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2907 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002908 } else {
2909 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002910 if (!springLoaded) {
2911 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2912 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002913 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002914 }
2915
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002916 void showAllApps(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002917 if (mState != State.WORKSPACE) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002918 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002919 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002920
Michael Jurka79212d82010-07-30 16:36:20 -07002921 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002922 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002923 } else {
2924 mAllAppsGrid.zoom(1.0f, animated);
2925 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002926
Romain Guyc16fea72010-03-12 17:17:56 -08002927 ((View) mAllAppsGrid).setFocusable(true);
2928 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002929
Joe Onorato7c312c12009-08-13 21:36:53 -07002930 // TODO: fade these two too
2931 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002932
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002933 // Change the state *after* we've called all the transition code
2934 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002935
Winson Chung5fb63472011-02-02 17:03:37 -08002936 // Pause the auto-advance of widgets until we are out of AllApps
2937 mUserPresent = false;
2938 updateRunning();
2939
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002940 // send an accessibility event to announce the context change
2941 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2942 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002943
2944 void showWorkspace(boolean animated) {
2945 showWorkspace(animated, null);
2946 }
2947
2948 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002949 if (layout != null) {
2950 // always animated, but that's ok since we never specify a layout and
2951 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002952 mWorkspace.unshrink(layout);
2953 } else {
2954 mWorkspace.unshrink(animated);
2955 }
2956 if (mState == State.ALL_APPS) {
2957 closeAllApps(animated);
2958 } else if (mState == State.CUSTOMIZE) {
2959 hideCustomizationDrawer(animated);
2960 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002961
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002962 // Change the state *after* we've called all the transition code
2963 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002964
Winson Chung5fb63472011-02-02 17:03:37 -08002965 // Resume the auto-advance of widgets
2966 mUserPresent = true;
2967 updateRunning();
2968
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002969 // send an accessibility event to announce the context change
2970 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002971 }
2972
Michael Jurkad3ef3062010-11-23 16:23:58 -08002973 void enterSpringLoadedDragMode(CellLayout layout) {
2974 mWorkspace.enterSpringLoadedDragMode(layout);
2975 if (mState == State.ALL_APPS) {
2976 cameraZoomIn(State.ALL_APPS, true, true);
2977 mState = State.ALL_APPS_SPRING_LOADED;
2978 } else if (mState == State.CUSTOMIZE) {
2979 cameraZoomIn(State.CUSTOMIZE, true, true);
2980 mState = State.CUSTOMIZE_SPRING_LOADED;
2981 }/* else {
2982 // we're already in spring loaded mode; don't do anything
2983 }*/
2984 }
2985
2986 void exitSpringLoadedDragMode() {
2987 if (mState == State.ALL_APPS_SPRING_LOADED) {
2988 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2989 cameraZoomOut(State.ALL_APPS, true);
2990 mState = State.ALL_APPS;
2991 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2992 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2993 cameraZoomOut(State.CUSTOMIZE, true);
2994 mState = State.CUSTOMIZE;
2995 }/* else {
2996 // we're not in spring loaded mode; don't do anything
2997 }*/
2998 }
2999
Joe Onoratob2061212009-11-24 16:13:54 -05003000 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08003001 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05003002 * - Home from workspace
3003 * - from center screen
3004 * - from other screens
3005 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003006 * - from center screen
3007 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003008 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003009 * - from center screen
3010 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003011 * - Launch app from workspace and quit
3012 * - with back
3013 * - with home
3014 * - Launch app from all apps and quit
3015 * - with back
3016 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003017 * - Go to a screen that's not the default, then all
3018 * apps, and launch and app, and go back
3019 * - with back
3020 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003021 * - On workspace, long press power and go back
3022 * - with back
3023 * - with home
3024 * - On all apps, long press power and go back
3025 * - with back
3026 * - with home
3027 * - On workspace, power off
3028 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003029 * - Launch an app and turn off the screen while in that app
3030 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003031 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003032 * - From all apps
3033 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003034 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3035 * - From all apps
3036 * - From the center workspace
3037 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003038 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003039 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003040 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003041 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003042 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003043 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003044 } else {
3045 mAllAppsGrid.zoom(0.0f, animated);
3046 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003047 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003048 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003049 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003050 }
3051
Joe Onorato7c312c12009-08-13 21:36:53 -07003052 void lockAllApps() {
3053 // TODO
3054 }
3055
3056 void unlockAllApps() {
3057 // TODO
3058 }
3059
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003060 // Show the customization drawer (only exists in x-large configuration)
3061 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003062 if (mState != State.WORKSPACE) {
3063 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003064 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003065
3066 cameraZoomOut(State.CUSTOMIZE, animated);
3067
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003068 // Change the state *after* we've called all the transition code
3069 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003070
3071 // Pause the auto-advance of widgets until we are out of Customization drawer
3072 mUserPresent = false;
3073 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003074 }
3075
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003076 // Hide the customization drawer (only exists in x-large configuration)
3077 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003078 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003079 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003080 }
3081 }
3082
Patrick Dubroy5f445422011-02-18 14:35:21 -08003083 /**
3084 * Add an item from all apps or customize onto the given workspace screen.
3085 * If layout is null, add to the current screen.
3086 */
3087 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003088 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3089 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003090 } else {
3091 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003092 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003093 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003094
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003095 void onWorkspaceClick(CellLayout layout) {
3096 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003097 }
3098
Michael Jurka4ef207b2010-11-29 17:05:45 -08003099 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003100 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003101 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3102 ImageView button = (ImageView) findViewById(buttonId);
3103 Drawable toolbarIcon = null;
3104 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003105 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003106 // Look for the toolbar icon specified in the activity meta-data
3107 Bundle metaData = packageManager.getActivityInfo(
3108 activityName, PackageManager.GET_META_DATA).metaData;
3109 if (metaData != null) {
3110 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3111 if (iconResId != 0) {
3112 Resources res = packageManager.getResourcesForActivity(activityName);
3113 toolbarIcon = res.getDrawable(iconResId);
3114 }
3115 }
3116 } catch (NameNotFoundException e) {
3117 // Do nothing
3118 }
3119 // If we were unable to find the icon via the meta-data, use a generic one
3120 if (toolbarIcon == null) {
3121 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003122 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003123 } else {
3124 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003125 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003126 }
3127 }
3128
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003129 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003130 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003131 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003132 }
3133
Michael Jurka0423dcf2010-10-05 14:56:18 -07003134 private void updateGlobalSearchIcon() {
3135 if (LauncherApplication.isScreenXLarge()) {
3136 final SearchManager searchManager =
3137 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3138 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003139 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003140 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003141 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003142 } else {
3143 findViewById(R.id.search_button).setVisibility(View.GONE);
3144 }
3145 }
3146 }
3147
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003148 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003149 updateButtonWithDrawable(R.id.search_button, d);
3150 }
3151
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003152 private void updateVoiceSearchIcon() {
3153 if (LauncherApplication.isScreenXLarge()) {
3154 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3155 ComponentName activityName = intent.resolveActivity(getPackageManager());
3156 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003157 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003158 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3159 } else {
3160 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003161 }
3162 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003163 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003164
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003165 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003166 updateButtonWithDrawable(R.id.voice_button, d);
3167 }
3168
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003169 /**
3170 * Sets the app market icon (shown when all apps is visible on x-large screens)
3171 */
3172 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003173 if (LauncherApplication.isScreenXLarge()) {
3174 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3175 // Find the app market activity by resolving an intent.
3176 // (If multiple app markets are installed, it will return the ResolverActivity.)
3177 ComponentName activityName = intent.resolveActivity(getPackageManager());
3178 if (activityName != null) {
3179 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003180 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003181 R.id.market_button, activityName, R.drawable.app_market_generic);
3182 }
3183 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003184 }
3185
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003186 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003187 updateButtonWithDrawable(R.id.market_button, d);
3188 }
3189
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003190 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003191 * Displays the shortcut creation dialog and launches, if necessary, the
3192 * appropriate activity.
3193 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003194 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003195 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3196 DialogInterface.OnShowListener {
3197
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003198 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003199
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003200 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003201 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003203 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3204 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003205 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003207 builder.setInverseBackgroundForced(true);
3208
3209 AlertDialog dialog = builder.create();
3210 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003211 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003212 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003214 return dialog;
3215 }
3216
3217 public void onCancel(DialogInterface dialog) {
3218 mWaitingForResult = false;
3219 cleanup();
3220 }
3221
Romain Guycbb89e42009-06-08 15:52:54 -07003222 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003223 }
3224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003226 try {
3227 dismissDialog(DIALOG_CREATE_SHORTCUT);
3228 } catch (Exception e) {
3229 // An exception is thrown if the dialog is not visible, which is fine
3230 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003231 }
3232
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003233 /**
3234 * Handle the action clicked in the "Add to home" dialog.
3235 */
3236 public void onClick(DialogInterface dialog, int which) {
3237 Resources res = getResources();
3238 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003239
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003240 switch (which) {
3241 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003242 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003243 break;
3244 }
Romain Guycbb89e42009-06-08 15:52:54 -07003245
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003246 case AddAdapter.ITEM_APPWIDGET: {
3247 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003248
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003249 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3250 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003251 // start the pick activity
3252 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3253 break;
3254 }
Romain Guycbb89e42009-06-08 15:52:54 -07003255
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003256 case AddAdapter.ITEM_LIVE_FOLDER: {
3257 // Insert extra item to handle inserting folder
3258 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003259
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003260 ArrayList<String> shortcutNames = new ArrayList<String>();
3261 shortcutNames.add(res.getString(R.string.group_folder));
3262 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003263
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003264 ArrayList<ShortcutIconResource> shortcutIcons =
3265 new ArrayList<ShortcutIconResource>();
3266 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3267 R.drawable.ic_launcher_folder));
3268 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3269
3270 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3271 pickIntent.putExtra(Intent.EXTRA_INTENT,
3272 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3273 pickIntent.putExtra(Intent.EXTRA_TITLE,
3274 getText(R.string.title_select_live_folder));
3275 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003276
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003277 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3278 break;
3279 }
3280
3281 case AddAdapter.ITEM_WALLPAPER: {
3282 startWallpaper();
3283 break;
3284 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003285 }
3286 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003287
3288 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003289 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003290 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003291 }
3292
3293 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003294 * Receives notifications when applications are added/removed.
3295 */
3296 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3297 @Override
3298 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003299 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003300 String reason = intent.getStringExtra("reason");
3301 if (!"homekey".equals(reason)) {
3302 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003303 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003304 animate = false;
3305 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003306 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003307 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003308 }
3309 }
3310
3311 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003312 * Receives notifications whenever the appwidgets are reset.
3313 */
3314 private class AppWidgetResetObserver extends ContentObserver {
3315 public AppWidgetResetObserver() {
3316 super(new Handler());
3317 }
3318
3319 @Override
3320 public void onChange(boolean selfChange) {
3321 onAppWidgetReset();
3322 }
3323 }
3324
3325 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003326 * If the activity is currently paused, signal that we need to re-run the loader
3327 * in onResume.
3328 *
3329 * This needs to be called from incoming places where resources might have been loaded
3330 * while we are paused. That is becaues the Configuration might be wrong
3331 * when we're not running, and if it comes back to what it was when we
3332 * were paused, we are not restarted.
3333 *
3334 * Implementation of the method from LauncherModel.Callbacks.
3335 *
3336 * @return true if we are currently paused. The caller might be able to
3337 * skip some work in that case since we will come back again.
3338 */
3339 public boolean setLoadOnResume() {
3340 if (mPaused) {
3341 Log.i(TAG, "setLoadOnResume");
3342 mOnResumeNeedsLoad = true;
3343 return true;
3344 } else {
3345 return false;
3346 }
3347 }
3348
3349 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003350 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003351 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003352 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003353 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003354 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003355 } else {
3356 return SCREEN_COUNT / 2;
3357 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003358 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003359
Winson Chunga12a2502010-12-20 14:41:35 -08003360 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003361 mAllAppsPagedView = view;
3362 }
3363
Joe Onorato9c1289c2009-08-17 11:03:03 -04003364 /**
3365 * Refreshes the shortcuts shown on the workspace.
3366 *
3367 * Implementation of the method from LauncherModel.Callbacks.
3368 */
3369 public void startBinding() {
3370 final Workspace workspace = mWorkspace;
3371 int count = workspace.getChildCount();
3372 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003373 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003374 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3375 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003376 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003377
Joe Onorato9c1289c2009-08-17 11:03:03 -04003378 if (DEBUG_USER_INTERFACE) {
3379 android.widget.Button finishButton = new android.widget.Button(this);
3380 finishButton.setText("Finish");
3381 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3382
3383 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3384 public void onClick(View v) {
3385 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003386 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003387 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003388 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003389
3390 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3391 // mDesktopItems -> AppWidgetInfo -> hostView).
3392 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003393 }
3394
3395 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003396 * Bind the items start-end from the list.
3397 *
3398 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003399 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003400 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3401
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003402 setLoadOnResume();
3403
Joe Onorato9c1289c2009-08-17 11:03:03 -04003404 final Workspace workspace = mWorkspace;
3405
3406 for (int i=start; i<end; i++) {
3407 final ItemInfo item = shortcuts.get(i);
3408 mDesktopItems.add(item);
3409 switch (item.itemType) {
3410 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3411 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003412 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003413 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3414 false);
3415 break;
3416 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3417 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003418 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003419 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003420 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3421 false);
3422 break;
3423 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3424 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3425 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003426 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003427 (LiveFolderInfo) item);
3428 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3429 false);
3430 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003431 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003432 }
3433
Joe Onorato9c1289c2009-08-17 11:03:03 -04003434 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003435 }
3436
Joe Onorato9c1289c2009-08-17 11:03:03 -04003437 /**
3438 * Implementation of the method from LauncherModel.Callbacks.
3439 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003440 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003441 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003442 sFolders.clear();
3443 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003444 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003445
3446 /**
3447 * Add the views for a widget to the workspace.
3448 *
3449 * Implementation of the method from LauncherModel.Callbacks.
3450 */
3451 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003452 setLoadOnResume();
3453
Daniel Sandler843e8602010-06-07 14:59:01 -04003454 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3455 if (DEBUG_WIDGETS) {
3456 Log.d(TAG, "bindAppWidget: " + item);
3457 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003458 final Workspace workspace = mWorkspace;
3459
3460 final int appWidgetId = item.appWidgetId;
3461 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003462 if (DEBUG_WIDGETS) {
3463 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3464 }
3465
Joe Onorato9c1289c2009-08-17 11:03:03 -04003466 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3467
Joe Onorato9c1289c2009-08-17 11:03:03 -04003468 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3469 item.hostView.setTag(item);
3470
3471 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3472 item.cellY, item.spanX, item.spanY, false);
3473
Adam Cohended9f8d2010-11-03 13:25:16 -07003474 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3475
Joe Onorato9c1289c2009-08-17 11:03:03 -04003476 workspace.requestLayout();
3477
3478 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003479
3480 if (DEBUG_WIDGETS) {
3481 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3482 + (SystemClock.uptimeMillis()-start) + "ms");
3483 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003484 }
3485
3486 /**
3487 * Callback saying that there aren't any more items to bind.
3488 *
3489 * Implementation of the method from LauncherModel.Callbacks.
3490 */
3491 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003492 setLoadOnResume();
3493
Joe Onorato9c1289c2009-08-17 11:03:03 -04003494 if (mSavedState != null) {
3495 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003496 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003497 }
3498
3499 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3500 if (userFolders != null) {
3501 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003502 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003503 if (info != null) {
3504 openFolder(info);
3505 }
3506 }
3507 final Folder openFolder = mWorkspace.getOpenFolder();
3508 if (openFolder != null) {
3509 openFolder.requestFocus();
3510 }
3511 }
3512
Joe Onorato9c1289c2009-08-17 11:03:03 -04003513 mSavedState = null;
3514 }
3515
3516 if (mSavedInstanceState != null) {
3517 super.onRestoreInstanceState(mSavedInstanceState);
3518 mSavedInstanceState = null;
3519 }
3520
Winson Chung32bb5e52011-01-31 14:51:56 -08003521 // Workaround a bug that occurs when rotating the device while the customization mode is
3522 // open, we trigger a new layout on all the CellLayout children.
3523 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3524 final int childCount = mWorkspace.getChildCount();
3525 for (int i = 0; i < childCount; ++i) {
3526 mWorkspace.getChildAt(i).requestLayout();
3527 }
3528 }
3529
Joe Onorato9c1289c2009-08-17 11:03:03 -04003530 mWorkspaceLoading = false;
3531 }
3532
3533 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003534 * Updates the icons on the launcher that are affected by changes to the package list
3535 * on the device.
3536 */
3537 private void updateIconsAffectedByPackageManagerChanges() {
3538 updateAppMarketIcon();
3539 updateGlobalSearchIcon();
3540 updateVoiceSearchIcon();
3541 }
3542
3543 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003544 * Add the icons for all apps.
3545 *
3546 * Implementation of the method from LauncherModel.Callbacks.
3547 */
3548 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003549 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003550 if (mCustomizePagedView != null) {
3551 mCustomizePagedView.setApps(apps);
3552 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003553 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003554 }
3555
3556 /**
3557 * A package was installed.
3558 *
3559 * Implementation of the method from LauncherModel.Callbacks.
3560 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003561 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003562 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003563 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003564 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003565 if (mCustomizePagedView != null) {
3566 mCustomizePagedView.addApps(apps);
3567 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003568 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 }
3570
3571 /**
3572 * A package was updated.
3573 *
3574 * Implementation of the method from LauncherModel.Callbacks.
3575 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003576 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003577 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003578 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003579 if (mWorkspace != null) {
3580 mWorkspace.updateShortcuts(apps);
3581 }
3582 if (mAllAppsGrid != null) {
3583 mAllAppsGrid.updateApps(apps);
3584 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003585 if (mCustomizePagedView != null) {
3586 mCustomizePagedView.updateApps(apps);
3587 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003588 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589 }
3590
3591 /**
3592 * A package was uninstalled.
3593 *
3594 * Implementation of the method from LauncherModel.Callbacks.
3595 */
Joe Onorato36115782010-06-17 13:28:48 -04003596 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003597 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003598 if (permanent) {
3599 mWorkspace.removeItems(apps);
3600 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003601 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003602 if (mCustomizePagedView != null) {
3603 mCustomizePagedView.removeApps(apps);
3604 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003605 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 }
Joe Onoratobe386092009-11-17 17:32:16 -08003607
3608 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003609 * A number of packages were updated.
3610 */
3611 public void bindPackagesUpdated() {
3612 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003613 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003614 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003615 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003616 }
3617
Winson Chung400438b2011-01-16 17:53:48 -08003618 private int mapConfigurationOriActivityInfoOri(int configOri) {
3619 final Display d = getWindowManager().getDefaultDisplay();
3620 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3621 switch (d.getRotation()) {
3622 case Surface.ROTATION_0:
3623 case Surface.ROTATION_180:
3624 // We are currently in the same basic orientation as the natural orientation
3625 naturalOri = configOri;
3626 break;
3627 case Surface.ROTATION_90:
3628 case Surface.ROTATION_270:
3629 // We are currently in the other basic orientation to the natural orientation
3630 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3631 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3632 break;
3633 }
3634
3635 int[] oriMap = {
3636 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3637 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3638 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3639 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3640 };
3641 // Since the map starts at portrait, we need to offset if this device's natural orientation
3642 // is landscape.
3643 int indexOffset = 0;
3644 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3645 indexOffset = 1;
3646 }
3647 return oriMap[(d.getRotation() + indexOffset) % 4];
3648 }
3649 public void lockScreenOrientation() {
3650 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3651 .getConfiguration().orientation));
3652 }
3653 public void unlockScreenOrientation() {
3654 mHandler.postDelayed(new Runnable() {
3655 public void run() {
3656 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3657 }
3658 }, mRestoreScreenOrientationDelay);
3659 }
3660
Winson Chung80baf5a2010-08-09 16:03:15 -07003661 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003662 * Prints out out state for debugging.
3663 */
3664 public void dumpState() {
3665 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003666 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003667 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3668 Log.d(TAG, "mRestoring=" + mRestoring);
3669 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3670 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3671 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003672 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003673 mModel.dumpState();
3674 mAllAppsGrid.dumpState();
3675 Log.d(TAG, "END launcher2 dump state");
3676 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003677}