blob: 65082c366327222149107cce7fc46d1e58e8040f [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka742574b2011-02-02 23:51:01 -080020import com.android.common.Search;
21import com.android.launcher.R;
22import com.android.launcher2.CustomizePagedView.CustomizationType;
23import com.android.launcher2.Workspace.ShrinkState;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070024
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080026import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070028import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070029import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080030import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070031import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.app.Dialog;
34import android.app.SearchManager;
35import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070036import android.appwidget.AppWidgetManager;
37import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080039import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070040import android.content.ClipData;
41import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040042import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080043import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.Context;
45import android.content.DialogInterface;
46import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080047import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080048import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080051import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080052import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070054import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040055import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080056import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080058import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080060import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070061import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040062import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070063import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020065import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080066import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070067import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040069import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080070import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070071import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070072import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080073import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.text.Selection;
75import android.text.SpannableStringBuilder;
76import android.text.TextUtils;
77import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070078import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080079import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080080import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.KeyEvent;
82import android.view.LayoutInflater;
83import android.view.Menu;
84import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070085import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080086import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.View;
88import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070089import android.view.WindowManager;
Michael Jurkae0f5a612011-02-07 16:45:41 -080090import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080091import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080092import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080093import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070095import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070097import android.widget.ImageView;
98import android.widget.LinearLayout;
99import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700100import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -0800101import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -0700102import android.widget.TextView;
103import android.widget.Toast;
Adam Cohen860f4c52011-01-27 20:16:13 -0800104import android.widget.TabHost.OnTabChangeListener;
105import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700106
Michael Jurka742574b2011-02-02 23:51:01 -0800107import java.io.DataInputStream;
108import java.io.DataOutputStream;
109import java.io.FileNotFoundException;
110import java.io.IOException;
111import java.util.ArrayList;
112import java.util.HashMap;
113import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800114
Adam Cohended9f8d2010-11-03 13:25:16 -0700115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116/**
117 * Default launcher application.
118 */
Michael Jurka946ad472010-07-09 18:05:18 -0700119public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700120 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
121 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800122 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700123 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400126 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400127 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800130 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700133 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
134 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
136 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700137 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
139 private static final int REQUEST_CREATE_SHORTCUT = 1;
140 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700141 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 private static final int REQUEST_PICK_APPLICATION = 6;
143 private static final int REQUEST_PICK_SHORTCUT = 7;
144 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700146 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
148 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
149
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800150 static final int SCREEN_COUNT = 5;
151 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Joe Onorato7c312c12009-08-13 21:36:53 -0700153 static final int DIALOG_CREATE_SHORTCUT = 1;
154 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Romain Guy98d01652009-06-30 16:21:04 -0700156 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
158 // Type: int
159 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700160 // Type: int
161 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: long
163 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
164 // Type: int
165 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
166 // Type: int
167 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
168 // Type: int
169 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 // Type: boolean
171 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
172 // Type: long
173 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
174
Winson Chung80baf5a2010-08-09 16:03:15 -0700175 // tags for the customization tabs
176 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700177 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700178 private static final String SHORTCUTS_TAG = "shortcuts";
179 private static final String WALLPAPERS_TAG = "wallpapers";
180
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700181 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
182
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700183 /** The different states that Launcher can be in. */
Michael Jurkaafca9532011-02-16 14:50:35 -0800184 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800185 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700186 private State mState = State.WORKSPACE;
187 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800192 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800194 private final BroadcastReceiver mCloseSystemDialogsReceiver
195 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800196 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private LayoutInflater mInflater;
199
Joe Onorato00acb122009-08-04 16:04:30 -0400200 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700202
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 private AppWidgetManager mAppWidgetManager;
204 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700205
Michael Jurka0280c3b2010-09-17 15:00:07 -0700206 private int mAddScreen = -1;
207 private int mAddIntersectCellX = -1;
208 private int mAddIntersectCellY = -1;
209 private int[] mAddDropPosition;
210 private int[] mTmpAddItemCellCoordinates = new int[2];
211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 private FolderInfo mFolderInfo;
213
Joe Onorato7c312c12009-08-13 21:36:53 -0700214 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700216 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700217 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700218 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700219
Michael Jurka838a4ca2011-02-07 13:33:06 -0800220 private View mButtonCluster;
221 private View mAllAppsButton;
222 private View mDivider;
223 private View mConfigureButton;
224
Winson Chunga12a2502010-12-20 14:41:35 -0800225 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700226 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 private Bundle mSavedState;
229
230 private SpannableStringBuilder mDefaultKeySsb = null;
231
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232 private boolean mWorkspaceLoading = true;
233
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800234 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 private boolean mRestoring;
236 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700237 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238
239 private Bundle mSavedInstanceState;
240
Joe Onorato9c1289c2009-08-17 11:03:03 -0400241 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800242 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800243 private boolean mUserPresent = true;
244 private boolean mVisible = false;
245 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700247 private static LocaleConfiguration sLocaleConfiguration = null;
248
Romain Guy84f296c2009-11-04 15:00:44 -0800249 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700250
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700251 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700252
Patrick Dubroy94f78a52011-02-28 17:39:16 -0800253 // The "signpost" images along the bottom of the screen (only in some layouts)
Romain Guy1fbc1c82009-11-09 20:43:08 -0800254 private ImageView mPreviousView;
255 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500256
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400257 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400258 private String[] mHotseatConfig = null;
259 private Intent[] mHotseats = null;
260 private Drawable[] mHotseatIcons = null;
261 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400262
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700263 private Intent mAppMarketIntent = null;
264
Adam Cohended9f8d2010-11-03 13:25:16 -0700265 // Related to the auto-advancing of widgets
266 private final int ADVANCE_MSG = 1;
267 private final int mAdvanceInterval = 20000;
268 private final int mAdvanceStagger = 250;
269 private long mAutoAdvanceSentTime;
270 private long mAutoAdvanceTimeLeft = -1;
271 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
272 new HashMap<View, AppWidgetProviderInfo>();
273
Winson Chung400438b2011-01-16 17:53:48 -0800274 // Determines how long to wait after a rotation before restoring the screen orientation to
275 // match the sensor state.
276 private final int mRestoreScreenOrientationDelay = 500;
277
Michael Jurka4ef207b2010-11-29 17:05:45 -0800278 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800279 private static Drawable.ConstantState sGlobalSearchIcon;
280 private static Drawable.ConstantState sVoiceSearchIcon;
281 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800282
Michael Jurkaddd62e92011-02-16 17:49:14 -0800283 private BubbleTextView mWaitingForResume;
284
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800285 private static ArrayList<PendingAddArguments> sPendingAddList
286 = new ArrayList<PendingAddArguments>();
287
288 private static class PendingAddArguments {
289 int requestCode;
290 Intent intent;
291 int screen;
292 int cellX;
293 int cellY;
294 }
295
Winson Chunga12a2502010-12-20 14:41:35 -0800296 private CustomizationType getCustomizeFilterForTabTag(String tag) {
297 if (tag.equals(WIDGETS_TAG)) {
298 return CustomizationType.WidgetCustomization;
299 } else if (tag.equals(APPLICATIONS_TAG)) {
300 return CustomizationType.ApplicationCustomization;
301 } else if (tag.equals(WALLPAPERS_TAG)) {
302 return CustomizePagedView.CustomizationType.WallpaperCustomization;
303 } else if (tag.equals(SHORTCUTS_TAG)) {
304 return CustomizePagedView.CustomizationType.ShortcutCustomization;
305 }
306 return CustomizationType.WidgetCustomization;
307 }
308
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 @Override
310 protected void onCreate(Bundle savedInstanceState) {
311 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700312
Winson Chungaafa03c2010-06-11 17:34:16 -0700313 if (LauncherApplication.isInPlaceRotationEnabled()) {
314 // hide the status bar (temporary until we get the status bar design figured out)
315 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
316 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
317 }
318
Joe Onorato0589f0f2010-02-08 13:44:00 -0800319 LauncherApplication app = ((LauncherApplication)getApplication());
320 mModel = app.setLauncher(this);
321 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400322 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700324
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700325 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700326 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
327 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700328
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200330 android.os.Debug.startMethodTracing(
331 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 }
333
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400334 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800337 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700338 if (mHomeCustomizationDrawer != null) {
339 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700340
Winson Chung80baf5a2010-08-09 16:03:15 -0700341 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700342 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
Michael Jurka9c6fbed2011-03-02 17:41:34 -0800343 R.layout.customization_drawer_tab_contents, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700344 TabContentFactory contentFactory = new TabContentFactory() {
345 public View createTabContent(String tag) {
346 return mCustomizePagedView;
347 }
348 };
349
Winson Chung49767ae2010-11-29 14:48:30 -0800350
351 TextView tabView;
352 TabWidget tabWidget = (TabWidget)
353 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
354
355 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
356 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700357 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800358 .setIndicator(tabView).setContent(contentFactory));
359 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
360 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700361 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800362 .setIndicator(tabView).setContent(contentFactory));
363 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
364 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700365 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800366 .setIndicator(tabView).setContent(contentFactory));
367 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
368 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700369 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800370 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700371 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
372 public void onTabChanged(String tabId) {
Winson Chunga12a2502010-12-20 14:41:35 -0800373 final CustomizePagedView.CustomizationType newType =
374 getCustomizeFilterForTabTag(tabId);
375 if (newType != mCustomizePagedView.getCustomizationFilter()) {
376 // animate the changing of the tab content by fading pages in and out
377 final Resources res = getResources();
378 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
379 final float alpha = mCustomizePagedView.getAlpha();
380 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
381 "alpha", alpha, 0.0f);
382 alphaAnim.setDuration(duration);
383 alphaAnim.addListener(new AnimatorListenerAdapter() {
384 @Override
385 public void onAnimationEnd(Animator animation) {
Winson Chunga12a2502010-12-20 14:41:35 -0800386 mCustomizePagedView.setCustomizationFilter(newType);
Winson Chung80baf5a2010-08-09 16:03:15 -0700387
Winson Chunga12a2502010-12-20 14:41:35 -0800388 final float alpha = mCustomizePagedView.getAlpha();
389 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
390 mCustomizePagedView, "alpha", alpha, 1.0f);
391 alphaAnim.setDuration(duration);
392 alphaAnim.start();
393 }
394 });
395 alphaAnim.start();
396 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700397 }
398 });
Michael Jurka946ad472010-07-09 18:05:18 -0700399 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 setupViews();
401
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800402 registerContentObservers();
403
Joe Onorato7c312c12009-08-13 21:36:53 -0700404 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 mSavedState = savedInstanceState;
407 restoreState(mSavedState);
408
Winson Chunga12a2502010-12-20 14:41:35 -0800409 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800410 if (mCustomizePagedView != null) {
411 mCustomizePagedView.update();
412 }
Winson Chunga12a2502010-12-20 14:41:35 -0800413
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 if (PROFILE_STARTUP) {
415 android.os.Debug.stopMethodTracing();
416 }
417
418 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400419 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 }
421
422 // For handling default keys
423 mDefaultKeySsb = new SpannableStringBuilder();
424 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800425
426 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
427 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800428
429 // If we have a saved version of these external icons, we load them up immediately
430 if (LauncherApplication.isScreenXLarge()) {
Winson Chungc6d20002011-03-02 11:57:29 -0800431 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
432 updateIconsAffectedByPackageManagerChanges();
Michael Jurka4ef207b2010-11-29 17:05:45 -0800433 }
Winson Chungc6d20002011-03-02 11:57:29 -0800434 updateGlobalSearchIcon(sGlobalSearchIcon);
435 updateVoiceSearchIcon(sVoiceSearchIcon);
436 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800437 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 }
Romain Guycbb89e42009-06-08 15:52:54 -0700439
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800440 @Override
441 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
442 super.dispatchPopulateAccessibilityEvent(event);
443
444 // we want to take over text population so it is context dependent
445 event.getText().clear();
446 if (mState == State.ALL_APPS) {
447 event.getText().add(getString(R.string.all_apps_button_label));
448 } else if (mState == State.WORKSPACE) {
449 event.getText().add(getString(R.string.all_apps_home_button_label));
450 }
451
452 return true;
453 }
454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700456 if (sLocaleConfiguration == null) {
457 new AsyncTask<Void, Void, LocaleConfiguration>() {
458 @Override
459 protected LocaleConfiguration doInBackground(Void... unused) {
460 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
461 readConfiguration(Launcher.this, localeConfiguration);
462 return localeConfiguration;
463 }
464
465 @Override
466 protected void onPostExecute(LocaleConfiguration result) {
467 sLocaleConfiguration = result;
468 checkForLocaleChange(); // recursive, but now with a locale configuration
469 }
470 }.execute();
471 return;
472 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700473
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 final Configuration configuration = getResources().getConfiguration();
475
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700476 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 final String locale = configuration.locale.toString();
478
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700479 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 final int mcc = configuration.mcc;
481
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700482 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 final int mnc = configuration.mnc;
484
Romain Guy84f296c2009-11-04 15:00:44 -0800485 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486
Romain Guy84f296c2009-11-04 15:00:44 -0800487 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700488 sLocaleConfiguration.locale = locale;
489 sLocaleConfiguration.mcc = mcc;
490 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700491
Joe Onorato0589f0f2010-02-08 13:44:00 -0800492 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400493 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700494
495 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
496 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700497 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700498 public void run() {
499 writeConfiguration(Launcher.this, localeConfiguration);
500 }
501 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700502 }
503 }
504
505 private static class LocaleConfiguration {
506 public String locale;
507 public int mcc = -1;
508 public int mnc = -1;
509 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700510
Romain Guy98d01652009-06-30 16:21:04 -0700511 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
512 DataInputStream in = null;
513 try {
514 in = new DataInputStream(context.openFileInput(PREFERENCES));
515 configuration.locale = in.readUTF();
516 configuration.mcc = in.readInt();
517 configuration.mnc = in.readInt();
518 } catch (FileNotFoundException e) {
519 // Ignore
520 } catch (IOException e) {
521 // Ignore
522 } finally {
523 if (in != null) {
524 try {
525 in.close();
526 } catch (IOException e) {
527 // Ignore
528 }
529 }
530 }
531 }
532
533 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
534 DataOutputStream out = null;
535 try {
536 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
537 out.writeUTF(configuration.locale);
538 out.writeInt(configuration.mcc);
539 out.writeInt(configuration.mnc);
540 out.flush();
541 } catch (FileNotFoundException e) {
542 // Ignore
543 } catch (IOException e) {
544 //noinspection ResultOfMethodCallIgnored
545 context.getFileStreamPath(PREFERENCES).delete();
546 } finally {
547 if (out != null) {
548 try {
549 out.close();
550 } catch (IOException e) {
551 // Ignore
552 }
553 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 }
555 }
556
557 static int getScreen() {
558 synchronized (sLock) {
559 return sScreen;
560 }
561 }
562
563 static void setScreen(int screen) {
564 synchronized (sLock) {
565 sScreen = screen;
566 }
567 }
568
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400569 // Note: This doesn't do all the client-id magic that BrowserProvider does
570 // in Browser. (http://b/2425179)
571 private Uri getDefaultBrowserUri() {
572 String url = getString(R.string.default_browser_url);
573 if (url.indexOf("{CID}") != -1) {
574 url = url.replace("{CID}", "android-google");
575 }
576 return Uri.parse(url);
577 }
578
579 // Load the Intent templates from arrays.xml to populate the hotseats. For
580 // each Intent, if it resolves to a single app, use that as the launch
581 // intent & use that app's label as the contentDescription. Otherwise,
582 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400583 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400584 if (mHotseatConfig == null) {
585 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
586 if (mHotseatConfig.length > 0) {
587 mHotseats = new Intent[mHotseatConfig.length];
588 mHotseatLabels = new CharSequence[mHotseatConfig.length];
589 mHotseatIcons = new Drawable[mHotseatConfig.length];
590 } else {
591 mHotseats = null;
592 mHotseatIcons = null;
593 mHotseatLabels = null;
594 }
595
596 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
597 for (int i=0; i<mHotseatConfig.length; i++) {
598 // load icon for this slot; currently unrelated to the actual activity
599 try {
600 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
601 } catch (ArrayIndexOutOfBoundsException ex) {
602 Log.w(TAG, "Missing hotseat_icons array item #" + i);
603 mHotseatIcons[i] = null;
604 }
605 }
606 hotseatIconDrawables.recycle();
607 }
608
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400609 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400610 for (int i=0; i<mHotseatConfig.length; i++) {
611 Intent intent = null;
612 if (mHotseatConfig[i].equals("*BROWSER*")) {
613 // magic value meaning "launch user's default web browser"
614 // replace it with a generic web request so we can see if there is indeed a default
615 String defaultUri = getString(R.string.default_browser_url);
616 intent = new Intent(
617 Intent.ACTION_VIEW,
618 ((defaultUri != null)
619 ? Uri.parse(defaultUri)
620 : getDefaultBrowserUri())
621 ).addCategory(Intent.CATEGORY_BROWSABLE);
622 // note: if the user launches this without a default set, she
623 // will always be taken to the default URL above; this is
624 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700625 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400626 // URL is unavoidably sent to the chosen app.
627 } else {
628 try {
629 intent = Intent.parseUri(mHotseatConfig[i], 0);
630 } catch (java.net.URISyntaxException ex) {
631 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
632 // bogus; leave intent=null
633 }
634 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700635
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400636 if (intent == null) {
637 mHotseats[i] = null;
638 mHotseatLabels[i] = getText(R.string.activity_not_found);
639 continue;
640 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400641
642 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700643 Log.d(TAG, "loadHotseats: hotseat " + i
644 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400645 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400646 + "]");
647 }
648
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400649 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
650 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700651 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400652 Log.d(TAG, "Best match for intent: " + bestMatch);
653 Log.d(TAG, "All matches: ");
654 for (ResolveInfo ri : allMatches) {
655 Log.d(TAG, " --> " + ri);
656 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400657 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400658 // did this resolve to a single app, or the resolver?
659 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700660 // can't find any activity to handle this. let's leave the
661 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400662 // to launch.
663 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400664
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400665 // set accessibility text to "Not installed"
666 mHotseatLabels[i] = getText(R.string.activity_not_found);
667 } else {
668 boolean found = false;
669 for (ResolveInfo ri : allMatches) {
670 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
671 && bestMatch.activityInfo.applicationInfo.packageName
672 .equals(ri.activityInfo.applicationInfo.packageName)) {
673 found = true;
674 break;
675 }
676 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700677
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400678 if (!found) {
679 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
680 // the bestMatch is probably the ResolveActivity, meaning the
681 // user has not yet selected a default
682 // so: we'll keep the original intent for now
683 mHotseats[i] = intent;
684
685 // set the accessibility text to "Select shortcut"
686 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
687 } else {
688 // we have an app!
689 // now reconstruct the intent to launch it through the front
690 // door
691 ComponentName com = new ComponentName(
692 bestMatch.activityInfo.applicationInfo.packageName,
693 bestMatch.activityInfo.name);
694 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
695
696 // load the app label for accessibility
697 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
698 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400699 }
700
701 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700702 Log.d(TAG, "loadHotseats: hotseat " + i
703 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400704 + ((mHotseats[i] == null)
705 ? "null"
706 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400707 + "] label=[" + mHotseatLabels[i]
708 + "]"
709 );
710 }
711 }
712 }
713
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800714 private void completeAdd(PendingAddArguments args) {
715 switch (args.requestCode) {
716 case REQUEST_PICK_APPLICATION:
717 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
718 break;
719 case REQUEST_PICK_SHORTCUT:
720 processShortcut(args.intent);
721 break;
722 case REQUEST_CREATE_SHORTCUT:
723 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
724 break;
725 case REQUEST_PICK_LIVE_FOLDER:
726 addLiveFolder(args.intent);
727 break;
728 case REQUEST_CREATE_LIVE_FOLDER:
729 completeAddLiveFolder(args.intent, args.screen, args.cellX, args.cellY);
730 break;
731 case REQUEST_PICK_APPWIDGET:
732 addAppWidgetFromPick(args.intent);
733 break;
734 case REQUEST_CREATE_APPWIDGET:
735 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
736 completeAddAppWidget(appWidgetId, args.screen);
737 break;
738 case REQUEST_PICK_WALLPAPER:
739 // We just wanted the activity result here so we can clear mWaitingForResult
740 break;
741 }
742 }
743
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800745 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700746 mWaitingForResult = false;
747
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 // The pattern used here is that a user PICKs a specific application,
749 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700750
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
752 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700753
Michael Jurka0280c3b2010-09-17 15:00:07 -0700754 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800755 final PendingAddArguments args = new PendingAddArguments();
756 args.requestCode = requestCode;
757 args.intent = data;
758 args.screen = mAddScreen;
759 args.cellX = mAddIntersectCellX;
760 args.cellY = mAddIntersectCellY;
761
762 // If the loader is still running, defer the add until it is done.
763 if (isWorkspaceLocked()) {
764 sPendingAddList.add(args);
765 } else {
766 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 }
Romain Guy18042c82009-11-06 11:44:55 -0800768 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
769 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
770 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700771 // Clean up the appWidgetId if we canceled
772 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
773 if (appWidgetId != -1) {
774 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775 }
776 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 }
778
779 @Override
780 protected void onResume() {
781 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800782 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700783 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400784 mWorkspaceLoading = true;
785 mModel.startLoader(this, true);
786 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700787 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800789 if (mWaitingForResume != null) {
790 mWaitingForResume.setStayPressed(false);
791 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700792 // When we resume Launcher, a different Activity might be responsible for the app
793 // market intent, so refresh the icon
794 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 }
796
797 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700798 protected void onPause() {
799 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700800 // Some launcher layouts don't have a previous and next view
801 if (mPreviousView != null) {
802 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700803 }
804 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700805 dismissPreview(mNextView);
806 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700807 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800808 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700809 }
Romain Guycbb89e42009-06-08 15:52:54 -0700810
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700811 @Override
812 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813 // Flag the loader to stop early before switching
814 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800815 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800816 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700817 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700818
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800819 // We can't hide the IME if it was forced open. So don't bother
820 /*
821 @Override
822 public void onWindowFocusChanged(boolean hasFocus) {
823 super.onWindowFocusChanged(hasFocus);
824
825 if (hasFocus) {
826 final InputMethodManager inputManager = (InputMethodManager)
827 getSystemService(Context.INPUT_METHOD_SERVICE);
828 WindowManager.LayoutParams lp = getWindow().getAttributes();
829 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
830 android.os.Handler()) {
831 protected void onReceiveResult(int resultCode, Bundle resultData) {
832 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
833 }
834 });
835 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
836 }
837 }
838 */
839
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 private boolean acceptFilter() {
841 final InputMethodManager inputManager = (InputMethodManager)
842 getSystemService(Context.INPUT_METHOD_SERVICE);
843 return !inputManager.isFullscreenMode();
844 }
845
846 @Override
847 public boolean onKeyDown(int keyCode, KeyEvent event) {
848 boolean handled = super.onKeyDown(keyCode, event);
849 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
850 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
851 keyCode, event);
852 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700853 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700854 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700855 // showSearchDialog()
856 // If there are multiple keystrokes before the search dialog takes focus,
857 // onSearchRequested() will be called for every keystroke,
858 // but it is idempotent, so it's fine.
859 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 }
861 }
862
Joe Onorato8a9625e2010-01-28 15:55:35 -0800863 // Eat the long press event so the keyboard doesn't come up.
864 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
865 return true;
866 }
867
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 return handled;
869 }
870
Karl Rosaen138a0412009-04-23 19:00:21 -0700871 private String getTypedText() {
872 return mDefaultKeySsb.toString();
873 }
874
875 private void clearTypedText() {
876 mDefaultKeySsb.clear();
877 mDefaultKeySsb.clearSpans();
878 Selection.setSelection(mDefaultKeySsb, 0);
879 }
880
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700882 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
883 * State
884 */
885 private static State intToState(int stateOrdinal) {
886 State state = State.WORKSPACE;
887 final State[] stateValues = State.values();
888 for (int i = 0; i < stateValues.length; i++) {
889 if (stateValues[i].ordinal() == stateOrdinal) {
890 state = stateValues[i];
891 break;
892 }
893 }
894 return state;
895 }
896
897 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 * Restores the previous state, if it exists.
899 *
900 * @param savedState The previous state.
901 */
902 private void restoreState(Bundle savedState) {
903 if (savedState == null) {
904 return;
905 }
906
Michael Jurka883f55b2010-10-21 15:47:14 -0700907 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
908
909 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800910 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700911 } else if (state == State.CUSTOMIZE) {
912 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800913 }
914
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
916 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700917 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 }
919
920 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700921
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700923 mAddScreen = addScreen;
924 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
925 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 mRestoring = true;
927 }
928
929 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
930 if (renameFolder) {
931 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700932 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 mRestoring = true;
934 }
Winson Chunga12a2502010-12-20 14:41:35 -0800935
936 // Restore the current AllApps drawer tab
937 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
938 String curTab = savedState.getString("allapps_currentTab");
939 if (curTab != null) {
940 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
941 tabhost.setCurrentTabByTag(curTab);
942 }
943 int curPage = savedState.getInt("allapps_currentPage", -1);
944 if (curPage > -1) {
945 mAllAppsPagedView.setRestorePage(curPage);
946 }
947 }
948
949 // Restore the current customization drawer tab
950 if (mHomeCustomizationDrawer != null) {
951 String curTab = savedState.getString("customize_currentTab");
952 if (curTab != null) {
953 // We set this directly so that there is no delay before the tab is set
954 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
955 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
956 }
957
958 // Note: currently we do not restore the page for the customization tray because unlike
959 // AllApps, the page content can change drastically
960 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 }
962
963 /**
964 * Finds all the views we need and configure them properly.
965 */
966 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700967 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400968
Romain Guyb1b69f52009-08-10 15:10:15 -0700969 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400970 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971
Joe Onorato7c312c12009-08-13 21:36:53 -0700972 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700973 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700974 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800975 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700976 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700977 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700978
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700979 if (LauncherApplication.isScreenXLarge()) {
980 // They need to be INVISIBLE initially so that they will be measured in the layout.
981 // Otherwise the animations are messed up when we show them for the first time.
982 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
983 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
984 }
985
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700987
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500989 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700990
Joe Onorato7c312c12009-08-13 21:36:53 -0700991 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
992 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700994 View handleView = findViewById(R.id.all_apps_button);
995 if (handleView != null && handleView instanceof HandleView) {
996 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700997 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700998 mHandleView.setLauncher(this);
999 mHandleView.setOnClickListener(this);
1000 mHandleView.setOnLongClickListener(this);
1001 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001002
Winson Chung80baf5a2010-08-09 16:03:15 -07001003 if (mCustomizePagedView != null) {
1004 mCustomizePagedView.setLauncher(this);
1005 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -07001006 } else {
1007 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
1008 hotseatLeft.setContentDescription(mHotseatLabels[0]);
1009 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
1010 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
1011 hotseatRight.setContentDescription(mHotseatLabels[1]);
1012 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001013
Michael Jurkaaf442092010-06-10 17:01:57 -07001014 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
1015 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001016
Michael Jurkaaf442092010-06-10 17:01:57 -07001017 Drawable previous = mPreviousView.getDrawable();
1018 Drawable next = mNextView.getDrawable();
1019 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020
Michael Jurkaaf442092010-06-10 17:01:57 -07001021 mPreviousView.setHapticFeedbackEnabled(false);
1022 mPreviousView.setOnLongClickListener(this);
1023 mNextView.setHapticFeedbackEnabled(false);
1024 mNextView.setOnLongClickListener(this);
1025 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001026
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001028 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -08001030 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031
1032 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001033 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -08001034
1035 final View allAppsButton = findViewById(R.id.all_apps_button);
1036 final View divider = findViewById(R.id.divider);
1037 final View configureButton = findViewById(R.id.configure_button);
1038
Michael Jurka54f7ac32010-08-02 13:56:46 -07001039 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001040 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -07001041 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001042 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001043 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001044 dragController.addDragListener(deleteZone);
1045
Adam Cohencdc30d52010-12-01 15:09:47 -08001046 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1047 if (allAppsDeleteZone != null) {
1048 allAppsDeleteZone.setLauncher(this);
1049 allAppsDeleteZone.setDragController(dragController);
1050 allAppsDeleteZone.setDragAndDropEnabled(false);
1051 dragController.addDragListener(allAppsDeleteZone);
1052 dragController.addDropTarget(allAppsDeleteZone);
1053 }
1054
1055 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1056 findViewById(R.id.all_apps_info_target);
1057 if (allAppsInfoTarget != null) {
1058 allAppsInfoTarget.setLauncher(this);
1059 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001060 allAppsInfoTarget.setDragAndDropEnabled(false);
1061 View marketButton = findViewById(R.id.market_button);
1062 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001063 allAppsInfoTarget.setOverlappingView(marketButton);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08001064 marketButton.setOnClickListener(new OnClickListener() {
1065 public void onClick(View v) {
1066 onClickAppMarketButton(v);
1067 }
1068 });
Adam Cohencdc30d52010-12-01 15:09:47 -08001069 }
1070 }
1071
Joe Onorato00acb122009-08-04 16:04:30 -04001072 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001073 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001074 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001075
Joe Onorato00acb122009-08-04 16:04:30 -04001076 // The order here is bottom to top.
1077 dragController.addDropTarget(workspace);
1078 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001079 if (allAppsInfoTarget != null) {
1080 dragController.addDropTarget(allAppsInfoTarget);
1081 }
1082 if (allAppsDeleteZone != null) {
1083 dragController.addDropTarget(allAppsDeleteZone);
1084 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001085 mButtonCluster = findViewById(R.id.all_apps_button_cluster);
1086
1087 mAllAppsButton = findViewById(R.id.all_apps_button);
1088 mDivider = findViewById(R.id.divider);
1089 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001090
1091 // We had previously set these click handlers in XML, but the first time we launched
1092 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1093 // found the right handler. Setting the handlers directly here eliminates that cost.
1094 if (mConfigureButton != null) {
1095 mConfigureButton.setOnClickListener(new OnClickListener() {
1096 public void onClick(View v) {
1097 onClickConfigureButton(v);
1098 }
1099 });
1100 }
1101 if (mDivider != null) {
1102 mDivider.setOnClickListener(new OnClickListener() {
1103 public void onClick(View v) {
1104 onClickAllAppsButton(v);
1105 }
1106 });
1107 }
1108 if (mAllAppsButton != null) {
1109 mAllAppsButton.setOnClickListener(new OnClickListener() {
1110 public void onClick(View v) {
1111 onClickAllAppsButton(v);
1112 }
1113 });
1114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 }
1116
Romain Guy8a73c512009-11-09 19:19:59 -08001117 @SuppressWarnings({"UnusedDeclaration"})
1118 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001119 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001120 mWorkspace.scrollLeft();
1121 }
Romain Guy8a73c512009-11-09 19:19:59 -08001122 }
1123
1124 @SuppressWarnings({"UnusedDeclaration"})
1125 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001126 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001127 mWorkspace.scrollRight();
1128 }
Romain Guy8a73c512009-11-09 19:19:59 -08001129 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001130
1131 @SuppressWarnings({"UnusedDeclaration"})
1132 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001133 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001134
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001135 int index = -1;
1136 if (v.getId() == R.id.hotseat_left) {
1137 index = 0;
1138 } else if (v.getId() == R.id.hotseat_right) {
1139 index = 1;
1140 }
1141
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001142 // reload these every tap; you never know when they might change
1143 loadHotseats();
1144 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001145 startActivitySafely(
1146 mHotseats[index],
1147 "hotseat"
1148 );
1149 }
1150 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001151
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 /**
1153 * Creates a view representing a shortcut.
1154 *
1155 * @param info The data structure describing the shortcut.
1156 *
1157 * @return A View inflated from R.layout.application.
1158 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001159 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001161 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 }
1163
1164 /**
1165 * Creates a view representing a shortcut inflated from the specified resource.
1166 *
1167 * @param layoutResId The id of the XML layout used to create the shortcut.
1168 * @param parent The group the shortcut belongs to.
1169 * @param info The data structure describing the shortcut.
1170 *
1171 * @return A View inflated from layoutResId.
1172 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001173 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001174 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1175 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 return favorite;
1178 }
1179
1180 /**
1181 * Add an application shortcut to the workspace.
1182 *
1183 * @param data The intent describing the application.
1184 * @param cellInfo The position on screen where to create the shortcut.
1185 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001186 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001187 int intersectCellX, int intersectCellY) {
1188 final int[] cellXY = mTmpAddItemCellCoordinates;
1189 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1190
1191 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1192 showOutOfSpaceMessage();
1193 return;
1194 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001196 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001197
Romain Guy73b979d2009-06-09 12:57:21 -07001198 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001199 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001201 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001202 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1203 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001204 } else {
1205 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 }
1207 }
Romain Guycbb89e42009-06-08 15:52:54 -07001208
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 /**
1210 * Add a shortcut to the workspace.
1211 *
1212 * @param data The intent describing the shortcut.
1213 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001215 private void completeAddShortcut(Intent data, int screen,
1216 int intersectCellX, int intersectCellY) {
1217 final int[] cellXY = mTmpAddItemCellCoordinates;
1218 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001219
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001220 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001221 boolean foundCellSpan = false;
1222 if (touchXY != null) {
1223 // when dragging and dropping, just find the closest free spot
1224 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1225 int[] result = screenLayout.findNearestVacantArea(
1226 touchXY[0], touchXY[1], 1, 1, cellXY);
1227 foundCellSpan = (result != null);
1228 } else {
1229 foundCellSpan = layout.findCellForSpanThatIntersects(
1230 cellXY, 1, 1, intersectCellX, intersectCellY);
1231 }
1232
1233 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001234 showOutOfSpaceMessage();
1235 return;
1236 }
1237
1238 final ShortcutInfo info = mModel.addShortcut(
1239 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240
1241 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001243 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 }
1245 }
1246
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001248 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001250 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001251 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 */
Winson Chungf7640c82011-02-28 13:47:29 -08001253 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001254 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001255
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001256 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001257 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1258 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001261 // We have saved the position to which the widget was dragged-- this really only matters
1262 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001263 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001264
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001265 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001266 boolean foundCellSpan = false;
1267 if (touchXY != null) {
1268 // when dragging and dropping, just find the closest free spot
1269 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1270 int[] result = screenLayout.findNearestVacantArea(
1271 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001272 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001273 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001274 // if we long pressed on an empty cell to bring up a menu,
1275 // make sure we intersect the empty cell
1276 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1277 // findCellForSpanThatIntersects will just ignore them
1278 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1279 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001280 }
1281
Michael Jurka0280c3b2010-09-17 15:00:07 -07001282 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001283 if (appWidgetId != -1) {
1284 // Deleting an app widget ID is a void call but writes to disk before returning
1285 // to the caller...
1286 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1287 new Thread("deleteAppWidgetId") {
1288 public void run() {
1289 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1290 }
1291 }.start();
1292 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001293 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001294 return;
1295 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001297 // Build Launcher-specific widget info and save to database
1298 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001299 launcherInfo.spanX = spanXY[0];
1300 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001301
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 LauncherModel.addItemToDatabase(this, launcherInfo,
1303 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001304 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305
1306 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001307 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001308
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001310 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001311
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001312 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001314
Michael Jurka0280c3b2010-09-17 15:00:07 -07001315 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001316 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001317
1318 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320 }
Romain Guycbb89e42009-06-08 15:52:54 -07001321
Adam Cohended9f8d2010-11-03 13:25:16 -07001322 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1323 @Override
1324 public void onReceive(Context context, Intent intent) {
1325 final String action = intent.getAction();
1326 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1327 mUserPresent = false;
1328 updateRunning();
1329 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1330 mUserPresent = true;
1331 updateRunning();
1332 }
1333 }
1334 };
1335
1336 @Override
1337 public void onAttachedToWindow() {
1338 super.onAttachedToWindow();
1339
1340 // Listen for broadcasts related to user-presence
1341 final IntentFilter filter = new IntentFilter();
1342 filter.addAction(Intent.ACTION_SCREEN_OFF);
1343 filter.addAction(Intent.ACTION_USER_PRESENT);
1344 registerReceiver(mReceiver, filter);
1345
Adam Cohend113e0c2010-11-11 10:48:05 -08001346 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001347 mVisible = true;
1348 }
1349
1350 @Override
1351 public void onDetachedFromWindow() {
1352 super.onDetachedFromWindow();
1353 mVisible = false;
1354
Adam Cohend113e0c2010-11-11 10:48:05 -08001355 if (mAttached) {
1356 unregisterReceiver(mReceiver);
1357 mAttached = false;
1358 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001359 updateRunning();
1360 }
1361
1362 public void onWindowVisibilityChanged(int visibility) {
1363 mVisible = visibility == View.VISIBLE;
1364 updateRunning();
1365 }
1366
1367 private void sendAdvanceMessage(long delay) {
1368 mHandler.removeMessages(ADVANCE_MSG);
1369 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1370 mHandler.sendMessageDelayed(msg, delay);
1371 mAutoAdvanceSentTime = System.currentTimeMillis();
1372 }
1373
1374 private void updateRunning() {
1375 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1376 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1377 mAutoAdvanceRunning = autoAdvanceRunning;
1378 if (autoAdvanceRunning) {
1379 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1380 sendAdvanceMessage(delay);
1381 } else {
1382 if (!mWidgetsToAdvance.isEmpty()) {
1383 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1384 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1385 }
1386 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001387 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001388 }
1389 }
1390 }
1391
1392 private final Handler mHandler = new Handler() {
1393 @Override
1394 public void handleMessage(Message msg) {
1395 if (msg.what == ADVANCE_MSG) {
1396 int i = 0;
1397 for (View key: mWidgetsToAdvance.keySet()) {
1398 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1399 final int delay = mAdvanceStagger * i;
1400 if (v instanceof Advanceable) {
1401 postDelayed(new Runnable() {
1402 public void run() {
1403 ((Advanceable) v).advance();
1404 }
1405 }, delay);
1406 }
1407 i++;
1408 }
1409 sendAdvanceMessage(mAdvanceInterval);
1410 }
1411 }
1412 };
1413
1414 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1415 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1416 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1417 if (v instanceof Advanceable) {
1418 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001419 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001420 updateRunning();
1421 }
1422 }
1423
1424 void removeWidgetToAutoAdvance(View hostView) {
1425 if (mWidgetsToAdvance.containsKey(hostView)) {
1426 mWidgetsToAdvance.remove(hostView);
1427 updateRunning();
1428 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001429 }
1430
Joe Onorato9c1289c2009-08-17 11:03:03 -04001431 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1432 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001433 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001434 launcherInfo.hostView = null;
1435 }
1436
Adam Cohended9f8d2010-11-03 13:25:16 -07001437 void showOutOfSpaceMessage() {
1438 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1439 }
1440
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001441 public LauncherAppWidgetHost getAppWidgetHost() {
1442 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 }
Romain Guycbb89e42009-06-08 15:52:54 -07001444
Winson Chunga9abd0e2010-10-27 17:18:37 -07001445 public LauncherModel getModel() {
1446 return mModel;
1447 }
1448
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001449 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001450 getWindow().closeAllPanels();
1451
1452 try {
1453 dismissDialog(DIALOG_CREATE_SHORTCUT);
1454 // Unlock the workspace if the dialog was showing
1455 } catch (Exception e) {
1456 // An exception is thrown if the dialog is not visible, which is fine
1457 }
1458
1459 try {
1460 dismissDialog(DIALOG_RENAME_FOLDER);
1461 // Unlock the workspace if the dialog was showing
1462 } catch (Exception e) {
1463 // An exception is thrown if the dialog is not visible, which is fine
1464 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001465
1466 // Whatever we were doing is hereby canceled.
1467 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001468 }
1469
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 @Override
1471 protected void onNewIntent(Intent intent) {
1472 super.onNewIntent(intent);
1473
1474 // Close the menu
1475 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001476 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001477 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001478
Joe Onorato14f122b2009-11-19 14:06:36 -08001479 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1480 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001481
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001482 // In all these cases, only animate if we're already on home
1483
Michael Jurka4cb37242010-08-09 21:05:32 -07001484 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001485 mWorkspace.unshrink(alreadyOnHome);
1486 }
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001487
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001488 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001489 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001490 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001491 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001492
Joe Onorato3a8820b2009-11-10 15:06:42 -08001493 final View v = getWindow().peekDecorView();
1494 if (v != null && v.getWindowToken() != null) {
1495 InputMethodManager imm = (InputMethodManager)getSystemService(
1496 INPUT_METHOD_SERVICE);
1497 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 }
1499 }
1500 }
1501
1502 @Override
1503 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1504 // Do not call super here
1505 mSavedInstanceState = savedInstanceState;
1506 }
1507
1508 @Override
1509 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001510 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511
1512 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1513 if (folders.size() > 0) {
1514 final int count = folders.size();
1515 long[] ids = new long[count];
1516 for (int i = 0; i < count; i++) {
1517 final FolderInfo info = folders.get(i).getInfo();
1518 ids[i] = info.id;
1519 }
1520 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1521 } else {
1522 super.onSaveInstanceState(outState);
1523 }
1524
Michael Jurka883f55b2010-10-21 15:47:14 -07001525 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526
Michael Jurka0280c3b2010-09-17 15:00:07 -07001527 if (mAddScreen > -1 && mWaitingForResult) {
1528 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1529 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1530 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
1532
1533 if (mFolderInfo != null && mWaitingForResult) {
1534 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1535 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1536 }
Michael Jurka946ad472010-07-09 18:05:18 -07001537
Winson Chung43b119d2010-12-06 16:45:05 -08001538 // Save the current AllApps drawer tab
1539 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1540 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1541 String currentTabTag = tabhost.getCurrentTabTag();
1542 if (currentTabTag != null) {
1543 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001544 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001545 }
1546 }
1547
1548 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001549 if (mHomeCustomizationDrawer != null) {
1550 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1551 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001552 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001553 }
1554 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
1556
1557 @Override
1558 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001562 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001564 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001566 mAppWidgetHost = null;
1567
1568 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569
1570 TextKeyListener.getInstance().release();
1571
Joe Onorato9c1289c2009-08-17 11:03:03 -04001572 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573
Joe Onorato9c1289c2009-08-17 11:03:03 -04001574 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001575
1576 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001577
Patrick Dubroyab962b72010-07-26 12:10:10 -07001578 // Some launcher layouts don't have a previous and next view
1579 if (mPreviousView != null) {
1580 dismissPreview(mPreviousView);
1581 }
1582 if (mNextView != null) {
1583 dismissPreview(mNextView);
1584 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001585
1586 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001587
1588 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1589 mWorkspace.removeAllViews();
1590 mWorkspace = null;
1591 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001592
1593 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 }
1595
1596 @Override
1597 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001598 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 super.startActivityForResult(intent, requestCode);
1600 }
1601
1602 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001603 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001605
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001606 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001607
Karl Rosaen138a0412009-04-23 19:00:21 -07001608 if (initialQuery == null) {
1609 // Use any text typed in the launcher as the initial query
1610 initialQuery = getTypedText();
1611 clearTypedText();
1612 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 if (appSearchData == null) {
1614 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001615 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616 }
Romain Guycbb89e42009-06-08 15:52:54 -07001617
Karl Rosaen138a0412009-04-23 19:00:21 -07001618 final SearchManager searchManager =
1619 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001620 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001621 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 }
1623
1624 @Override
1625 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001626 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001627 return false;
1628 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629
1630 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001631
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1633 .setIcon(android.R.drawable.ic_menu_add)
1634 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001635 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1636 .setIcon(android.R.drawable.ic_menu_manage)
1637 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001638 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 .setIcon(android.R.drawable.ic_menu_gallery)
1640 .setAlphabeticShortcut('W');
1641 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1642 .setIcon(android.R.drawable.ic_search_category_default)
1643 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1644 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1645 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1646 .setAlphabeticShortcut('N');
1647
1648 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001649 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1650 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651
1652 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1653 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1654 .setIntent(settings);
1655
1656 return true;
1657 }
1658
1659 @Override
1660 public boolean onPrepareOptionsMenu(Menu menu) {
1661 super.onPrepareOptionsMenu(menu);
1662
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001663 // If all apps is animating, don't show the menu, because we don't know
1664 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001665 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001666 return false;
1667 }
1668
1669 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001670 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001671 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1672 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1673
1674 // Disable add if the workspace is full.
1675 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001676 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1677 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679
1680 return true;
1681 }
1682
1683 @Override
1684 public boolean onOptionsItemSelected(MenuItem item) {
1685 switch (item.getItemId()) {
1686 case MENU_ADD:
1687 addItems();
1688 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001689 case MENU_MANAGE_APPS:
1690 manageApps();
1691 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 case MENU_WALLPAPER_SETTINGS:
1693 startWallpaper();
1694 return true;
1695 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001696 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 return true;
1698 case MENU_NOTIFICATIONS:
1699 showNotifications();
1700 return true;
1701 }
1702
1703 return super.onOptionsItemSelected(item);
1704 }
Romain Guycbb89e42009-06-08 15:52:54 -07001705
Karl Rosaen138a0412009-04-23 19:00:21 -07001706 /**
1707 * Indicates that we want global search for this activity by setting the globalSearch
1708 * argument for {@link #startSearch} to true.
1709 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001711 @Override
1712 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001713 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001714 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001715 }
1716
Joe Onorato9c1289c2009-08-17 11:03:03 -04001717 public boolean isWorkspaceLocked() {
1718 return mWorkspaceLoading || mWaitingForResult;
1719 }
1720
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001721 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1722 private boolean isPreviewVisible() {
1723 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1724 (mNextView != null && mNextView.getTag() != null);
1725 }
1726
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001728 if (LauncherApplication.isScreenXLarge()) {
1729 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001730 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001731 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001732 }
1733 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001734 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001735 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001736 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 }
1738
Michael Jurka0280c3b2010-09-17 15:00:07 -07001739 private void resetAddInfo() {
1740 mAddScreen = -1;
1741 mAddIntersectCellX = -1;
1742 mAddIntersectCellY = -1;
1743 mAddDropPosition = null;
1744 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001745
Winson Chung55cef262010-10-28 14:14:18 -07001746 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001747 resetAddInfo();
1748 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001749 mAddDropPosition = position;
1750
Michael Jurkaaf442092010-06-10 17:01:57 -07001751 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001752 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1753 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001754 }
1755
Winson Chung7ad01412010-09-27 11:33:03 -07001756 private void manageApps() {
1757 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1758 }
1759
Michael Jurkaaf442092010-06-10 17:01:57 -07001760 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001761 // TODO: catch bad widget exception when sent
1762 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001763 // TODO: Is this log message meaningful?
1764 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001765 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001766 }
1767
Winson Chung55cef262010-10-28 14:14:18 -07001768 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001769 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770
Bjorn Bringert7984c942009-12-09 15:38:25 +00001771 if (appWidget.configure != null) {
1772 // Launch over to configure widget, if needed
1773 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1774 intent.setComponent(appWidget.configure);
1775 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001776 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001777 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1778 intent.putExtra(
1779 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1780 info.mimeType);
1781
1782 final String mimeType = info.mimeType;
1783 final ClipData clipData = (ClipData) info.configurationData;
1784 final ClipDescription clipDesc = clipData.getDescription();
1785 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1786 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001787 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001788 final CharSequence stringData = item.getText();
1789 final Uri uriData = item.getUri();
1790 final Intent intentData = item.getIntent();
1791 final String key =
1792 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1793 if (uriData != null) {
1794 intent.putExtra(key, uriData);
1795 } else if (intentData != null) {
1796 intent.putExtra(key, intentData);
1797 } else if (stringData != null) {
1798 intent.putExtra(key, stringData);
1799 }
1800 break;
1801 }
1802 }
1803 }
Winson Chung55cef262010-10-28 14:14:18 -07001804 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001805
Romain Guy8e633c52010-03-04 12:51:36 -08001806 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001808 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001809 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 }
1811 }
Romain Guycbb89e42009-06-08 15:52:54 -07001812
Michael Jurka0280c3b2010-09-17 15:00:07 -07001813 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1814 resetAddInfo();
1815 mAddScreen = screen;
1816 mAddDropPosition = position;
1817
1818 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1819 createShortcutIntent.setComponent(componentName);
1820 processShortcut(createShortcutIntent);
1821 }
1822
Joe Onoratodeb98af2010-02-19 14:59:39 -08001823 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001824 // Handle case where user selected "Applications"
1825 String applicationName = getResources().getString(R.string.group_applications);
1826 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001827
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001828 if (applicationName != null && applicationName.equals(shortcutName)) {
1829 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1830 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001831
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001832 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1833 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001834 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001835 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001836 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001837 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 }
1840
Winson Chung24ab2f12010-09-16 14:10:47 -07001841 void processWallpaper(Intent intent) {
1842 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1843 }
1844
Michael Jurka0280c3b2010-09-17 15:00:07 -07001845 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1846 resetAddInfo();
1847 mAddScreen = screen;
1848 mAddDropPosition = position;
1849
1850 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1851 createFolderIntent.setComponent(componentName);
1852
1853 addLiveFolder(createFolderIntent);
1854 }
1855
1856 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001857 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001858 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001859 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001860
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001861 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001862 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001863 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001864 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001865 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 }
1867
Michael Jurka0280c3b2010-09-17 15:00:07 -07001868 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 UserFolderInfo folderInfo = new UserFolderInfo();
1870 folderInfo.title = getText(R.string.folder_name);
1871
Michael Jurka0280c3b2010-09-17 15:00:07 -07001872 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1873 final int[] cellXY = mTmpAddItemCellCoordinates;
1874 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1875 showOutOfSpaceMessage();
1876 return;
1877 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878
1879 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001880 LauncherModel.addItemToDatabase(this, folderInfo,
1881 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001882 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001883 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884
1885 // Create the view
1886 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001887 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1888 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001889 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 }
Romain Guycbb89e42009-06-08 15:52:54 -07001891
Joe Onorato9c1289c2009-08-17 11:03:03 -04001892 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001893 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001894 }
1895
Michael Jurka28750fb2010-09-24 17:43:49 -07001896 private void completeAddLiveFolder(
1897 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001898 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1899 final int[] cellXY = mTmpAddItemCellCoordinates;
1900 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1901 showOutOfSpaceMessage();
1902 return;
1903 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904
Michael Jurka0280c3b2010-09-17 15:00:07 -07001905 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906
1907 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001909 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001910 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 }
1912 }
1913
1914 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001915 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916
1917 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1918 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1919
1920 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 Intent.ShortcutIconResource iconResource = null;
1922
1923 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1924 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1925 try {
1926 iconResource = (Intent.ShortcutIconResource) extra;
1927 final PackageManager packageManager = context.getPackageManager();
1928 Resources resources = packageManager.getResourcesForApplication(
1929 iconResource.packageName);
1930 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1931 icon = resources.getDrawable(id);
1932 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001933 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 }
1935 }
1936
1937 if (icon == null) {
1938 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1939 }
1940
1941 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001942 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 info.title = name;
1944 info.iconResource = iconResource;
1945 info.uri = data.getData();
1946 info.baseIntent = baseIntent;
1947 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1948 LiveFolders.DISPLAY_MODE_GRID);
1949
1950 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001951 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001952 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953
1954 return info;
1955 }
1956
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 private void showNotifications() {
1958 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1959 if (statusBar != null) {
1960 statusBar.expand();
1961 }
1962 }
1963
1964 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001965 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001967 Intent chooser = Intent.createChooser(pickWallpaper,
1968 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001969 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1970 // Removed in Eclair MR1
1971// WallpaperManager wm = (WallpaperManager)
1972// getSystemService(Context.WALLPAPER_SERVICE);
1973// WallpaperInfo wi = wm.getWallpaperInfo();
1974// if (wi != null && wi.getSettingsActivity() != null) {
1975// LabeledIntent li = new LabeledIntent(getPackageName(),
1976// R.string.configure_wallpaper, 0);
1977// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1978// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1979// }
Mike Clerona0618e42009-10-22 13:55:21 -07001980 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 }
1982
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001983 /**
1984 * Registers various content observers. The current implementation registers
1985 * only a favorites observer to keep track of the favorites applications.
1986 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001987 private void registerContentObservers() {
1988 ContentResolver resolver = getContentResolver();
1989 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1990 true, mWidgetObserver);
1991 }
1992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 @Override
1994 public boolean dispatchKeyEvent(KeyEvent event) {
1995 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1996 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001998 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001999 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08002000 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08002001 dumpState();
2002 return true;
2003 }
2004 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002005 }
2006 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2007 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002008 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 return true;
2010 }
2011 }
2012
2013 return super.dispatchKeyEvent(event);
2014 }
2015
Joe Onorato88ec0992009-11-19 13:16:06 -08002016 @Override
2017 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002018 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
2019 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002020 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08002021 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002022 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07002023 dismissPreview(mPreviousView);
2024 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002025 } else {
2026 // Back button is a no-op here, but give at least some feedback for the button press
2027 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002028 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002029 }
2030
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 private void closeFolder() {
2032 Folder folder = mWorkspace.getOpenFolder();
2033 if (folder != null) {
2034 closeFolder(folder);
2035 }
2036 }
2037
2038 void closeFolder(Folder folder) {
2039 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08002040 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07002042 CellLayout cl = (CellLayout) parent;
2043 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05002044 if (folder instanceof DropTarget) {
2045 // Live folders aren't DropTargets.
2046 mDragController.removeDropTarget((DropTarget)folder);
2047 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 }
2049 folder.onClose();
2050 }
2051
2052 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002053 * Re-listen when widgets are reset.
2054 */
2055 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002056 if (mAppWidgetHost != null) {
2057 mAppWidgetHost.startListening();
2058 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002059 }
2060
2061 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002062 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2063 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002065 private void unbindDesktopItems() {
2066 for (ItemInfo item: mDesktopItems) {
2067 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002069 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 /**
2073 * Launches the intent referred by the clicked shortcut.
2074 *
2075 * @param v The view representing the clicked shortcut.
2076 */
2077 public void onClick(View v) {
2078 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002079 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002081 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002082 int[] pos = new int[2];
2083 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002084 intent.setSourceBounds(new Rect(pos[0], pos[1],
2085 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08002086 boolean success = startActivitySafely(intent, tag);
2087
2088 if (success && v instanceof BubbleTextView) {
2089 mWaitingForResume = (BubbleTextView) v;
2090 mWaitingForResume.setStayPressed(true);
2091 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 } else if (tag instanceof FolderInfo) {
2093 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002094 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002095 if (mState == State.ALL_APPS) {
2096 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002097 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002098 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002099 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 }
2101 }
2102
Michael Jurka0e260592010-06-30 17:07:39 -07002103 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002104 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002105 // clicking anywhere on the workspace causes the customization drawer to slide down
2106 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002107 return false;
2108 }
2109
Michael Jurkaaf442092010-06-10 17:01:57 -07002110 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002111 * Event handler for the search button
2112 *
2113 * @param v The view that was clicked.
2114 */
2115 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002116 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002117 // Use a custom animation for launching search
2118 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002119 }
2120
2121 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002122 * Event handler for the voice button
2123 *
2124 * @param v The view that was clicked.
2125 */
2126 public void onClickVoiceButton(View v) {
2127 startVoiceSearch();
2128 }
2129
2130 private void startVoiceSearch() {
2131 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2132 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2133 startActivity(intent);
2134 }
2135
2136 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002137 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002138 * enters home screen customization mode.
2139 *
2140 * @param v The view that was clicked.
2141 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002142 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002143 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002144 }
2145
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002146 /**
2147 * Event handler for the "grid" button that appears on the home screen, which
2148 * enters all apps mode.
2149 *
2150 * @param v The view that was clicked.
2151 */
2152 public void onClickAllAppsButton(View v) {
2153 showAllApps(true);
2154 }
2155
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002156 public void onClickAppMarketButton(View v) {
2157 if (mAppMarketIntent != null) {
2158 startActivitySafely(mAppMarketIntent, "app market");
2159 }
2160 }
2161
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002162 void startApplicationDetailsActivity(ComponentName componentName) {
2163 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002164 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2165 Uri.fromParts("package", packageName, null));
2166 startActivity(intent);
2167 }
2168
Patrick Dubroy5539af72010-09-07 15:22:01 -07002169 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2170 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2171 // System applications cannot be installed. For now, show a toast explaining that.
2172 // We may give them the option of disabling apps this way.
2173 int messageId = R.string.uninstall_system_app_text;
2174 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2175 } else {
2176 String packageName = appInfo.componentName.getPackageName();
2177 String className = appInfo.componentName.getClassName();
2178 Intent intent = new Intent(
2179 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2180 startActivity(intent);
2181 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002182 }
2183
Michael Jurkaddd62e92011-02-16 17:49:14 -08002184 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2186 try {
2187 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002188 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 } catch (ActivityNotFoundException e) {
2190 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002191 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 } catch (SecurityException e) {
2193 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002194 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002196 "or use the exported attribute for this activity. "
2197 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002199 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002201
Romain Guy8e633c52010-03-04 12:51:36 -08002202 void startActivityForResultSafely(Intent intent, int requestCode) {
2203 try {
2204 startActivityForResult(intent, requestCode);
2205 } catch (ActivityNotFoundException e) {
2206 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2207 } catch (SecurityException e) {
2208 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2209 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2210 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2211 "or use the exported attribute for this activity.", e);
2212 }
2213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214
2215 private void handleFolderClick(FolderInfo folderInfo) {
2216 if (!folderInfo.opened) {
2217 // Close any open folder
2218 closeFolder();
2219 // Open the requested folder
2220 openFolder(folderInfo);
2221 } else {
2222 // Find the open folder...
2223 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2224 int folderScreen;
2225 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002226 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 // .. and close it
2228 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002229 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 // Close any folder open on the current screen
2231 closeFolder();
2232 // Pull the folder onto this screen
2233 openFolder(folderInfo);
2234 }
2235 }
2236 }
2237 }
2238
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002240 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 * is animated relative to the specified View. If the View is null, no animation
2242 * is played.
2243 *
2244 * @param folderInfo The FolderInfo describing the folder to open.
2245 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002246 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002247 Folder openFolder;
2248
2249 if (folderInfo instanceof UserFolderInfo) {
2250 openFolder = UserFolder.fromXml(this);
2251 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002252 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 } else {
2254 return;
2255 }
2256
Joe Onorato00acb122009-08-04 16:04:30 -04002257 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258 openFolder.setLauncher(this);
2259
2260 openFolder.bind(folderInfo);
2261 folderInfo.opened = true;
2262
Winson Chungaafa03c2010-06-11 17:34:16 -07002263 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002265 openFolder.onOpen();
2266 }
2267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002268 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002269 if (mState != State.WORKSPACE) {
2270 return false;
2271 }
2272
Romain Guy1fbc1c82009-11-09 20:43:08 -08002273 switch (v.getId()) {
2274 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002275 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002276 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2277 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002278 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002279 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002280 return true;
2281 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002282 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002283 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2284 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002285 showPreviews(v);
2286 }
2287 return true;
2288 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002289 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002290 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2291 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2292 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002293 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002294 return true;
2295 }
2296
Joe Onorato9c1289c2009-08-17 11:03:03 -04002297 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002298 return false;
2299 }
2300
2301 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002302 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303 }
2304
Michael Jurka0280c3b2010-09-17 15:00:07 -07002305 resetAddInfo();
2306 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002307 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002308 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 return true;
2310 }
2311
Michael Jurka0280c3b2010-09-17 15:00:07 -07002312 final View itemUnderLongClick = longClickCellInfo.cell;
2313
Winson Chung304dcde2011-01-07 11:17:23 -08002314 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002315 if (itemUnderLongClick == null) {
2316 // User long pressed on empty space
2317 mWorkspace.setAllowLongPress(false);
2318 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2319 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002320 if (LauncherApplication.isScreenXLarge()) {
2321 addItems();
2322 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002323 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002324 }
2325 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002326 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002328 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2329 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002330 mAddIntersectCellX = longClickCellInfo.cellX;
2331 mAddIntersectCellY = longClickCellInfo.cellY;
2332 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 }
2334 }
2335 }
2336 return true;
2337 }
2338
Romain Guye6b8e2f2009-11-10 11:56:55 -08002339 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002340 private void dismissPreview(final View v) {
2341 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002342 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002343 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2344 public void onDismiss() {
2345 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2346 int count = group.getChildCount();
2347 for (int i = 0; i < count; i++) {
2348 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2349 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002350 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2351 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2352
2353 v.setTag(R.id.workspace, null);
2354 v.setTag(R.id.icon, null);
2355 window.setOnDismissListener(null);
2356 }
2357 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002358 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002359 }
2360 v.setTag(null);
2361 }
2362
Romain Guyf8e6a802009-12-07 17:48:02 -08002363 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002364 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002365 }
2366
Romain Guya6abce82009-11-10 02:54:41 -08002367 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002368 final Resources resources = getResources();
2369 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002370
Romain Guya6abce82009-11-10 02:54:41 -08002371 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002372
Romain Guy9d31e9f2009-11-11 18:54:28 -08002373 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002374
Romain Guyf8e6a802009-12-07 17:48:02 -08002375 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002376 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002377 int extraW = (int) ((r.left + r.right) * max);
2378 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002379
2380 int aW = cell.getWidth() - extraW;
2381 float w = aW / max;
2382
2383 int width = cell.getWidth();
2384 int height = cell.getHeight();
2385 int x = cell.getLeftPadding();
2386 int y = cell.getTopPadding();
2387 width -= (x + cell.getRightPadding());
2388 height -= (y + cell.getBottomPadding());
2389
2390 float scale = w / width;
2391
2392 int count = end - start;
2393
2394 final float sWidth = width * scale;
2395 float sHeight = height * scale;
2396
Romain Guye6b8e2f2009-11-10 11:56:55 -08002397 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002398
Romain Guye6b8e2f2009-11-10 11:56:55 -08002399 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2400 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002401
2402 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002403 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002404 cell = (CellLayout) workspace.getChildAt(i);
2405
Romain Guyf8e6a802009-12-07 17:48:02 -08002406 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002407 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002408
2409 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002410 c.scale(scale, scale);
2411 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002412 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002413
Romain Guy9d31e9f2009-11-11 18:54:28 -08002414 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002415 image.setImageBitmap(bitmap);
2416 image.setTag(i);
2417 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002418 image.setOnFocusChangeListener(handler);
2419 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002420 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002421
2422 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002423 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2424
Winson Chungaafa03c2010-06-11 17:34:16 -07002425 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002426 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002427
2428 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002429 p.setContentView(preview);
2430 p.setWidth((int) (sWidth * count + extraW));
2431 p.setHeight((int) (sHeight + extraH));
2432 p.setAnimationStyle(R.style.AnimationPreview);
2433 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002434 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002435 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002436 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002437
Romain Guye6b8e2f2009-11-10 11:56:55 -08002438 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2439 public void onDismiss() {
2440 dismissPreview(anchor);
2441 }
2442 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002443
2444 anchor.setTag(p);
2445 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002446 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002447 }
2448
Romain Guy9d31e9f2009-11-11 18:54:28 -08002449 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002450 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002451
Romain Guye6b8e2f2009-11-10 11:56:55 -08002452 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002453 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002454 }
2455
2456 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002457 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002458 v.post(this);
2459 }
2460
2461 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002462 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002463 }
2464
2465 public void onFocusChange(View v, boolean hasFocus) {
2466 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002467 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002468 }
Romain Guya6abce82009-11-10 02:54:41 -08002469 }
2470 }
2471
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002472 Workspace getWorkspace() {
2473 return mWorkspace;
2474 }
2475
Michael Jurka3c38d562011-02-18 19:22:12 -08002476 TabHost getCustomizationDrawer() {
2477 return mHomeCustomizationDrawer;
2478 }
2479
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 @Override
2481 protected Dialog onCreateDialog(int id) {
2482 switch (id) {
2483 case DIALOG_CREATE_SHORTCUT:
2484 return new CreateShortcut().createDialog();
2485 case DIALOG_RENAME_FOLDER:
2486 return new RenameFolder().createDialog();
2487 }
2488
2489 return super.onCreateDialog(id);
2490 }
2491
2492 @Override
2493 protected void onPrepareDialog(int id, Dialog dialog) {
2494 switch (id) {
2495 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002496 break;
2497 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002498 if (mFolderInfo != null) {
2499 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2500 final CharSequence text = mFolderInfo.title;
2501 input.setText(text);
2502 input.setSelection(0, text.length());
2503 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002504 break;
2505 }
2506 }
2507
2508 void showRenameDialog(FolderInfo info) {
2509 mFolderInfo = info;
2510 mWaitingForResult = true;
2511 showDialog(DIALOG_RENAME_FOLDER);
2512 }
2513
Michael Jurka0280c3b2010-09-17 15:00:07 -07002514 private void showAddDialog(int intersectX, int intersectY) {
2515 resetAddInfo();
2516 mAddIntersectCellX = intersectX;
2517 mAddIntersectCellY = intersectY;
2518 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002519 mWaitingForResult = true;
2520 showDialog(DIALOG_CREATE_SHORTCUT);
2521 }
2522
Joe Onoratodeb98af2010-02-19 14:59:39 -08002523 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002524 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002525 Bundle bundle = new Bundle();
2526
2527 ArrayList<String> shortcutNames = new ArrayList<String>();
2528 shortcutNames.add(getString(R.string.group_applications));
2529 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2530
2531 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2532 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2533 R.drawable.ic_launcher_application));
2534 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2535
2536 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2537 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002538 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002539 pickIntent.putExtras(bundle);
2540
Joe Onoratodeb98af2010-02-19 14:59:39 -08002541 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002542 }
2543
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002544 private class RenameFolder {
2545 private EditText mInput;
2546
2547 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2549 mInput = (EditText) layout.findViewById(R.id.folder_name);
2550
2551 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2552 builder.setIcon(0);
2553 builder.setTitle(getString(R.string.rename_folder_title));
2554 builder.setCancelable(true);
2555 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2556 public void onCancel(DialogInterface dialog) {
2557 cleanup();
2558 }
2559 });
2560 builder.setNegativeButton(getString(R.string.cancel_action),
2561 new Dialog.OnClickListener() {
2562 public void onClick(DialogInterface dialog, int which) {
2563 cleanup();
2564 }
2565 }
2566 );
2567 builder.setPositiveButton(getString(R.string.rename_action),
2568 new Dialog.OnClickListener() {
2569 public void onClick(DialogInterface dialog, int which) {
2570 changeFolderName();
2571 }
2572 }
2573 );
2574 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002575
2576 final AlertDialog dialog = builder.create();
2577 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2578 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002579 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002580 mInput.requestFocus();
2581 InputMethodManager inputManager = (InputMethodManager)
2582 getSystemService(Context.INPUT_METHOD_SERVICE);
2583 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002584 }
2585 });
2586
2587 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002588 }
2589
2590 private void changeFolderName() {
2591 final String name = mInput.getText().toString();
2592 if (!TextUtils.isEmpty(name)) {
2593 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002594 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002595 mFolderInfo.title = name;
2596 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2597
Joe Onorato9c1289c2009-08-17 11:03:03 -04002598 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002599 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002600 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002601 } else {
2602 final FolderIcon folderIcon = (FolderIcon)
2603 mWorkspace.getViewForTag(mFolderInfo);
2604 if (folderIcon != null) {
2605 folderIcon.setText(name);
2606 getWorkspace().requestLayout();
2607 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002608 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002609 mWorkspaceLoading = true;
2610 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002611 }
2612 }
2613 }
2614 cleanup();
2615 }
2616
2617 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002618 dismissDialog(DIALOG_RENAME_FOLDER);
2619 mWaitingForResult = false;
2620 mFolderInfo = null;
2621 }
2622 }
2623
Daniel Sandler843e8602010-06-07 14:59:01 -04002624 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2625 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002626 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002627 }
2628
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002629 // AllAppsView.Watcher
2630 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002631 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2632 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002633 mWorkspace.setVisibility(View.GONE);
2634 }
2635 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002636
Michael Jurkaafca9532011-02-16 14:50:35 -08002637 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002638 button.setVisibility(View.VISIBLE);
2639 button.setFocusable(true);
2640 button.setClickable(true);
2641 }
2642
2643 private void hideToolbarButton(View button) {
2644 button.setAlpha(0.0f);
2645 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2646 button.setVisibility(View.INVISIBLE);
Michael Jurkaafca9532011-02-16 14:50:35 -08002647 }
2648
2649 private void disableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002650 button.setFocusable(false);
2651 button.setClickable(false);
2652 }
2653
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002654 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002655 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002656 *
2657 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2658 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002659 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002660 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002661 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002662 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002663 final boolean showing = show;
2664 final boolean hiding = !show;
2665
2666 final int duration = show ?
2667 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2668 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2669
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002670 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002671 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002672 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002673 anim.addUpdateListener(new AnimatorUpdateListener() {
2674 public void onAnimationUpdate(ValueAnimator animation) {
2675 view.setAlpha((Float) animation.getAnimatedValue());
2676 }
2677 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002678 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002679 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002680 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002681 if (showing) showAndEnableToolbarButton(view);
2682 if (hiding) disableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002683 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002684 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002685 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002686 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002687 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002688 });
2689 seq.play(anim);
2690 } else {
2691 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002692 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002693 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002694 } else {
Michael Jurkaafca9532011-02-16 14:50:35 -08002695 disableToolbarButton(view);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002696 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002697 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002698 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002699 }
2700
2701 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002702 * Show/hide the appropriate toolbar buttons for newState.
2703 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002704 *
2705 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002706 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2707 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002708 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002709 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002710 switch (newState) {
2711 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002712 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2713 mDeleteZone.setOverlappingViews(
2714 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002715 mDeleteZone.setDragAndDropEnabled(true);
2716 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002717 break;
2718 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002719 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002720 mDeleteZone.setDragAndDropEnabled(false);
2721 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002722 break;
2723 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002724 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002725 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002726 break;
2727 }
2728 }
2729
2730 /**
2731 * Helper method for the cameraZoomIn/cameraZoomOut animations
2732 * @param view The view being animated
2733 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2734 * @param scaleFactor The scale factor used for the zoom
2735 */
2736 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2737 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002738
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002739 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002740 // Set pivotY so that at the starting zoom factor, the view is partially
2741 // visible. Modifying initialHeightFactor changes how much of the view is
2742 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002743 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002744 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002745 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002746 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002747 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002748 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002749 }
2750 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002751
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002752 /**
2753 * Zoom the camera out from the workspace to reveal 'toView'.
2754 * Assumes that the view to show is anchored at either the very top or very bottom
2755 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002756 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002757 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002758 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002759 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002760 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002761
2762 final int duration = toAllApps ?
2763 res.getInteger(R.integer.config_allAppsZoomInTime) :
2764 res.getInteger(R.integer.config_customizeZoomInTime);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002765 final int fadeDuration = toAllApps ?
2766 res.getInteger(R.integer.config_allAppsFadeInTime) :
2767 res.getInteger(R.integer.config_customizeFadeInTime);
Adam Cohenf16e5712011-01-13 13:31:45 -08002768
2769 final float scale = toAllApps ?
2770 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2771 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2772
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002773 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002774
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002775 setPivotsForZoom(toView, toState, scale);
2776
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002777 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002778 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002779 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002780 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002781 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002782
2783 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002784 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2785 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
2786 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2787 public void onAnimationUpdate(ValueAnimator animation) {
2788 final float b = (Float) animation.getAnimatedValue();
2789 final float a = 1f - b;
2790 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002791 toView.setFastScaleX(a * scale + b * 1f);
2792 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002793 }
2794 });
Adam Cohen61033d32010-11-15 18:29:44 -08002795
Adam Cohenf16e5712011-01-13 13:31:45 -08002796 if (toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002797 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002798 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002799 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002800 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2801 public void onAnimationUpdate(ValueAnimator animation) {
2802 final float b = (Float) animation.getAnimatedValue();
2803 final float a = 1f - b;
2804 // don't need to invalidate because we do so above
2805 toView.setFastAlpha(a * 0f + b * 1f);
2806 }
2807 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002808 alphaAnim.start();
2809 }
2810
Michael Jurka2763be32011-02-24 11:19:57 -08002811 if (toView instanceof LauncherAnimatable) {
2812 ((LauncherAnimatable) toView).onLauncherAnimationStart();
Michael Jurkabfadaad2011-01-31 21:35:39 -08002813 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002814 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002815 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002816 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002817 // Prepare the position
2818 toView.setTranslationX(0.0f);
2819 toView.setTranslationY(0.0f);
2820 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002821 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002822 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002823 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002824 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002825 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002826 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002827 // If we don't set the final scale values here, if this animation is cancelled
2828 // it will have the wrong scale value and subsequent cameraPan animations will
2829 // not fix that
2830 toView.setScaleX(1.0f);
2831 toView.setScaleY(1.0f);
Michael Jurka2763be32011-02-24 11:19:57 -08002832 if (toView instanceof LauncherAnimatable) {
2833 ((LauncherAnimatable) toView).onLauncherAnimationEnd();
2834 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002835 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002836 });
2837
Chet Haaseb1254a62010-09-07 13:35:00 -07002838 AnimatorSet toolbarHideAnim = new AnimatorSet();
2839 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002840 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2841
2842 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002843 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002844
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002845 if (mStateAnimation != null) mStateAnimation.cancel();
2846 mStateAnimation = new AnimatorSet();
2847 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2848 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002849
2850 // Show the new toolbar buttons just as the main animation is ending
2851 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002852 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2853 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002854 } else {
2855 toView.setTranslationX(0.0f);
2856 toView.setTranslationY(0.0f);
2857 toView.setScaleX(1.0f);
2858 toView.setScaleY(1.0f);
2859 toView.setVisibility(View.VISIBLE);
2860 hideAndShowToolbarButtons(toState, null, null);
2861 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002862 }
2863
2864 /**
2865 * Zoom the camera back into the workspace, hiding 'fromView'.
2866 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002867 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2868 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002869 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002870 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002871 cameraZoomIn(fromState, animated, false);
2872 }
2873
2874 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002875 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002876 final boolean fromAllApps = (fromState == State.ALL_APPS);
2877
2878 int duration = fromAllApps ?
2879 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2880 res.getInteger(R.integer.config_customizeZoomOutTime);
2881
Michael Jurka742574b2011-02-02 23:51:01 -08002882 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002883 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2884 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2885
2886 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002887
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002888 mCustomizePagedView.endChoiceMode();
2889 mAllAppsPagedView.endChoiceMode();
2890
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002891 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002892
Michael Jurkad3ef3062010-11-23 16:23:58 -08002893 if (!springLoaded) {
2894 mWorkspace.unshrink(animated);
2895 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002896
2897 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002898 if (mStateAnimation != null) mStateAnimation.cancel();
2899 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002900
Michael Jurka742574b2011-02-02 23:51:01 -08002901 final float oldScaleX = fromView.getScaleX();
2902 final float oldScaleY = fromView.getScaleY();
2903
2904 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2905 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2906 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2907 public void onAnimationUpdate(ValueAnimator animation) {
2908 final float b = (Float) animation.getAnimatedValue();
2909 final float a = 1f - b;
2910 ((View)fromView.getParent()).fastInvalidate();
2911 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2912 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2913 }
2914 });
2915 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002916 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002917 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002918 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2919 public void onAnimationUpdate(ValueAnimator animation) {
2920 final float b = (Float) animation.getAnimatedValue();
2921 final float a = 1f - b;
2922 // don't need to invalidate because we do so above
2923 fromView.setFastAlpha(a * 1f + b * 0f);
2924 }
2925 });
Michael Jurka2763be32011-02-24 11:19:57 -08002926 if (fromView instanceof LauncherAnimatable) {
2927 ((LauncherAnimatable) fromView).onLauncherAnimationStart();
2928 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002929 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002930 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002931 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002932 fromView.setVisibility(View.GONE);
Michael Jurka2763be32011-02-24 11:19:57 -08002933 if (fromView instanceof LauncherAnimatable) {
2934 ((LauncherAnimatable) fromView).onLauncherAnimationEnd();
2935 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002936 }
2937 });
2938
Chet Haaseb1254a62010-09-07 13:35:00 -07002939 AnimatorSet toolbarHideAnim = new AnimatorSet();
2940 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002941 if (!springLoaded) {
2942 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2943 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002944
Adam Cohen61033d32010-11-15 18:29:44 -08002945 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002946
2947 // Show the new toolbar buttons at the very end of the whole animation
2948 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2949 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002950 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2951 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002952 } else {
2953 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002954 if (!springLoaded) {
2955 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2956 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002957 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002958 }
2959
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002960 void showAllApps(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002961 if (mState != State.WORKSPACE) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002962 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002963 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002964
Michael Jurka79212d82010-07-30 16:36:20 -07002965 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002966 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002967 } else {
2968 mAllAppsGrid.zoom(1.0f, animated);
2969 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002970
Romain Guyc16fea72010-03-12 17:17:56 -08002971 ((View) mAllAppsGrid).setFocusable(true);
2972 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002973
Joe Onorato7c312c12009-08-13 21:36:53 -07002974 // TODO: fade these two too
2975 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002976
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002977 // Change the state *after* we've called all the transition code
2978 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002979
Winson Chung5fb63472011-02-02 17:03:37 -08002980 // Pause the auto-advance of widgets until we are out of AllApps
2981 mUserPresent = false;
2982 updateRunning();
2983
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002984 // send an accessibility event to announce the context change
2985 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2986 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002987
2988 void showWorkspace(boolean animated) {
2989 showWorkspace(animated, null);
2990 }
2991
2992 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002993 if (layout != null) {
2994 // always animated, but that's ok since we never specify a layout and
2995 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002996 mWorkspace.unshrink(layout);
2997 } else {
2998 mWorkspace.unshrink(animated);
2999 }
3000 if (mState == State.ALL_APPS) {
3001 closeAllApps(animated);
3002 } else if (mState == State.CUSTOMIZE) {
3003 hideCustomizationDrawer(animated);
3004 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003005
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003006 // Change the state *after* we've called all the transition code
3007 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003008
Winson Chung5fb63472011-02-02 17:03:37 -08003009 // Resume the auto-advance of widgets
3010 mUserPresent = true;
3011 updateRunning();
3012
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003013 // send an accessibility event to announce the context change
3014 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04003015 }
3016
Michael Jurkad3ef3062010-11-23 16:23:58 -08003017 void enterSpringLoadedDragMode(CellLayout layout) {
3018 mWorkspace.enterSpringLoadedDragMode(layout);
3019 if (mState == State.ALL_APPS) {
3020 cameraZoomIn(State.ALL_APPS, true, true);
3021 mState = State.ALL_APPS_SPRING_LOADED;
3022 } else if (mState == State.CUSTOMIZE) {
3023 cameraZoomIn(State.CUSTOMIZE, true, true);
3024 mState = State.CUSTOMIZE_SPRING_LOADED;
3025 }/* else {
3026 // we're already in spring loaded mode; don't do anything
3027 }*/
3028 }
3029
3030 void exitSpringLoadedDragMode() {
3031 if (mState == State.ALL_APPS_SPRING_LOADED) {
3032 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
3033 cameraZoomOut(State.ALL_APPS, true);
3034 mState = State.ALL_APPS;
3035 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
3036 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
3037 cameraZoomOut(State.CUSTOMIZE, true);
3038 mState = State.CUSTOMIZE;
3039 }/* else {
3040 // we're not in spring loaded mode; don't do anything
3041 }*/
3042 }
3043
Joe Onoratob2061212009-11-24 16:13:54 -05003044 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08003045 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05003046 * - Home from workspace
3047 * - from center screen
3048 * - from other screens
3049 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003050 * - from center screen
3051 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003052 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003053 * - from center screen
3054 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003055 * - Launch app from workspace and quit
3056 * - with back
3057 * - with home
3058 * - Launch app from all apps and quit
3059 * - with back
3060 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003061 * - Go to a screen that's not the default, then all
3062 * apps, and launch and app, and go back
3063 * - with back
3064 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003065 * - On workspace, long press power and go back
3066 * - with back
3067 * - with home
3068 * - On all apps, long press power and go back
3069 * - with back
3070 * - with home
3071 * - On workspace, power off
3072 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003073 * - Launch an app and turn off the screen while in that app
3074 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003075 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003076 * - From all apps
3077 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003078 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3079 * - From all apps
3080 * - From the center workspace
3081 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003082 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003083 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003084 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003085 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003086 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003087 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003088 } else {
3089 mAllAppsGrid.zoom(0.0f, animated);
3090 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003091 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003092 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003093 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003094 }
3095
Joe Onorato7c312c12009-08-13 21:36:53 -07003096 void lockAllApps() {
3097 // TODO
3098 }
3099
3100 void unlockAllApps() {
3101 // TODO
3102 }
3103
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003104 // Show the customization drawer (only exists in x-large configuration)
3105 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003106 if (mState != State.WORKSPACE) {
3107 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003108 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003109
3110 cameraZoomOut(State.CUSTOMIZE, animated);
3111
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003112 // Change the state *after* we've called all the transition code
3113 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003114
3115 // Pause the auto-advance of widgets until we are out of Customization drawer
3116 mUserPresent = false;
3117 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003118 }
3119
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003120 // Hide the customization drawer (only exists in x-large configuration)
3121 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003122 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003123 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003124 }
3125 }
3126
Patrick Dubroy5f445422011-02-18 14:35:21 -08003127 /**
3128 * Add an item from all apps or customize onto the given workspace screen.
3129 * If layout is null, add to the current screen.
3130 */
3131 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003132 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3133 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003134 } else {
3135 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003136 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003137 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003138
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003139 void onWorkspaceClick(CellLayout layout) {
3140 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003141 }
3142
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003143 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003144 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003145 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003146 // Look for the toolbar icon specified in the activity meta-data
3147 Bundle metaData = packageManager.getActivityInfo(
3148 activityName, PackageManager.GET_META_DATA).metaData;
3149 if (metaData != null) {
3150 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3151 if (iconResId != 0) {
3152 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003153 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003154 }
3155 }
3156 } catch (NameNotFoundException e) {
3157 // Do nothing
3158 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003159 return null;
3160 }
3161
3162 // if successful in getting icon, return it; otherwise, set button to use default drawable
3163 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3164 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3165 TextView button = (TextView) findViewById(buttonId);
3166 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3167
3168 // If we were unable to find the icon via the meta-data, use a generic one
3169 if (toolbarIcon == null) {
3170 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3171 return null;
3172 } else {
3173 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3174 return toolbarIcon.getConstantState();
3175 }
3176 }
3177
3178 // if successful in getting icon, return it; otherwise, set button to use default drawable
3179 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3180 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3181 ImageView button = (ImageView) findViewById(buttonId);
3182 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3183
Michael Jurka0423dcf2010-10-05 14:56:18 -07003184 // If we were unable to find the icon via the meta-data, use a generic one
3185 if (toolbarIcon == null) {
3186 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003187 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003188 } else {
3189 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003190 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003191 }
3192 }
3193
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003194 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3195 TextView button = (TextView) findViewById(buttonId);
3196 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3197 }
3198
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003199 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003200 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003201 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003202 }
3203
Michael Jurka0423dcf2010-10-05 14:56:18 -07003204 private void updateGlobalSearchIcon() {
3205 if (LauncherApplication.isScreenXLarge()) {
3206 final SearchManager searchManager =
3207 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3208 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003209 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003210 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003211 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003212 } else {
3213 findViewById(R.id.search_button).setVisibility(View.GONE);
3214 }
3215 }
3216 }
3217
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003218 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003219 updateButtonWithDrawable(R.id.search_button, d);
3220 }
3221
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003222 private void updateVoiceSearchIcon() {
3223 if (LauncherApplication.isScreenXLarge()) {
3224 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3225 ComponentName activityName = intent.resolveActivity(getPackageManager());
3226 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003227 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003228 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3229 } else {
3230 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003231 }
3232 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003233 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003234
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003235 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003236 updateButtonWithDrawable(R.id.voice_button, d);
3237 }
3238
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003239 /**
3240 * Sets the app market icon (shown when all apps is visible on x-large screens)
3241 */
3242 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003243 if (LauncherApplication.isScreenXLarge()) {
3244 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3245 // Find the app market activity by resolving an intent.
3246 // (If multiple app markets are installed, it will return the ResolverActivity.)
3247 ComponentName activityName = intent.resolveActivity(getPackageManager());
3248 if (activityName != null) {
3249 mAppMarketIntent = intent;
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003250 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003251 R.id.market_button, activityName, R.drawable.app_market_generic);
3252 }
3253 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003254 }
3255
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003256 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003257 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003258 }
3259
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003260 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003261 * Displays the shortcut creation dialog and launches, if necessary, the
3262 * appropriate activity.
3263 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003264 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003265 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3266 DialogInterface.OnShowListener {
3267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003268 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003269
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003270 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003271 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003272
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003273 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3274 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003275 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003276
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003277 builder.setInverseBackgroundForced(true);
3278
3279 AlertDialog dialog = builder.create();
3280 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003281 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003282 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003283
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003284 return dialog;
3285 }
3286
3287 public void onCancel(DialogInterface dialog) {
3288 mWaitingForResult = false;
3289 cleanup();
3290 }
3291
Romain Guycbb89e42009-06-08 15:52:54 -07003292 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003293 }
3294
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003295 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003296 try {
3297 dismissDialog(DIALOG_CREATE_SHORTCUT);
3298 } catch (Exception e) {
3299 // An exception is thrown if the dialog is not visible, which is fine
3300 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003301 }
3302
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003303 /**
3304 * Handle the action clicked in the "Add to home" dialog.
3305 */
3306 public void onClick(DialogInterface dialog, int which) {
3307 Resources res = getResources();
3308 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003309
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003310 switch (which) {
3311 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003312 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003313 break;
3314 }
Romain Guycbb89e42009-06-08 15:52:54 -07003315
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003316 case AddAdapter.ITEM_APPWIDGET: {
3317 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003318
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003319 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3320 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003321 // start the pick activity
3322 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3323 break;
3324 }
Romain Guycbb89e42009-06-08 15:52:54 -07003325
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003326 case AddAdapter.ITEM_LIVE_FOLDER: {
3327 // Insert extra item to handle inserting folder
3328 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003329
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003330 ArrayList<String> shortcutNames = new ArrayList<String>();
3331 shortcutNames.add(res.getString(R.string.group_folder));
3332 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003333
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003334 ArrayList<ShortcutIconResource> shortcutIcons =
3335 new ArrayList<ShortcutIconResource>();
3336 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3337 R.drawable.ic_launcher_folder));
3338 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3339
3340 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3341 pickIntent.putExtra(Intent.EXTRA_INTENT,
3342 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3343 pickIntent.putExtra(Intent.EXTRA_TITLE,
3344 getText(R.string.title_select_live_folder));
3345 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003346
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003347 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3348 break;
3349 }
3350
3351 case AddAdapter.ITEM_WALLPAPER: {
3352 startWallpaper();
3353 break;
3354 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003355 }
3356 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003357
3358 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003359 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003360 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003361 }
3362
3363 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003364 * Receives notifications when applications are added/removed.
3365 */
3366 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3367 @Override
3368 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003369 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003370 String reason = intent.getStringExtra("reason");
3371 if (!"homekey".equals(reason)) {
3372 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003373 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003374 animate = false;
3375 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003376 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003377 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003378 }
3379 }
3380
3381 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003382 * Receives notifications whenever the appwidgets are reset.
3383 */
3384 private class AppWidgetResetObserver extends ContentObserver {
3385 public AppWidgetResetObserver() {
3386 super(new Handler());
3387 }
3388
3389 @Override
3390 public void onChange(boolean selfChange) {
3391 onAppWidgetReset();
3392 }
3393 }
3394
3395 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003396 * If the activity is currently paused, signal that we need to re-run the loader
3397 * in onResume.
3398 *
3399 * This needs to be called from incoming places where resources might have been loaded
3400 * while we are paused. That is becaues the Configuration might be wrong
3401 * when we're not running, and if it comes back to what it was when we
3402 * were paused, we are not restarted.
3403 *
3404 * Implementation of the method from LauncherModel.Callbacks.
3405 *
3406 * @return true if we are currently paused. The caller might be able to
3407 * skip some work in that case since we will come back again.
3408 */
3409 public boolean setLoadOnResume() {
3410 if (mPaused) {
3411 Log.i(TAG, "setLoadOnResume");
3412 mOnResumeNeedsLoad = true;
3413 return true;
3414 } else {
3415 return false;
3416 }
3417 }
3418
3419 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003420 * 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 int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003423 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003424 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003425 } else {
3426 return SCREEN_COUNT / 2;
3427 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003428 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003429
Winson Chunga12a2502010-12-20 14:41:35 -08003430 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003431 mAllAppsPagedView = view;
3432 }
3433
Joe Onorato9c1289c2009-08-17 11:03:03 -04003434 /**
3435 * Refreshes the shortcuts shown on the workspace.
3436 *
3437 * Implementation of the method from LauncherModel.Callbacks.
3438 */
3439 public void startBinding() {
3440 final Workspace workspace = mWorkspace;
3441 int count = workspace.getChildCount();
3442 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003443 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003444 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3445 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003446 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003447
Joe Onorato9c1289c2009-08-17 11:03:03 -04003448 if (DEBUG_USER_INTERFACE) {
3449 android.widget.Button finishButton = new android.widget.Button(this);
3450 finishButton.setText("Finish");
3451 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3452
3453 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3454 public void onClick(View v) {
3455 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003456 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003457 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003458 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003459
3460 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3461 // mDesktopItems -> AppWidgetInfo -> hostView).
3462 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003463 }
3464
3465 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003466 * Bind the items start-end from the list.
3467 *
3468 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003469 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003470 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3471
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003472 setLoadOnResume();
3473
Joe Onorato9c1289c2009-08-17 11:03:03 -04003474 final Workspace workspace = mWorkspace;
3475
3476 for (int i=start; i<end; i++) {
3477 final ItemInfo item = shortcuts.get(i);
3478 mDesktopItems.add(item);
3479 switch (item.itemType) {
3480 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3481 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003482 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003483 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3484 false);
3485 break;
3486 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3487 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003488 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003489 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003490 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3491 false);
3492 break;
3493 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3494 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3495 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003496 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003497 (LiveFolderInfo) item);
3498 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3499 false);
3500 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003501 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003502 }
3503
Joe Onorato9c1289c2009-08-17 11:03:03 -04003504 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003505 }
3506
Joe Onorato9c1289c2009-08-17 11:03:03 -04003507 /**
3508 * Implementation of the method from LauncherModel.Callbacks.
3509 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003510 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003511 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003512 sFolders.clear();
3513 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003514 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003515
3516 /**
3517 * Add the views for a widget to the workspace.
3518 *
3519 * Implementation of the method from LauncherModel.Callbacks.
3520 */
3521 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003522 setLoadOnResume();
3523
Daniel Sandler843e8602010-06-07 14:59:01 -04003524 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3525 if (DEBUG_WIDGETS) {
3526 Log.d(TAG, "bindAppWidget: " + item);
3527 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003528 final Workspace workspace = mWorkspace;
3529
3530 final int appWidgetId = item.appWidgetId;
3531 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003532 if (DEBUG_WIDGETS) {
3533 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3534 }
3535
Joe Onorato9c1289c2009-08-17 11:03:03 -04003536 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3537
Joe Onorato9c1289c2009-08-17 11:03:03 -04003538 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3539 item.hostView.setTag(item);
3540
3541 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3542 item.cellY, item.spanX, item.spanY, false);
3543
Adam Cohended9f8d2010-11-03 13:25:16 -07003544 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3545
Joe Onorato9c1289c2009-08-17 11:03:03 -04003546 workspace.requestLayout();
3547
3548 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003549
3550 if (DEBUG_WIDGETS) {
3551 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3552 + (SystemClock.uptimeMillis()-start) + "ms");
3553 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003554 }
3555
3556 /**
3557 * Callback saying that there aren't any more items to bind.
3558 *
3559 * Implementation of the method from LauncherModel.Callbacks.
3560 */
3561 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003562 setLoadOnResume();
3563
Joe Onorato9c1289c2009-08-17 11:03:03 -04003564 if (mSavedState != null) {
3565 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003566 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 }
3568
3569 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3570 if (userFolders != null) {
3571 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003572 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003573 if (info != null) {
3574 openFolder(info);
3575 }
3576 }
3577 final Folder openFolder = mWorkspace.getOpenFolder();
3578 if (openFolder != null) {
3579 openFolder.requestFocus();
3580 }
3581 }
3582
Joe Onorato9c1289c2009-08-17 11:03:03 -04003583 mSavedState = null;
3584 }
3585
3586 if (mSavedInstanceState != null) {
3587 super.onRestoreInstanceState(mSavedInstanceState);
3588 mSavedInstanceState = null;
3589 }
3590
Winson Chung32bb5e52011-01-31 14:51:56 -08003591 // Workaround a bug that occurs when rotating the device while the customization mode is
3592 // open, we trigger a new layout on all the CellLayout children.
3593 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3594 final int childCount = mWorkspace.getChildCount();
3595 for (int i = 0; i < childCount; ++i) {
3596 mWorkspace.getChildAt(i).requestLayout();
3597 }
3598 }
3599
Joe Onorato9c1289c2009-08-17 11:03:03 -04003600 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003601
3602 // If we received the result of any pending adds while the loader was running (e.g. the
3603 // widget configuration forced an orientation change), process them now.
3604 for (int i = 0; i < sPendingAddList.size(); i++) {
3605 completeAdd(sPendingAddList.get(i));
3606 }
3607 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003608 }
3609
3610 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003611 * Updates the icons on the launcher that are affected by changes to the package list
3612 * on the device.
3613 */
3614 private void updateIconsAffectedByPackageManagerChanges() {
3615 updateAppMarketIcon();
3616 updateGlobalSearchIcon();
3617 updateVoiceSearchIcon();
3618 }
3619
3620 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003621 * Add the icons for all apps.
3622 *
3623 * Implementation of the method from LauncherModel.Callbacks.
3624 */
3625 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003626 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003627 if (mCustomizePagedView != null) {
3628 mCustomizePagedView.setApps(apps);
3629 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003630 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003631 }
3632
3633 /**
3634 * A package was installed.
3635 *
3636 * Implementation of the method from LauncherModel.Callbacks.
3637 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003638 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003639 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003640 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003641 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003642 if (mCustomizePagedView != null) {
3643 mCustomizePagedView.addApps(apps);
3644 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003645 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003646 }
3647
3648 /**
3649 * A package was updated.
3650 *
3651 * Implementation of the method from LauncherModel.Callbacks.
3652 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003653 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003654 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003655 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003656 if (mWorkspace != null) {
3657 mWorkspace.updateShortcuts(apps);
3658 }
3659 if (mAllAppsGrid != null) {
3660 mAllAppsGrid.updateApps(apps);
3661 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003662 if (mCustomizePagedView != null) {
3663 mCustomizePagedView.updateApps(apps);
3664 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003665 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003666 }
3667
3668 /**
3669 * A package was uninstalled.
3670 *
3671 * Implementation of the method from LauncherModel.Callbacks.
3672 */
Joe Onorato36115782010-06-17 13:28:48 -04003673 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003674 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003675 if (permanent) {
3676 mWorkspace.removeItems(apps);
3677 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003678 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003679 if (mCustomizePagedView != null) {
3680 mCustomizePagedView.removeApps(apps);
3681 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003682 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003683 }
Joe Onoratobe386092009-11-17 17:32:16 -08003684
3685 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003686 * A number of packages were updated.
3687 */
3688 public void bindPackagesUpdated() {
3689 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003690 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003691 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003692 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003693 }
3694
Winson Chung400438b2011-01-16 17:53:48 -08003695 private int mapConfigurationOriActivityInfoOri(int configOri) {
3696 final Display d = getWindowManager().getDefaultDisplay();
3697 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3698 switch (d.getRotation()) {
3699 case Surface.ROTATION_0:
3700 case Surface.ROTATION_180:
3701 // We are currently in the same basic orientation as the natural orientation
3702 naturalOri = configOri;
3703 break;
3704 case Surface.ROTATION_90:
3705 case Surface.ROTATION_270:
3706 // We are currently in the other basic orientation to the natural orientation
3707 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3708 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3709 break;
3710 }
3711
3712 int[] oriMap = {
3713 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3714 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3715 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3716 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3717 };
3718 // Since the map starts at portrait, we need to offset if this device's natural orientation
3719 // is landscape.
3720 int indexOffset = 0;
3721 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3722 indexOffset = 1;
3723 }
3724 return oriMap[(d.getRotation() + indexOffset) % 4];
3725 }
3726 public void lockScreenOrientation() {
3727 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3728 .getConfiguration().orientation));
3729 }
3730 public void unlockScreenOrientation() {
3731 mHandler.postDelayed(new Runnable() {
3732 public void run() {
3733 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3734 }
3735 }, mRestoreScreenOrientationDelay);
3736 }
3737
Winson Chung80baf5a2010-08-09 16:03:15 -07003738 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003739 * Prints out out state for debugging.
3740 */
3741 public void dumpState() {
3742 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003743 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003744 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3745 Log.d(TAG, "mRestoring=" + mRestoring);
3746 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3747 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3748 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003749 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003750 mModel.dumpState();
3751 mAllAppsGrid.dumpState();
3752 Log.d(TAG, "END launcher2 dump state");
3753 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003754}
Michael Jurka2763be32011-02-24 11:19:57 -08003755
3756interface LauncherAnimatable {
3757 void onLauncherAnimationStart();
3758 void onLauncherAnimationEnd();
3759}