blob: d5bf7c5b4935e8b28153cc0b81211bb21b2aa5af [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka742574b2011-02-02 23:51:01 -080020import com.android.common.Search;
21import com.android.launcher.R;
22import com.android.launcher2.CustomizePagedView.CustomizationType;
23import com.android.launcher2.Workspace.ShrinkState;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070024
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080026import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070028import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070029import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080031import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070032import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.app.Dialog;
35import android.app.SearchManager;
36import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070037import android.appwidget.AppWidgetManager;
38import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080040import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070041import android.content.ClipData;
42import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040043import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080044import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.Context;
46import android.content.DialogInterface;
47import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080048import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080049import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080052import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080053import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070055import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040056import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080057import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080059import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080061import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070062import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040063import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070064import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020066import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080067import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070068import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040070import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080071import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070072import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070073import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080074import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.text.Selection;
76import android.text.SpannableStringBuilder;
77import android.text.TextUtils;
78import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070079import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080080import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080081import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082import android.view.KeyEvent;
83import android.view.LayoutInflater;
84import android.view.Menu;
85import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070086import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080087import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.View;
89import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070090import android.view.WindowManager;
Michael Jurkae0f5a612011-02-07 16:45:41 -080091import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080092import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080093import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080094import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070096import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070098import android.widget.ImageView;
99import android.widget.LinearLayout;
100import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700101import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -0800102import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -0700103import android.widget.TextView;
104import android.widget.Toast;
Adam Cohen860f4c52011-01-27 20:16:13 -0800105import android.widget.TabHost.OnTabChangeListener;
106import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700107
Michael Jurka742574b2011-02-02 23:51:01 -0800108import java.io.DataInputStream;
109import java.io.DataOutputStream;
110import java.io.FileNotFoundException;
111import java.io.IOException;
112import java.util.ArrayList;
113import java.util.HashMap;
114import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800115
Adam Cohended9f8d2010-11-03 13:25:16 -0700116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117/**
118 * Default launcher application.
119 */
Michael Jurka946ad472010-07-09 18:05:18 -0700120public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700121 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
122 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800123 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Joe Onorato9c1289c2009-08-17 11:03:03 -0400126 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400127 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400128 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800131 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
132
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700134 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
135 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
137 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700138 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
140 private static final int REQUEST_CREATE_SHORTCUT = 1;
141 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700142 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 private static final int REQUEST_PICK_APPLICATION = 6;
144 private static final int REQUEST_PICK_SHORTCUT = 7;
145 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700146 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700147 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
149 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
150
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800151 static final int SCREEN_COUNT = 5;
152 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Joe Onorato7c312c12009-08-13 21:36:53 -0700154 static final int DIALOG_CREATE_SHORTCUT = 1;
155 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Romain Guy98d01652009-06-30 16:21:04 -0700157 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
159 // Type: int
160 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700161 // Type: int
162 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: long
164 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
169 // Type: int
170 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 // Type: boolean
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
173 // Type: long
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
175
Winson Chung80baf5a2010-08-09 16:03:15 -0700176 // tags for the customization tabs
177 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700178 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700179 private static final String SHORTCUTS_TAG = "shortcuts";
180 private static final String WALLPAPERS_TAG = "wallpapers";
181
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700182 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
183
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700184 /** The different states that Launcher can be in. */
Michael Jurkaafca9532011-02-16 14:50:35 -0800185 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800186 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700187 private State mState = State.WORKSPACE;
188 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700189
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800193 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800195 private final BroadcastReceiver mCloseSystemDialogsReceiver
196 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800197 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 private LayoutInflater mInflater;
200
Joe Onorato00acb122009-08-04 16:04:30 -0400201 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700203
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700204 private AppWidgetManager mAppWidgetManager;
205 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700206
Michael Jurka0280c3b2010-09-17 15:00:07 -0700207 private int mAddScreen = -1;
208 private int mAddIntersectCellX = -1;
209 private int mAddIntersectCellY = -1;
210 private int[] mAddDropPosition;
211 private int[] mTmpAddItemCellCoordinates = new int[2];
212
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 private FolderInfo mFolderInfo;
214
Joe Onorato7c312c12009-08-13 21:36:53 -0700215 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700216 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700217 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700218 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700219 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700220
Michael Jurka838a4ca2011-02-07 13:33:06 -0800221 private View mButtonCluster;
222 private View mAllAppsButton;
223 private View mDivider;
224 private View mConfigureButton;
225
Winson Chunga12a2502010-12-20 14:41:35 -0800226 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700227 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 private Bundle mSavedState;
230
231 private SpannableStringBuilder mDefaultKeySsb = null;
232
Joe Onorato9c1289c2009-08-17 11:03:03 -0400233 private boolean mWorkspaceLoading = true;
234
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800235 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 private boolean mRestoring;
237 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700238 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239
240 private Bundle mSavedInstanceState;
241
Joe Onorato9c1289c2009-08-17 11:03:03 -0400242 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800243 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800244 private boolean mUserPresent = true;
245 private boolean mVisible = false;
246 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400247
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700248 private static LocaleConfiguration sLocaleConfiguration = null;
249
Romain Guy84f296c2009-11-04 15:00:44 -0800250 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700251
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700252 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700253
Romain Guy1fbc1c82009-11-09 20:43:08 -0800254 private ImageView mPreviousView;
255 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500256
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400257 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400258 private String[] mHotseatConfig = null;
259 private Intent[] mHotseats = null;
260 private Drawable[] mHotseatIcons = null;
261 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400262
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700263 private Intent mAppMarketIntent = null;
264
Adam Cohended9f8d2010-11-03 13:25:16 -0700265 // Related to the auto-advancing of widgets
266 private final int ADVANCE_MSG = 1;
267 private final int mAdvanceInterval = 20000;
268 private final int mAdvanceStagger = 250;
269 private long mAutoAdvanceSentTime;
270 private long mAutoAdvanceTimeLeft = -1;
271 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
272 new HashMap<View, AppWidgetProviderInfo>();
273
Winson Chung400438b2011-01-16 17:53:48 -0800274 // Determines how long to wait after a rotation before restoring the screen orientation to
275 // match the sensor state.
276 private final int mRestoreScreenOrientationDelay = 500;
277
Michael Jurka4ef207b2010-11-29 17:05:45 -0800278 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800279 private static Drawable.ConstantState sGlobalSearchIcon;
280 private static Drawable.ConstantState sVoiceSearchIcon;
281 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800282
Michael Jurkaddd62e92011-02-16 17:49:14 -0800283 private BubbleTextView mWaitingForResume;
284
Winson Chunga12a2502010-12-20 14:41:35 -0800285 private CustomizationType getCustomizeFilterForTabTag(String tag) {
286 if (tag.equals(WIDGETS_TAG)) {
287 return CustomizationType.WidgetCustomization;
288 } else if (tag.equals(APPLICATIONS_TAG)) {
289 return CustomizationType.ApplicationCustomization;
290 } else if (tag.equals(WALLPAPERS_TAG)) {
291 return CustomizePagedView.CustomizationType.WallpaperCustomization;
292 } else if (tag.equals(SHORTCUTS_TAG)) {
293 return CustomizePagedView.CustomizationType.ShortcutCustomization;
294 }
295 return CustomizationType.WidgetCustomization;
296 }
297
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 @Override
299 protected void onCreate(Bundle savedInstanceState) {
300 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700301
Winson Chungaafa03c2010-06-11 17:34:16 -0700302 if (LauncherApplication.isInPlaceRotationEnabled()) {
303 // hide the status bar (temporary until we get the status bar design figured out)
304 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
305 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
306 }
307
Joe Onorato0589f0f2010-02-08 13:44:00 -0800308 LauncherApplication app = ((LauncherApplication)getApplication());
309 mModel = app.setLauncher(this);
310 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400311 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700313
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700314 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700315 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
316 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700317
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200319 android.os.Debug.startMethodTracing(
320 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 }
322
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400323 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800324 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800326 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700327 if (mHomeCustomizationDrawer != null) {
328 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700329
Winson Chung80baf5a2010-08-09 16:03:15 -0700330 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700331 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
332 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700333 TabContentFactory contentFactory = new TabContentFactory() {
334 public View createTabContent(String tag) {
335 return mCustomizePagedView;
336 }
337 };
338
Winson Chung49767ae2010-11-29 14:48:30 -0800339
340 TextView tabView;
341 TabWidget tabWidget = (TabWidget)
342 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
343
344 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
345 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700346 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800347 .setIndicator(tabView).setContent(contentFactory));
348 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
349 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700350 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800351 .setIndicator(tabView).setContent(contentFactory));
352 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
353 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700354 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800355 .setIndicator(tabView).setContent(contentFactory));
356 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
357 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700358 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800359 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700360 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
361 public void onTabChanged(String tabId) {
Winson Chunga12a2502010-12-20 14:41:35 -0800362 final CustomizePagedView.CustomizationType newType =
363 getCustomizeFilterForTabTag(tabId);
364 if (newType != mCustomizePagedView.getCustomizationFilter()) {
365 // animate the changing of the tab content by fading pages in and out
366 final Resources res = getResources();
367 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
368 final float alpha = mCustomizePagedView.getAlpha();
369 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
370 "alpha", alpha, 0.0f);
371 alphaAnim.setDuration(duration);
372 alphaAnim.addListener(new AnimatorListenerAdapter() {
373 @Override
374 public void onAnimationEnd(Animator animation) {
Winson Chunga12a2502010-12-20 14:41:35 -0800375 mCustomizePagedView.setCustomizationFilter(newType);
Winson Chung80baf5a2010-08-09 16:03:15 -0700376
Winson Chunga12a2502010-12-20 14:41:35 -0800377 final float alpha = mCustomizePagedView.getAlpha();
378 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
379 mCustomizePagedView, "alpha", alpha, 1.0f);
380 alphaAnim.setDuration(duration);
381 alphaAnim.start();
382 }
383 });
384 alphaAnim.start();
385 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700386 }
387 });
Michael Jurka946ad472010-07-09 18:05:18 -0700388 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 setupViews();
390
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800391 registerContentObservers();
392
Joe Onorato7c312c12009-08-13 21:36:53 -0700393 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 mSavedState = savedInstanceState;
396 restoreState(mSavedState);
397
Winson Chunga12a2502010-12-20 14:41:35 -0800398 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800399 if (mCustomizePagedView != null) {
400 mCustomizePagedView.update();
401 }
Winson Chunga12a2502010-12-20 14:41:35 -0800402
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 if (PROFILE_STARTUP) {
404 android.os.Debug.stopMethodTracing();
405 }
406
407 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400408 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 }
410
411 // For handling default keys
412 mDefaultKeySsb = new SpannableStringBuilder();
413 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800414
415 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
416 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800417
418 // If we have a saved version of these external icons, we load them up immediately
419 if (LauncherApplication.isScreenXLarge()) {
420 if (sGlobalSearchIcon != null) {
421 updateGlobalSearchIcon(sGlobalSearchIcon);
422 }
423 if (sVoiceSearchIcon != null) {
424 updateVoiceSearchIcon(sVoiceSearchIcon);
425 }
426 if (sAppMarketIcon != null) {
427 updateAppMarketIcon(sAppMarketIcon);
428 }
429 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 }
Romain Guycbb89e42009-06-08 15:52:54 -0700431
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800432 @Override
433 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
434 super.dispatchPopulateAccessibilityEvent(event);
435
436 // we want to take over text population so it is context dependent
437 event.getText().clear();
438 if (mState == State.ALL_APPS) {
439 event.getText().add(getString(R.string.all_apps_button_label));
440 } else if (mState == State.WORKSPACE) {
441 event.getText().add(getString(R.string.all_apps_home_button_label));
442 }
443
444 return true;
445 }
446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700448 if (sLocaleConfiguration == null) {
449 new AsyncTask<Void, Void, LocaleConfiguration>() {
450 @Override
451 protected LocaleConfiguration doInBackground(Void... unused) {
452 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
453 readConfiguration(Launcher.this, localeConfiguration);
454 return localeConfiguration;
455 }
456
457 @Override
458 protected void onPostExecute(LocaleConfiguration result) {
459 sLocaleConfiguration = result;
460 checkForLocaleChange(); // recursive, but now with a locale configuration
461 }
462 }.execute();
463 return;
464 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700465
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 final Configuration configuration = getResources().getConfiguration();
467
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700468 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 final String locale = configuration.locale.toString();
470
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700471 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 final int mcc = configuration.mcc;
473
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700474 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 final int mnc = configuration.mnc;
476
Romain Guy84f296c2009-11-04 15:00:44 -0800477 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478
Romain Guy84f296c2009-11-04 15:00:44 -0800479 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700480 sLocaleConfiguration.locale = locale;
481 sLocaleConfiguration.mcc = mcc;
482 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700483
Joe Onorato0589f0f2010-02-08 13:44:00 -0800484 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400485 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700486
487 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
488 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700489 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700490 public void run() {
491 writeConfiguration(Launcher.this, localeConfiguration);
492 }
493 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700494 }
495 }
496
497 private static class LocaleConfiguration {
498 public String locale;
499 public int mcc = -1;
500 public int mnc = -1;
501 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700502
Romain Guy98d01652009-06-30 16:21:04 -0700503 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
504 DataInputStream in = null;
505 try {
506 in = new DataInputStream(context.openFileInput(PREFERENCES));
507 configuration.locale = in.readUTF();
508 configuration.mcc = in.readInt();
509 configuration.mnc = in.readInt();
510 } catch (FileNotFoundException e) {
511 // Ignore
512 } catch (IOException e) {
513 // Ignore
514 } finally {
515 if (in != null) {
516 try {
517 in.close();
518 } catch (IOException e) {
519 // Ignore
520 }
521 }
522 }
523 }
524
525 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
526 DataOutputStream out = null;
527 try {
528 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
529 out.writeUTF(configuration.locale);
530 out.writeInt(configuration.mcc);
531 out.writeInt(configuration.mnc);
532 out.flush();
533 } catch (FileNotFoundException e) {
534 // Ignore
535 } catch (IOException e) {
536 //noinspection ResultOfMethodCallIgnored
537 context.getFileStreamPath(PREFERENCES).delete();
538 } finally {
539 if (out != null) {
540 try {
541 out.close();
542 } catch (IOException e) {
543 // Ignore
544 }
545 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 }
547 }
548
549 static int getScreen() {
550 synchronized (sLock) {
551 return sScreen;
552 }
553 }
554
555 static void setScreen(int screen) {
556 synchronized (sLock) {
557 sScreen = screen;
558 }
559 }
560
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400561 // Note: This doesn't do all the client-id magic that BrowserProvider does
562 // in Browser. (http://b/2425179)
563 private Uri getDefaultBrowserUri() {
564 String url = getString(R.string.default_browser_url);
565 if (url.indexOf("{CID}") != -1) {
566 url = url.replace("{CID}", "android-google");
567 }
568 return Uri.parse(url);
569 }
570
571 // Load the Intent templates from arrays.xml to populate the hotseats. For
572 // each Intent, if it resolves to a single app, use that as the launch
573 // intent & use that app's label as the contentDescription. Otherwise,
574 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400575 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400576 if (mHotseatConfig == null) {
577 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
578 if (mHotseatConfig.length > 0) {
579 mHotseats = new Intent[mHotseatConfig.length];
580 mHotseatLabels = new CharSequence[mHotseatConfig.length];
581 mHotseatIcons = new Drawable[mHotseatConfig.length];
582 } else {
583 mHotseats = null;
584 mHotseatIcons = null;
585 mHotseatLabels = null;
586 }
587
588 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
589 for (int i=0; i<mHotseatConfig.length; i++) {
590 // load icon for this slot; currently unrelated to the actual activity
591 try {
592 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
593 } catch (ArrayIndexOutOfBoundsException ex) {
594 Log.w(TAG, "Missing hotseat_icons array item #" + i);
595 mHotseatIcons[i] = null;
596 }
597 }
598 hotseatIconDrawables.recycle();
599 }
600
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400601 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400602 for (int i=0; i<mHotseatConfig.length; i++) {
603 Intent intent = null;
604 if (mHotseatConfig[i].equals("*BROWSER*")) {
605 // magic value meaning "launch user's default web browser"
606 // replace it with a generic web request so we can see if there is indeed a default
607 String defaultUri = getString(R.string.default_browser_url);
608 intent = new Intent(
609 Intent.ACTION_VIEW,
610 ((defaultUri != null)
611 ? Uri.parse(defaultUri)
612 : getDefaultBrowserUri())
613 ).addCategory(Intent.CATEGORY_BROWSABLE);
614 // note: if the user launches this without a default set, she
615 // will always be taken to the default URL above; this is
616 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700617 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400618 // URL is unavoidably sent to the chosen app.
619 } else {
620 try {
621 intent = Intent.parseUri(mHotseatConfig[i], 0);
622 } catch (java.net.URISyntaxException ex) {
623 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
624 // bogus; leave intent=null
625 }
626 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700627
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400628 if (intent == null) {
629 mHotseats[i] = null;
630 mHotseatLabels[i] = getText(R.string.activity_not_found);
631 continue;
632 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400633
634 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700635 Log.d(TAG, "loadHotseats: hotseat " + i
636 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400637 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400638 + "]");
639 }
640
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400641 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
642 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700643 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400644 Log.d(TAG, "Best match for intent: " + bestMatch);
645 Log.d(TAG, "All matches: ");
646 for (ResolveInfo ri : allMatches) {
647 Log.d(TAG, " --> " + ri);
648 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400649 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400650 // did this resolve to a single app, or the resolver?
651 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700652 // can't find any activity to handle this. let's leave the
653 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400654 // to launch.
655 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400656
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400657 // set accessibility text to "Not installed"
658 mHotseatLabels[i] = getText(R.string.activity_not_found);
659 } else {
660 boolean found = false;
661 for (ResolveInfo ri : allMatches) {
662 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
663 && bestMatch.activityInfo.applicationInfo.packageName
664 .equals(ri.activityInfo.applicationInfo.packageName)) {
665 found = true;
666 break;
667 }
668 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700669
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400670 if (!found) {
671 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
672 // the bestMatch is probably the ResolveActivity, meaning the
673 // user has not yet selected a default
674 // so: we'll keep the original intent for now
675 mHotseats[i] = intent;
676
677 // set the accessibility text to "Select shortcut"
678 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
679 } else {
680 // we have an app!
681 // now reconstruct the intent to launch it through the front
682 // door
683 ComponentName com = new ComponentName(
684 bestMatch.activityInfo.applicationInfo.packageName,
685 bestMatch.activityInfo.name);
686 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
687
688 // load the app label for accessibility
689 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
690 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400691 }
692
693 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700694 Log.d(TAG, "loadHotseats: hotseat " + i
695 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400696 + ((mHotseats[i] == null)
697 ? "null"
698 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400699 + "] label=[" + mHotseatLabels[i]
700 + "]"
701 );
702 }
703 }
704 }
705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 @Override
707 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700708 mWaitingForResult = false;
709
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 // The pattern used here is that a user PICKs a specific application,
711 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700712
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
714 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700715
Michael Jurka0280c3b2010-09-17 15:00:07 -0700716 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 switch (requestCode) {
718 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700719 completeAddApplication(
720 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 break;
722 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800723 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 break;
725 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700726 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 break;
728 case REQUEST_PICK_LIVE_FOLDER:
729 addLiveFolder(data);
730 break;
731 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700732 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700734 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700735 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700737 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700738 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700739 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700741 case REQUEST_PICK_WALLPAPER:
742 // We just wanted the activity result here so we can clear mWaitingForResult
743 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 }
Romain Guy18042c82009-11-06 11:44:55 -0800745 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
746 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
747 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700748 // Clean up the appWidgetId if we canceled
749 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
750 if (appWidgetId != -1) {
751 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 }
755
756 @Override
757 protected void onResume() {
758 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800759 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700760 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400761 mWorkspaceLoading = true;
762 mModel.startLoader(this, true);
763 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700764 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800766 if (mWaitingForResume != null) {
767 mWaitingForResume.setStayPressed(false);
768 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700769 // When we resume Launcher, a different Activity might be responsible for the app
770 // market intent, so refresh the icon
771 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 }
773
774 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700775 protected void onPause() {
776 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700777 // Some launcher layouts don't have a previous and next view
778 if (mPreviousView != null) {
779 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700780 }
781 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700782 dismissPreview(mNextView);
783 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700784 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800785 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700786 }
Romain Guycbb89e42009-06-08 15:52:54 -0700787
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700788 @Override
789 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790 // Flag the loader to stop early before switching
791 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800792 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800793 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700794 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700795
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800796 // We can't hide the IME if it was forced open. So don't bother
797 /*
798 @Override
799 public void onWindowFocusChanged(boolean hasFocus) {
800 super.onWindowFocusChanged(hasFocus);
801
802 if (hasFocus) {
803 final InputMethodManager inputManager = (InputMethodManager)
804 getSystemService(Context.INPUT_METHOD_SERVICE);
805 WindowManager.LayoutParams lp = getWindow().getAttributes();
806 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
807 android.os.Handler()) {
808 protected void onReceiveResult(int resultCode, Bundle resultData) {
809 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
810 }
811 });
812 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
813 }
814 }
815 */
816
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 private boolean acceptFilter() {
818 final InputMethodManager inputManager = (InputMethodManager)
819 getSystemService(Context.INPUT_METHOD_SERVICE);
820 return !inputManager.isFullscreenMode();
821 }
822
823 @Override
824 public boolean onKeyDown(int keyCode, KeyEvent event) {
825 boolean handled = super.onKeyDown(keyCode, event);
826 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
827 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
828 keyCode, event);
829 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700830 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700831 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700832 // showSearchDialog()
833 // If there are multiple keystrokes before the search dialog takes focus,
834 // onSearchRequested() will be called for every keystroke,
835 // but it is idempotent, so it's fine.
836 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 }
838 }
839
Joe Onorato8a9625e2010-01-28 15:55:35 -0800840 // Eat the long press event so the keyboard doesn't come up.
841 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
842 return true;
843 }
844
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 return handled;
846 }
847
Karl Rosaen138a0412009-04-23 19:00:21 -0700848 private String getTypedText() {
849 return mDefaultKeySsb.toString();
850 }
851
852 private void clearTypedText() {
853 mDefaultKeySsb.clear();
854 mDefaultKeySsb.clearSpans();
855 Selection.setSelection(mDefaultKeySsb, 0);
856 }
857
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700859 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
860 * State
861 */
862 private static State intToState(int stateOrdinal) {
863 State state = State.WORKSPACE;
864 final State[] stateValues = State.values();
865 for (int i = 0; i < stateValues.length; i++) {
866 if (stateValues[i].ordinal() == stateOrdinal) {
867 state = stateValues[i];
868 break;
869 }
870 }
871 return state;
872 }
873
874 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 * Restores the previous state, if it exists.
876 *
877 * @param savedState The previous state.
878 */
879 private void restoreState(Bundle savedState) {
880 if (savedState == null) {
881 return;
882 }
883
Michael Jurka883f55b2010-10-21 15:47:14 -0700884 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
885
886 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800887 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700888 } else if (state == State.CUSTOMIZE) {
889 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800890 }
891
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
893 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700894 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 }
896
897 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700898
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700900 mAddScreen = addScreen;
901 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
902 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 mRestoring = true;
904 }
905
906 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
907 if (renameFolder) {
908 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700909 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 mRestoring = true;
911 }
Winson Chunga12a2502010-12-20 14:41:35 -0800912
913 // Restore the current AllApps drawer tab
914 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
915 String curTab = savedState.getString("allapps_currentTab");
916 if (curTab != null) {
917 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
918 tabhost.setCurrentTabByTag(curTab);
919 }
920 int curPage = savedState.getInt("allapps_currentPage", -1);
921 if (curPage > -1) {
922 mAllAppsPagedView.setRestorePage(curPage);
923 }
924 }
925
926 // Restore the current customization drawer tab
927 if (mHomeCustomizationDrawer != null) {
928 String curTab = savedState.getString("customize_currentTab");
929 if (curTab != null) {
930 // We set this directly so that there is no delay before the tab is set
931 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
932 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
933 }
934
935 // Note: currently we do not restore the page for the customization tray because unlike
936 // AllApps, the page content can change drastically
937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939
940 /**
941 * Finds all the views we need and configure them properly.
942 */
943 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700944 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400945
Romain Guyb1b69f52009-08-10 15:10:15 -0700946 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400947 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948
Joe Onorato7c312c12009-08-13 21:36:53 -0700949 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700950 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700951 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800952 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700953 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700954 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700955
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700956 if (LauncherApplication.isScreenXLarge()) {
957 // They need to be INVISIBLE initially so that they will be measured in the layout.
958 // Otherwise the animations are messed up when we show them for the first time.
959 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
960 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
961 }
962
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700964
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500966 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700967
Joe Onorato7c312c12009-08-13 21:36:53 -0700968 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
969 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700971 View handleView = findViewById(R.id.all_apps_button);
972 if (handleView != null && handleView instanceof HandleView) {
973 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700974 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700975 mHandleView.setLauncher(this);
976 mHandleView.setOnClickListener(this);
977 mHandleView.setOnLongClickListener(this);
978 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800979
Winson Chung80baf5a2010-08-09 16:03:15 -0700980 if (mCustomizePagedView != null) {
981 mCustomizePagedView.setLauncher(this);
982 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700983 } else {
984 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
985 hotseatLeft.setContentDescription(mHotseatLabels[0]);
986 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
987 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
988 hotseatRight.setContentDescription(mHotseatLabels[1]);
989 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400990
Michael Jurkaaf442092010-06-10 17:01:57 -0700991 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
992 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800993
Michael Jurkaaf442092010-06-10 17:01:57 -0700994 Drawable previous = mPreviousView.getDrawable();
995 Drawable next = mNextView.getDrawable();
996 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997
Michael Jurkaaf442092010-06-10 17:01:57 -0700998 mPreviousView.setHapticFeedbackEnabled(false);
999 mPreviousView.setOnLongClickListener(this);
1000 mNextView.setHapticFeedbackEnabled(false);
1001 mNextView.setOnLongClickListener(this);
1002 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001003
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001005 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -08001007 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008
1009 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001010 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -08001011
1012 final View allAppsButton = findViewById(R.id.all_apps_button);
1013 final View divider = findViewById(R.id.divider);
1014 final View configureButton = findViewById(R.id.configure_button);
1015
Michael Jurka54f7ac32010-08-02 13:56:46 -07001016 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001017 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -07001018 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001019 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001020 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001021 dragController.addDragListener(deleteZone);
1022
Adam Cohencdc30d52010-12-01 15:09:47 -08001023 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1024 if (allAppsDeleteZone != null) {
1025 allAppsDeleteZone.setLauncher(this);
1026 allAppsDeleteZone.setDragController(dragController);
1027 allAppsDeleteZone.setDragAndDropEnabled(false);
1028 dragController.addDragListener(allAppsDeleteZone);
1029 dragController.addDropTarget(allAppsDeleteZone);
1030 }
1031
1032 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1033 findViewById(R.id.all_apps_info_target);
1034 if (allAppsInfoTarget != null) {
1035 allAppsInfoTarget.setLauncher(this);
1036 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001037 allAppsInfoTarget.setDragAndDropEnabled(false);
1038 View marketButton = findViewById(R.id.market_button);
1039 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001040 allAppsInfoTarget.setOverlappingView(marketButton);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08001041 marketButton.setOnClickListener(new OnClickListener() {
1042 public void onClick(View v) {
1043 onClickAppMarketButton(v);
1044 }
1045 });
Adam Cohencdc30d52010-12-01 15:09:47 -08001046 }
1047 }
1048
Joe Onorato00acb122009-08-04 16:04:30 -04001049 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001050 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001051 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001052
Joe Onorato00acb122009-08-04 16:04:30 -04001053 // The order here is bottom to top.
1054 dragController.addDropTarget(workspace);
1055 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001056 if (allAppsInfoTarget != null) {
1057 dragController.addDropTarget(allAppsInfoTarget);
1058 }
1059 if (allAppsDeleteZone != null) {
1060 dragController.addDropTarget(allAppsDeleteZone);
1061 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001062 mButtonCluster = findViewById(R.id.all_apps_button_cluster);
1063
1064 mAllAppsButton = findViewById(R.id.all_apps_button);
1065 mDivider = findViewById(R.id.divider);
1066 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001067
1068 // We had previously set these click handlers in XML, but the first time we launched
1069 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1070 // found the right handler. Setting the handlers directly here eliminates that cost.
1071 if (mConfigureButton != null) {
1072 mConfigureButton.setOnClickListener(new OnClickListener() {
1073 public void onClick(View v) {
1074 onClickConfigureButton(v);
1075 }
1076 });
1077 }
1078 if (mDivider != null) {
1079 mDivider.setOnClickListener(new OnClickListener() {
1080 public void onClick(View v) {
1081 onClickAllAppsButton(v);
1082 }
1083 });
1084 }
1085 if (mAllAppsButton != null) {
1086 mAllAppsButton.setOnClickListener(new OnClickListener() {
1087 public void onClick(View v) {
1088 onClickAllAppsButton(v);
1089 }
1090 });
1091 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 }
1093
Romain Guy8a73c512009-11-09 19:19:59 -08001094 @SuppressWarnings({"UnusedDeclaration"})
1095 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001096 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001097 mWorkspace.scrollLeft();
1098 }
Romain Guy8a73c512009-11-09 19:19:59 -08001099 }
1100
1101 @SuppressWarnings({"UnusedDeclaration"})
1102 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001103 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001104 mWorkspace.scrollRight();
1105 }
Romain Guy8a73c512009-11-09 19:19:59 -08001106 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001107
1108 @SuppressWarnings({"UnusedDeclaration"})
1109 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001110 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001111
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001112 int index = -1;
1113 if (v.getId() == R.id.hotseat_left) {
1114 index = 0;
1115 } else if (v.getId() == R.id.hotseat_right) {
1116 index = 1;
1117 }
1118
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001119 // reload these every tap; you never know when they might change
1120 loadHotseats();
1121 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001122 startActivitySafely(
1123 mHotseats[index],
1124 "hotseat"
1125 );
1126 }
1127 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 /**
1130 * Creates a view representing a shortcut.
1131 *
1132 * @param info The data structure describing the shortcut.
1133 *
1134 * @return A View inflated from R.layout.application.
1135 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001136 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001138 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 }
1140
1141 /**
1142 * Creates a view representing a shortcut inflated from the specified resource.
1143 *
1144 * @param layoutResId The id of the XML layout used to create the shortcut.
1145 * @param parent The group the shortcut belongs to.
1146 * @param info The data structure describing the shortcut.
1147 *
1148 * @return A View inflated from layoutResId.
1149 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001150 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001151 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1152 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 return favorite;
1155 }
1156
1157 /**
1158 * Add an application shortcut to the workspace.
1159 *
1160 * @param data The intent describing the application.
1161 * @param cellInfo The position on screen where to create the shortcut.
1162 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001163 void completeAddApplication(Context context, Intent data, int screen,
1164 int intersectCellX, int intersectCellY) {
1165 final int[] cellXY = mTmpAddItemCellCoordinates;
1166 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1167
1168 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1169 showOutOfSpaceMessage();
1170 return;
1171 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172
Joe Onorato0589f0f2010-02-08 13:44:00 -08001173 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1174 data, context);
1175
Romain Guy73b979d2009-06-09 12:57:21 -07001176 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001177 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001179 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001180 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1181 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001182 } else {
1183 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 }
1185 }
Romain Guycbb89e42009-06-08 15:52:54 -07001186
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 /**
1188 * Add a shortcut to the workspace.
1189 *
1190 * @param data The intent describing the shortcut.
1191 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001193 private void completeAddShortcut(Intent data, int screen,
1194 int intersectCellX, int intersectCellY) {
1195 final int[] cellXY = mTmpAddItemCellCoordinates;
1196 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001197
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001198 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001199 boolean foundCellSpan = false;
1200 if (touchXY != null) {
1201 // when dragging and dropping, just find the closest free spot
1202 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1203 int[] result = screenLayout.findNearestVacantArea(
1204 touchXY[0], touchXY[1], 1, 1, cellXY);
1205 foundCellSpan = (result != null);
1206 } else {
1207 foundCellSpan = layout.findCellForSpanThatIntersects(
1208 cellXY, 1, 1, intersectCellX, intersectCellY);
1209 }
1210
1211 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001212 showOutOfSpaceMessage();
1213 return;
1214 }
1215
1216 final ShortcutInfo info = mModel.addShortcut(
1217 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218
1219 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001221 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 }
1223 }
1224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001226 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001228 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001229 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001231 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001232 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001233
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001234 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001235 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1236 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001239 // We have saved the position to which the widget was dragged-- this really only matters
1240 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001241 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001242
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001243 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001244 boolean foundCellSpan = false;
1245 if (touchXY != null) {
1246 // when dragging and dropping, just find the closest free spot
1247 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1248 int[] result = screenLayout.findNearestVacantArea(
1249 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001250 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001251 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001252 // if we long pressed on an empty cell to bring up a menu,
1253 // make sure we intersect the empty cell
1254 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1255 // findCellForSpanThatIntersects will just ignore them
1256 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1257 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001258 }
1259
Michael Jurka0280c3b2010-09-17 15:00:07 -07001260 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001261 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001262 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001263 return;
1264 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001266 // Build Launcher-specific widget info and save to database
1267 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001268 launcherInfo.spanX = spanXY[0];
1269 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 LauncherModel.addItemToDatabase(this, launcherInfo,
1272 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001273 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274
1275 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001276 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001277
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001279 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001280
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001281 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001283
Michael Jurka0280c3b2010-09-17 15:00:07 -07001284 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001285 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001286
1287 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 }
1289 }
Romain Guycbb89e42009-06-08 15:52:54 -07001290
Adam Cohended9f8d2010-11-03 13:25:16 -07001291 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1292 @Override
1293 public void onReceive(Context context, Intent intent) {
1294 final String action = intent.getAction();
1295 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1296 mUserPresent = false;
1297 updateRunning();
1298 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1299 mUserPresent = true;
1300 updateRunning();
1301 }
1302 }
1303 };
1304
1305 @Override
1306 public void onAttachedToWindow() {
1307 super.onAttachedToWindow();
1308
1309 // Listen for broadcasts related to user-presence
1310 final IntentFilter filter = new IntentFilter();
1311 filter.addAction(Intent.ACTION_SCREEN_OFF);
1312 filter.addAction(Intent.ACTION_USER_PRESENT);
1313 registerReceiver(mReceiver, filter);
1314
Adam Cohend113e0c2010-11-11 10:48:05 -08001315 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001316 mVisible = true;
1317 }
1318
1319 @Override
1320 public void onDetachedFromWindow() {
1321 super.onDetachedFromWindow();
1322 mVisible = false;
1323
Adam Cohend113e0c2010-11-11 10:48:05 -08001324 if (mAttached) {
1325 unregisterReceiver(mReceiver);
1326 mAttached = false;
1327 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001328 updateRunning();
1329 }
1330
1331 public void onWindowVisibilityChanged(int visibility) {
1332 mVisible = visibility == View.VISIBLE;
1333 updateRunning();
1334 }
1335
1336 private void sendAdvanceMessage(long delay) {
1337 mHandler.removeMessages(ADVANCE_MSG);
1338 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1339 mHandler.sendMessageDelayed(msg, delay);
1340 mAutoAdvanceSentTime = System.currentTimeMillis();
1341 }
1342
1343 private void updateRunning() {
1344 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1345 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1346 mAutoAdvanceRunning = autoAdvanceRunning;
1347 if (autoAdvanceRunning) {
1348 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1349 sendAdvanceMessage(delay);
1350 } else {
1351 if (!mWidgetsToAdvance.isEmpty()) {
1352 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1353 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1354 }
1355 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001356 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001357 }
1358 }
1359 }
1360
1361 private final Handler mHandler = new Handler() {
1362 @Override
1363 public void handleMessage(Message msg) {
1364 if (msg.what == ADVANCE_MSG) {
1365 int i = 0;
1366 for (View key: mWidgetsToAdvance.keySet()) {
1367 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1368 final int delay = mAdvanceStagger * i;
1369 if (v instanceof Advanceable) {
1370 postDelayed(new Runnable() {
1371 public void run() {
1372 ((Advanceable) v).advance();
1373 }
1374 }, delay);
1375 }
1376 i++;
1377 }
1378 sendAdvanceMessage(mAdvanceInterval);
1379 }
1380 }
1381 };
1382
1383 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1384 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1385 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1386 if (v instanceof Advanceable) {
1387 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001388 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001389 updateRunning();
1390 }
1391 }
1392
1393 void removeWidgetToAutoAdvance(View hostView) {
1394 if (mWidgetsToAdvance.containsKey(hostView)) {
1395 mWidgetsToAdvance.remove(hostView);
1396 updateRunning();
1397 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001398 }
1399
Joe Onorato9c1289c2009-08-17 11:03:03 -04001400 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1401 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001402 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001403 launcherInfo.hostView = null;
1404 }
1405
Adam Cohended9f8d2010-11-03 13:25:16 -07001406 void showOutOfSpaceMessage() {
1407 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1408 }
1409
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001410 public LauncherAppWidgetHost getAppWidgetHost() {
1411 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 }
Romain Guycbb89e42009-06-08 15:52:54 -07001413
Winson Chunga9abd0e2010-10-27 17:18:37 -07001414 public LauncherModel getModel() {
1415 return mModel;
1416 }
1417
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001418 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001419 getWindow().closeAllPanels();
1420
1421 try {
1422 dismissDialog(DIALOG_CREATE_SHORTCUT);
1423 // Unlock the workspace if the dialog was showing
1424 } catch (Exception e) {
1425 // An exception is thrown if the dialog is not visible, which is fine
1426 }
1427
1428 try {
1429 dismissDialog(DIALOG_RENAME_FOLDER);
1430 // Unlock the workspace if the dialog was showing
1431 } catch (Exception e) {
1432 // An exception is thrown if the dialog is not visible, which is fine
1433 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001434
1435 // Whatever we were doing is hereby canceled.
1436 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001437 }
1438
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 @Override
1440 protected void onNewIntent(Intent intent) {
1441 super.onNewIntent(intent);
1442
1443 // Close the menu
1444 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001445 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001446 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001447
Joe Onorato14f122b2009-11-19 14:06:36 -08001448 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1449 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001450
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001451 // In all these cases, only animate if we're already on home
1452
Michael Jurka4cb37242010-08-09 21:05:32 -07001453 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001454 mWorkspace.unshrink(alreadyOnHome);
1455 }
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001456
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001457 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001458 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001459 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001460 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001461
Joe Onorato3a8820b2009-11-10 15:06:42 -08001462 final View v = getWindow().peekDecorView();
1463 if (v != null && v.getWindowToken() != null) {
1464 InputMethodManager imm = (InputMethodManager)getSystemService(
1465 INPUT_METHOD_SERVICE);
1466 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
1468 }
1469 }
1470
1471 @Override
1472 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1473 // Do not call super here
1474 mSavedInstanceState = savedInstanceState;
1475 }
1476
1477 @Override
1478 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001479 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480
1481 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1482 if (folders.size() > 0) {
1483 final int count = folders.size();
1484 long[] ids = new long[count];
1485 for (int i = 0; i < count; i++) {
1486 final FolderInfo info = folders.get(i).getInfo();
1487 ids[i] = info.id;
1488 }
1489 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1490 } else {
1491 super.onSaveInstanceState(outState);
1492 }
1493
Michael Jurka883f55b2010-10-21 15:47:14 -07001494 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495
Michael Jurka0280c3b2010-09-17 15:00:07 -07001496 if (mAddScreen > -1 && mWaitingForResult) {
1497 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1498 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1499 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 }
1501
1502 if (mFolderInfo != null && mWaitingForResult) {
1503 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1504 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1505 }
Michael Jurka946ad472010-07-09 18:05:18 -07001506
Winson Chung43b119d2010-12-06 16:45:05 -08001507 // Save the current AllApps drawer tab
1508 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1509 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1510 String currentTabTag = tabhost.getCurrentTabTag();
1511 if (currentTabTag != null) {
1512 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001513 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001514 }
1515 }
1516
1517 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001518 if (mHomeCustomizationDrawer != null) {
1519 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1520 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001521 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001522 }
1523 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 }
1525
1526 @Override
1527 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001529
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001531 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001533 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001535 mAppWidgetHost = null;
1536
1537 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538
1539 TextKeyListener.getInstance().release();
1540
Joe Onorato9c1289c2009-08-17 11:03:03 -04001541 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542
Joe Onorato9c1289c2009-08-17 11:03:03 -04001543 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001544
1545 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001546
Patrick Dubroyab962b72010-07-26 12:10:10 -07001547 // Some launcher layouts don't have a previous and next view
1548 if (mPreviousView != null) {
1549 dismissPreview(mPreviousView);
1550 }
1551 if (mNextView != null) {
1552 dismissPreview(mNextView);
1553 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001554
1555 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001556
1557 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1558 mWorkspace.removeAllViews();
1559 mWorkspace = null;
1560 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001561
1562 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 }
1564
1565 @Override
1566 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001567 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 super.startActivityForResult(intent, requestCode);
1569 }
1570
1571 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001572 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001574
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001575 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001576
Karl Rosaen138a0412009-04-23 19:00:21 -07001577 if (initialQuery == null) {
1578 // Use any text typed in the launcher as the initial query
1579 initialQuery = getTypedText();
1580 clearTypedText();
1581 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 if (appSearchData == null) {
1583 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001584 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 }
Romain Guycbb89e42009-06-08 15:52:54 -07001586
Karl Rosaen138a0412009-04-23 19:00:21 -07001587 final SearchManager searchManager =
1588 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001589 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001590 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 }
1592
1593 @Override
1594 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001595 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001596 return false;
1597 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598
1599 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001600
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1602 .setIcon(android.R.drawable.ic_menu_add)
1603 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001604 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1605 .setIcon(android.R.drawable.ic_menu_manage)
1606 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001607 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 .setIcon(android.R.drawable.ic_menu_gallery)
1609 .setAlphabeticShortcut('W');
1610 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1611 .setIcon(android.R.drawable.ic_search_category_default)
1612 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1613 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1614 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1615 .setAlphabeticShortcut('N');
1616
1617 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001618 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1619 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620
1621 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1622 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1623 .setIntent(settings);
1624
1625 return true;
1626 }
1627
1628 @Override
1629 public boolean onPrepareOptionsMenu(Menu menu) {
1630 super.onPrepareOptionsMenu(menu);
1631
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001632 // If all apps is animating, don't show the menu, because we don't know
1633 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001634 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001635 return false;
1636 }
1637
1638 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001639 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001640 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1641 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1642
1643 // Disable add if the workspace is full.
1644 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001645 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1646 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001647 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648
1649 return true;
1650 }
1651
1652 @Override
1653 public boolean onOptionsItemSelected(MenuItem item) {
1654 switch (item.getItemId()) {
1655 case MENU_ADD:
1656 addItems();
1657 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001658 case MENU_MANAGE_APPS:
1659 manageApps();
1660 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 case MENU_WALLPAPER_SETTINGS:
1662 startWallpaper();
1663 return true;
1664 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001665 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 return true;
1667 case MENU_NOTIFICATIONS:
1668 showNotifications();
1669 return true;
1670 }
1671
1672 return super.onOptionsItemSelected(item);
1673 }
Romain Guycbb89e42009-06-08 15:52:54 -07001674
Karl Rosaen138a0412009-04-23 19:00:21 -07001675 /**
1676 * Indicates that we want global search for this activity by setting the globalSearch
1677 * argument for {@link #startSearch} to true.
1678 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001680 @Override
1681 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001682 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001683 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001684 }
1685
Joe Onorato9c1289c2009-08-17 11:03:03 -04001686 public boolean isWorkspaceLocked() {
1687 return mWorkspaceLoading || mWaitingForResult;
1688 }
1689
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001691 if (LauncherApplication.isScreenXLarge()) {
1692 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001693 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001694 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001695 }
1696 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001697 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001698 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001699 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 }
1701
Michael Jurka0280c3b2010-09-17 15:00:07 -07001702 private void resetAddInfo() {
1703 mAddScreen = -1;
1704 mAddIntersectCellX = -1;
1705 mAddIntersectCellY = -1;
1706 mAddDropPosition = null;
1707 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001708
Winson Chung55cef262010-10-28 14:14:18 -07001709 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001710 resetAddInfo();
1711 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001712 mAddDropPosition = position;
1713
Michael Jurkaaf442092010-06-10 17:01:57 -07001714 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001715 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1716 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001717 }
1718
Winson Chung7ad01412010-09-27 11:33:03 -07001719 private void manageApps() {
1720 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1721 }
1722
Michael Jurkaaf442092010-06-10 17:01:57 -07001723 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001724 // TODO: catch bad widget exception when sent
1725 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001726 // TODO: Is this log message meaningful?
1727 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001728 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001729 }
1730
Winson Chung55cef262010-10-28 14:14:18 -07001731 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001732 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733
Bjorn Bringert7984c942009-12-09 15:38:25 +00001734 if (appWidget.configure != null) {
1735 // Launch over to configure widget, if needed
1736 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1737 intent.setComponent(appWidget.configure);
1738 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001739 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001740 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1741 intent.putExtra(
1742 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1743 info.mimeType);
1744
1745 final String mimeType = info.mimeType;
1746 final ClipData clipData = (ClipData) info.configurationData;
1747 final ClipDescription clipDesc = clipData.getDescription();
1748 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1749 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001750 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001751 final CharSequence stringData = item.getText();
1752 final Uri uriData = item.getUri();
1753 final Intent intentData = item.getIntent();
1754 final String key =
1755 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1756 if (uriData != null) {
1757 intent.putExtra(key, uriData);
1758 } else if (intentData != null) {
1759 intent.putExtra(key, intentData);
1760 } else if (stringData != null) {
1761 intent.putExtra(key, stringData);
1762 }
1763 break;
1764 }
1765 }
1766 }
Winson Chung55cef262010-10-28 14:14:18 -07001767 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001768
Romain Guy8e633c52010-03-04 12:51:36 -08001769 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001771 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001772 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 }
1774 }
Romain Guycbb89e42009-06-08 15:52:54 -07001775
Michael Jurka0280c3b2010-09-17 15:00:07 -07001776 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1777 resetAddInfo();
1778 mAddScreen = screen;
1779 mAddDropPosition = position;
1780
1781 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1782 createShortcutIntent.setComponent(componentName);
1783 processShortcut(createShortcutIntent);
1784 }
1785
Joe Onoratodeb98af2010-02-19 14:59:39 -08001786 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001787 // Handle case where user selected "Applications"
1788 String applicationName = getResources().getString(R.string.group_applications);
1789 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001790
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001791 if (applicationName != null && applicationName.equals(shortcutName)) {
1792 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1793 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001794
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001795 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1796 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001797 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001798 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001799 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001800 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001801 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 }
1803
Winson Chung24ab2f12010-09-16 14:10:47 -07001804 void processWallpaper(Intent intent) {
1805 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1806 }
1807
Michael Jurka0280c3b2010-09-17 15:00:07 -07001808 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1809 resetAddInfo();
1810 mAddScreen = screen;
1811 mAddDropPosition = position;
1812
1813 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1814 createFolderIntent.setComponent(componentName);
1815
1816 addLiveFolder(createFolderIntent);
1817 }
1818
1819 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001820 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001821 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001822 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001823
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001824 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001825 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001826 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001827 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001828 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 }
1830
Michael Jurka0280c3b2010-09-17 15:00:07 -07001831 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 UserFolderInfo folderInfo = new UserFolderInfo();
1833 folderInfo.title = getText(R.string.folder_name);
1834
Michael Jurka0280c3b2010-09-17 15:00:07 -07001835 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1836 final int[] cellXY = mTmpAddItemCellCoordinates;
1837 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1838 showOutOfSpaceMessage();
1839 return;
1840 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841
1842 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001843 LauncherModel.addItemToDatabase(this, folderInfo,
1844 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001845 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001846 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847
1848 // Create the view
1849 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001850 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1851 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001852 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 }
Romain Guycbb89e42009-06-08 15:52:54 -07001854
Joe Onorato9c1289c2009-08-17 11:03:03 -04001855 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001856 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001857 }
1858
Michael Jurka28750fb2010-09-24 17:43:49 -07001859 private void completeAddLiveFolder(
1860 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001861 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1862 final int[] cellXY = mTmpAddItemCellCoordinates;
1863 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1864 showOutOfSpaceMessage();
1865 return;
1866 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867
Michael Jurka0280c3b2010-09-17 15:00:07 -07001868 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869
1870 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001872 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001873 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 }
1875 }
1876
1877 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001878 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879
1880 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1881 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1882
1883 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 Intent.ShortcutIconResource iconResource = null;
1885
1886 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1887 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1888 try {
1889 iconResource = (Intent.ShortcutIconResource) extra;
1890 final PackageManager packageManager = context.getPackageManager();
1891 Resources resources = packageManager.getResourcesForApplication(
1892 iconResource.packageName);
1893 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1894 icon = resources.getDrawable(id);
1895 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001896 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 }
1898 }
1899
1900 if (icon == null) {
1901 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1902 }
1903
1904 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001905 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 info.title = name;
1907 info.iconResource = iconResource;
1908 info.uri = data.getData();
1909 info.baseIntent = baseIntent;
1910 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1911 LiveFolders.DISPLAY_MODE_GRID);
1912
1913 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001914 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001915 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916
1917 return info;
1918 }
1919
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 private void showNotifications() {
1921 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1922 if (statusBar != null) {
1923 statusBar.expand();
1924 }
1925 }
1926
1927 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001928 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001930 Intent chooser = Intent.createChooser(pickWallpaper,
1931 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001932 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1933 // Removed in Eclair MR1
1934// WallpaperManager wm = (WallpaperManager)
1935// getSystemService(Context.WALLPAPER_SERVICE);
1936// WallpaperInfo wi = wm.getWallpaperInfo();
1937// if (wi != null && wi.getSettingsActivity() != null) {
1938// LabeledIntent li = new LabeledIntent(getPackageName(),
1939// R.string.configure_wallpaper, 0);
1940// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1941// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1942// }
Mike Clerona0618e42009-10-22 13:55:21 -07001943 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 }
1945
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001946 /**
1947 * Registers various content observers. The current implementation registers
1948 * only a favorites observer to keep track of the favorites applications.
1949 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001950 private void registerContentObservers() {
1951 ContentResolver resolver = getContentResolver();
1952 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1953 true, mWidgetObserver);
1954 }
1955
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 @Override
1957 public boolean dispatchKeyEvent(KeyEvent event) {
1958 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1959 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001961 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001962 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001963 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001964 dumpState();
1965 return true;
1966 }
1967 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001968 }
1969 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1970 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001971 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 return true;
1973 }
1974 }
1975
1976 return super.dispatchKeyEvent(event);
1977 }
1978
Joe Onorato88ec0992009-11-19 13:16:06 -08001979 @Override
1980 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001981 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1982 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001983 } else {
1984 closeFolder();
1985 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001986 // Some launcher layouts don't have a previous and next view
1987 if (mPreviousView != null) {
1988 dismissPreview(mPreviousView);
1989 dismissPreview(mNextView);
1990 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001991 }
1992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 private void closeFolder() {
1994 Folder folder = mWorkspace.getOpenFolder();
1995 if (folder != null) {
1996 closeFolder(folder);
1997 }
1998 }
1999
2000 void closeFolder(Folder folder) {
2001 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08002002 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07002004 CellLayout cl = (CellLayout) parent;
2005 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05002006 if (folder instanceof DropTarget) {
2007 // Live folders aren't DropTargets.
2008 mDragController.removeDropTarget((DropTarget)folder);
2009 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 }
2011 folder.onClose();
2012 }
2013
2014 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002015 * Re-listen when widgets are reset.
2016 */
2017 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002018 if (mAppWidgetHost != null) {
2019 mAppWidgetHost.startListening();
2020 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002021 }
2022
2023 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002024 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2025 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002027 private void unbindDesktopItems() {
2028 for (ItemInfo item: mDesktopItems) {
2029 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002031 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002033
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 /**
2035 * Launches the intent referred by the clicked shortcut.
2036 *
2037 * @param v The view representing the clicked shortcut.
2038 */
2039 public void onClick(View v) {
2040 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002041 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002043 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002044 int[] pos = new int[2];
2045 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002046 intent.setSourceBounds(new Rect(pos[0], pos[1],
2047 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08002048 boolean success = startActivitySafely(intent, tag);
2049
2050 if (success && v instanceof BubbleTextView) {
2051 mWaitingForResume = (BubbleTextView) v;
2052 mWaitingForResume.setStayPressed(true);
2053 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 } else if (tag instanceof FolderInfo) {
2055 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002056 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002057 if (mState == State.ALL_APPS) {
2058 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002059 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002060 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002061 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 }
2063 }
2064
Michael Jurka0e260592010-06-30 17:07:39 -07002065 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002066 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002067 // clicking anywhere on the workspace causes the customization drawer to slide down
2068 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002069 return false;
2070 }
2071
Michael Jurkaaf442092010-06-10 17:01:57 -07002072 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002073 * Event handler for the search button
2074 *
2075 * @param v The view that was clicked.
2076 */
2077 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002078 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002079 // Use a custom animation for launching search
2080 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002081 }
2082
2083 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002084 * Event handler for the voice button
2085 *
2086 * @param v The view that was clicked.
2087 */
2088 public void onClickVoiceButton(View v) {
2089 startVoiceSearch();
2090 }
2091
2092 private void startVoiceSearch() {
2093 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2094 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2095 startActivity(intent);
2096 }
2097
2098 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002099 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002100 * enters home screen customization mode.
2101 *
2102 * @param v The view that was clicked.
2103 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002104 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002105 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002106 }
2107
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002108 /**
2109 * Event handler for the "grid" button that appears on the home screen, which
2110 * enters all apps mode.
2111 *
2112 * @param v The view that was clicked.
2113 */
2114 public void onClickAllAppsButton(View v) {
2115 showAllApps(true);
2116 }
2117
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002118 public void onClickAppMarketButton(View v) {
2119 if (mAppMarketIntent != null) {
2120 startActivitySafely(mAppMarketIntent, "app market");
2121 }
2122 }
2123
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002124 void startApplicationDetailsActivity(ComponentName componentName) {
2125 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002126 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2127 Uri.fromParts("package", packageName, null));
2128 startActivity(intent);
2129 }
2130
Patrick Dubroy5539af72010-09-07 15:22:01 -07002131 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2132 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2133 // System applications cannot be installed. For now, show a toast explaining that.
2134 // We may give them the option of disabling apps this way.
2135 int messageId = R.string.uninstall_system_app_text;
2136 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2137 } else {
2138 String packageName = appInfo.componentName.getPackageName();
2139 String className = appInfo.componentName.getClassName();
2140 Intent intent = new Intent(
2141 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2142 startActivity(intent);
2143 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002144 }
2145
Michael Jurkaddd62e92011-02-16 17:49:14 -08002146 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2148 try {
2149 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002150 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002151 } catch (ActivityNotFoundException e) {
2152 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002153 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 } catch (SecurityException e) {
2155 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002156 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002157 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002158 "or use the exported attribute for this activity. "
2159 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002161 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002163
Romain Guy8e633c52010-03-04 12:51:36 -08002164 void startActivityForResultSafely(Intent intent, int requestCode) {
2165 try {
2166 startActivityForResult(intent, requestCode);
2167 } catch (ActivityNotFoundException e) {
2168 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2169 } catch (SecurityException e) {
2170 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2171 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2172 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2173 "or use the exported attribute for this activity.", e);
2174 }
2175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176
2177 private void handleFolderClick(FolderInfo folderInfo) {
2178 if (!folderInfo.opened) {
2179 // Close any open folder
2180 closeFolder();
2181 // Open the requested folder
2182 openFolder(folderInfo);
2183 } else {
2184 // Find the open folder...
2185 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2186 int folderScreen;
2187 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002188 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 // .. and close it
2190 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002191 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 // Close any folder open on the current screen
2193 closeFolder();
2194 // Pull the folder onto this screen
2195 openFolder(folderInfo);
2196 }
2197 }
2198 }
2199 }
2200
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002202 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 * is animated relative to the specified View. If the View is null, no animation
2204 * is played.
2205 *
2206 * @param folderInfo The FolderInfo describing the folder to open.
2207 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002208 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 Folder openFolder;
2210
2211 if (folderInfo instanceof UserFolderInfo) {
2212 openFolder = UserFolder.fromXml(this);
2213 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002214 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 } else {
2216 return;
2217 }
2218
Joe Onorato00acb122009-08-04 16:04:30 -04002219 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 openFolder.setLauncher(this);
2221
2222 openFolder.bind(folderInfo);
2223 folderInfo.opened = true;
2224
Winson Chungaafa03c2010-06-11 17:34:16 -07002225 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 openFolder.onOpen();
2228 }
2229
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002231 switch (v.getId()) {
2232 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002233 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002234 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2235 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002236 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002237 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002238 return true;
2239 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002240 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002241 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2242 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002243 showPreviews(v);
2244 }
2245 return true;
2246 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002247 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002248 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2249 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2250 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002251 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002252 return true;
2253 }
2254
Joe Onorato9c1289c2009-08-17 11:03:03 -04002255 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 return false;
2257 }
2258
2259 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002260 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002261 }
2262
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002263
Michael Jurka0280c3b2010-09-17 15:00:07 -07002264 resetAddInfo();
2265 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002267 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002268 return true;
2269 }
2270
Michael Jurka0280c3b2010-09-17 15:00:07 -07002271 final View itemUnderLongClick = longClickCellInfo.cell;
2272
Winson Chung304dcde2011-01-07 11:17:23 -08002273 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002274 if (itemUnderLongClick == null) {
2275 // User long pressed on empty space
2276 mWorkspace.setAllowLongPress(false);
2277 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2278 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002279 if (LauncherApplication.isScreenXLarge()) {
2280 addItems();
2281 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002282 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002283 }
2284 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002285 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002287 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2288 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002289 mAddIntersectCellX = longClickCellInfo.cellX;
2290 mAddIntersectCellY = longClickCellInfo.cellY;
2291 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002292 }
2293 }
2294 }
2295 return true;
2296 }
2297
Romain Guye6b8e2f2009-11-10 11:56:55 -08002298 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002299 private void dismissPreview(final View v) {
2300 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002301 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002302 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2303 public void onDismiss() {
2304 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2305 int count = group.getChildCount();
2306 for (int i = 0; i < count; i++) {
2307 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2308 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002309 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2310 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2311
2312 v.setTag(R.id.workspace, null);
2313 v.setTag(R.id.icon, null);
2314 window.setOnDismissListener(null);
2315 }
2316 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002317 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002318 }
2319 v.setTag(null);
2320 }
2321
Romain Guyf8e6a802009-12-07 17:48:02 -08002322 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002323 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002324 }
2325
Romain Guya6abce82009-11-10 02:54:41 -08002326 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002327 final Resources resources = getResources();
2328 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002329
Romain Guya6abce82009-11-10 02:54:41 -08002330 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002331
Romain Guy9d31e9f2009-11-11 18:54:28 -08002332 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002333
Romain Guyf8e6a802009-12-07 17:48:02 -08002334 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002335 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002336 int extraW = (int) ((r.left + r.right) * max);
2337 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002338
2339 int aW = cell.getWidth() - extraW;
2340 float w = aW / max;
2341
2342 int width = cell.getWidth();
2343 int height = cell.getHeight();
2344 int x = cell.getLeftPadding();
2345 int y = cell.getTopPadding();
2346 width -= (x + cell.getRightPadding());
2347 height -= (y + cell.getBottomPadding());
2348
2349 float scale = w / width;
2350
2351 int count = end - start;
2352
2353 final float sWidth = width * scale;
2354 float sHeight = height * scale;
2355
Romain Guye6b8e2f2009-11-10 11:56:55 -08002356 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002357
Romain Guye6b8e2f2009-11-10 11:56:55 -08002358 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2359 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002360
2361 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002362 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002363 cell = (CellLayout) workspace.getChildAt(i);
2364
Romain Guyf8e6a802009-12-07 17:48:02 -08002365 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002366 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002367
2368 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002369 c.scale(scale, scale);
2370 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002371 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002372
Romain Guy9d31e9f2009-11-11 18:54:28 -08002373 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002374 image.setImageBitmap(bitmap);
2375 image.setTag(i);
2376 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002377 image.setOnFocusChangeListener(handler);
2378 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002379 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002380
2381 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002382 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2383
Winson Chungaafa03c2010-06-11 17:34:16 -07002384 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002385 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002386
2387 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002388 p.setContentView(preview);
2389 p.setWidth((int) (sWidth * count + extraW));
2390 p.setHeight((int) (sHeight + extraH));
2391 p.setAnimationStyle(R.style.AnimationPreview);
2392 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002393 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002394 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002395 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002396
Romain Guye6b8e2f2009-11-10 11:56:55 -08002397 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2398 public void onDismiss() {
2399 dismissPreview(anchor);
2400 }
2401 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002402
2403 anchor.setTag(p);
2404 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002405 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002406 }
2407
Romain Guy9d31e9f2009-11-11 18:54:28 -08002408 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002409 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002410
Romain Guye6b8e2f2009-11-10 11:56:55 -08002411 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002412 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002413 }
2414
2415 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002416 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002417 v.post(this);
2418 }
2419
2420 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002421 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002422 }
2423
2424 public void onFocusChange(View v, boolean hasFocus) {
2425 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002426 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002427 }
Romain Guya6abce82009-11-10 02:54:41 -08002428 }
2429 }
2430
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 Workspace getWorkspace() {
2432 return mWorkspace;
2433 }
2434
Michael Jurka3c38d562011-02-18 19:22:12 -08002435 TabHost getCustomizationDrawer() {
2436 return mHomeCustomizationDrawer;
2437 }
2438
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002439 @Override
2440 protected Dialog onCreateDialog(int id) {
2441 switch (id) {
2442 case DIALOG_CREATE_SHORTCUT:
2443 return new CreateShortcut().createDialog();
2444 case DIALOG_RENAME_FOLDER:
2445 return new RenameFolder().createDialog();
2446 }
2447
2448 return super.onCreateDialog(id);
2449 }
2450
2451 @Override
2452 protected void onPrepareDialog(int id, Dialog dialog) {
2453 switch (id) {
2454 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 break;
2456 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002457 if (mFolderInfo != null) {
2458 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2459 final CharSequence text = mFolderInfo.title;
2460 input.setText(text);
2461 input.setSelection(0, text.length());
2462 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002463 break;
2464 }
2465 }
2466
2467 void showRenameDialog(FolderInfo info) {
2468 mFolderInfo = info;
2469 mWaitingForResult = true;
2470 showDialog(DIALOG_RENAME_FOLDER);
2471 }
2472
Michael Jurka0280c3b2010-09-17 15:00:07 -07002473 private void showAddDialog(int intersectX, int intersectY) {
2474 resetAddInfo();
2475 mAddIntersectCellX = intersectX;
2476 mAddIntersectCellY = intersectY;
2477 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478 mWaitingForResult = true;
2479 showDialog(DIALOG_CREATE_SHORTCUT);
2480 }
2481
Joe Onoratodeb98af2010-02-19 14:59:39 -08002482 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002483 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002484 Bundle bundle = new Bundle();
2485
2486 ArrayList<String> shortcutNames = new ArrayList<String>();
2487 shortcutNames.add(getString(R.string.group_applications));
2488 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2489
2490 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2491 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2492 R.drawable.ic_launcher_application));
2493 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2494
2495 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2496 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002497 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002498 pickIntent.putExtras(bundle);
2499
Joe Onoratodeb98af2010-02-19 14:59:39 -08002500 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002501 }
2502
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002503 private class RenameFolder {
2504 private EditText mInput;
2505
2506 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002507 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2508 mInput = (EditText) layout.findViewById(R.id.folder_name);
2509
2510 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2511 builder.setIcon(0);
2512 builder.setTitle(getString(R.string.rename_folder_title));
2513 builder.setCancelable(true);
2514 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2515 public void onCancel(DialogInterface dialog) {
2516 cleanup();
2517 }
2518 });
2519 builder.setNegativeButton(getString(R.string.cancel_action),
2520 new Dialog.OnClickListener() {
2521 public void onClick(DialogInterface dialog, int which) {
2522 cleanup();
2523 }
2524 }
2525 );
2526 builder.setPositiveButton(getString(R.string.rename_action),
2527 new Dialog.OnClickListener() {
2528 public void onClick(DialogInterface dialog, int which) {
2529 changeFolderName();
2530 }
2531 }
2532 );
2533 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002534
2535 final AlertDialog dialog = builder.create();
2536 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2537 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002538 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002539 mInput.requestFocus();
2540 InputMethodManager inputManager = (InputMethodManager)
2541 getSystemService(Context.INPUT_METHOD_SERVICE);
2542 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002543 }
2544 });
2545
2546 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002547 }
2548
2549 private void changeFolderName() {
2550 final String name = mInput.getText().toString();
2551 if (!TextUtils.isEmpty(name)) {
2552 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002553 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 mFolderInfo.title = name;
2555 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2556
Joe Onorato9c1289c2009-08-17 11:03:03 -04002557 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002558 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002559 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 } else {
2561 final FolderIcon folderIcon = (FolderIcon)
2562 mWorkspace.getViewForTag(mFolderInfo);
2563 if (folderIcon != null) {
2564 folderIcon.setText(name);
2565 getWorkspace().requestLayout();
2566 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002567 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002568 mWorkspaceLoading = true;
2569 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002570 }
2571 }
2572 }
2573 cleanup();
2574 }
2575
2576 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 dismissDialog(DIALOG_RENAME_FOLDER);
2578 mWaitingForResult = false;
2579 mFolderInfo = null;
2580 }
2581 }
2582
Daniel Sandler843e8602010-06-07 14:59:01 -04002583 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2584 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002585 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002586 }
2587
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002588 // AllAppsView.Watcher
2589 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002590 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2591 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002592 mWorkspace.setVisibility(View.GONE);
2593 }
2594 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002595
Michael Jurkaafca9532011-02-16 14:50:35 -08002596 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002597 button.setVisibility(View.VISIBLE);
2598 button.setFocusable(true);
2599 button.setClickable(true);
2600 }
2601
2602 private void hideToolbarButton(View button) {
2603 button.setAlpha(0.0f);
2604 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2605 button.setVisibility(View.INVISIBLE);
Michael Jurkaafca9532011-02-16 14:50:35 -08002606 }
2607
2608 private void disableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002609 button.setFocusable(false);
2610 button.setClickable(false);
2611 }
2612
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002613 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002614 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002615 *
2616 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2617 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002618 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002619 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002620 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002621 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002622 final boolean showing = show;
2623 final boolean hiding = !show;
2624
2625 final int duration = show ?
2626 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2627 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2628
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002629 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002630 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002631 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002632 anim.addUpdateListener(new AnimatorUpdateListener() {
2633 public void onAnimationUpdate(ValueAnimator animation) {
2634 view.setAlpha((Float) animation.getAnimatedValue());
2635 }
2636 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002637 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002638 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002639 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002640 if (showing) showAndEnableToolbarButton(view);
2641 if (hiding) disableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002642 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002643 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002644 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002645 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002646 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002647 });
2648 seq.play(anim);
2649 } else {
2650 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002651 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002652 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002653 } else {
Michael Jurkaafca9532011-02-16 14:50:35 -08002654 disableToolbarButton(view);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002655 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002656 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002657 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002658 }
2659
2660 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002661 * Show/hide the appropriate toolbar buttons for newState.
2662 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002663 *
2664 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002665 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2666 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002667 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002668 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002669 switch (newState) {
2670 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002671 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2672 mDeleteZone.setOverlappingViews(
2673 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002674 mDeleteZone.setDragAndDropEnabled(true);
2675 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002676 break;
2677 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002678 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002679 mDeleteZone.setDragAndDropEnabled(false);
2680 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002681 break;
2682 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002683 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002684 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002685 break;
2686 }
2687 }
2688
2689 /**
2690 * Helper method for the cameraZoomIn/cameraZoomOut animations
2691 * @param view The view being animated
2692 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2693 * @param scaleFactor The scale factor used for the zoom
2694 */
2695 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2696 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002697
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002698 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002699 // Set pivotY so that at the starting zoom factor, the view is partially
2700 // visible. Modifying initialHeightFactor changes how much of the view is
2701 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002702 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002703 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002704 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002705 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002706 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002707 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002708 }
2709 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002710
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002711 /**
2712 * Zoom the camera out from the workspace to reveal 'toView'.
2713 * Assumes that the view to show is anchored at either the very top or very bottom
2714 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002715 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002716 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002717 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002718 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002719 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002720
2721 final int duration = toAllApps ?
2722 res.getInteger(R.integer.config_allAppsZoomInTime) :
2723 res.getInteger(R.integer.config_customizeZoomInTime);
2724
2725 final float scale = toAllApps ?
2726 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2727 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2728
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002729 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002730
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002731 setPivotsForZoom(toView, toState, scale);
2732
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002733 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002734 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002735 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002736 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002737 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002738
2739 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002740 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2741 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
2742 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2743 public void onAnimationUpdate(ValueAnimator animation) {
2744 final float b = (Float) animation.getAnimatedValue();
2745 final float a = 1f - b;
2746 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002747 toView.setFastScaleX(a * scale + b * 1f);
2748 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002749 }
2750 });
Adam Cohen61033d32010-11-15 18:29:44 -08002751
Adam Cohenf16e5712011-01-13 13:31:45 -08002752 if (toAllApps) {
Michael Jurkad99cf8b2011-01-16 16:53:18 -08002753 toView.setAlpha(0f);
Michael Jurka742574b2011-02-02 23:51:01 -08002754 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurkaea573482011-02-03 19:48:18 -08002755 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002756 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2757 public void onAnimationUpdate(ValueAnimator animation) {
2758 final float b = (Float) animation.getAnimatedValue();
2759 final float a = 1f - b;
2760 // don't need to invalidate because we do so above
2761 toView.setFastAlpha(a * 0f + b * 1f);
2762 }
2763 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002764 alphaAnim.start();
2765 }
2766
Michael Jurkabfadaad2011-01-31 21:35:39 -08002767 // Only use hardware layers in portrait mode, they don't give any gains in landscape
2768 if (mWorkspace.getWidth() < mWorkspace.getHeight()) {
2769 toView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
2770 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002771 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002772 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002773 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002774 // Prepare the position
2775 toView.setTranslationX(0.0f);
2776 toView.setTranslationY(0.0f);
2777 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002778 if (!toAllApps) {
2779 toView.setAlpha(1.0f);
2780 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002781 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002782 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002783 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002784 // If we don't set the final scale values here, if this animation is cancelled
2785 // it will have the wrong scale value and subsequent cameraPan animations will
2786 // not fix that
Michael Jurka3eeaea82011-01-31 15:02:36 -08002787 toView.setLayerType(View.LAYER_TYPE_NONE, null);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002788 toView.setScaleX(1.0f);
2789 toView.setScaleY(1.0f);
2790 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002791 });
2792
Chet Haaseb1254a62010-09-07 13:35:00 -07002793 AnimatorSet toolbarHideAnim = new AnimatorSet();
2794 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002795 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2796
2797 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002798 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002799
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002800 if (mStateAnimation != null) mStateAnimation.cancel();
2801 mStateAnimation = new AnimatorSet();
2802 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2803 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002804
2805 // Show the new toolbar buttons just as the main animation is ending
2806 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002807 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2808 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002809 } else {
2810 toView.setTranslationX(0.0f);
2811 toView.setTranslationY(0.0f);
2812 toView.setScaleX(1.0f);
2813 toView.setScaleY(1.0f);
2814 toView.setVisibility(View.VISIBLE);
2815 hideAndShowToolbarButtons(toState, null, null);
2816 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002817 }
2818
2819 /**
2820 * Zoom the camera back into the workspace, hiding 'fromView'.
2821 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002822 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2823 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002824 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002825 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002826 cameraZoomIn(fromState, animated, false);
2827 }
2828
2829 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002830 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002831 final boolean fromAllApps = (fromState == State.ALL_APPS);
2832
2833 int duration = fromAllApps ?
2834 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2835 res.getInteger(R.integer.config_customizeZoomOutTime);
2836
Michael Jurka742574b2011-02-02 23:51:01 -08002837 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002838 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2839 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2840
2841 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002842
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002843 mCustomizePagedView.endChoiceMode();
2844 mAllAppsPagedView.endChoiceMode();
2845
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002846 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002847
Michael Jurkad3ef3062010-11-23 16:23:58 -08002848 if (!springLoaded) {
2849 mWorkspace.unshrink(animated);
2850 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002851
2852 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002853 if (mStateAnimation != null) mStateAnimation.cancel();
2854 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002855
Michael Jurka742574b2011-02-02 23:51:01 -08002856 final float oldScaleX = fromView.getScaleX();
2857 final float oldScaleY = fromView.getScaleY();
2858
2859 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2860 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2861 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2862 public void onAnimationUpdate(ValueAnimator animation) {
2863 final float b = (Float) animation.getAnimatedValue();
2864 final float a = 1f - b;
2865 ((View)fromView.getParent()).fastInvalidate();
2866 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2867 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2868 }
2869 });
2870 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002871 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002872 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002873 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2874 public void onAnimationUpdate(ValueAnimator animation) {
2875 final float b = (Float) animation.getAnimatedValue();
2876 final float a = 1f - b;
2877 // don't need to invalidate because we do so above
2878 fromView.setFastAlpha(a * 1f + b * 0f);
2879 }
2880 });
2881
Michael Jurkacf6125c2011-01-28 15:20:01 -08002882 fromView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002883 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002884 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002885 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002886 fromView.setVisibility(View.GONE);
Michael Jurkacf6125c2011-01-28 15:20:01 -08002887 fromView.setLayerType(View.LAYER_TYPE_NONE, null);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002888 }
2889 });
2890
Chet Haaseb1254a62010-09-07 13:35:00 -07002891 AnimatorSet toolbarHideAnim = new AnimatorSet();
2892 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002893 if (!springLoaded) {
2894 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2895 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002896
Adam Cohen61033d32010-11-15 18:29:44 -08002897 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002898
2899 // Show the new toolbar buttons at the very end of the whole animation
2900 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2901 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002902 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2903 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002904 } else {
2905 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002906 if (!springLoaded) {
2907 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2908 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002909 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002910 }
2911
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002912 void showAllApps(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002913 if (mState != State.WORKSPACE) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002914 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002915 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002916
Michael Jurka79212d82010-07-30 16:36:20 -07002917 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002918 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002919 } else {
2920 mAllAppsGrid.zoom(1.0f, animated);
2921 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002922
Romain Guyc16fea72010-03-12 17:17:56 -08002923 ((View) mAllAppsGrid).setFocusable(true);
2924 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002925
Joe Onorato7c312c12009-08-13 21:36:53 -07002926 // TODO: fade these two too
2927 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002928
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002929 // Change the state *after* we've called all the transition code
2930 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002931
Winson Chung5fb63472011-02-02 17:03:37 -08002932 // Pause the auto-advance of widgets until we are out of AllApps
2933 mUserPresent = false;
2934 updateRunning();
2935
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002936 // send an accessibility event to announce the context change
2937 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2938 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002939
2940 void showWorkspace(boolean animated) {
2941 showWorkspace(animated, null);
2942 }
2943
2944 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002945 if (layout != null) {
2946 // always animated, but that's ok since we never specify a layout and
2947 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002948 mWorkspace.unshrink(layout);
2949 } else {
2950 mWorkspace.unshrink(animated);
2951 }
2952 if (mState == State.ALL_APPS) {
2953 closeAllApps(animated);
2954 } else if (mState == State.CUSTOMIZE) {
2955 hideCustomizationDrawer(animated);
2956 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002957
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002958 // Change the state *after* we've called all the transition code
2959 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002960
Winson Chung5fb63472011-02-02 17:03:37 -08002961 // Resume the auto-advance of widgets
2962 mUserPresent = true;
2963 updateRunning();
2964
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002965 // send an accessibility event to announce the context change
2966 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002967 }
2968
Michael Jurkad3ef3062010-11-23 16:23:58 -08002969 void enterSpringLoadedDragMode(CellLayout layout) {
2970 mWorkspace.enterSpringLoadedDragMode(layout);
2971 if (mState == State.ALL_APPS) {
2972 cameraZoomIn(State.ALL_APPS, true, true);
2973 mState = State.ALL_APPS_SPRING_LOADED;
2974 } else if (mState == State.CUSTOMIZE) {
2975 cameraZoomIn(State.CUSTOMIZE, true, true);
2976 mState = State.CUSTOMIZE_SPRING_LOADED;
2977 }/* else {
2978 // we're already in spring loaded mode; don't do anything
2979 }*/
2980 }
2981
2982 void exitSpringLoadedDragMode() {
2983 if (mState == State.ALL_APPS_SPRING_LOADED) {
2984 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2985 cameraZoomOut(State.ALL_APPS, true);
2986 mState = State.ALL_APPS;
2987 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2988 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2989 cameraZoomOut(State.CUSTOMIZE, true);
2990 mState = State.CUSTOMIZE;
2991 }/* else {
2992 // we're not in spring loaded mode; don't do anything
2993 }*/
2994 }
2995
Joe Onoratob2061212009-11-24 16:13:54 -05002996 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002997 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002998 * - Home from workspace
2999 * - from center screen
3000 * - from other screens
3001 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003002 * - from center screen
3003 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003004 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003005 * - from center screen
3006 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003007 * - Launch app from workspace and quit
3008 * - with back
3009 * - with home
3010 * - Launch app from all apps and quit
3011 * - with back
3012 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003013 * - Go to a screen that's not the default, then all
3014 * apps, and launch and app, and go back
3015 * - with back
3016 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003017 * - On workspace, long press power and go back
3018 * - with back
3019 * - with home
3020 * - On all apps, long press power and go back
3021 * - with back
3022 * - with home
3023 * - On workspace, power off
3024 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003025 * - Launch an app and turn off the screen while in that app
3026 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003027 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003028 * - From all apps
3029 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003030 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3031 * - From all apps
3032 * - From the center workspace
3033 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003034 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003035 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003036 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003037 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003038 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003039 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003040 } else {
3041 mAllAppsGrid.zoom(0.0f, animated);
3042 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003043 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003044 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003045 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003046 }
3047
Joe Onorato7c312c12009-08-13 21:36:53 -07003048 void lockAllApps() {
3049 // TODO
3050 }
3051
3052 void unlockAllApps() {
3053 // TODO
3054 }
3055
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003056 // Show the customization drawer (only exists in x-large configuration)
3057 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003058 if (mState != State.WORKSPACE) {
3059 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003060 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003061
3062 cameraZoomOut(State.CUSTOMIZE, animated);
3063
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003064 // Change the state *after* we've called all the transition code
3065 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003066
3067 // Pause the auto-advance of widgets until we are out of Customization drawer
3068 mUserPresent = false;
3069 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003070 }
3071
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003072 // Hide the customization drawer (only exists in x-large configuration)
3073 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003074 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003075 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003076 }
3077 }
3078
Patrick Dubroy5f445422011-02-18 14:35:21 -08003079 /**
3080 * Add an item from all apps or customize onto the given workspace screen.
3081 * If layout is null, add to the current screen.
3082 */
3083 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003084 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3085 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003086 } else {
3087 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003088 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003089 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003090
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003091 void onWorkspaceClick(CellLayout layout) {
3092 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003093 }
3094
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003095 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003096 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003097 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003098 // Look for the toolbar icon specified in the activity meta-data
3099 Bundle metaData = packageManager.getActivityInfo(
3100 activityName, PackageManager.GET_META_DATA).metaData;
3101 if (metaData != null) {
3102 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3103 if (iconResId != 0) {
3104 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003105 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003106 }
3107 }
3108 } catch (NameNotFoundException e) {
3109 // Do nothing
3110 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003111 return null;
3112 }
3113
3114 // if successful in getting icon, return it; otherwise, set button to use default drawable
3115 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3116 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3117 TextView button = (TextView) findViewById(buttonId);
3118 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3119
3120 // If we were unable to find the icon via the meta-data, use a generic one
3121 if (toolbarIcon == null) {
3122 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3123 return null;
3124 } else {
3125 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3126 return toolbarIcon.getConstantState();
3127 }
3128 }
3129
3130 // if successful in getting icon, return it; otherwise, set button to use default drawable
3131 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3132 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3133 ImageView button = (ImageView) findViewById(buttonId);
3134 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3135
Michael Jurka0423dcf2010-10-05 14:56:18 -07003136 // If we were unable to find the icon via the meta-data, use a generic one
3137 if (toolbarIcon == null) {
3138 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003139 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003140 } else {
3141 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003142 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003143 }
3144 }
3145
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003146 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3147 TextView button = (TextView) findViewById(buttonId);
3148 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3149 }
3150
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003151 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003152 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003153 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003154 }
3155
Michael Jurka0423dcf2010-10-05 14:56:18 -07003156 private void updateGlobalSearchIcon() {
3157 if (LauncherApplication.isScreenXLarge()) {
3158 final SearchManager searchManager =
3159 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3160 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003161 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003162 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003163 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003164 } else {
3165 findViewById(R.id.search_button).setVisibility(View.GONE);
3166 }
3167 }
3168 }
3169
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003170 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003171 updateButtonWithDrawable(R.id.search_button, d);
3172 }
3173
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003174 private void updateVoiceSearchIcon() {
3175 if (LauncherApplication.isScreenXLarge()) {
3176 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3177 ComponentName activityName = intent.resolveActivity(getPackageManager());
3178 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003179 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003180 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3181 } else {
3182 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003183 }
3184 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003185 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003186
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003187 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003188 updateButtonWithDrawable(R.id.voice_button, d);
3189 }
3190
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003191 /**
3192 * Sets the app market icon (shown when all apps is visible on x-large screens)
3193 */
3194 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003195 if (LauncherApplication.isScreenXLarge()) {
3196 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3197 // Find the app market activity by resolving an intent.
3198 // (If multiple app markets are installed, it will return the ResolverActivity.)
3199 ComponentName activityName = intent.resolveActivity(getPackageManager());
3200 if (activityName != null) {
3201 mAppMarketIntent = intent;
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003202 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003203 R.id.market_button, activityName, R.drawable.app_market_generic);
3204 }
3205 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003206 }
3207
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003208 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003209 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003210 }
3211
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003212 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003213 * Displays the shortcut creation dialog and launches, if necessary, the
3214 * appropriate activity.
3215 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003216 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003217 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3218 DialogInterface.OnShowListener {
3219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003220 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003222 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003223 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3226 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003227 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003228
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003229 builder.setInverseBackgroundForced(true);
3230
3231 AlertDialog dialog = builder.create();
3232 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003233 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003234 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003236 return dialog;
3237 }
3238
3239 public void onCancel(DialogInterface dialog) {
3240 mWaitingForResult = false;
3241 cleanup();
3242 }
3243
Romain Guycbb89e42009-06-08 15:52:54 -07003244 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003245 }
3246
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003247 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003248 try {
3249 dismissDialog(DIALOG_CREATE_SHORTCUT);
3250 } catch (Exception e) {
3251 // An exception is thrown if the dialog is not visible, which is fine
3252 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003253 }
3254
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003255 /**
3256 * Handle the action clicked in the "Add to home" dialog.
3257 */
3258 public void onClick(DialogInterface dialog, int which) {
3259 Resources res = getResources();
3260 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003261
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003262 switch (which) {
3263 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003264 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003265 break;
3266 }
Romain Guycbb89e42009-06-08 15:52:54 -07003267
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003268 case AddAdapter.ITEM_APPWIDGET: {
3269 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003270
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003271 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3272 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003273 // start the pick activity
3274 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3275 break;
3276 }
Romain Guycbb89e42009-06-08 15:52:54 -07003277
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003278 case AddAdapter.ITEM_LIVE_FOLDER: {
3279 // Insert extra item to handle inserting folder
3280 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003281
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003282 ArrayList<String> shortcutNames = new ArrayList<String>();
3283 shortcutNames.add(res.getString(R.string.group_folder));
3284 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003285
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003286 ArrayList<ShortcutIconResource> shortcutIcons =
3287 new ArrayList<ShortcutIconResource>();
3288 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3289 R.drawable.ic_launcher_folder));
3290 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3291
3292 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3293 pickIntent.putExtra(Intent.EXTRA_INTENT,
3294 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3295 pickIntent.putExtra(Intent.EXTRA_TITLE,
3296 getText(R.string.title_select_live_folder));
3297 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003298
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003299 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3300 break;
3301 }
3302
3303 case AddAdapter.ITEM_WALLPAPER: {
3304 startWallpaper();
3305 break;
3306 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003307 }
3308 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003309
3310 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003311 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003312 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003313 }
3314
3315 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003316 * Receives notifications when applications are added/removed.
3317 */
3318 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3319 @Override
3320 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003321 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003322 String reason = intent.getStringExtra("reason");
3323 if (!"homekey".equals(reason)) {
3324 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003325 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003326 animate = false;
3327 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003328 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003329 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003330 }
3331 }
3332
3333 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003334 * Receives notifications whenever the appwidgets are reset.
3335 */
3336 private class AppWidgetResetObserver extends ContentObserver {
3337 public AppWidgetResetObserver() {
3338 super(new Handler());
3339 }
3340
3341 @Override
3342 public void onChange(boolean selfChange) {
3343 onAppWidgetReset();
3344 }
3345 }
3346
3347 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003348 * If the activity is currently paused, signal that we need to re-run the loader
3349 * in onResume.
3350 *
3351 * This needs to be called from incoming places where resources might have been loaded
3352 * while we are paused. That is becaues the Configuration might be wrong
3353 * when we're not running, and if it comes back to what it was when we
3354 * were paused, we are not restarted.
3355 *
3356 * Implementation of the method from LauncherModel.Callbacks.
3357 *
3358 * @return true if we are currently paused. The caller might be able to
3359 * skip some work in that case since we will come back again.
3360 */
3361 public boolean setLoadOnResume() {
3362 if (mPaused) {
3363 Log.i(TAG, "setLoadOnResume");
3364 mOnResumeNeedsLoad = true;
3365 return true;
3366 } else {
3367 return false;
3368 }
3369 }
3370
3371 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003372 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003373 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003374 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003375 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003376 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003377 } else {
3378 return SCREEN_COUNT / 2;
3379 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003380 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003381
Winson Chunga12a2502010-12-20 14:41:35 -08003382 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003383 mAllAppsPagedView = view;
3384 }
3385
Joe Onorato9c1289c2009-08-17 11:03:03 -04003386 /**
3387 * Refreshes the shortcuts shown on the workspace.
3388 *
3389 * Implementation of the method from LauncherModel.Callbacks.
3390 */
3391 public void startBinding() {
3392 final Workspace workspace = mWorkspace;
3393 int count = workspace.getChildCount();
3394 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003395 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003396 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3397 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003398 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003399
Joe Onorato9c1289c2009-08-17 11:03:03 -04003400 if (DEBUG_USER_INTERFACE) {
3401 android.widget.Button finishButton = new android.widget.Button(this);
3402 finishButton.setText("Finish");
3403 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3404
3405 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3406 public void onClick(View v) {
3407 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003408 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003409 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003410 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003411
3412 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3413 // mDesktopItems -> AppWidgetInfo -> hostView).
3414 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003415 }
3416
3417 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003418 * Bind the items start-end from the list.
3419 *
3420 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003421 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3423
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003424 setLoadOnResume();
3425
Joe Onorato9c1289c2009-08-17 11:03:03 -04003426 final Workspace workspace = mWorkspace;
3427
3428 for (int i=start; i<end; i++) {
3429 final ItemInfo item = shortcuts.get(i);
3430 mDesktopItems.add(item);
3431 switch (item.itemType) {
3432 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3433 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003434 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003435 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3436 false);
3437 break;
3438 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3439 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003440 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003441 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003442 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3443 false);
3444 break;
3445 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3446 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3447 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003448 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003449 (LiveFolderInfo) item);
3450 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3451 false);
3452 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003453 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003454 }
3455
Joe Onorato9c1289c2009-08-17 11:03:03 -04003456 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003457 }
3458
Joe Onorato9c1289c2009-08-17 11:03:03 -04003459 /**
3460 * Implementation of the method from LauncherModel.Callbacks.
3461 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003462 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003463 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003464 sFolders.clear();
3465 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003466 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003467
3468 /**
3469 * Add the views for a widget to the workspace.
3470 *
3471 * Implementation of the method from LauncherModel.Callbacks.
3472 */
3473 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003474 setLoadOnResume();
3475
Daniel Sandler843e8602010-06-07 14:59:01 -04003476 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3477 if (DEBUG_WIDGETS) {
3478 Log.d(TAG, "bindAppWidget: " + item);
3479 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003480 final Workspace workspace = mWorkspace;
3481
3482 final int appWidgetId = item.appWidgetId;
3483 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003484 if (DEBUG_WIDGETS) {
3485 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3486 }
3487
Joe Onorato9c1289c2009-08-17 11:03:03 -04003488 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3489
Joe Onorato9c1289c2009-08-17 11:03:03 -04003490 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3491 item.hostView.setTag(item);
3492
3493 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3494 item.cellY, item.spanX, item.spanY, false);
3495
Adam Cohended9f8d2010-11-03 13:25:16 -07003496 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3497
Joe Onorato9c1289c2009-08-17 11:03:03 -04003498 workspace.requestLayout();
3499
3500 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003501
3502 if (DEBUG_WIDGETS) {
3503 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3504 + (SystemClock.uptimeMillis()-start) + "ms");
3505 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003506 }
3507
3508 /**
3509 * Callback saying that there aren't any more items to bind.
3510 *
3511 * Implementation of the method from LauncherModel.Callbacks.
3512 */
3513 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003514 setLoadOnResume();
3515
Joe Onorato9c1289c2009-08-17 11:03:03 -04003516 if (mSavedState != null) {
3517 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003518 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003519 }
3520
3521 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3522 if (userFolders != null) {
3523 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003524 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003525 if (info != null) {
3526 openFolder(info);
3527 }
3528 }
3529 final Folder openFolder = mWorkspace.getOpenFolder();
3530 if (openFolder != null) {
3531 openFolder.requestFocus();
3532 }
3533 }
3534
Joe Onorato9c1289c2009-08-17 11:03:03 -04003535 mSavedState = null;
3536 }
3537
3538 if (mSavedInstanceState != null) {
3539 super.onRestoreInstanceState(mSavedInstanceState);
3540 mSavedInstanceState = null;
3541 }
3542
Winson Chung32bb5e52011-01-31 14:51:56 -08003543 // Workaround a bug that occurs when rotating the device while the customization mode is
3544 // open, we trigger a new layout on all the CellLayout children.
3545 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3546 final int childCount = mWorkspace.getChildCount();
3547 for (int i = 0; i < childCount; ++i) {
3548 mWorkspace.getChildAt(i).requestLayout();
3549 }
3550 }
3551
Joe Onorato9c1289c2009-08-17 11:03:03 -04003552 mWorkspaceLoading = false;
3553 }
3554
3555 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003556 * Updates the icons on the launcher that are affected by changes to the package list
3557 * on the device.
3558 */
3559 private void updateIconsAffectedByPackageManagerChanges() {
3560 updateAppMarketIcon();
3561 updateGlobalSearchIcon();
3562 updateVoiceSearchIcon();
3563 }
3564
3565 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003566 * Add the icons for all apps.
3567 *
3568 * Implementation of the method from LauncherModel.Callbacks.
3569 */
3570 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003571 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003572 if (mCustomizePagedView != null) {
3573 mCustomizePagedView.setApps(apps);
3574 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003575 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003576 }
3577
3578 /**
3579 * A package was installed.
3580 *
3581 * Implementation of the method from LauncherModel.Callbacks.
3582 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003583 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003584 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003585 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003586 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003587 if (mCustomizePagedView != null) {
3588 mCustomizePagedView.addApps(apps);
3589 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003590 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003591 }
3592
3593 /**
3594 * A package was updated.
3595 *
3596 * Implementation of the method from LauncherModel.Callbacks.
3597 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003598 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003599 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003600 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003601 if (mWorkspace != null) {
3602 mWorkspace.updateShortcuts(apps);
3603 }
3604 if (mAllAppsGrid != null) {
3605 mAllAppsGrid.updateApps(apps);
3606 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003607 if (mCustomizePagedView != null) {
3608 mCustomizePagedView.updateApps(apps);
3609 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003610 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 }
3612
3613 /**
3614 * A package was uninstalled.
3615 *
3616 * Implementation of the method from LauncherModel.Callbacks.
3617 */
Joe Onorato36115782010-06-17 13:28:48 -04003618 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003619 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003620 if (permanent) {
3621 mWorkspace.removeItems(apps);
3622 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003623 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003624 if (mCustomizePagedView != null) {
3625 mCustomizePagedView.removeApps(apps);
3626 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003627 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003628 }
Joe Onoratobe386092009-11-17 17:32:16 -08003629
3630 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003631 * A number of packages were updated.
3632 */
3633 public void bindPackagesUpdated() {
3634 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003635 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003636 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003637 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003638 }
3639
Winson Chung400438b2011-01-16 17:53:48 -08003640 private int mapConfigurationOriActivityInfoOri(int configOri) {
3641 final Display d = getWindowManager().getDefaultDisplay();
3642 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3643 switch (d.getRotation()) {
3644 case Surface.ROTATION_0:
3645 case Surface.ROTATION_180:
3646 // We are currently in the same basic orientation as the natural orientation
3647 naturalOri = configOri;
3648 break;
3649 case Surface.ROTATION_90:
3650 case Surface.ROTATION_270:
3651 // We are currently in the other basic orientation to the natural orientation
3652 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3653 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3654 break;
3655 }
3656
3657 int[] oriMap = {
3658 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3659 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3660 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3661 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3662 };
3663 // Since the map starts at portrait, we need to offset if this device's natural orientation
3664 // is landscape.
3665 int indexOffset = 0;
3666 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3667 indexOffset = 1;
3668 }
3669 return oriMap[(d.getRotation() + indexOffset) % 4];
3670 }
3671 public void lockScreenOrientation() {
3672 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3673 .getConfiguration().orientation));
3674 }
3675 public void unlockScreenOrientation() {
3676 mHandler.postDelayed(new Runnable() {
3677 public void run() {
3678 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3679 }
3680 }, mRestoreScreenOrientationDelay);
3681 }
3682
Winson Chung80baf5a2010-08-09 16:03:15 -07003683 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003684 * Prints out out state for debugging.
3685 */
3686 public void dumpState() {
3687 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003688 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003689 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3690 Log.d(TAG, "mRestoring=" + mRestoring);
3691 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3692 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3693 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003694 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003695 mModel.dumpState();
3696 mAllAppsGrid.dumpState();
3697 Log.d(TAG, "END launcher2 dump state");
3698 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003699}