blob: 64cbee4cea47b24709413f13d5175aeeca433be8 [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;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070029import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080030import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070031import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.app.Dialog;
34import android.app.SearchManager;
35import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070036import android.appwidget.AppWidgetManager;
37import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080039import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070040import android.content.ClipData;
41import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040042import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080043import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.Context;
45import android.content.DialogInterface;
46import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080047import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080048import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080051import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080052import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070054import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040055import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080056import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080058import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080060import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070061import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040062import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070063import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020065import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080066import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070067import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040069import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080070import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070071import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070072import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080073import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.text.Selection;
75import android.text.SpannableStringBuilder;
76import android.text.TextUtils;
77import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070078import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080079import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080080import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.KeyEvent;
82import android.view.LayoutInflater;
83import android.view.Menu;
84import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070085import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080086import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.View;
88import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070089import android.view.WindowManager;
Michael Jurkae0f5a612011-02-07 16:45:41 -080090import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080091import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080092import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080093import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070095import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070097import android.widget.ImageView;
98import android.widget.LinearLayout;
99import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700100import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -0800101import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -0700102import android.widget.TextView;
103import android.widget.Toast;
Adam Cohen860f4c52011-01-27 20:16:13 -0800104import android.widget.TabHost.OnTabChangeListener;
105import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700106
Michael Jurka742574b2011-02-02 23:51:01 -0800107import java.io.DataInputStream;
108import java.io.DataOutputStream;
109import java.io.FileNotFoundException;
110import java.io.IOException;
111import java.util.ArrayList;
112import java.util.HashMap;
113import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800114
Adam Cohended9f8d2010-11-03 13:25:16 -0700115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116/**
117 * Default launcher application.
118 */
Michael Jurka946ad472010-07-09 18:05:18 -0700119public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700120 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
121 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800122 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700123 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400126 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400127 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800130 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700133 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
134 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
136 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700137 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
139 private static final int REQUEST_CREATE_SHORTCUT = 1;
140 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700141 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 private static final int REQUEST_PICK_APPLICATION = 6;
143 private static final int REQUEST_PICK_SHORTCUT = 7;
144 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700146 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
148 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
149
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800150 static final int SCREEN_COUNT = 5;
151 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Joe Onorato7c312c12009-08-13 21:36:53 -0700153 static final int DIALOG_CREATE_SHORTCUT = 1;
154 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Romain Guy98d01652009-06-30 16:21:04 -0700156 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
158 // Type: int
159 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700160 // Type: int
161 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: long
163 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
164 // Type: int
165 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
166 // Type: int
167 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
168 // Type: int
169 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 // Type: boolean
171 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
172 // Type: long
173 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
174
Winson Chung80baf5a2010-08-09 16:03:15 -0700175 // tags for the customization tabs
176 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700177 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700178 private static final String SHORTCUTS_TAG = "shortcuts";
179 private static final String WALLPAPERS_TAG = "wallpapers";
180
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700181 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
182
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700183 /** The different states that Launcher can be in. */
Michael Jurkaafca9532011-02-16 14:50:35 -0800184 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800185 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700186 private State mState = State.WORKSPACE;
187 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800192 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800194 private final BroadcastReceiver mCloseSystemDialogsReceiver
195 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800196 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private LayoutInflater mInflater;
199
Joe Onorato00acb122009-08-04 16:04:30 -0400200 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700202
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 private AppWidgetManager mAppWidgetManager;
204 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700205
Michael Jurka0280c3b2010-09-17 15:00:07 -0700206 private int mAddScreen = -1;
207 private int mAddIntersectCellX = -1;
208 private int mAddIntersectCellY = -1;
209 private int[] mAddDropPosition;
210 private int[] mTmpAddItemCellCoordinates = new int[2];
211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 private FolderInfo mFolderInfo;
213
Joe Onorato7c312c12009-08-13 21:36:53 -0700214 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700216 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700217 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700218 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700219
Michael Jurka838a4ca2011-02-07 13:33:06 -0800220 private View mButtonCluster;
221 private View mAllAppsButton;
222 private View mDivider;
223 private View mConfigureButton;
224
Winson Chunga12a2502010-12-20 14:41:35 -0800225 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700226 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 private Bundle mSavedState;
229
230 private SpannableStringBuilder mDefaultKeySsb = null;
231
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232 private boolean mWorkspaceLoading = true;
233
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800234 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 private boolean mRestoring;
236 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700237 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238
239 private Bundle mSavedInstanceState;
240
Joe Onorato9c1289c2009-08-17 11:03:03 -0400241 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800242 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800243 private boolean mUserPresent = true;
244 private boolean mVisible = false;
245 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700247 private static LocaleConfiguration sLocaleConfiguration = null;
248
Romain Guy84f296c2009-11-04 15:00:44 -0800249 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700250
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700251 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700252
Patrick Dubroy94f78a52011-02-28 17:39:16 -0800253 // The "signpost" images along the bottom of the screen (only in some layouts)
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
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800285 private static ArrayList<PendingAddArguments> sPendingAddList
286 = new ArrayList<PendingAddArguments>();
287
288 private static class PendingAddArguments {
289 int requestCode;
290 Intent intent;
291 int screen;
292 int cellX;
293 int cellY;
294 }
295
Winson Chunga12a2502010-12-20 14:41:35 -0800296 private CustomizationType getCustomizeFilterForTabTag(String tag) {
297 if (tag.equals(WIDGETS_TAG)) {
298 return CustomizationType.WidgetCustomization;
299 } else if (tag.equals(APPLICATIONS_TAG)) {
300 return CustomizationType.ApplicationCustomization;
301 } else if (tag.equals(WALLPAPERS_TAG)) {
302 return CustomizePagedView.CustomizationType.WallpaperCustomization;
303 } else if (tag.equals(SHORTCUTS_TAG)) {
304 return CustomizePagedView.CustomizationType.ShortcutCustomization;
305 }
306 return CustomizationType.WidgetCustomization;
307 }
308
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 @Override
310 protected void onCreate(Bundle savedInstanceState) {
311 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700312
Winson Chungaafa03c2010-06-11 17:34:16 -0700313 if (LauncherApplication.isInPlaceRotationEnabled()) {
314 // hide the status bar (temporary until we get the status bar design figured out)
315 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
316 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
317 }
318
Joe Onorato0589f0f2010-02-08 13:44:00 -0800319 LauncherApplication app = ((LauncherApplication)getApplication());
320 mModel = app.setLauncher(this);
321 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400322 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700324
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700325 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700326 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
327 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700328
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200330 android.os.Debug.startMethodTracing(
331 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 }
333
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400334 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800337 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700338 if (mHomeCustomizationDrawer != null) {
339 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700340
Winson Chung80baf5a2010-08-09 16:03:15 -0700341 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700342 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
Michael Jurka9c6fbed2011-03-02 17:41:34 -0800343 R.layout.customization_drawer_tab_contents, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700344 TabContentFactory contentFactory = new TabContentFactory() {
345 public View createTabContent(String tag) {
346 return mCustomizePagedView;
347 }
348 };
349
Winson Chung49767ae2010-11-29 14:48:30 -0800350
351 TextView tabView;
352 TabWidget tabWidget = (TabWidget)
353 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
354
355 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
356 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700357 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800358 .setIndicator(tabView).setContent(contentFactory));
359 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
360 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700361 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800362 .setIndicator(tabView).setContent(contentFactory));
363 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
364 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700365 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800366 .setIndicator(tabView).setContent(contentFactory));
367 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
368 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700369 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800370 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700371 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
372 public void onTabChanged(String tabId) {
Winson Chunga12a2502010-12-20 14:41:35 -0800373 final CustomizePagedView.CustomizationType newType =
374 getCustomizeFilterForTabTag(tabId);
375 if (newType != mCustomizePagedView.getCustomizationFilter()) {
376 // animate the changing of the tab content by fading pages in and out
377 final Resources res = getResources();
378 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
379 final float alpha = mCustomizePagedView.getAlpha();
380 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
381 "alpha", alpha, 0.0f);
382 alphaAnim.setDuration(duration);
383 alphaAnim.addListener(new AnimatorListenerAdapter() {
384 @Override
385 public void onAnimationEnd(Animator animation) {
Winson Chunga12a2502010-12-20 14:41:35 -0800386 mCustomizePagedView.setCustomizationFilter(newType);
Winson Chung80baf5a2010-08-09 16:03:15 -0700387
Winson Chunga12a2502010-12-20 14:41:35 -0800388 final float alpha = mCustomizePagedView.getAlpha();
389 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
390 mCustomizePagedView, "alpha", alpha, 1.0f);
391 alphaAnim.setDuration(duration);
392 alphaAnim.start();
393 }
394 });
395 alphaAnim.start();
396 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700397 }
398 });
Michael Jurka946ad472010-07-09 18:05:18 -0700399 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 setupViews();
401
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800402 registerContentObservers();
403
Joe Onorato7c312c12009-08-13 21:36:53 -0700404 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 mSavedState = savedInstanceState;
407 restoreState(mSavedState);
408
Winson Chunga12a2502010-12-20 14:41:35 -0800409 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800410 if (mCustomizePagedView != null) {
411 mCustomizePagedView.update();
412 }
Winson Chunga12a2502010-12-20 14:41:35 -0800413
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 if (PROFILE_STARTUP) {
415 android.os.Debug.stopMethodTracing();
416 }
417
418 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400419 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 }
421
422 // For handling default keys
423 mDefaultKeySsb = new SpannableStringBuilder();
424 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800425
426 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
427 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800428
429 // If we have a saved version of these external icons, we load them up immediately
430 if (LauncherApplication.isScreenXLarge()) {
Winson Chungc6d20002011-03-02 11:57:29 -0800431 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
432 updateIconsAffectedByPackageManagerChanges();
Michael Jurka4ef207b2010-11-29 17:05:45 -0800433 }
Michael Jurka1c3f8ec2011-03-07 16:41:21 -0800434 if (sGlobalSearchIcon != null) {
435 updateGlobalSearchIcon(sGlobalSearchIcon);
436 }
437 if (sVoiceSearchIcon != null) {
438 updateVoiceSearchIcon(sVoiceSearchIcon);
439 }
440 if (sAppMarketIcon != null) {
441 updateAppMarketIcon(sAppMarketIcon);
442 }
Michael Jurka4ef207b2010-11-29 17:05:45 -0800443 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 }
Romain Guycbb89e42009-06-08 15:52:54 -0700445
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800446 @Override
447 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
448 super.dispatchPopulateAccessibilityEvent(event);
449
450 // we want to take over text population so it is context dependent
451 event.getText().clear();
452 if (mState == State.ALL_APPS) {
453 event.getText().add(getString(R.string.all_apps_button_label));
454 } else if (mState == State.WORKSPACE) {
455 event.getText().add(getString(R.string.all_apps_home_button_label));
456 }
457
458 return true;
459 }
460
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700462 if (sLocaleConfiguration == null) {
463 new AsyncTask<Void, Void, LocaleConfiguration>() {
464 @Override
465 protected LocaleConfiguration doInBackground(Void... unused) {
466 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
467 readConfiguration(Launcher.this, localeConfiguration);
468 return localeConfiguration;
469 }
470
471 @Override
472 protected void onPostExecute(LocaleConfiguration result) {
473 sLocaleConfiguration = result;
474 checkForLocaleChange(); // recursive, but now with a locale configuration
475 }
476 }.execute();
477 return;
478 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700479
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 final Configuration configuration = getResources().getConfiguration();
481
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700482 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 final String locale = configuration.locale.toString();
484
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700485 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 final int mcc = configuration.mcc;
487
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700488 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489 final int mnc = configuration.mnc;
490
Romain Guy84f296c2009-11-04 15:00:44 -0800491 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492
Romain Guy84f296c2009-11-04 15:00:44 -0800493 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700494 sLocaleConfiguration.locale = locale;
495 sLocaleConfiguration.mcc = mcc;
496 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700497
Joe Onorato0589f0f2010-02-08 13:44:00 -0800498 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400499 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700500
501 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
502 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700503 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700504 public void run() {
505 writeConfiguration(Launcher.this, localeConfiguration);
506 }
507 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700508 }
509 }
510
511 private static class LocaleConfiguration {
512 public String locale;
513 public int mcc = -1;
514 public int mnc = -1;
515 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700516
Romain Guy98d01652009-06-30 16:21:04 -0700517 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
518 DataInputStream in = null;
519 try {
520 in = new DataInputStream(context.openFileInput(PREFERENCES));
521 configuration.locale = in.readUTF();
522 configuration.mcc = in.readInt();
523 configuration.mnc = in.readInt();
524 } catch (FileNotFoundException e) {
525 // Ignore
526 } catch (IOException e) {
527 // Ignore
528 } finally {
529 if (in != null) {
530 try {
531 in.close();
532 } catch (IOException e) {
533 // Ignore
534 }
535 }
536 }
537 }
538
539 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
540 DataOutputStream out = null;
541 try {
542 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
543 out.writeUTF(configuration.locale);
544 out.writeInt(configuration.mcc);
545 out.writeInt(configuration.mnc);
546 out.flush();
547 } catch (FileNotFoundException e) {
548 // Ignore
549 } catch (IOException e) {
550 //noinspection ResultOfMethodCallIgnored
551 context.getFileStreamPath(PREFERENCES).delete();
552 } finally {
553 if (out != null) {
554 try {
555 out.close();
556 } catch (IOException e) {
557 // Ignore
558 }
559 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 }
561 }
562
563 static int getScreen() {
564 synchronized (sLock) {
565 return sScreen;
566 }
567 }
568
569 static void setScreen(int screen) {
570 synchronized (sLock) {
571 sScreen = screen;
572 }
573 }
574
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400575 // Note: This doesn't do all the client-id magic that BrowserProvider does
576 // in Browser. (http://b/2425179)
577 private Uri getDefaultBrowserUri() {
578 String url = getString(R.string.default_browser_url);
579 if (url.indexOf("{CID}") != -1) {
580 url = url.replace("{CID}", "android-google");
581 }
582 return Uri.parse(url);
583 }
584
585 // Load the Intent templates from arrays.xml to populate the hotseats. For
586 // each Intent, if it resolves to a single app, use that as the launch
587 // intent & use that app's label as the contentDescription. Otherwise,
588 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400589 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400590 if (mHotseatConfig == null) {
591 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
592 if (mHotseatConfig.length > 0) {
593 mHotseats = new Intent[mHotseatConfig.length];
594 mHotseatLabels = new CharSequence[mHotseatConfig.length];
595 mHotseatIcons = new Drawable[mHotseatConfig.length];
596 } else {
597 mHotseats = null;
598 mHotseatIcons = null;
599 mHotseatLabels = null;
600 }
601
602 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
603 for (int i=0; i<mHotseatConfig.length; i++) {
604 // load icon for this slot; currently unrelated to the actual activity
605 try {
606 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
607 } catch (ArrayIndexOutOfBoundsException ex) {
608 Log.w(TAG, "Missing hotseat_icons array item #" + i);
609 mHotseatIcons[i] = null;
610 }
611 }
612 hotseatIconDrawables.recycle();
613 }
614
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400615 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400616 for (int i=0; i<mHotseatConfig.length; i++) {
617 Intent intent = null;
618 if (mHotseatConfig[i].equals("*BROWSER*")) {
619 // magic value meaning "launch user's default web browser"
620 // replace it with a generic web request so we can see if there is indeed a default
621 String defaultUri = getString(R.string.default_browser_url);
622 intent = new Intent(
623 Intent.ACTION_VIEW,
624 ((defaultUri != null)
625 ? Uri.parse(defaultUri)
626 : getDefaultBrowserUri())
627 ).addCategory(Intent.CATEGORY_BROWSABLE);
628 // note: if the user launches this without a default set, she
629 // will always be taken to the default URL above; this is
630 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700631 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400632 // URL is unavoidably sent to the chosen app.
633 } else {
634 try {
635 intent = Intent.parseUri(mHotseatConfig[i], 0);
636 } catch (java.net.URISyntaxException ex) {
637 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
638 // bogus; leave intent=null
639 }
640 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700641
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400642 if (intent == null) {
643 mHotseats[i] = null;
644 mHotseatLabels[i] = getText(R.string.activity_not_found);
645 continue;
646 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400647
648 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700649 Log.d(TAG, "loadHotseats: hotseat " + i
650 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400651 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400652 + "]");
653 }
654
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400655 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
656 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700657 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400658 Log.d(TAG, "Best match for intent: " + bestMatch);
659 Log.d(TAG, "All matches: ");
660 for (ResolveInfo ri : allMatches) {
661 Log.d(TAG, " --> " + ri);
662 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400663 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400664 // did this resolve to a single app, or the resolver?
665 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700666 // can't find any activity to handle this. let's leave the
667 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400668 // to launch.
669 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400670
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400671 // set accessibility text to "Not installed"
672 mHotseatLabels[i] = getText(R.string.activity_not_found);
673 } else {
674 boolean found = false;
675 for (ResolveInfo ri : allMatches) {
676 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
677 && bestMatch.activityInfo.applicationInfo.packageName
678 .equals(ri.activityInfo.applicationInfo.packageName)) {
679 found = true;
680 break;
681 }
682 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700683
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400684 if (!found) {
685 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
686 // the bestMatch is probably the ResolveActivity, meaning the
687 // user has not yet selected a default
688 // so: we'll keep the original intent for now
689 mHotseats[i] = intent;
690
691 // set the accessibility text to "Select shortcut"
692 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
693 } else {
694 // we have an app!
695 // now reconstruct the intent to launch it through the front
696 // door
697 ComponentName com = new ComponentName(
698 bestMatch.activityInfo.applicationInfo.packageName,
699 bestMatch.activityInfo.name);
700 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
701
702 // load the app label for accessibility
703 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
704 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400705 }
706
707 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700708 Log.d(TAG, "loadHotseats: hotseat " + i
709 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400710 + ((mHotseats[i] == null)
711 ? "null"
712 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400713 + "] label=[" + mHotseatLabels[i]
714 + "]"
715 );
716 }
717 }
718 }
719
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800720 private void completeAdd(PendingAddArguments args) {
721 switch (args.requestCode) {
722 case REQUEST_PICK_APPLICATION:
723 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
724 break;
725 case REQUEST_PICK_SHORTCUT:
726 processShortcut(args.intent);
727 break;
728 case REQUEST_CREATE_SHORTCUT:
729 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
730 break;
731 case REQUEST_PICK_LIVE_FOLDER:
732 addLiveFolder(args.intent);
733 break;
734 case REQUEST_CREATE_LIVE_FOLDER:
735 completeAddLiveFolder(args.intent, args.screen, args.cellX, args.cellY);
736 break;
737 case REQUEST_PICK_APPWIDGET:
738 addAppWidgetFromPick(args.intent);
739 break;
740 case REQUEST_CREATE_APPWIDGET:
741 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
742 completeAddAppWidget(appWidgetId, args.screen);
743 break;
744 case REQUEST_PICK_WALLPAPER:
745 // We just wanted the activity result here so we can clear mWaitingForResult
746 break;
747 }
748 }
749
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800751 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700752 mWaitingForResult = false;
753
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 // The pattern used here is that a user PICKs a specific application,
755 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700756
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
758 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700759
Michael Jurka0280c3b2010-09-17 15:00:07 -0700760 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800761 final PendingAddArguments args = new PendingAddArguments();
762 args.requestCode = requestCode;
763 args.intent = data;
764 args.screen = mAddScreen;
765 args.cellX = mAddIntersectCellX;
766 args.cellY = mAddIntersectCellY;
767
768 // If the loader is still running, defer the add until it is done.
769 if (isWorkspaceLocked()) {
770 sPendingAddList.add(args);
771 } else {
772 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 }
Romain Guy18042c82009-11-06 11:44:55 -0800774 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
775 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
776 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700777 // Clean up the appWidgetId if we canceled
778 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
779 if (appWidgetId != -1) {
780 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 }
782 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 }
784
785 @Override
786 protected void onResume() {
787 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800788 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700789 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790 mWorkspaceLoading = true;
791 mModel.startLoader(this, true);
792 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700793 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800795 if (mWaitingForResume != null) {
796 mWaitingForResume.setStayPressed(false);
797 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700798 // When we resume Launcher, a different Activity might be responsible for the app
799 // market intent, so refresh the icon
800 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 }
802
803 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700804 protected void onPause() {
805 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700806 // Some launcher layouts don't have a previous and next view
807 if (mPreviousView != null) {
808 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700809 }
810 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700811 dismissPreview(mNextView);
812 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700813 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800814 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700815 }
Romain Guycbb89e42009-06-08 15:52:54 -0700816
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700817 @Override
818 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400819 // Flag the loader to stop early before switching
820 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800821 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800822 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700823 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700824
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800825 // We can't hide the IME if it was forced open. So don't bother
826 /*
827 @Override
828 public void onWindowFocusChanged(boolean hasFocus) {
829 super.onWindowFocusChanged(hasFocus);
830
831 if (hasFocus) {
832 final InputMethodManager inputManager = (InputMethodManager)
833 getSystemService(Context.INPUT_METHOD_SERVICE);
834 WindowManager.LayoutParams lp = getWindow().getAttributes();
835 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
836 android.os.Handler()) {
837 protected void onReceiveResult(int resultCode, Bundle resultData) {
838 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
839 }
840 });
841 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
842 }
843 }
844 */
845
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 private boolean acceptFilter() {
847 final InputMethodManager inputManager = (InputMethodManager)
848 getSystemService(Context.INPUT_METHOD_SERVICE);
849 return !inputManager.isFullscreenMode();
850 }
851
852 @Override
853 public boolean onKeyDown(int keyCode, KeyEvent event) {
854 boolean handled = super.onKeyDown(keyCode, event);
855 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
856 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
857 keyCode, event);
858 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700859 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700860 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700861 // showSearchDialog()
862 // If there are multiple keystrokes before the search dialog takes focus,
863 // onSearchRequested() will be called for every keystroke,
864 // but it is idempotent, so it's fine.
865 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 }
867 }
868
Joe Onorato8a9625e2010-01-28 15:55:35 -0800869 // Eat the long press event so the keyboard doesn't come up.
870 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
871 return true;
872 }
873
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 return handled;
875 }
876
Karl Rosaen138a0412009-04-23 19:00:21 -0700877 private String getTypedText() {
878 return mDefaultKeySsb.toString();
879 }
880
881 private void clearTypedText() {
882 mDefaultKeySsb.clear();
883 mDefaultKeySsb.clearSpans();
884 Selection.setSelection(mDefaultKeySsb, 0);
885 }
886
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700888 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
889 * State
890 */
891 private static State intToState(int stateOrdinal) {
892 State state = State.WORKSPACE;
893 final State[] stateValues = State.values();
894 for (int i = 0; i < stateValues.length; i++) {
895 if (stateValues[i].ordinal() == stateOrdinal) {
896 state = stateValues[i];
897 break;
898 }
899 }
900 return state;
901 }
902
903 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 * Restores the previous state, if it exists.
905 *
906 * @param savedState The previous state.
907 */
908 private void restoreState(Bundle savedState) {
909 if (savedState == null) {
910 return;
911 }
912
Michael Jurka883f55b2010-10-21 15:47:14 -0700913 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
914
915 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800916 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700917 } else if (state == State.CUSTOMIZE) {
918 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800919 }
920
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
922 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700923 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 }
925
926 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700927
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700929 mAddScreen = addScreen;
930 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
931 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 mRestoring = true;
933 }
934
935 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
936 if (renameFolder) {
937 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700938 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 mRestoring = true;
940 }
Winson Chunga12a2502010-12-20 14:41:35 -0800941
942 // Restore the current AllApps drawer tab
943 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
944 String curTab = savedState.getString("allapps_currentTab");
945 if (curTab != null) {
946 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
947 tabhost.setCurrentTabByTag(curTab);
948 }
949 int curPage = savedState.getInt("allapps_currentPage", -1);
950 if (curPage > -1) {
951 mAllAppsPagedView.setRestorePage(curPage);
952 }
953 }
954
955 // Restore the current customization drawer tab
956 if (mHomeCustomizationDrawer != null) {
957 String curTab = savedState.getString("customize_currentTab");
958 if (curTab != null) {
959 // We set this directly so that there is no delay before the tab is set
960 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
961 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
962 }
963
964 // Note: currently we do not restore the page for the customization tray because unlike
965 // AllApps, the page content can change drastically
966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 }
968
969 /**
970 * Finds all the views we need and configure them properly.
971 */
972 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700973 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400974
Romain Guyb1b69f52009-08-10 15:10:15 -0700975 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400976 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977
Joe Onorato7c312c12009-08-13 21:36:53 -0700978 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700979 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700980 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800981 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700982 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700983 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700984
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700985 if (LauncherApplication.isScreenXLarge()) {
986 // They need to be INVISIBLE initially so that they will be measured in the layout.
987 // Otherwise the animations are messed up when we show them for the first time.
988 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
989 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
990 }
991
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700993
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500995 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700996
Joe Onorato7c312c12009-08-13 21:36:53 -0700997 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
998 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001000 View handleView = findViewById(R.id.all_apps_button);
1001 if (handleView != null && handleView instanceof HandleView) {
1002 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -07001003 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001004 mHandleView.setLauncher(this);
1005 mHandleView.setOnClickListener(this);
1006 mHandleView.setOnLongClickListener(this);
1007 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001008
Winson Chung80baf5a2010-08-09 16:03:15 -07001009 if (mCustomizePagedView != null) {
1010 mCustomizePagedView.setLauncher(this);
1011 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -07001012 } else {
1013 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
1014 hotseatLeft.setContentDescription(mHotseatLabels[0]);
1015 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
1016 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
1017 hotseatRight.setContentDescription(mHotseatLabels[1]);
1018 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001019
Michael Jurkaaf442092010-06-10 17:01:57 -07001020 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
1021 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001022
Michael Jurkaaf442092010-06-10 17:01:57 -07001023 Drawable previous = mPreviousView.getDrawable();
1024 Drawable next = mNextView.getDrawable();
1025 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026
Michael Jurkaaf442092010-06-10 17:01:57 -07001027 mPreviousView.setHapticFeedbackEnabled(false);
1028 mPreviousView.setOnLongClickListener(this);
1029 mNextView.setHapticFeedbackEnabled(false);
1030 mNextView.setOnLongClickListener(this);
1031 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001032
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001034 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -08001036 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037
1038 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001039 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -08001040
1041 final View allAppsButton = findViewById(R.id.all_apps_button);
1042 final View divider = findViewById(R.id.divider);
1043 final View configureButton = findViewById(R.id.configure_button);
1044
Michael Jurka54f7ac32010-08-02 13:56:46 -07001045 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001046 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -07001047 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001048 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001049 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001050 dragController.addDragListener(deleteZone);
1051
Adam Cohencdc30d52010-12-01 15:09:47 -08001052 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1053 if (allAppsDeleteZone != null) {
1054 allAppsDeleteZone.setLauncher(this);
1055 allAppsDeleteZone.setDragController(dragController);
1056 allAppsDeleteZone.setDragAndDropEnabled(false);
1057 dragController.addDragListener(allAppsDeleteZone);
1058 dragController.addDropTarget(allAppsDeleteZone);
1059 }
1060
1061 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1062 findViewById(R.id.all_apps_info_target);
1063 if (allAppsInfoTarget != null) {
1064 allAppsInfoTarget.setLauncher(this);
1065 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001066 allAppsInfoTarget.setDragAndDropEnabled(false);
1067 View marketButton = findViewById(R.id.market_button);
1068 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001069 allAppsInfoTarget.setOverlappingView(marketButton);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08001070 marketButton.setOnClickListener(new OnClickListener() {
1071 public void onClick(View v) {
1072 onClickAppMarketButton(v);
1073 }
1074 });
Adam Cohencdc30d52010-12-01 15:09:47 -08001075 }
1076 }
1077
Joe Onorato00acb122009-08-04 16:04:30 -04001078 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001079 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001080 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001081
Joe Onorato00acb122009-08-04 16:04:30 -04001082 // The order here is bottom to top.
1083 dragController.addDropTarget(workspace);
1084 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001085 if (allAppsInfoTarget != null) {
1086 dragController.addDropTarget(allAppsInfoTarget);
1087 }
1088 if (allAppsDeleteZone != null) {
1089 dragController.addDropTarget(allAppsDeleteZone);
1090 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001091 mButtonCluster = findViewById(R.id.all_apps_button_cluster);
1092
1093 mAllAppsButton = findViewById(R.id.all_apps_button);
1094 mDivider = findViewById(R.id.divider);
1095 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001096
1097 // We had previously set these click handlers in XML, but the first time we launched
1098 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1099 // found the right handler. Setting the handlers directly here eliminates that cost.
1100 if (mConfigureButton != null) {
1101 mConfigureButton.setOnClickListener(new OnClickListener() {
1102 public void onClick(View v) {
1103 onClickConfigureButton(v);
1104 }
1105 });
1106 }
1107 if (mDivider != null) {
1108 mDivider.setOnClickListener(new OnClickListener() {
1109 public void onClick(View v) {
1110 onClickAllAppsButton(v);
1111 }
1112 });
1113 }
1114 if (mAllAppsButton != null) {
1115 mAllAppsButton.setOnClickListener(new OnClickListener() {
1116 public void onClick(View v) {
1117 onClickAllAppsButton(v);
1118 }
1119 });
1120 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 }
1122
Romain Guy8a73c512009-11-09 19:19:59 -08001123 @SuppressWarnings({"UnusedDeclaration"})
1124 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001125 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001126 mWorkspace.scrollLeft();
1127 }
Romain Guy8a73c512009-11-09 19:19:59 -08001128 }
1129
1130 @SuppressWarnings({"UnusedDeclaration"})
1131 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001132 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001133 mWorkspace.scrollRight();
1134 }
Romain Guy8a73c512009-11-09 19:19:59 -08001135 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001136
1137 @SuppressWarnings({"UnusedDeclaration"})
1138 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001139 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001140
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001141 int index = -1;
1142 if (v.getId() == R.id.hotseat_left) {
1143 index = 0;
1144 } else if (v.getId() == R.id.hotseat_right) {
1145 index = 1;
1146 }
1147
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001148 // reload these every tap; you never know when they might change
1149 loadHotseats();
1150 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001151 startActivitySafely(
1152 mHotseats[index],
1153 "hotseat"
1154 );
1155 }
1156 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001157
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 /**
1159 * Creates a view representing a shortcut.
1160 *
1161 * @param info The data structure describing the shortcut.
1162 *
1163 * @return A View inflated from R.layout.application.
1164 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001165 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001167 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 }
1169
1170 /**
1171 * Creates a view representing a shortcut inflated from the specified resource.
1172 *
1173 * @param layoutResId The id of the XML layout used to create the shortcut.
1174 * @param parent The group the shortcut belongs to.
1175 * @param info The data structure describing the shortcut.
1176 *
1177 * @return A View inflated from layoutResId.
1178 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001179 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001180 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1181 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 return favorite;
1184 }
1185
1186 /**
1187 * Add an application shortcut to the workspace.
1188 *
1189 * @param data The intent describing the application.
1190 * @param cellInfo The position on screen where to create the shortcut.
1191 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001192 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001193 int intersectCellX, int intersectCellY) {
1194 final int[] cellXY = mTmpAddItemCellCoordinates;
1195 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1196
1197 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1198 showOutOfSpaceMessage();
1199 return;
1200 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001202 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001203
Romain Guy73b979d2009-06-09 12:57:21 -07001204 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001205 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001207 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001208 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1209 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001210 } else {
1211 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 }
1213 }
Romain Guycbb89e42009-06-08 15:52:54 -07001214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 /**
1216 * Add a shortcut to the workspace.
1217 *
1218 * @param data The intent describing the shortcut.
1219 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001221 private void completeAddShortcut(Intent data, int screen,
1222 int intersectCellX, int intersectCellY) {
1223 final int[] cellXY = mTmpAddItemCellCoordinates;
1224 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001225
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001226 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001227 boolean foundCellSpan = false;
1228 if (touchXY != null) {
1229 // when dragging and dropping, just find the closest free spot
1230 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1231 int[] result = screenLayout.findNearestVacantArea(
1232 touchXY[0], touchXY[1], 1, 1, cellXY);
1233 foundCellSpan = (result != null);
1234 } else {
1235 foundCellSpan = layout.findCellForSpanThatIntersects(
1236 cellXY, 1, 1, intersectCellX, intersectCellY);
1237 }
1238
1239 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001240 showOutOfSpaceMessage();
1241 return;
1242 }
1243
1244 final ShortcutInfo info = mModel.addShortcut(
1245 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246
1247 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001249 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 }
1251 }
1252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001254 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001256 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001257 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 */
Winson Chungf7640c82011-02-28 13:47:29 -08001259 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001260 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001261
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001262 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001263 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1264 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001265
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001267 // We have saved the position to which the widget was dragged-- this really only matters
1268 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001269 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001270
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001271 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001272 boolean foundCellSpan = false;
1273 if (touchXY != null) {
1274 // when dragging and dropping, just find the closest free spot
1275 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1276 int[] result = screenLayout.findNearestVacantArea(
1277 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001278 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001279 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001280 // if we long pressed on an empty cell to bring up a menu,
1281 // make sure we intersect the empty cell
1282 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1283 // findCellForSpanThatIntersects will just ignore them
1284 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1285 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001286 }
1287
Michael Jurka0280c3b2010-09-17 15:00:07 -07001288 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001289 if (appWidgetId != -1) {
1290 // Deleting an app widget ID is a void call but writes to disk before returning
1291 // to the caller...
1292 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1293 new Thread("deleteAppWidgetId") {
1294 public void run() {
1295 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1296 }
1297 }.start();
1298 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001299 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001300 return;
1301 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001303 // Build Launcher-specific widget info and save to database
1304 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001305 launcherInfo.spanX = spanXY[0];
1306 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001307
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 LauncherModel.addItemToDatabase(this, launcherInfo,
1309 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001310 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311
1312 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001313 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001314
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001316 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001317
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001318 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001320
Michael Jurka0280c3b2010-09-17 15:00:07 -07001321 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001322 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001323
1324 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 }
1326 }
Romain Guycbb89e42009-06-08 15:52:54 -07001327
Adam Cohended9f8d2010-11-03 13:25:16 -07001328 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1329 @Override
1330 public void onReceive(Context context, Intent intent) {
1331 final String action = intent.getAction();
1332 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1333 mUserPresent = false;
1334 updateRunning();
1335 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1336 mUserPresent = true;
1337 updateRunning();
1338 }
1339 }
1340 };
1341
1342 @Override
1343 public void onAttachedToWindow() {
1344 super.onAttachedToWindow();
1345
1346 // Listen for broadcasts related to user-presence
1347 final IntentFilter filter = new IntentFilter();
1348 filter.addAction(Intent.ACTION_SCREEN_OFF);
1349 filter.addAction(Intent.ACTION_USER_PRESENT);
1350 registerReceiver(mReceiver, filter);
1351
Adam Cohend113e0c2010-11-11 10:48:05 -08001352 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001353 mVisible = true;
1354 }
1355
1356 @Override
1357 public void onDetachedFromWindow() {
1358 super.onDetachedFromWindow();
1359 mVisible = false;
1360
Adam Cohend113e0c2010-11-11 10:48:05 -08001361 if (mAttached) {
1362 unregisterReceiver(mReceiver);
1363 mAttached = false;
1364 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001365 updateRunning();
1366 }
1367
1368 public void onWindowVisibilityChanged(int visibility) {
1369 mVisible = visibility == View.VISIBLE;
1370 updateRunning();
1371 }
1372
1373 private void sendAdvanceMessage(long delay) {
1374 mHandler.removeMessages(ADVANCE_MSG);
1375 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1376 mHandler.sendMessageDelayed(msg, delay);
1377 mAutoAdvanceSentTime = System.currentTimeMillis();
1378 }
1379
1380 private void updateRunning() {
1381 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1382 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1383 mAutoAdvanceRunning = autoAdvanceRunning;
1384 if (autoAdvanceRunning) {
1385 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1386 sendAdvanceMessage(delay);
1387 } else {
1388 if (!mWidgetsToAdvance.isEmpty()) {
1389 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1390 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1391 }
1392 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001393 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001394 }
1395 }
1396 }
1397
1398 private final Handler mHandler = new Handler() {
1399 @Override
1400 public void handleMessage(Message msg) {
1401 if (msg.what == ADVANCE_MSG) {
1402 int i = 0;
1403 for (View key: mWidgetsToAdvance.keySet()) {
1404 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1405 final int delay = mAdvanceStagger * i;
1406 if (v instanceof Advanceable) {
1407 postDelayed(new Runnable() {
1408 public void run() {
1409 ((Advanceable) v).advance();
1410 }
1411 }, delay);
1412 }
1413 i++;
1414 }
1415 sendAdvanceMessage(mAdvanceInterval);
1416 }
1417 }
1418 };
1419
1420 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1421 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1422 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1423 if (v instanceof Advanceable) {
1424 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001425 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001426 updateRunning();
1427 }
1428 }
1429
1430 void removeWidgetToAutoAdvance(View hostView) {
1431 if (mWidgetsToAdvance.containsKey(hostView)) {
1432 mWidgetsToAdvance.remove(hostView);
1433 updateRunning();
1434 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001435 }
1436
Joe Onorato9c1289c2009-08-17 11:03:03 -04001437 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1438 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001439 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001440 launcherInfo.hostView = null;
1441 }
1442
Adam Cohended9f8d2010-11-03 13:25:16 -07001443 void showOutOfSpaceMessage() {
1444 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1445 }
1446
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001447 public LauncherAppWidgetHost getAppWidgetHost() {
1448 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 }
Romain Guycbb89e42009-06-08 15:52:54 -07001450
Winson Chunga9abd0e2010-10-27 17:18:37 -07001451 public LauncherModel getModel() {
1452 return mModel;
1453 }
1454
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001455 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001456 getWindow().closeAllPanels();
1457
1458 try {
1459 dismissDialog(DIALOG_CREATE_SHORTCUT);
1460 // Unlock the workspace if the dialog was showing
1461 } catch (Exception e) {
1462 // An exception is thrown if the dialog is not visible, which is fine
1463 }
1464
1465 try {
1466 dismissDialog(DIALOG_RENAME_FOLDER);
1467 // Unlock the workspace if the dialog was showing
1468 } catch (Exception e) {
1469 // An exception is thrown if the dialog is not visible, which is fine
1470 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001471
1472 // Whatever we were doing is hereby canceled.
1473 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001474 }
1475
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 @Override
1477 protected void onNewIntent(Intent intent) {
1478 super.onNewIntent(intent);
1479
1480 // Close the menu
1481 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001482 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001483 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001484
Joe Onorato14f122b2009-11-19 14:06:36 -08001485 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1486 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001487
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001488 // In all these cases, only animate if we're already on home
1489
Michael Jurka4cb37242010-08-09 21:05:32 -07001490 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001491 mWorkspace.unshrink(alreadyOnHome);
1492 }
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001493
Patrick Dubroy758a9232011-03-03 19:54:56 -08001494 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001495 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001496 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001497 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001498 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001499
Joe Onorato3a8820b2009-11-10 15:06:42 -08001500 final View v = getWindow().peekDecorView();
1501 if (v != null && v.getWindowToken() != null) {
1502 InputMethodManager imm = (InputMethodManager)getSystemService(
1503 INPUT_METHOD_SERVICE);
1504 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 }
1506 }
1507 }
1508
1509 @Override
1510 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1511 // Do not call super here
1512 mSavedInstanceState = savedInstanceState;
1513 }
1514
1515 @Override
1516 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001517 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518
1519 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1520 if (folders.size() > 0) {
1521 final int count = folders.size();
1522 long[] ids = new long[count];
1523 for (int i = 0; i < count; i++) {
1524 final FolderInfo info = folders.get(i).getInfo();
1525 ids[i] = info.id;
1526 }
1527 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1528 } else {
1529 super.onSaveInstanceState(outState);
1530 }
1531
Michael Jurka883f55b2010-10-21 15:47:14 -07001532 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533
Michael Jurka0280c3b2010-09-17 15:00:07 -07001534 if (mAddScreen > -1 && mWaitingForResult) {
1535 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1536 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1537 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 }
1539
1540 if (mFolderInfo != null && mWaitingForResult) {
1541 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1542 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1543 }
Michael Jurka946ad472010-07-09 18:05:18 -07001544
Winson Chung43b119d2010-12-06 16:45:05 -08001545 // Save the current AllApps drawer tab
1546 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1547 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1548 String currentTabTag = tabhost.getCurrentTabTag();
1549 if (currentTabTag != null) {
1550 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001551 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001552 }
1553 }
1554
1555 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001556 if (mHomeCustomizationDrawer != null) {
1557 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1558 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001559 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001560 }
1561 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
1563
1564 @Override
1565 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001567
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001569 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001571 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001573 mAppWidgetHost = null;
1574
1575 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576
1577 TextKeyListener.getInstance().release();
1578
Joe Onorato9c1289c2009-08-17 11:03:03 -04001579 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580
Joe Onorato9c1289c2009-08-17 11:03:03 -04001581 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001582
1583 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001584
Patrick Dubroyab962b72010-07-26 12:10:10 -07001585 // Some launcher layouts don't have a previous and next view
1586 if (mPreviousView != null) {
1587 dismissPreview(mPreviousView);
1588 }
1589 if (mNextView != null) {
1590 dismissPreview(mNextView);
1591 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001592
1593 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001594
1595 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1596 mWorkspace.removeAllViews();
1597 mWorkspace = null;
1598 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001599
1600 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 }
1602
1603 @Override
1604 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001605 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 super.startActivityForResult(intent, requestCode);
1607 }
1608
1609 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001610 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001612
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001613 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001614
Karl Rosaen138a0412009-04-23 19:00:21 -07001615 if (initialQuery == null) {
1616 // Use any text typed in the launcher as the initial query
1617 initialQuery = getTypedText();
1618 clearTypedText();
1619 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 if (appSearchData == null) {
1621 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001622 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 }
Romain Guycbb89e42009-06-08 15:52:54 -07001624
Karl Rosaen138a0412009-04-23 19:00:21 -07001625 final SearchManager searchManager =
1626 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001627 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001628 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 }
1630
1631 @Override
1632 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001633 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001634 return false;
1635 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636
1637 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001638
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1640 .setIcon(android.R.drawable.ic_menu_add)
1641 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001642 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1643 .setIcon(android.R.drawable.ic_menu_manage)
1644 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001645 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 .setIcon(android.R.drawable.ic_menu_gallery)
1647 .setAlphabeticShortcut('W');
1648 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1649 .setIcon(android.R.drawable.ic_search_category_default)
1650 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1651 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1652 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1653 .setAlphabeticShortcut('N');
1654
1655 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001656 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1657 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658
1659 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1660 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1661 .setIntent(settings);
1662
1663 return true;
1664 }
1665
1666 @Override
1667 public boolean onPrepareOptionsMenu(Menu menu) {
1668 super.onPrepareOptionsMenu(menu);
1669
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001670 // If all apps is animating, don't show the menu, because we don't know
1671 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001672 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001673 return false;
1674 }
1675
1676 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001677 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001678 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1679 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1680
1681 // Disable add if the workspace is full.
1682 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001683 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1684 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001685 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686
1687 return true;
1688 }
1689
1690 @Override
1691 public boolean onOptionsItemSelected(MenuItem item) {
1692 switch (item.getItemId()) {
1693 case MENU_ADD:
1694 addItems();
1695 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001696 case MENU_MANAGE_APPS:
1697 manageApps();
1698 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 case MENU_WALLPAPER_SETTINGS:
1700 startWallpaper();
1701 return true;
1702 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001703 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 return true;
1705 case MENU_NOTIFICATIONS:
1706 showNotifications();
1707 return true;
1708 }
1709
1710 return super.onOptionsItemSelected(item);
1711 }
Romain Guycbb89e42009-06-08 15:52:54 -07001712
Karl Rosaen138a0412009-04-23 19:00:21 -07001713 /**
1714 * Indicates that we want global search for this activity by setting the globalSearch
1715 * argument for {@link #startSearch} to true.
1716 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001718 @Override
1719 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001720 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001721 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001722 }
1723
Joe Onorato9c1289c2009-08-17 11:03:03 -04001724 public boolean isWorkspaceLocked() {
1725 return mWorkspaceLoading || mWaitingForResult;
1726 }
1727
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001728 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1729 private boolean isPreviewVisible() {
1730 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1731 (mNextView != null && mNextView.getTag() != null);
1732 }
1733
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001735 if (LauncherApplication.isScreenXLarge()) {
1736 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001737 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001738 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001739 }
1740 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001741 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001742 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001743 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001744 }
1745
Michael Jurka0280c3b2010-09-17 15:00:07 -07001746 private void resetAddInfo() {
1747 mAddScreen = -1;
1748 mAddIntersectCellX = -1;
1749 mAddIntersectCellY = -1;
1750 mAddDropPosition = null;
1751 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001752
Winson Chung55cef262010-10-28 14:14:18 -07001753 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001754 resetAddInfo();
1755 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001756 mAddDropPosition = position;
1757
Michael Jurkaaf442092010-06-10 17:01:57 -07001758 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001759 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1760 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001761 }
1762
Winson Chung7ad01412010-09-27 11:33:03 -07001763 private void manageApps() {
1764 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1765 }
1766
Michael Jurkaaf442092010-06-10 17:01:57 -07001767 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001768 // TODO: catch bad widget exception when sent
1769 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001770 // TODO: Is this log message meaningful?
1771 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001772 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001773 }
1774
Winson Chung55cef262010-10-28 14:14:18 -07001775 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001776 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777
Bjorn Bringert7984c942009-12-09 15:38:25 +00001778 if (appWidget.configure != null) {
1779 // Launch over to configure widget, if needed
1780 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1781 intent.setComponent(appWidget.configure);
1782 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001783 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001784 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1785 intent.putExtra(
1786 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1787 info.mimeType);
1788
1789 final String mimeType = info.mimeType;
1790 final ClipData clipData = (ClipData) info.configurationData;
1791 final ClipDescription clipDesc = clipData.getDescription();
1792 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1793 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001794 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001795 final CharSequence stringData = item.getText();
1796 final Uri uriData = item.getUri();
1797 final Intent intentData = item.getIntent();
1798 final String key =
1799 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1800 if (uriData != null) {
1801 intent.putExtra(key, uriData);
1802 } else if (intentData != null) {
1803 intent.putExtra(key, intentData);
1804 } else if (stringData != null) {
1805 intent.putExtra(key, stringData);
1806 }
1807 break;
1808 }
1809 }
1810 }
Winson Chung55cef262010-10-28 14:14:18 -07001811 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001812
Romain Guy8e633c52010-03-04 12:51:36 -08001813 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001815 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001816 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 }
1818 }
Romain Guycbb89e42009-06-08 15:52:54 -07001819
Michael Jurka0280c3b2010-09-17 15:00:07 -07001820 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1821 resetAddInfo();
1822 mAddScreen = screen;
1823 mAddDropPosition = position;
1824
1825 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1826 createShortcutIntent.setComponent(componentName);
1827 processShortcut(createShortcutIntent);
1828 }
1829
Joe Onoratodeb98af2010-02-19 14:59:39 -08001830 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001831 // Handle case where user selected "Applications"
1832 String applicationName = getResources().getString(R.string.group_applications);
1833 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001834
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001835 if (applicationName != null && applicationName.equals(shortcutName)) {
1836 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1837 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001838
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001839 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1840 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001841 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001842 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001843 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001844 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001845 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 }
1847
Winson Chung24ab2f12010-09-16 14:10:47 -07001848 void processWallpaper(Intent intent) {
1849 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1850 }
1851
Michael Jurka0280c3b2010-09-17 15:00:07 -07001852 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1853 resetAddInfo();
1854 mAddScreen = screen;
1855 mAddDropPosition = position;
1856
1857 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1858 createFolderIntent.setComponent(componentName);
1859
1860 addLiveFolder(createFolderIntent);
1861 }
1862
1863 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001864 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001865 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001866 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001867
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001868 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001869 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001870 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001871 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001872 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 }
1874
Michael Jurka0280c3b2010-09-17 15:00:07 -07001875 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 UserFolderInfo folderInfo = new UserFolderInfo();
1877 folderInfo.title = getText(R.string.folder_name);
1878
Michael Jurka0280c3b2010-09-17 15:00:07 -07001879 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1880 final int[] cellXY = mTmpAddItemCellCoordinates;
1881 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1882 showOutOfSpaceMessage();
1883 return;
1884 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885
1886 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001887 LauncherModel.addItemToDatabase(this, folderInfo,
1888 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001889 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001890 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891
1892 // Create the view
1893 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001894 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1895 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001896 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 }
Romain Guycbb89e42009-06-08 15:52:54 -07001898
Joe Onorato9c1289c2009-08-17 11:03:03 -04001899 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001900 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001901 }
1902
Michael Jurka28750fb2010-09-24 17:43:49 -07001903 private void completeAddLiveFolder(
1904 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001905 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1906 final int[] cellXY = mTmpAddItemCellCoordinates;
1907 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1908 showOutOfSpaceMessage();
1909 return;
1910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911
Michael Jurka0280c3b2010-09-17 15:00:07 -07001912 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913
1914 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001916 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001917 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 }
1919 }
1920
1921 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001922 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923
1924 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1925 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1926
1927 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 Intent.ShortcutIconResource iconResource = null;
1929
1930 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1931 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1932 try {
1933 iconResource = (Intent.ShortcutIconResource) extra;
1934 final PackageManager packageManager = context.getPackageManager();
1935 Resources resources = packageManager.getResourcesForApplication(
1936 iconResource.packageName);
1937 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1938 icon = resources.getDrawable(id);
1939 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001940 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 }
1942 }
1943
1944 if (icon == null) {
1945 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1946 }
1947
1948 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001949 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 info.title = name;
1951 info.iconResource = iconResource;
1952 info.uri = data.getData();
1953 info.baseIntent = baseIntent;
1954 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1955 LiveFolders.DISPLAY_MODE_GRID);
1956
1957 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001958 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001959 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960
1961 return info;
1962 }
1963
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 private void showNotifications() {
1965 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1966 if (statusBar != null) {
1967 statusBar.expand();
1968 }
1969 }
1970
1971 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001972 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001974 Intent chooser = Intent.createChooser(pickWallpaper,
1975 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001976 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1977 // Removed in Eclair MR1
1978// WallpaperManager wm = (WallpaperManager)
1979// getSystemService(Context.WALLPAPER_SERVICE);
1980// WallpaperInfo wi = wm.getWallpaperInfo();
1981// if (wi != null && wi.getSettingsActivity() != null) {
1982// LabeledIntent li = new LabeledIntent(getPackageName(),
1983// R.string.configure_wallpaper, 0);
1984// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1985// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1986// }
Mike Clerona0618e42009-10-22 13:55:21 -07001987 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
1989
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001990 /**
1991 * Registers various content observers. The current implementation registers
1992 * only a favorites observer to keep track of the favorites applications.
1993 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001994 private void registerContentObservers() {
1995 ContentResolver resolver = getContentResolver();
1996 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1997 true, mWidgetObserver);
1998 }
1999
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 @Override
2001 public boolean dispatchKeyEvent(KeyEvent event) {
2002 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2003 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002005 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002006 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08002007 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08002008 dumpState();
2009 return true;
2010 }
2011 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002012 }
2013 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2014 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002015 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 return true;
2017 }
2018 }
2019
2020 return super.dispatchKeyEvent(event);
2021 }
2022
Joe Onorato88ec0992009-11-19 13:16:06 -08002023 @Override
2024 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002025 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
2026 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002027 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08002028 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002029 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07002030 dismissPreview(mPreviousView);
2031 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002032 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002033 mWorkspace.exitWidgetResizeMode();
2034
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002035 // Back button is a no-op here, but give at least some feedback for the button press
2036 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002037 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002038 }
2039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 private void closeFolder() {
2041 Folder folder = mWorkspace.getOpenFolder();
2042 if (folder != null) {
2043 closeFolder(folder);
2044 }
2045 }
2046
2047 void closeFolder(Folder folder) {
2048 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08002049 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07002051 CellLayout cl = (CellLayout) parent;
2052 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05002053 if (folder instanceof DropTarget) {
2054 // Live folders aren't DropTargets.
2055 mDragController.removeDropTarget((DropTarget)folder);
2056 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 }
2058 folder.onClose();
2059 }
2060
2061 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002062 * Re-listen when widgets are reset.
2063 */
2064 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002065 if (mAppWidgetHost != null) {
2066 mAppWidgetHost.startListening();
2067 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002068 }
2069
2070 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002071 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2072 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002074 private void unbindDesktopItems() {
2075 for (ItemInfo item: mDesktopItems) {
2076 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002078 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 /**
2082 * Launches the intent referred by the clicked shortcut.
2083 *
2084 * @param v The view representing the clicked shortcut.
2085 */
2086 public void onClick(View v) {
2087 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002088 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002090 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002091 int[] pos = new int[2];
2092 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002093 intent.setSourceBounds(new Rect(pos[0], pos[1],
2094 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08002095 boolean success = startActivitySafely(intent, tag);
2096
2097 if (success && v instanceof BubbleTextView) {
2098 mWaitingForResume = (BubbleTextView) v;
2099 mWaitingForResume.setStayPressed(true);
2100 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 } else if (tag instanceof FolderInfo) {
2102 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002103 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002104 if (mState == State.ALL_APPS) {
2105 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002106 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002107 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002108 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 }
2110 }
2111
Michael Jurka0e260592010-06-30 17:07:39 -07002112 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002113 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002114 // clicking anywhere on the workspace causes the customization drawer to slide down
2115 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002116 return false;
2117 }
2118
Michael Jurkaaf442092010-06-10 17:01:57 -07002119 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002120 * Event handler for the search button
2121 *
2122 * @param v The view that was clicked.
2123 */
2124 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002125 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002126 // Use a custom animation for launching search
2127 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002128 }
2129
2130 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002131 * Event handler for the voice button
2132 *
2133 * @param v The view that was clicked.
2134 */
2135 public void onClickVoiceButton(View v) {
2136 startVoiceSearch();
2137 }
2138
2139 private void startVoiceSearch() {
2140 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2141 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2142 startActivity(intent);
2143 }
2144
2145 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002146 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002147 * enters home screen customization mode.
2148 *
2149 * @param v The view that was clicked.
2150 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002151 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002152 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002153 }
2154
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002155 /**
2156 * Event handler for the "grid" button that appears on the home screen, which
2157 * enters all apps mode.
2158 *
2159 * @param v The view that was clicked.
2160 */
2161 public void onClickAllAppsButton(View v) {
2162 showAllApps(true);
2163 }
2164
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002165 public void onClickAppMarketButton(View v) {
2166 if (mAppMarketIntent != null) {
2167 startActivitySafely(mAppMarketIntent, "app market");
2168 }
2169 }
2170
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002171 void startApplicationDetailsActivity(ComponentName componentName) {
2172 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002173 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2174 Uri.fromParts("package", packageName, null));
2175 startActivity(intent);
2176 }
2177
Patrick Dubroy5539af72010-09-07 15:22:01 -07002178 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2179 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2180 // System applications cannot be installed. For now, show a toast explaining that.
2181 // We may give them the option of disabling apps this way.
2182 int messageId = R.string.uninstall_system_app_text;
2183 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2184 } else {
2185 String packageName = appInfo.componentName.getPackageName();
2186 String className = appInfo.componentName.getClassName();
2187 Intent intent = new Intent(
2188 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2189 startActivity(intent);
2190 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002191 }
2192
Michael Jurkaddd62e92011-02-16 17:49:14 -08002193 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2195 try {
2196 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002197 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 } catch (ActivityNotFoundException e) {
2199 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002200 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 } catch (SecurityException e) {
2202 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002203 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002205 "or use the exported attribute for this activity. "
2206 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002208 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002210
Romain Guy8e633c52010-03-04 12:51:36 -08002211 void startActivityForResultSafely(Intent intent, int requestCode) {
2212 try {
2213 startActivityForResult(intent, requestCode);
2214 } catch (ActivityNotFoundException e) {
2215 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2216 } catch (SecurityException e) {
2217 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2218 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2219 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2220 "or use the exported attribute for this activity.", e);
2221 }
2222 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223
2224 private void handleFolderClick(FolderInfo folderInfo) {
2225 if (!folderInfo.opened) {
2226 // Close any open folder
2227 closeFolder();
2228 // Open the requested folder
2229 openFolder(folderInfo);
2230 } else {
2231 // Find the open folder...
2232 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2233 int folderScreen;
2234 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002235 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 // .. and close it
2237 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002238 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 // Close any folder open on the current screen
2240 closeFolder();
2241 // Pull the folder onto this screen
2242 openFolder(folderInfo);
2243 }
2244 }
2245 }
2246 }
2247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002249 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002250 * is animated relative to the specified View. If the View is null, no animation
2251 * is played.
2252 *
2253 * @param folderInfo The FolderInfo describing the folder to open.
2254 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002255 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 Folder openFolder;
2257
2258 if (folderInfo instanceof UserFolderInfo) {
2259 openFolder = UserFolder.fromXml(this);
2260 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002261 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002262 } else {
2263 return;
2264 }
2265
Joe Onorato00acb122009-08-04 16:04:30 -04002266 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 openFolder.setLauncher(this);
2268
2269 openFolder.bind(folderInfo);
2270 folderInfo.opened = true;
2271
Winson Chungaafa03c2010-06-11 17:34:16 -07002272 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2273
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002274 openFolder.onOpen();
2275 }
2276
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002277 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002278 if (mState != State.WORKSPACE) {
2279 return false;
2280 }
2281
Romain Guy1fbc1c82009-11-09 20:43:08 -08002282 switch (v.getId()) {
2283 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002284 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002285 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2286 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002287 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002288 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002289 return true;
2290 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002291 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002292 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2293 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002294 showPreviews(v);
2295 }
2296 return true;
2297 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002298 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002299 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2300 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2301 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002302 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002303 return true;
2304 }
2305
Joe Onorato9c1289c2009-08-17 11:03:03 -04002306 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002307 return false;
2308 }
2309
2310 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002311 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002312 }
2313
Michael Jurka0280c3b2010-09-17 15:00:07 -07002314 resetAddInfo();
2315 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002316 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002317 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002318 return true;
2319 }
2320
Michael Jurka0280c3b2010-09-17 15:00:07 -07002321 final View itemUnderLongClick = longClickCellInfo.cell;
2322
Winson Chung304dcde2011-01-07 11:17:23 -08002323 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002324 if (itemUnderLongClick == null) {
2325 // User long pressed on empty space
2326 mWorkspace.setAllowLongPress(false);
2327 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2328 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002329 if (LauncherApplication.isScreenXLarge()) {
2330 addItems();
2331 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002332 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 }
2334 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002335 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002337 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2338 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002339 mAddIntersectCellX = longClickCellInfo.cellX;
2340 mAddIntersectCellY = longClickCellInfo.cellY;
2341 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 }
2343 }
2344 }
2345 return true;
2346 }
2347
Romain Guye6b8e2f2009-11-10 11:56:55 -08002348 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002349 private void dismissPreview(final View v) {
2350 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002351 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002352 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2353 public void onDismiss() {
2354 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2355 int count = group.getChildCount();
2356 for (int i = 0; i < count; i++) {
2357 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2358 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002359 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2360 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2361
2362 v.setTag(R.id.workspace, null);
2363 v.setTag(R.id.icon, null);
2364 window.setOnDismissListener(null);
2365 }
2366 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002367 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002368 }
2369 v.setTag(null);
2370 }
2371
Romain Guyf8e6a802009-12-07 17:48:02 -08002372 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002373 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002374 }
2375
Romain Guya6abce82009-11-10 02:54:41 -08002376 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002377 final Resources resources = getResources();
2378 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002379
Romain Guya6abce82009-11-10 02:54:41 -08002380 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002381
Romain Guy9d31e9f2009-11-11 18:54:28 -08002382 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002383
Romain Guyf8e6a802009-12-07 17:48:02 -08002384 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002385 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002386 int extraW = (int) ((r.left + r.right) * max);
2387 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002388
2389 int aW = cell.getWidth() - extraW;
2390 float w = aW / max;
2391
2392 int width = cell.getWidth();
2393 int height = cell.getHeight();
2394 int x = cell.getLeftPadding();
2395 int y = cell.getTopPadding();
2396 width -= (x + cell.getRightPadding());
2397 height -= (y + cell.getBottomPadding());
2398
2399 float scale = w / width;
2400
2401 int count = end - start;
2402
2403 final float sWidth = width * scale;
2404 float sHeight = height * scale;
2405
Romain Guye6b8e2f2009-11-10 11:56:55 -08002406 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002407
Romain Guye6b8e2f2009-11-10 11:56:55 -08002408 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2409 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002410
2411 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002412 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002413 cell = (CellLayout) workspace.getChildAt(i);
2414
Romain Guyf8e6a802009-12-07 17:48:02 -08002415 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002416 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002417
2418 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002419 c.scale(scale, scale);
2420 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002421 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002422
Romain Guy9d31e9f2009-11-11 18:54:28 -08002423 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002424 image.setImageBitmap(bitmap);
2425 image.setTag(i);
2426 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002427 image.setOnFocusChangeListener(handler);
2428 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002429 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002430
2431 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002432 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2433
Winson Chungaafa03c2010-06-11 17:34:16 -07002434 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002435 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002436
2437 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002438 p.setContentView(preview);
2439 p.setWidth((int) (sWidth * count + extraW));
2440 p.setHeight((int) (sHeight + extraH));
2441 p.setAnimationStyle(R.style.AnimationPreview);
2442 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002443 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002444 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002445 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002446
Romain Guye6b8e2f2009-11-10 11:56:55 -08002447 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2448 public void onDismiss() {
2449 dismissPreview(anchor);
2450 }
2451 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002452
2453 anchor.setTag(p);
2454 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002455 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002456 }
2457
Romain Guy9d31e9f2009-11-11 18:54:28 -08002458 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002459 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002460
Romain Guye6b8e2f2009-11-10 11:56:55 -08002461 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002462 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002463 }
2464
2465 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002466 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002467 v.post(this);
2468 }
2469
2470 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002471 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002472 }
2473
2474 public void onFocusChange(View v, boolean hasFocus) {
2475 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002476 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002477 }
Romain Guya6abce82009-11-10 02:54:41 -08002478 }
2479 }
2480
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002481 Workspace getWorkspace() {
2482 return mWorkspace;
2483 }
2484
Michael Jurka3c38d562011-02-18 19:22:12 -08002485 TabHost getCustomizationDrawer() {
2486 return mHomeCustomizationDrawer;
2487 }
2488
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002489 @Override
2490 protected Dialog onCreateDialog(int id) {
2491 switch (id) {
2492 case DIALOG_CREATE_SHORTCUT:
2493 return new CreateShortcut().createDialog();
2494 case DIALOG_RENAME_FOLDER:
2495 return new RenameFolder().createDialog();
2496 }
2497
2498 return super.onCreateDialog(id);
2499 }
2500
2501 @Override
2502 protected void onPrepareDialog(int id, Dialog dialog) {
2503 switch (id) {
2504 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 break;
2506 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002507 if (mFolderInfo != null) {
2508 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2509 final CharSequence text = mFolderInfo.title;
2510 input.setText(text);
2511 input.setSelection(0, text.length());
2512 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 break;
2514 }
2515 }
2516
2517 void showRenameDialog(FolderInfo info) {
2518 mFolderInfo = info;
2519 mWaitingForResult = true;
2520 showDialog(DIALOG_RENAME_FOLDER);
2521 }
2522
Michael Jurka0280c3b2010-09-17 15:00:07 -07002523 private void showAddDialog(int intersectX, int intersectY) {
2524 resetAddInfo();
2525 mAddIntersectCellX = intersectX;
2526 mAddIntersectCellY = intersectY;
2527 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002528 mWaitingForResult = true;
2529 showDialog(DIALOG_CREATE_SHORTCUT);
2530 }
2531
Joe Onoratodeb98af2010-02-19 14:59:39 -08002532 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002533 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002534 Bundle bundle = new Bundle();
2535
2536 ArrayList<String> shortcutNames = new ArrayList<String>();
2537 shortcutNames.add(getString(R.string.group_applications));
2538 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2539
2540 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2541 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2542 R.drawable.ic_launcher_application));
2543 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2544
2545 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2546 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002547 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002548 pickIntent.putExtras(bundle);
2549
Joe Onoratodeb98af2010-02-19 14:59:39 -08002550 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002551 }
2552
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 private class RenameFolder {
2554 private EditText mInput;
2555
2556 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2558 mInput = (EditText) layout.findViewById(R.id.folder_name);
2559
2560 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2561 builder.setIcon(0);
2562 builder.setTitle(getString(R.string.rename_folder_title));
2563 builder.setCancelable(true);
2564 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2565 public void onCancel(DialogInterface dialog) {
2566 cleanup();
2567 }
2568 });
2569 builder.setNegativeButton(getString(R.string.cancel_action),
2570 new Dialog.OnClickListener() {
2571 public void onClick(DialogInterface dialog, int which) {
2572 cleanup();
2573 }
2574 }
2575 );
2576 builder.setPositiveButton(getString(R.string.rename_action),
2577 new Dialog.OnClickListener() {
2578 public void onClick(DialogInterface dialog, int which) {
2579 changeFolderName();
2580 }
2581 }
2582 );
2583 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002584
2585 final AlertDialog dialog = builder.create();
2586 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2587 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002588 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002589 mInput.requestFocus();
2590 InputMethodManager inputManager = (InputMethodManager)
2591 getSystemService(Context.INPUT_METHOD_SERVICE);
2592 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002593 }
2594 });
2595
2596 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002597 }
2598
2599 private void changeFolderName() {
2600 final String name = mInput.getText().toString();
2601 if (!TextUtils.isEmpty(name)) {
2602 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002603 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002604 mFolderInfo.title = name;
2605 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2606
Joe Onorato9c1289c2009-08-17 11:03:03 -04002607 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002608 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002609 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002610 } else {
2611 final FolderIcon folderIcon = (FolderIcon)
2612 mWorkspace.getViewForTag(mFolderInfo);
2613 if (folderIcon != null) {
2614 folderIcon.setText(name);
2615 getWorkspace().requestLayout();
2616 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002617 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002618 mWorkspaceLoading = true;
2619 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002620 }
2621 }
2622 }
2623 cleanup();
2624 }
2625
2626 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002627 dismissDialog(DIALOG_RENAME_FOLDER);
2628 mWaitingForResult = false;
2629 mFolderInfo = null;
2630 }
2631 }
2632
Daniel Sandler843e8602010-06-07 14:59:01 -04002633 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2634 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002635 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002636 }
2637
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002638 // AllAppsView.Watcher
2639 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002640 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2641 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002642 mWorkspace.setVisibility(View.GONE);
2643 }
2644 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002645
Michael Jurkaafca9532011-02-16 14:50:35 -08002646 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002647 button.setVisibility(View.VISIBLE);
2648 button.setFocusable(true);
2649 button.setClickable(true);
2650 }
2651
2652 private void hideToolbarButton(View button) {
2653 button.setAlpha(0.0f);
2654 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2655 button.setVisibility(View.INVISIBLE);
Michael Jurkaafca9532011-02-16 14:50:35 -08002656 }
2657
2658 private void disableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002659 button.setFocusable(false);
2660 button.setClickable(false);
2661 }
2662
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002663 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002664 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002665 *
2666 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2667 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002668 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002669 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002670 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002671 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002672 final boolean showing = show;
2673 final boolean hiding = !show;
2674
2675 final int duration = show ?
2676 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2677 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2678
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002679 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002680 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002681 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002682 anim.addUpdateListener(new AnimatorUpdateListener() {
2683 public void onAnimationUpdate(ValueAnimator animation) {
2684 view.setAlpha((Float) animation.getAnimatedValue());
2685 }
2686 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002687 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002688 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002689 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002690 if (showing) showAndEnableToolbarButton(view);
2691 if (hiding) disableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002692 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002693 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002694 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002695 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002696 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002697 });
2698 seq.play(anim);
2699 } else {
2700 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002701 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002702 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002703 } else {
Michael Jurkaafca9532011-02-16 14:50:35 -08002704 disableToolbarButton(view);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002705 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002706 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002707 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002708 }
2709
2710 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002711 * Show/hide the appropriate toolbar buttons for newState.
2712 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002713 *
2714 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002715 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2716 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002717 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002718 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002719 switch (newState) {
2720 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002721 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2722 mDeleteZone.setOverlappingViews(
2723 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002724 mDeleteZone.setDragAndDropEnabled(true);
2725 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002726 break;
2727 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002728 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002729 mDeleteZone.setDragAndDropEnabled(false);
2730 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002731 break;
2732 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002733 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002734 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002735 break;
2736 }
2737 }
2738
2739 /**
2740 * Helper method for the cameraZoomIn/cameraZoomOut animations
2741 * @param view The view being animated
2742 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2743 * @param scaleFactor The scale factor used for the zoom
2744 */
2745 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2746 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002747
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002748 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002749 // Set pivotY so that at the starting zoom factor, the view is partially
2750 // visible. Modifying initialHeightFactor changes how much of the view is
2751 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002752 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002753 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002754 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002755 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002756 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002757 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002758 }
2759 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002760
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002761 /**
2762 * Zoom the camera out from the workspace to reveal 'toView'.
2763 * Assumes that the view to show is anchored at either the very top or very bottom
2764 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002765 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002766 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002767 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002768 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002769 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002770
2771 final int duration = toAllApps ?
2772 res.getInteger(R.integer.config_allAppsZoomInTime) :
2773 res.getInteger(R.integer.config_customizeZoomInTime);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002774 final int fadeDuration = toAllApps ?
2775 res.getInteger(R.integer.config_allAppsFadeInTime) :
2776 res.getInteger(R.integer.config_customizeFadeInTime);
Adam Cohenf16e5712011-01-13 13:31:45 -08002777
2778 final float scale = toAllApps ?
2779 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2780 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2781
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002782 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002783
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002784 setPivotsForZoom(toView, toState, scale);
2785
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002786 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002787 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002788 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002789 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002790 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002791
2792 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002793 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002794 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
2795 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2796 public void onAnimationUpdate(ValueAnimator animation) {
2797 final float b = (Float) animation.getAnimatedValue();
2798 final float a = 1f - b;
2799 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002800 toView.setFastScaleX(a * scale + b * 1f);
2801 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002802 }
2803 });
Adam Cohen61033d32010-11-15 18:29:44 -08002804
Adam Cohenf16e5712011-01-13 13:31:45 -08002805 if (toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002806 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002807 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002808 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002809 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2810 public void onAnimationUpdate(ValueAnimator animation) {
2811 final float b = (Float) animation.getAnimatedValue();
2812 final float a = 1f - b;
2813 // don't need to invalidate because we do so above
2814 toView.setFastAlpha(a * 0f + b * 1f);
2815 }
2816 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002817 alphaAnim.start();
2818 }
2819
Michael Jurkaabded662011-03-04 12:06:57 -08002820 if (toView instanceof LauncherTransitionable) {
2821 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002822 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002823 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002824 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002825 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002826 // Prepare the position
2827 toView.setTranslationX(0.0f);
2828 toView.setTranslationY(0.0f);
2829 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002830 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002831 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002832 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002833 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002834 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002835 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002836 // If we don't set the final scale values here, if this animation is cancelled
2837 // it will have the wrong scale value and subsequent cameraPan animations will
2838 // not fix that
2839 toView.setScaleX(1.0f);
2840 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002841 if (toView instanceof LauncherTransitionable) {
2842 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002843 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002844 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002845 });
2846
Chet Haaseb1254a62010-09-07 13:35:00 -07002847 AnimatorSet toolbarHideAnim = new AnimatorSet();
2848 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002849 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2850
2851 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002852 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002853
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002854 if (mStateAnimation != null) mStateAnimation.cancel();
2855 mStateAnimation = new AnimatorSet();
2856 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2857 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002858
2859 // Show the new toolbar buttons just as the main animation is ending
2860 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002861 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2862 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002863 } else {
2864 toView.setTranslationX(0.0f);
2865 toView.setTranslationY(0.0f);
2866 toView.setScaleX(1.0f);
2867 toView.setScaleY(1.0f);
2868 toView.setVisibility(View.VISIBLE);
Michael Jurkaabded662011-03-04 12:06:57 -08002869 if (toView instanceof LauncherTransitionable) {
2870 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2871 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2872 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002873 hideAndShowToolbarButtons(toState, null, null);
2874 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002875 }
2876
2877 /**
2878 * Zoom the camera back into the workspace, hiding 'fromView'.
2879 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002880 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2881 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002882 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002883 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002884 cameraZoomIn(fromState, animated, false);
2885 }
2886
2887 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002888 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002889 final boolean fromAllApps = (fromState == State.ALL_APPS);
2890
2891 int duration = fromAllApps ?
2892 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2893 res.getInteger(R.integer.config_customizeZoomOutTime);
2894
Michael Jurka742574b2011-02-02 23:51:01 -08002895 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002896 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2897 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2898
2899 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002900
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002901 mCustomizePagedView.endChoiceMode();
2902 mAllAppsPagedView.endChoiceMode();
2903
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002904 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002905
Michael Jurkad3ef3062010-11-23 16:23:58 -08002906 if (!springLoaded) {
2907 mWorkspace.unshrink(animated);
2908 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002909
2910 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002911 if (mStateAnimation != null) mStateAnimation.cancel();
2912 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002913
Michael Jurka742574b2011-02-02 23:51:01 -08002914 final float oldScaleX = fromView.getScaleX();
2915 final float oldScaleY = fromView.getScaleY();
2916
2917 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2918 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2919 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2920 public void onAnimationUpdate(ValueAnimator animation) {
2921 final float b = (Float) animation.getAnimatedValue();
2922 final float a = 1f - b;
2923 ((View)fromView.getParent()).fastInvalidate();
2924 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2925 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2926 }
2927 });
Michael Jurkaabded662011-03-04 12:06:57 -08002928 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002929 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002930 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002931 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2932 public void onAnimationUpdate(ValueAnimator animation) {
2933 final float b = (Float) animation.getAnimatedValue();
2934 final float a = 1f - b;
2935 // don't need to invalidate because we do so above
2936 fromView.setFastAlpha(a * 1f + b * 0f);
2937 }
2938 });
Michael Jurkaabded662011-03-04 12:06:57 -08002939 if (fromView instanceof LauncherTransitionable) {
2940 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002941 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002942 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002943 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002944 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002945 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002946 if (fromView instanceof LauncherTransitionable) {
2947 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002948 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002949 }
2950 });
2951
Chet Haaseb1254a62010-09-07 13:35:00 -07002952 AnimatorSet toolbarHideAnim = new AnimatorSet();
2953 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002954 if (!springLoaded) {
2955 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2956 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002957
Adam Cohen61033d32010-11-15 18:29:44 -08002958 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002959
2960 // Show the new toolbar buttons at the very end of the whole animation
2961 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2962 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002963 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2964 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002965 } else {
2966 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002967 if (fromView instanceof LauncherTransitionable) {
2968 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2969 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2970 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002971 if (!springLoaded) {
2972 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2973 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002974 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002975 }
2976
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002977 void showAllApps(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002978 if (mState != State.WORKSPACE) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002979 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002980 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002981
Michael Jurka79212d82010-07-30 16:36:20 -07002982 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002983 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002984 } else {
2985 mAllAppsGrid.zoom(1.0f, animated);
2986 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002987
Romain Guyc16fea72010-03-12 17:17:56 -08002988 ((View) mAllAppsGrid).setFocusable(true);
2989 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002990
Joe Onorato7c312c12009-08-13 21:36:53 -07002991 // TODO: fade these two too
2992 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002993
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002994 // Change the state *after* we've called all the transition code
2995 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002996
Winson Chung5fb63472011-02-02 17:03:37 -08002997 // Pause the auto-advance of widgets until we are out of AllApps
2998 mUserPresent = false;
2999 updateRunning();
3000
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003001 // send an accessibility event to announce the context change
3002 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
3003 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003004
3005 void showWorkspace(boolean animated) {
3006 showWorkspace(animated, null);
3007 }
3008
3009 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08003010 if (layout != null) {
3011 // always animated, but that's ok since we never specify a layout and
3012 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003013 mWorkspace.unshrink(layout);
3014 } else {
3015 mWorkspace.unshrink(animated);
3016 }
3017 if (mState == State.ALL_APPS) {
3018 closeAllApps(animated);
3019 } else if (mState == State.CUSTOMIZE) {
3020 hideCustomizationDrawer(animated);
3021 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003022
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003023 // Change the state *after* we've called all the transition code
3024 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003025
Winson Chung5fb63472011-02-02 17:03:37 -08003026 // Resume the auto-advance of widgets
3027 mUserPresent = true;
3028 updateRunning();
3029
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003030 // send an accessibility event to announce the context change
3031 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04003032 }
3033
Michael Jurkad3ef3062010-11-23 16:23:58 -08003034 void enterSpringLoadedDragMode(CellLayout layout) {
3035 mWorkspace.enterSpringLoadedDragMode(layout);
3036 if (mState == State.ALL_APPS) {
3037 cameraZoomIn(State.ALL_APPS, true, true);
3038 mState = State.ALL_APPS_SPRING_LOADED;
3039 } else if (mState == State.CUSTOMIZE) {
3040 cameraZoomIn(State.CUSTOMIZE, true, true);
3041 mState = State.CUSTOMIZE_SPRING_LOADED;
3042 }/* else {
3043 // we're already in spring loaded mode; don't do anything
3044 }*/
3045 }
3046
3047 void exitSpringLoadedDragMode() {
3048 if (mState == State.ALL_APPS_SPRING_LOADED) {
3049 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
3050 cameraZoomOut(State.ALL_APPS, true);
3051 mState = State.ALL_APPS;
3052 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
3053 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
3054 cameraZoomOut(State.CUSTOMIZE, true);
3055 mState = State.CUSTOMIZE;
3056 }/* else {
3057 // we're not in spring loaded mode; don't do anything
3058 }*/
3059 }
3060
Joe Onoratob2061212009-11-24 16:13:54 -05003061 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08003062 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05003063 * - Home from workspace
3064 * - from center screen
3065 * - from other screens
3066 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003067 * - from center screen
3068 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003069 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003070 * - from center screen
3071 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003072 * - Launch app from workspace and quit
3073 * - with back
3074 * - with home
3075 * - Launch app from all apps and quit
3076 * - with back
3077 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003078 * - Go to a screen that's not the default, then all
3079 * apps, and launch and app, and go back
3080 * - with back
3081 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003082 * - On workspace, long press power and go back
3083 * - with back
3084 * - with home
3085 * - On all apps, long press power and go back
3086 * - with back
3087 * - with home
3088 * - On workspace, power off
3089 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003090 * - Launch an app and turn off the screen while in that app
3091 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003092 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003093 * - From all apps
3094 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003095 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3096 * - From all apps
3097 * - From the center workspace
3098 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003099 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003100 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003101 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003102 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003103 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003104 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003105 } else {
3106 mAllAppsGrid.zoom(0.0f, animated);
3107 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003108 ((View)mAllAppsGrid).setFocusable(false);
Winson Chung097eb0a2011-03-18 16:56:08 -07003109 ((CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage())).getChildrenLayout().requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003110 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003111 }
3112
Joe Onorato7c312c12009-08-13 21:36:53 -07003113 void lockAllApps() {
3114 // TODO
3115 }
3116
3117 void unlockAllApps() {
3118 // TODO
3119 }
3120
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003121 // Show the customization drawer (only exists in x-large configuration)
3122 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003123 if (mState != State.WORKSPACE) {
3124 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003125 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003126
3127 cameraZoomOut(State.CUSTOMIZE, animated);
3128
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003129 // Change the state *after* we've called all the transition code
3130 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003131
3132 // Pause the auto-advance of widgets until we are out of Customization drawer
3133 mUserPresent = false;
3134 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003135 }
3136
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003137 // Hide the customization drawer (only exists in x-large configuration)
3138 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003139 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003140 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003141 }
3142 }
3143
Patrick Dubroy5f445422011-02-18 14:35:21 -08003144 /**
3145 * Add an item from all apps or customize onto the given workspace screen.
3146 * If layout is null, add to the current screen.
3147 */
3148 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003149 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3150 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003151 } else {
3152 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003153 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003154 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003155
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003156 void onWorkspaceClick(CellLayout layout) {
3157 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003158 }
3159
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003160 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003161 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003162 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003163 // Look for the toolbar icon specified in the activity meta-data
3164 Bundle metaData = packageManager.getActivityInfo(
3165 activityName, PackageManager.GET_META_DATA).metaData;
3166 if (metaData != null) {
3167 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3168 if (iconResId != 0) {
3169 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003170 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003171 }
3172 }
3173 } catch (NameNotFoundException e) {
3174 // Do nothing
3175 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003176 return null;
3177 }
3178
3179 // if successful in getting icon, return it; otherwise, set button to use default drawable
3180 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3181 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3182 TextView button = (TextView) findViewById(buttonId);
3183 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3184
3185 // If we were unable to find the icon via the meta-data, use a generic one
3186 if (toolbarIcon == null) {
3187 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3188 return null;
3189 } else {
3190 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3191 return toolbarIcon.getConstantState();
3192 }
3193 }
3194
3195 // if successful in getting icon, return it; otherwise, set button to use default drawable
3196 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3197 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3198 ImageView button = (ImageView) findViewById(buttonId);
3199 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3200
Michael Jurka0423dcf2010-10-05 14:56:18 -07003201 // If we were unable to find the icon via the meta-data, use a generic one
3202 if (toolbarIcon == null) {
3203 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003204 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003205 } else {
3206 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003207 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003208 }
3209 }
3210
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003211 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3212 TextView button = (TextView) findViewById(buttonId);
3213 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3214 }
3215
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003216 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003217 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003218 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003219 }
3220
Michael Jurka0423dcf2010-10-05 14:56:18 -07003221 private void updateGlobalSearchIcon() {
3222 if (LauncherApplication.isScreenXLarge()) {
3223 final SearchManager searchManager =
3224 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3225 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003226 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003227 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003228 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003229 } else {
3230 findViewById(R.id.search_button).setVisibility(View.GONE);
3231 }
3232 }
3233 }
3234
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003235 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003236 updateButtonWithDrawable(R.id.search_button, d);
3237 }
3238
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003239 private void updateVoiceSearchIcon() {
3240 if (LauncherApplication.isScreenXLarge()) {
3241 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3242 ComponentName activityName = intent.resolveActivity(getPackageManager());
3243 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003244 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003245 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3246 } else {
3247 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003248 }
3249 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003250 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003251
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003252 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003253 updateButtonWithDrawable(R.id.voice_button, d);
3254 }
3255
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003256 /**
3257 * Sets the app market icon (shown when all apps is visible on x-large screens)
3258 */
3259 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003260 if (LauncherApplication.isScreenXLarge()) {
3261 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3262 // Find the app market activity by resolving an intent.
3263 // (If multiple app markets are installed, it will return the ResolverActivity.)
3264 ComponentName activityName = intent.resolveActivity(getPackageManager());
3265 if (activityName != null) {
3266 mAppMarketIntent = intent;
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003267 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003268 R.id.market_button, activityName, R.drawable.app_market_generic);
3269 }
3270 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003271 }
3272
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003273 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003274 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003275 }
3276
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003277 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003278 * Displays the shortcut creation dialog and launches, if necessary, the
3279 * appropriate activity.
3280 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003281 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003282 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3283 DialogInterface.OnShowListener {
3284
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003285 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003286
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003287 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003288 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003289
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003290 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3291 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003292 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003293
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003294 builder.setInverseBackgroundForced(true);
3295
3296 AlertDialog dialog = builder.create();
3297 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003298 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003299 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003300
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003301 return dialog;
3302 }
3303
3304 public void onCancel(DialogInterface dialog) {
3305 mWaitingForResult = false;
3306 cleanup();
3307 }
3308
Romain Guycbb89e42009-06-08 15:52:54 -07003309 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003310 }
3311
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003312 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003313 try {
3314 dismissDialog(DIALOG_CREATE_SHORTCUT);
3315 } catch (Exception e) {
3316 // An exception is thrown if the dialog is not visible, which is fine
3317 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003318 }
3319
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003320 /**
3321 * Handle the action clicked in the "Add to home" dialog.
3322 */
3323 public void onClick(DialogInterface dialog, int which) {
3324 Resources res = getResources();
3325 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003326
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003327 switch (which) {
3328 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003329 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003330 break;
3331 }
Romain Guycbb89e42009-06-08 15:52:54 -07003332
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003333 case AddAdapter.ITEM_APPWIDGET: {
3334 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003335
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003336 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3337 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003338 // start the pick activity
3339 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3340 break;
3341 }
Romain Guycbb89e42009-06-08 15:52:54 -07003342
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003343 case AddAdapter.ITEM_LIVE_FOLDER: {
3344 // Insert extra item to handle inserting folder
3345 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003346
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003347 ArrayList<String> shortcutNames = new ArrayList<String>();
3348 shortcutNames.add(res.getString(R.string.group_folder));
3349 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003350
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003351 ArrayList<ShortcutIconResource> shortcutIcons =
3352 new ArrayList<ShortcutIconResource>();
3353 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3354 R.drawable.ic_launcher_folder));
3355 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3356
3357 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3358 pickIntent.putExtra(Intent.EXTRA_INTENT,
3359 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3360 pickIntent.putExtra(Intent.EXTRA_TITLE,
3361 getText(R.string.title_select_live_folder));
3362 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003363
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003364 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3365 break;
3366 }
3367
3368 case AddAdapter.ITEM_WALLPAPER: {
3369 startWallpaper();
3370 break;
3371 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003372 }
3373 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003374
3375 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003376 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003377 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003378 }
3379
3380 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003381 * Receives notifications when applications are added/removed.
3382 */
3383 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3384 @Override
3385 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003386 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003387 String reason = intent.getStringExtra("reason");
3388 if (!"homekey".equals(reason)) {
3389 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003390 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003391 animate = false;
3392 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003393 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003394 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003395 }
3396 }
3397
3398 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003399 * Receives notifications whenever the appwidgets are reset.
3400 */
3401 private class AppWidgetResetObserver extends ContentObserver {
3402 public AppWidgetResetObserver() {
3403 super(new Handler());
3404 }
3405
3406 @Override
3407 public void onChange(boolean selfChange) {
3408 onAppWidgetReset();
3409 }
3410 }
3411
3412 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003413 * If the activity is currently paused, signal that we need to re-run the loader
3414 * in onResume.
3415 *
3416 * This needs to be called from incoming places where resources might have been loaded
3417 * while we are paused. That is becaues the Configuration might be wrong
3418 * when we're not running, and if it comes back to what it was when we
3419 * were paused, we are not restarted.
3420 *
3421 * Implementation of the method from LauncherModel.Callbacks.
3422 *
3423 * @return true if we are currently paused. The caller might be able to
3424 * skip some work in that case since we will come back again.
3425 */
3426 public boolean setLoadOnResume() {
3427 if (mPaused) {
3428 Log.i(TAG, "setLoadOnResume");
3429 mOnResumeNeedsLoad = true;
3430 return true;
3431 } else {
3432 return false;
3433 }
3434 }
3435
3436 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003437 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003438 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003439 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003440 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003441 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003442 } else {
3443 return SCREEN_COUNT / 2;
3444 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003445 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003446
Winson Chunga12a2502010-12-20 14:41:35 -08003447 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003448 mAllAppsPagedView = view;
3449 }
3450
Joe Onorato9c1289c2009-08-17 11:03:03 -04003451 /**
3452 * Refreshes the shortcuts shown on the workspace.
3453 *
3454 * Implementation of the method from LauncherModel.Callbacks.
3455 */
3456 public void startBinding() {
3457 final Workspace workspace = mWorkspace;
3458 int count = workspace.getChildCount();
3459 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003460 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003461 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3462 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003463 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003464
Joe Onorato9c1289c2009-08-17 11:03:03 -04003465 if (DEBUG_USER_INTERFACE) {
3466 android.widget.Button finishButton = new android.widget.Button(this);
3467 finishButton.setText("Finish");
3468 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3469
3470 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3471 public void onClick(View v) {
3472 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003473 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003474 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003475 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003476
3477 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3478 // mDesktopItems -> AppWidgetInfo -> hostView).
3479 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003480 }
3481
3482 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003483 * Bind the items start-end from the list.
3484 *
3485 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003486 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003487 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3488
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003489 setLoadOnResume();
3490
Joe Onorato9c1289c2009-08-17 11:03:03 -04003491 final Workspace workspace = mWorkspace;
3492
3493 for (int i=start; i<end; i++) {
3494 final ItemInfo item = shortcuts.get(i);
3495 mDesktopItems.add(item);
3496 switch (item.itemType) {
3497 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3498 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003499 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003500 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3501 false);
3502 break;
3503 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3504 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003505 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003506 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003507 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3508 false);
3509 break;
3510 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3511 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3512 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003513 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003514 (LiveFolderInfo) item);
3515 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3516 false);
3517 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003518 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003519 }
3520
Joe Onorato9c1289c2009-08-17 11:03:03 -04003521 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003522 }
3523
Joe Onorato9c1289c2009-08-17 11:03:03 -04003524 /**
3525 * Implementation of the method from LauncherModel.Callbacks.
3526 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003527 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003528 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003529 sFolders.clear();
3530 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003531 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003532
3533 /**
3534 * Add the views for a widget to the workspace.
3535 *
3536 * Implementation of the method from LauncherModel.Callbacks.
3537 */
3538 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003539 setLoadOnResume();
3540
Daniel Sandler843e8602010-06-07 14:59:01 -04003541 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3542 if (DEBUG_WIDGETS) {
3543 Log.d(TAG, "bindAppWidget: " + item);
3544 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003545 final Workspace workspace = mWorkspace;
3546
3547 final int appWidgetId = item.appWidgetId;
3548 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003549 if (DEBUG_WIDGETS) {
3550 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3551 }
3552
Joe Onorato9c1289c2009-08-17 11:03:03 -04003553 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3554
Joe Onorato9c1289c2009-08-17 11:03:03 -04003555 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3556 item.hostView.setTag(item);
3557
3558 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3559 item.cellY, item.spanX, item.spanY, false);
3560
Adam Cohended9f8d2010-11-03 13:25:16 -07003561 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3562
Joe Onorato9c1289c2009-08-17 11:03:03 -04003563 workspace.requestLayout();
3564
3565 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003566
3567 if (DEBUG_WIDGETS) {
3568 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3569 + (SystemClock.uptimeMillis()-start) + "ms");
3570 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 }
3572
3573 /**
3574 * Callback saying that there aren't any more items to bind.
3575 *
3576 * Implementation of the method from LauncherModel.Callbacks.
3577 */
3578 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003579 setLoadOnResume();
3580
Joe Onorato9c1289c2009-08-17 11:03:03 -04003581 if (mSavedState != null) {
3582 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003583 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003584 }
3585
3586 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3587 if (userFolders != null) {
3588 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003589 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003590 if (info != null) {
3591 openFolder(info);
3592 }
3593 }
3594 final Folder openFolder = mWorkspace.getOpenFolder();
3595 if (openFolder != null) {
3596 openFolder.requestFocus();
3597 }
3598 }
3599
Joe Onorato9c1289c2009-08-17 11:03:03 -04003600 mSavedState = null;
3601 }
3602
3603 if (mSavedInstanceState != null) {
3604 super.onRestoreInstanceState(mSavedInstanceState);
3605 mSavedInstanceState = null;
3606 }
3607
Winson Chung32bb5e52011-01-31 14:51:56 -08003608 // Workaround a bug that occurs when rotating the device while the customization mode is
3609 // open, we trigger a new layout on all the CellLayout children.
3610 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3611 final int childCount = mWorkspace.getChildCount();
3612 for (int i = 0; i < childCount; ++i) {
3613 mWorkspace.getChildAt(i).requestLayout();
3614 }
3615 }
3616
Joe Onorato9c1289c2009-08-17 11:03:03 -04003617 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003618
3619 // If we received the result of any pending adds while the loader was running (e.g. the
3620 // widget configuration forced an orientation change), process them now.
3621 for (int i = 0; i < sPendingAddList.size(); i++) {
3622 completeAdd(sPendingAddList.get(i));
3623 }
3624 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003625 }
3626
3627 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003628 * Updates the icons on the launcher that are affected by changes to the package list
3629 * on the device.
3630 */
3631 private void updateIconsAffectedByPackageManagerChanges() {
3632 updateAppMarketIcon();
3633 updateGlobalSearchIcon();
3634 updateVoiceSearchIcon();
3635 }
3636
3637 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003638 * Add the icons for all apps.
3639 *
3640 * Implementation of the method from LauncherModel.Callbacks.
3641 */
3642 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003643 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003644 if (mCustomizePagedView != null) {
3645 mCustomizePagedView.setApps(apps);
3646 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003647 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003648 }
3649
3650 /**
3651 * A package was installed.
3652 *
3653 * Implementation of the method from LauncherModel.Callbacks.
3654 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003655 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003656 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003657 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003658 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003659 if (mCustomizePagedView != null) {
3660 mCustomizePagedView.addApps(apps);
3661 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003662 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003663 }
3664
3665 /**
3666 * A package was updated.
3667 *
3668 * Implementation of the method from LauncherModel.Callbacks.
3669 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003670 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003671 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003672 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003673 if (mWorkspace != null) {
3674 mWorkspace.updateShortcuts(apps);
3675 }
3676 if (mAllAppsGrid != null) {
3677 mAllAppsGrid.updateApps(apps);
3678 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003679 if (mCustomizePagedView != null) {
3680 mCustomizePagedView.updateApps(apps);
3681 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003682 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003683 }
3684
3685 /**
3686 * A package was uninstalled.
3687 *
3688 * Implementation of the method from LauncherModel.Callbacks.
3689 */
Joe Onorato36115782010-06-17 13:28:48 -04003690 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003691 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003692 if (permanent) {
3693 mWorkspace.removeItems(apps);
3694 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003695 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003696 if (mCustomizePagedView != null) {
3697 mCustomizePagedView.removeApps(apps);
3698 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003699 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003700 }
Joe Onoratobe386092009-11-17 17:32:16 -08003701
3702 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003703 * A number of packages were updated.
3704 */
3705 public void bindPackagesUpdated() {
3706 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003707 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003708 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003709 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003710 }
3711
Winson Chung400438b2011-01-16 17:53:48 -08003712 private int mapConfigurationOriActivityInfoOri(int configOri) {
3713 final Display d = getWindowManager().getDefaultDisplay();
3714 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3715 switch (d.getRotation()) {
3716 case Surface.ROTATION_0:
3717 case Surface.ROTATION_180:
3718 // We are currently in the same basic orientation as the natural orientation
3719 naturalOri = configOri;
3720 break;
3721 case Surface.ROTATION_90:
3722 case Surface.ROTATION_270:
3723 // We are currently in the other basic orientation to the natural orientation
3724 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3725 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3726 break;
3727 }
3728
3729 int[] oriMap = {
3730 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3731 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3732 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3733 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3734 };
3735 // Since the map starts at portrait, we need to offset if this device's natural orientation
3736 // is landscape.
3737 int indexOffset = 0;
3738 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3739 indexOffset = 1;
3740 }
3741 return oriMap[(d.getRotation() + indexOffset) % 4];
3742 }
3743 public void lockScreenOrientation() {
3744 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3745 .getConfiguration().orientation));
3746 }
3747 public void unlockScreenOrientation() {
3748 mHandler.postDelayed(new Runnable() {
3749 public void run() {
3750 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3751 }
3752 }, mRestoreScreenOrientationDelay);
3753 }
3754
Winson Chung80baf5a2010-08-09 16:03:15 -07003755 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003756 * Prints out out state for debugging.
3757 */
3758 public void dumpState() {
3759 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003760 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003761 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3762 Log.d(TAG, "mRestoring=" + mRestoring);
3763 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3764 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3765 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003766 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003767 mModel.dumpState();
3768 mAllAppsGrid.dumpState();
3769 Log.d(TAG, "END launcher2 dump state");
3770 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003771}
Michael Jurka2763be32011-02-24 11:19:57 -08003772
Michael Jurkaabded662011-03-04 12:06:57 -08003773interface LauncherTransitionable {
3774 void onLauncherTransitionStart(Animator animation);
3775 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003776}