blob: 9dfed6945817a554be4b7a383be51d5e2272be07 [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
Adam Cohendf2cc412011-04-27 16:56:57 -070020import java.io.DataInputStream;
21import java.io.DataOutputStream;
22import java.io.FileNotFoundException;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.List;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070027
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080029import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070031import android.animation.ObjectAnimator;
32import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070033import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080034import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070035import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.app.Dialog;
38import android.app.SearchManager;
39import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070040import android.appwidget.AppWidgetManager;
41import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080043import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070044import android.content.ClipData;
45import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040046import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080047import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Context;
49import android.content.DialogInterface;
50import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080051import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080052import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070053import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080055import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080056import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070058import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040059import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080062import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070063import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080064import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070065import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040066import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070067import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020069import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080070import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070071import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040072import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080073import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070074import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080075import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.text.Selection;
77import android.text.SpannableStringBuilder;
78import android.text.TextUtils;
79import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070080import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080081import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080082import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.KeyEvent;
84import android.view.LayoutInflater;
85import android.view.Menu;
86import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070087import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080088import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.View;
90import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070091import android.view.WindowManager;
Michael Jurkae0f5a612011-02-07 16:45:41 -080092import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080093import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080094import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080095import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070097import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070099import android.widget.ImageView;
100import android.widget.LinearLayout;
101import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700102import android.widget.TabHost;
Winson Chung68846fd2010-10-29 11:00:27 -0700103import android.widget.TextView;
104import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700105
Adam Cohendf2cc412011-04-27 16:56:57 -0700106import com.android.common.Search;
107import com.android.launcher.R;
108import com.android.launcher2.Workspace.ShrinkState;
Romain Guyedcce092010-03-04 13:03:17 -0800109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110/**
111 * Default launcher application.
112 */
Michael Jurka946ad472010-07-09 18:05:18 -0700113public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700114 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
115 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800116 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700117 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400120 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400121 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800124 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700127 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
128 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
130 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700131 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_PICK_APPLICATION = 6;
136 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700138 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
140 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
141
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800142 static final int SCREEN_COUNT = 5;
143 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Joe Onorato7c312c12009-08-13 21:36:53 -0700145 static final int DIALOG_CREATE_SHORTCUT = 1;
146 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Romain Guy98d01652009-06-30 16:21:04 -0700148 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
150 // Type: int
151 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700152 // Type: int
153 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 // Type: long
Adam Cohendf2cc412011-04-27 16:56:57 -0700155 private static final String RUNTIME_STATE_FOLDERS = "launcher.folder";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: int
157 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
158 // Type: int
159 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
160 // Type: int
161 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: boolean
163 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
164 // Type: long
165 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
166
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700167 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
168
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700169 /** The different states that Launcher can be in. */
Winson Chung785d2eb2011-04-14 16:08:02 -0700170 private enum State { WORKSPACE, APPS_CUSTOMIZE, ALL_APPS, CUSTOMIZE,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800171 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700172 private State mState = State.WORKSPACE;
173 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700174
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800178 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800180 private final BroadcastReceiver mCloseSystemDialogsReceiver
181 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800182 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private LayoutInflater mInflater;
185
Joe Onorato00acb122009-08-04 16:04:30 -0400186 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700188
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700189 private AppWidgetManager mAppWidgetManager;
190 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700191
Michael Jurka0280c3b2010-09-17 15:00:07 -0700192 private int mAddScreen = -1;
193 private int mAddIntersectCellX = -1;
194 private int mAddIntersectCellY = -1;
195 private int[] mAddDropPosition;
196 private int[] mTmpAddItemCellCoordinates = new int[2];
197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private FolderInfo mFolderInfo;
199
Joe Onorato7c312c12009-08-13 21:36:53 -0700200 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700201 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700202 private AllAppsView mAllAppsGrid;
Winson Chung785d2eb2011-04-14 16:08:02 -0700203 private AppsCustomizeTabHost mAppsCustomizeTabHost;
204 private AppsCustomizePagedView mAppsCustomizeContent;
Michael Jurka7ef959b2011-02-23 11:48:32 -0800205 private CustomizeTrayTabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700206 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700207
Winson Chung97d85d22011-04-13 11:27:36 -0700208 private ViewGroup mButtonCluster;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800209 private View mAllAppsButton;
210 private View mDivider;
211 private View mConfigureButton;
212
Winson Chunga12a2502010-12-20 14:41:35 -0800213 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700214 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private Bundle mSavedState;
217
218 private SpannableStringBuilder mDefaultKeySsb = null;
219
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220 private boolean mWorkspaceLoading = true;
221
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800222 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private boolean mRestoring;
224 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700225 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226
227 private Bundle mSavedInstanceState;
228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800230 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800231 private boolean mUserPresent = true;
232 private boolean mVisible = false;
233 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700235 private static LocaleConfiguration sLocaleConfiguration = null;
236
Romain Guy84f296c2009-11-04 15:00:44 -0800237 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700238
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700239 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700240
Patrick Dubroy94f78a52011-02-28 17:39:16 -0800241 // The "signpost" images along the bottom of the screen (only in some layouts)
Romain Guy1fbc1c82009-11-09 20:43:08 -0800242 private ImageView mPreviousView;
243 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500244
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400245 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400246 private String[] mHotseatConfig = null;
247 private Intent[] mHotseats = null;
248 private Drawable[] mHotseatIcons = null;
249 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400250
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700251 private Intent mAppMarketIntent = null;
252
Adam Cohended9f8d2010-11-03 13:25:16 -0700253 // Related to the auto-advancing of widgets
254 private final int ADVANCE_MSG = 1;
255 private final int mAdvanceInterval = 20000;
256 private final int mAdvanceStagger = 250;
257 private long mAutoAdvanceSentTime;
258 private long mAutoAdvanceTimeLeft = -1;
259 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
260 new HashMap<View, AppWidgetProviderInfo>();
261
Winson Chung400438b2011-01-16 17:53:48 -0800262 // Determines how long to wait after a rotation before restoring the screen orientation to
263 // match the sensor state.
264 private final int mRestoreScreenOrientationDelay = 500;
265
Michael Jurka4ef207b2010-11-29 17:05:45 -0800266 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800267 private static Drawable.ConstantState sGlobalSearchIcon;
268 private static Drawable.ConstantState sVoiceSearchIcon;
269 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800270
Adam Cohen2801caf2011-05-13 20:57:39 -0700271 private DragLayer mDragLayer;
272
Michael Jurkaddd62e92011-02-16 17:49:14 -0800273 private BubbleTextView mWaitingForResume;
274
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800275 private static ArrayList<PendingAddArguments> sPendingAddList
276 = new ArrayList<PendingAddArguments>();
277
278 private static class PendingAddArguments {
279 int requestCode;
280 Intent intent;
281 int screen;
282 int cellX;
283 int cellY;
284 }
285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 @Override
287 protected void onCreate(Bundle savedInstanceState) {
288 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700289
Winson Chungaafa03c2010-06-11 17:34:16 -0700290 if (LauncherApplication.isInPlaceRotationEnabled()) {
291 // hide the status bar (temporary until we get the status bar design figured out)
292 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
293 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
294 }
295
Joe Onorato0589f0f2010-02-08 13:44:00 -0800296 LauncherApplication app = ((LauncherApplication)getApplication());
297 mModel = app.setLauncher(this);
298 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400299 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700301
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700302 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700303 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
304 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700305
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800306 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200307 android.os.Debug.startMethodTracing(
308 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 }
310
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400311 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800313 setContentView(R.layout.launcher);
Michael Jurka7ef959b2011-02-23 11:48:32 -0800314 mHomeCustomizationDrawer = (CustomizeTrayTabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700315 if (mHomeCustomizationDrawer != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700316 // share the same customization workspace across all the tabs
Michael Jurka7ef959b2011-02-23 11:48:32 -0800317 mCustomizePagedView = (CustomizePagedView) findViewById(
318 R.id.customization_drawer_tab_contents);
Michael Jurka946ad472010-07-09 18:05:18 -0700319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 setupViews();
321
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800322 registerContentObservers();
323
Joe Onorato7c312c12009-08-13 21:36:53 -0700324 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700325
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 mSavedState = savedInstanceState;
327 restoreState(mSavedState);
328
Winson Chunga12a2502010-12-20 14:41:35 -0800329 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800330 if (mCustomizePagedView != null) {
331 mCustomizePagedView.update();
332 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700333 if (mAppsCustomizeContent != null) {
334 mAppsCustomizeContent.onPackagesUpdated();
335 }
Winson Chunga12a2502010-12-20 14:41:35 -0800336
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 if (PROFILE_STARTUP) {
338 android.os.Debug.stopMethodTracing();
339 }
340
341 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400342 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 }
344
345 // For handling default keys
346 mDefaultKeySsb = new SpannableStringBuilder();
347 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800348
349 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
350 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800351
352 // If we have a saved version of these external icons, we load them up immediately
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700353 if (LauncherApplication.isScreenLarge()) {
Winson Chungc6d20002011-03-02 11:57:29 -0800354 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
355 updateIconsAffectedByPackageManagerChanges();
Michael Jurka4ef207b2010-11-29 17:05:45 -0800356 }
Michael Jurka1c3f8ec2011-03-07 16:41:21 -0800357 if (sGlobalSearchIcon != null) {
358 updateGlobalSearchIcon(sGlobalSearchIcon);
359 }
360 if (sVoiceSearchIcon != null) {
361 updateVoiceSearchIcon(sVoiceSearchIcon);
362 }
363 if (sAppMarketIcon != null) {
364 updateAppMarketIcon(sAppMarketIcon);
365 }
Michael Jurka4ef207b2010-11-29 17:05:45 -0800366 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 }
Romain Guycbb89e42009-06-08 15:52:54 -0700368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700370 if (sLocaleConfiguration == null) {
371 new AsyncTask<Void, Void, LocaleConfiguration>() {
372 @Override
373 protected LocaleConfiguration doInBackground(Void... unused) {
374 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
375 readConfiguration(Launcher.this, localeConfiguration);
376 return localeConfiguration;
377 }
378
379 @Override
380 protected void onPostExecute(LocaleConfiguration result) {
381 sLocaleConfiguration = result;
382 checkForLocaleChange(); // recursive, but now with a locale configuration
383 }
384 }.execute();
385 return;
386 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700387
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 final Configuration configuration = getResources().getConfiguration();
389
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700390 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 final String locale = configuration.locale.toString();
392
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700393 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 final int mcc = configuration.mcc;
395
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700396 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 final int mnc = configuration.mnc;
398
Romain Guy84f296c2009-11-04 15:00:44 -0800399 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400
Romain Guy84f296c2009-11-04 15:00:44 -0800401 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700402 sLocaleConfiguration.locale = locale;
403 sLocaleConfiguration.mcc = mcc;
404 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700405
Joe Onorato0589f0f2010-02-08 13:44:00 -0800406 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400407 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700408
409 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
410 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700411 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700412 public void run() {
413 writeConfiguration(Launcher.this, localeConfiguration);
414 }
415 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700416 }
417 }
418
419 private static class LocaleConfiguration {
420 public String locale;
421 public int mcc = -1;
422 public int mnc = -1;
423 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700424
Romain Guy98d01652009-06-30 16:21:04 -0700425 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
426 DataInputStream in = null;
427 try {
428 in = new DataInputStream(context.openFileInput(PREFERENCES));
429 configuration.locale = in.readUTF();
430 configuration.mcc = in.readInt();
431 configuration.mnc = in.readInt();
432 } catch (FileNotFoundException e) {
433 // Ignore
434 } catch (IOException e) {
435 // Ignore
436 } finally {
437 if (in != null) {
438 try {
439 in.close();
440 } catch (IOException e) {
441 // Ignore
442 }
443 }
444 }
445 }
446
447 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
448 DataOutputStream out = null;
449 try {
450 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
451 out.writeUTF(configuration.locale);
452 out.writeInt(configuration.mcc);
453 out.writeInt(configuration.mnc);
454 out.flush();
455 } catch (FileNotFoundException e) {
456 // Ignore
457 } catch (IOException e) {
458 //noinspection ResultOfMethodCallIgnored
459 context.getFileStreamPath(PREFERENCES).delete();
460 } finally {
461 if (out != null) {
462 try {
463 out.close();
464 } catch (IOException e) {
465 // Ignore
466 }
467 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468 }
469 }
470
471 static int getScreen() {
472 synchronized (sLock) {
473 return sScreen;
474 }
475 }
476
477 static void setScreen(int screen) {
478 synchronized (sLock) {
479 sScreen = screen;
480 }
481 }
482
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400483 // Note: This doesn't do all the client-id magic that BrowserProvider does
484 // in Browser. (http://b/2425179)
485 private Uri getDefaultBrowserUri() {
486 String url = getString(R.string.default_browser_url);
487 if (url.indexOf("{CID}") != -1) {
488 url = url.replace("{CID}", "android-google");
489 }
490 return Uri.parse(url);
491 }
492
493 // Load the Intent templates from arrays.xml to populate the hotseats. For
494 // each Intent, if it resolves to a single app, use that as the launch
495 // intent & use that app's label as the contentDescription. Otherwise,
496 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400497 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400498 if (mHotseatConfig == null) {
499 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
500 if (mHotseatConfig.length > 0) {
501 mHotseats = new Intent[mHotseatConfig.length];
502 mHotseatLabels = new CharSequence[mHotseatConfig.length];
503 mHotseatIcons = new Drawable[mHotseatConfig.length];
504 } else {
505 mHotseats = null;
506 mHotseatIcons = null;
507 mHotseatLabels = null;
508 }
509
510 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
511 for (int i=0; i<mHotseatConfig.length; i++) {
512 // load icon for this slot; currently unrelated to the actual activity
513 try {
514 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
515 } catch (ArrayIndexOutOfBoundsException ex) {
516 Log.w(TAG, "Missing hotseat_icons array item #" + i);
517 mHotseatIcons[i] = null;
518 }
519 }
520 hotseatIconDrawables.recycle();
521 }
522
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400523 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400524 for (int i=0; i<mHotseatConfig.length; i++) {
525 Intent intent = null;
526 if (mHotseatConfig[i].equals("*BROWSER*")) {
527 // magic value meaning "launch user's default web browser"
528 // replace it with a generic web request so we can see if there is indeed a default
529 String defaultUri = getString(R.string.default_browser_url);
530 intent = new Intent(
531 Intent.ACTION_VIEW,
532 ((defaultUri != null)
533 ? Uri.parse(defaultUri)
534 : getDefaultBrowserUri())
535 ).addCategory(Intent.CATEGORY_BROWSABLE);
536 // note: if the user launches this without a default set, she
537 // will always be taken to the default URL above; this is
538 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700539 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400540 // URL is unavoidably sent to the chosen app.
541 } else {
542 try {
543 intent = Intent.parseUri(mHotseatConfig[i], 0);
544 } catch (java.net.URISyntaxException ex) {
545 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
546 // bogus; leave intent=null
547 }
548 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700549
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400550 if (intent == null) {
551 mHotseats[i] = null;
552 mHotseatLabels[i] = getText(R.string.activity_not_found);
553 continue;
554 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400555
556 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700557 Log.d(TAG, "loadHotseats: hotseat " + i
558 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400559 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400560 + "]");
561 }
562
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400563 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
564 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700565 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400566 Log.d(TAG, "Best match for intent: " + bestMatch);
567 Log.d(TAG, "All matches: ");
568 for (ResolveInfo ri : allMatches) {
569 Log.d(TAG, " --> " + ri);
570 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400571 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400572 // did this resolve to a single app, or the resolver?
573 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700574 // can't find any activity to handle this. let's leave the
575 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400576 // to launch.
577 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400578
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400579 // set accessibility text to "Not installed"
580 mHotseatLabels[i] = getText(R.string.activity_not_found);
581 } else {
582 boolean found = false;
583 for (ResolveInfo ri : allMatches) {
584 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
585 && bestMatch.activityInfo.applicationInfo.packageName
586 .equals(ri.activityInfo.applicationInfo.packageName)) {
587 found = true;
588 break;
589 }
590 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700591
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400592 if (!found) {
593 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
594 // the bestMatch is probably the ResolveActivity, meaning the
595 // user has not yet selected a default
596 // so: we'll keep the original intent for now
597 mHotseats[i] = intent;
598
599 // set the accessibility text to "Select shortcut"
600 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
601 } else {
602 // we have an app!
603 // now reconstruct the intent to launch it through the front
604 // door
605 ComponentName com = new ComponentName(
606 bestMatch.activityInfo.applicationInfo.packageName,
607 bestMatch.activityInfo.name);
608 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
609
610 // load the app label for accessibility
611 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
612 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400613 }
614
615 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700616 Log.d(TAG, "loadHotseats: hotseat " + i
617 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400618 + ((mHotseats[i] == null)
619 ? "null"
620 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400621 + "] label=[" + mHotseatLabels[i]
622 + "]"
623 );
624 }
625 }
626 }
627
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800628 private void completeAdd(PendingAddArguments args) {
629 switch (args.requestCode) {
630 case REQUEST_PICK_APPLICATION:
631 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
632 break;
633 case REQUEST_PICK_SHORTCUT:
634 processShortcut(args.intent);
635 break;
636 case REQUEST_CREATE_SHORTCUT:
637 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
638 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800639 case REQUEST_PICK_APPWIDGET:
640 addAppWidgetFromPick(args.intent);
641 break;
642 case REQUEST_CREATE_APPWIDGET:
643 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
644 completeAddAppWidget(appWidgetId, args.screen);
645 break;
646 case REQUEST_PICK_WALLPAPER:
647 // We just wanted the activity result here so we can clear mWaitingForResult
648 break;
649 }
650 }
651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800653 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700654 mWaitingForResult = false;
655
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 // The pattern used here is that a user PICKs a specific application,
657 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700658
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
660 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700661
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800663 final PendingAddArguments args = new PendingAddArguments();
664 args.requestCode = requestCode;
665 args.intent = data;
666 args.screen = mAddScreen;
667 args.cellX = mAddIntersectCellX;
668 args.cellY = mAddIntersectCellY;
669
670 // If the loader is still running, defer the add until it is done.
671 if (isWorkspaceLocked()) {
672 sPendingAddList.add(args);
673 } else {
674 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 }
Romain Guy18042c82009-11-06 11:44:55 -0800676 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
677 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
678 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700679 // Clean up the appWidgetId if we canceled
680 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
681 if (appWidgetId != -1) {
682 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 }
684 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 }
686
687 @Override
688 protected void onResume() {
689 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800690 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700691 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400692 mWorkspaceLoading = true;
693 mModel.startLoader(this, true);
694 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700695 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800697 if (mWaitingForResume != null) {
698 mWaitingForResume.setStayPressed(false);
699 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700700 // When we resume Launcher, a different Activity might be responsible for the app
701 // market intent, so refresh the icon
702 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 }
704
705 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706 protected void onPause() {
707 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700708 // Some launcher layouts don't have a previous and next view
709 if (mPreviousView != null) {
710 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700711 }
712 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700713 dismissPreview(mNextView);
714 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700715 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800716 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700717 }
Romain Guycbb89e42009-06-08 15:52:54 -0700718
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700719 @Override
720 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400721 // Flag the loader to stop early before switching
722 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700723 if (mAllAppsGrid != null) {
724 mAllAppsGrid.surrender();
725 }
726 if (mAppsCustomizeContent != null) {
727 mAppsCustomizeContent.surrender();
728 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800729 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700730 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700731
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800732 // We can't hide the IME if it was forced open. So don't bother
733 /*
734 @Override
735 public void onWindowFocusChanged(boolean hasFocus) {
736 super.onWindowFocusChanged(hasFocus);
737
738 if (hasFocus) {
739 final InputMethodManager inputManager = (InputMethodManager)
740 getSystemService(Context.INPUT_METHOD_SERVICE);
741 WindowManager.LayoutParams lp = getWindow().getAttributes();
742 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
743 android.os.Handler()) {
744 protected void onReceiveResult(int resultCode, Bundle resultData) {
745 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
746 }
747 });
748 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
749 }
750 }
751 */
752
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 private boolean acceptFilter() {
754 final InputMethodManager inputManager = (InputMethodManager)
755 getSystemService(Context.INPUT_METHOD_SERVICE);
756 return !inputManager.isFullscreenMode();
757 }
758
759 @Override
760 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700761 final int uniChar = event.getUnicodeChar();
762 final boolean handled = super.onKeyDown(keyCode, event);
763 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
764 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
766 keyCode, event);
767 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700768 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700769 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700770 // showSearchDialog()
771 // If there are multiple keystrokes before the search dialog takes focus,
772 // onSearchRequested() will be called for every keystroke,
773 // but it is idempotent, so it's fine.
774 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775 }
776 }
777
Joe Onorato8a9625e2010-01-28 15:55:35 -0800778 // Eat the long press event so the keyboard doesn't come up.
779 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
780 return true;
781 }
782
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 return handled;
784 }
785
Karl Rosaen138a0412009-04-23 19:00:21 -0700786 private String getTypedText() {
787 return mDefaultKeySsb.toString();
788 }
789
790 private void clearTypedText() {
791 mDefaultKeySsb.clear();
792 mDefaultKeySsb.clearSpans();
793 Selection.setSelection(mDefaultKeySsb, 0);
794 }
795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700797 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
798 * State
799 */
800 private static State intToState(int stateOrdinal) {
801 State state = State.WORKSPACE;
802 final State[] stateValues = State.values();
803 for (int i = 0; i < stateValues.length; i++) {
804 if (stateValues[i].ordinal() == stateOrdinal) {
805 state = stateValues[i];
806 break;
807 }
808 }
809 return state;
810 }
811
812 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 * Restores the previous state, if it exists.
814 *
815 * @param savedState The previous state.
816 */
817 private void restoreState(Bundle savedState) {
818 if (savedState == null) {
819 return;
820 }
821
Michael Jurka883f55b2010-10-21 15:47:14 -0700822 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
823
Winson Chung785d2eb2011-04-14 16:08:02 -0700824 if (state == State.ALL_APPS || state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800825 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700826 } else if (state == State.CUSTOMIZE) {
827 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800828 }
829
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
831 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700832 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 }
834
835 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700836
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700838 mAddScreen = addScreen;
839 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
840 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 mRestoring = true;
842 }
843
844 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
845 if (renameFolder) {
846 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700847 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 mRestoring = true;
849 }
Winson Chunga12a2502010-12-20 14:41:35 -0800850
851 // Restore the current AllApps drawer tab
852 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
853 String curTab = savedState.getString("allapps_currentTab");
854 if (curTab != null) {
855 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
856 tabhost.setCurrentTabByTag(curTab);
857 }
858 int curPage = savedState.getInt("allapps_currentPage", -1);
859 if (curPage > -1) {
860 mAllAppsPagedView.setRestorePage(curPage);
861 }
862 }
863
864 // Restore the current customization drawer tab
865 if (mHomeCustomizationDrawer != null) {
866 String curTab = savedState.getString("customize_currentTab");
867 if (curTab != null) {
868 // We set this directly so that there is no delay before the tab is set
Michael Jurka7ef959b2011-02-23 11:48:32 -0800869 mCustomizePagedView.setCustomizationFilter(
870 mHomeCustomizationDrawer.getCustomizeFilterForTabTag(curTab));
Winson Chunga12a2502010-12-20 14:41:35 -0800871 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
872 }
873
874 // Note: currently we do not restore the page for the customization tray because unlike
875 // AllApps, the page content can change drastically
876 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700877
878 // Restore the AppsCustomize tab
879 if (mAppsCustomizeTabHost != null) {
880 String curTab = savedState.getString("apps_customize_currentTab");
881 if (curTab != null) {
882 // We set this directly so that there is no delay before the tab is set
883 mAppsCustomizeContent.setContentType(
884 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
885 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
886 }
887
888 // Note: currently we do not restore the page for the AppsCustomize pane because the
889 // change in layout can drastically affect the saved page index
890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 }
892
893 /**
894 * Finds all the views we need and configure them properly.
895 */
896 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700897 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400898
Romain Guyb1b69f52009-08-10 15:10:15 -0700899 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400900 dragLayer.setDragController(dragController);
Adam Cohen2801caf2011-05-13 20:57:39 -0700901 dragLayer.setLauncher(this);
902 mDragLayer = dragLayer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700904 if (LauncherApplication.isScreenLarge()) {
Winson Chung63257c12011-05-05 17:06:13 -0700905 mAllAppsGrid = (AllAppsView) dragLayer.findViewById(R.id.all_apps_view);
Winson Chung785d2eb2011-04-14 16:08:02 -0700906 mAllAppsGrid.setup(this, dragController);
907 // We don't want a hole punched in our window.
908 ((View) mAllAppsGrid).setWillNotDraw(false);
909 } else {
910 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
911 findViewById(R.id.apps_customize_pane);
912 mAppsCustomizeContent = (AppsCustomizePagedView)
913 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
914 mAppsCustomizeContent.setup(this, dragController);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700915 }
916
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700918
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500920 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700921
Joe Onorato7c312c12009-08-13 21:36:53 -0700922 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
923 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700925 View handleView = findViewById(R.id.all_apps_button);
926 if (handleView != null && handleView instanceof HandleView) {
927 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700928 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700929 mHandleView.setLauncher(this);
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700930 mHandleView.setOnLongClickListener(this);
931 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800932
Winson Chung80baf5a2010-08-09 16:03:15 -0700933 if (mCustomizePagedView != null) {
934 mCustomizePagedView.setLauncher(this);
935 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700936 } else {
937 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
938 hotseatLeft.setContentDescription(mHotseatLabels[0]);
939 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
940 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
941 hotseatRight.setContentDescription(mHotseatLabels[1]);
942 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400943
Winson Chung4e6a9762011-05-09 11:56:34 -0700944 View.OnKeyListener listener = new IndicatorKeyEventListener();
Michael Jurkaaf442092010-06-10 17:01:57 -0700945 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
Winson Chung4e6a9762011-05-09 11:56:34 -0700946 mPreviousView.setOnKeyListener(listener);
Michael Jurkaaf442092010-06-10 17:01:57 -0700947 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Winson Chung4e6a9762011-05-09 11:56:34 -0700948 mNextView.setOnKeyListener(listener);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800949
Michael Jurkaaf442092010-06-10 17:01:57 -0700950 Drawable previous = mPreviousView.getDrawable();
951 Drawable next = mNextView.getDrawable();
952 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953
Michael Jurkaaf442092010-06-10 17:01:57 -0700954 mPreviousView.setHapticFeedbackEnabled(false);
955 mPreviousView.setOnLongClickListener(this);
956 mNextView.setHapticFeedbackEnabled(false);
957 mNextView.setOnLongClickListener(this);
958 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800959
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400961 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -0800963 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964
965 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400966 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -0800967
968 final View allAppsButton = findViewById(R.id.all_apps_button);
Winson Chung97d85d22011-04-13 11:27:36 -0700969 final View divider = findViewById(R.id.all_apps_divider);
Michael Jurkab8e14472010-12-20 16:06:10 -0800970 final View configureButton = findViewById(R.id.configure_button);
971
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700972 if (LauncherApplication.isScreenLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800973 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -0700974 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800975 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -0700976 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700977 dragController.addDragListener(deleteZone);
978
Adam Cohencdc30d52010-12-01 15:09:47 -0800979 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
980 if (allAppsDeleteZone != null) {
981 allAppsDeleteZone.setLauncher(this);
982 allAppsDeleteZone.setDragController(dragController);
983 allAppsDeleteZone.setDragAndDropEnabled(false);
984 dragController.addDragListener(allAppsDeleteZone);
985 dragController.addDropTarget(allAppsDeleteZone);
986 }
987
988 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
989 findViewById(R.id.all_apps_info_target);
990 if (allAppsInfoTarget != null) {
991 allAppsInfoTarget.setLauncher(this);
992 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -0800993 allAppsInfoTarget.setDragAndDropEnabled(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700994 }
995 View marketButton = findViewById(R.id.market_button);
996 if (marketButton != null) {
997 if (allAppsInfoTarget != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -0800998 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -0800999 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001000 marketButton.setOnClickListener(new OnClickListener() {
1001 public void onClick(View v) {
1002 onClickAppMarketButton(v);
1003 }
1004 });
Adam Cohencdc30d52010-12-01 15:09:47 -08001005 }
1006
Joe Onorato00acb122009-08-04 16:04:30 -04001007 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001008 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001009 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001010
Joe Onorato00acb122009-08-04 16:04:30 -04001011 // The order here is bottom to top.
1012 dragController.addDropTarget(workspace);
1013 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001014 if (allAppsInfoTarget != null) {
1015 dragController.addDropTarget(allAppsInfoTarget);
1016 }
1017 if (allAppsDeleteZone != null) {
1018 dragController.addDropTarget(allAppsDeleteZone);
1019 }
Winson Chung97d85d22011-04-13 11:27:36 -07001020 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
Winson Chung4e6a9762011-05-09 11:56:34 -07001021 View.OnKeyListener listener = null;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07001022 if (LauncherApplication.isScreenLarge()) {
Winson Chung4e6a9762011-05-09 11:56:34 -07001023 // For tablets, AllApps lives in the button bar at the top
1024 listener = new ButtonBarKeyEventListener();
1025 } else {
1026 // For phones, AppsCustomize lives in the "dock" at the bottom
1027 listener = new DockKeyEventListener();
1028 }
Winson Chung97d85d22011-04-13 11:27:36 -07001029 int buttonCount = mButtonCluster.getChildCount();
1030 for (int i = 0; i < buttonCount; ++i) {
1031 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
1032 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001033
1034 mAllAppsButton = findViewById(R.id.all_apps_button);
Winson Chung97d85d22011-04-13 11:27:36 -07001035 mDivider = findViewById(R.id.all_apps_divider);
Michael Jurka838a4ca2011-02-07 13:33:06 -08001036 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001037
1038 // We had previously set these click handlers in XML, but the first time we launched
1039 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1040 // found the right handler. Setting the handlers directly here eliminates that cost.
1041 if (mConfigureButton != null) {
1042 mConfigureButton.setOnClickListener(new OnClickListener() {
1043 public void onClick(View v) {
1044 onClickConfigureButton(v);
1045 }
1046 });
1047 }
1048 if (mDivider != null) {
1049 mDivider.setOnClickListener(new OnClickListener() {
1050 public void onClick(View v) {
1051 onClickAllAppsButton(v);
1052 }
1053 });
1054 }
1055 if (mAllAppsButton != null) {
1056 mAllAppsButton.setOnClickListener(new OnClickListener() {
1057 public void onClick(View v) {
1058 onClickAllAppsButton(v);
1059 }
1060 });
1061 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 }
1063
Romain Guy8a73c512009-11-09 19:19:59 -08001064 @SuppressWarnings({"UnusedDeclaration"})
1065 public void previousScreen(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001066 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001067 mWorkspace.scrollLeft();
1068 }
Romain Guy8a73c512009-11-09 19:19:59 -08001069 }
1070
1071 @SuppressWarnings({"UnusedDeclaration"})
1072 public void nextScreen(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001073 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001074 mWorkspace.scrollRight();
1075 }
Romain Guy8a73c512009-11-09 19:19:59 -08001076 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001077
1078 @SuppressWarnings({"UnusedDeclaration"})
1079 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001080 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001081
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001082 int index = -1;
1083 if (v.getId() == R.id.hotseat_left) {
1084 index = 0;
1085 } else if (v.getId() == R.id.hotseat_right) {
1086 index = 1;
1087 }
1088
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001089 // reload these every tap; you never know when they might change
1090 loadHotseats();
1091 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001092 startActivitySafely(
1093 mHotseats[index],
1094 "hotseat"
1095 );
1096 }
1097 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 /**
1100 * Creates a view representing a shortcut.
1101 *
1102 * @param info The data structure describing the shortcut.
1103 *
1104 * @return A View inflated from R.layout.application.
1105 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001106 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001108 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 }
1110
1111 /**
1112 * Creates a view representing a shortcut inflated from the specified resource.
1113 *
1114 * @param layoutResId The id of the XML layout used to create the shortcut.
1115 * @param parent The group the shortcut belongs to.
1116 * @param info The data structure describing the shortcut.
1117 *
1118 * @return A View inflated from layoutResId.
1119 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001120 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001121 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1122 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 return favorite;
1125 }
1126
1127 /**
1128 * Add an application shortcut to the workspace.
1129 *
1130 * @param data The intent describing the application.
1131 * @param cellInfo The position on screen where to create the shortcut.
1132 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001133 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001134 int intersectCellX, int intersectCellY) {
1135 final int[] cellXY = mTmpAddItemCellCoordinates;
1136 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1137
1138 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1139 showOutOfSpaceMessage();
1140 return;
1141 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001143 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001144
Romain Guy73b979d2009-06-09 12:57:21 -07001145 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001146 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001148 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001149 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1150 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001151 } else {
1152 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 }
1154 }
Romain Guycbb89e42009-06-08 15:52:54 -07001155
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 /**
1157 * Add a shortcut to the workspace.
1158 *
1159 * @param data The intent describing the shortcut.
1160 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001161 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001162 private void completeAddShortcut(Intent data, int screen,
1163 int intersectCellX, int intersectCellY) {
1164 final int[] cellXY = mTmpAddItemCellCoordinates;
1165 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001166
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001167 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001168 boolean foundCellSpan = false;
1169 if (touchXY != null) {
1170 // when dragging and dropping, just find the closest free spot
1171 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1172 int[] result = screenLayout.findNearestVacantArea(
1173 touchXY[0], touchXY[1], 1, 1, cellXY);
1174 foundCellSpan = (result != null);
1175 } else {
1176 foundCellSpan = layout.findCellForSpanThatIntersects(
1177 cellXY, 1, 1, intersectCellX, intersectCellY);
1178 }
1179
1180 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001181 showOutOfSpaceMessage();
1182 return;
1183 }
1184
1185 final ShortcutInfo info = mModel.addShortcut(
1186 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187
1188 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001190 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 }
1192 }
1193
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001195 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001197 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001198 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 */
Winson Chungf7640c82011-02-28 13:47:29 -08001200 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001201 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001202
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001203 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001204 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1205 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001208 // We have saved the position to which the widget was dragged-- this really only matters
1209 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001210 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001211
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001212 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001213 boolean foundCellSpan = false;
1214 if (touchXY != null) {
1215 // when dragging and dropping, just find the closest free spot
1216 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1217 int[] result = screenLayout.findNearestVacantArea(
1218 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001219 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001220 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001221 // if we long pressed on an empty cell to bring up a menu,
1222 // make sure we intersect the empty cell
1223 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1224 // findCellForSpanThatIntersects will just ignore them
1225 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1226 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001227 }
1228
Michael Jurka0280c3b2010-09-17 15:00:07 -07001229 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001230 if (appWidgetId != -1) {
1231 // Deleting an app widget ID is a void call but writes to disk before returning
1232 // to the caller...
1233 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1234 new Thread("deleteAppWidgetId") {
1235 public void run() {
1236 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1237 }
1238 }.start();
1239 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001240 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001241 return;
1242 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001244 // Build Launcher-specific widget info and save to database
1245 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001246 launcherInfo.spanX = spanXY[0];
1247 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001248
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 LauncherModel.addItemToDatabase(this, launcherInfo,
1250 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001251 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252
1253 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001254 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001255
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001257 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001258
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001259 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001261
Michael Jurka0280c3b2010-09-17 15:00:07 -07001262 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001263 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001264
1265 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 }
1267 }
Romain Guycbb89e42009-06-08 15:52:54 -07001268
Adam Cohended9f8d2010-11-03 13:25:16 -07001269 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1270 @Override
1271 public void onReceive(Context context, Intent intent) {
1272 final String action = intent.getAction();
1273 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1274 mUserPresent = false;
1275 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001276
1277 // Reset AllApps to it's initial state
1278 if (mAllAppsGrid != null) {
1279 mAllAppsGrid.reset();
1280 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001281 if (mAppsCustomizeContent != null) {
1282 mAppsCustomizeContent.reset();
1283 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001284 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1285 mUserPresent = true;
1286 updateRunning();
1287 }
1288 }
1289 };
1290
1291 @Override
1292 public void onAttachedToWindow() {
1293 super.onAttachedToWindow();
1294
1295 // Listen for broadcasts related to user-presence
1296 final IntentFilter filter = new IntentFilter();
1297 filter.addAction(Intent.ACTION_SCREEN_OFF);
1298 filter.addAction(Intent.ACTION_USER_PRESENT);
1299 registerReceiver(mReceiver, filter);
1300
Adam Cohend113e0c2010-11-11 10:48:05 -08001301 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001302 mVisible = true;
1303 }
1304
1305 @Override
1306 public void onDetachedFromWindow() {
1307 super.onDetachedFromWindow();
1308 mVisible = false;
1309
Adam Cohend113e0c2010-11-11 10:48:05 -08001310 if (mAttached) {
1311 unregisterReceiver(mReceiver);
1312 mAttached = false;
1313 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001314 updateRunning();
1315 }
1316
1317 public void onWindowVisibilityChanged(int visibility) {
1318 mVisible = visibility == View.VISIBLE;
1319 updateRunning();
1320 }
1321
1322 private void sendAdvanceMessage(long delay) {
1323 mHandler.removeMessages(ADVANCE_MSG);
1324 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1325 mHandler.sendMessageDelayed(msg, delay);
1326 mAutoAdvanceSentTime = System.currentTimeMillis();
1327 }
1328
1329 private void updateRunning() {
1330 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1331 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1332 mAutoAdvanceRunning = autoAdvanceRunning;
1333 if (autoAdvanceRunning) {
1334 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1335 sendAdvanceMessage(delay);
1336 } else {
1337 if (!mWidgetsToAdvance.isEmpty()) {
1338 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1339 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1340 }
1341 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001342 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001343 }
1344 }
1345 }
1346
1347 private final Handler mHandler = new Handler() {
1348 @Override
1349 public void handleMessage(Message msg) {
1350 if (msg.what == ADVANCE_MSG) {
1351 int i = 0;
1352 for (View key: mWidgetsToAdvance.keySet()) {
1353 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1354 final int delay = mAdvanceStagger * i;
1355 if (v instanceof Advanceable) {
1356 postDelayed(new Runnable() {
1357 public void run() {
1358 ((Advanceable) v).advance();
1359 }
1360 }, delay);
1361 }
1362 i++;
1363 }
1364 sendAdvanceMessage(mAdvanceInterval);
1365 }
1366 }
1367 };
1368
1369 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1370 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1371 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1372 if (v instanceof Advanceable) {
1373 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001374 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001375 updateRunning();
1376 }
1377 }
1378
1379 void removeWidgetToAutoAdvance(View hostView) {
1380 if (mWidgetsToAdvance.containsKey(hostView)) {
1381 mWidgetsToAdvance.remove(hostView);
1382 updateRunning();
1383 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001384 }
1385
Joe Onorato9c1289c2009-08-17 11:03:03 -04001386 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1387 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001388 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001389 launcherInfo.hostView = null;
1390 }
1391
Adam Cohended9f8d2010-11-03 13:25:16 -07001392 void showOutOfSpaceMessage() {
1393 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1394 }
1395
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001396 public LauncherAppWidgetHost getAppWidgetHost() {
1397 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 }
Romain Guycbb89e42009-06-08 15:52:54 -07001399
Winson Chunga9abd0e2010-10-27 17:18:37 -07001400 public LauncherModel getModel() {
1401 return mModel;
1402 }
1403
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001404 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001405 getWindow().closeAllPanels();
1406
1407 try {
1408 dismissDialog(DIALOG_CREATE_SHORTCUT);
1409 // Unlock the workspace if the dialog was showing
1410 } catch (Exception e) {
1411 // An exception is thrown if the dialog is not visible, which is fine
1412 }
1413
1414 try {
1415 dismissDialog(DIALOG_RENAME_FOLDER);
1416 // Unlock the workspace if the dialog was showing
1417 } catch (Exception e) {
1418 // An exception is thrown if the dialog is not visible, which is fine
1419 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001420
1421 // Whatever we were doing is hereby canceled.
1422 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001423 }
1424
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 @Override
1426 protected void onNewIntent(Intent intent) {
1427 super.onNewIntent(intent);
1428
1429 // Close the menu
1430 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001431 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001432 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001433
Joe Onorato14f122b2009-11-19 14:06:36 -08001434 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1435 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001436
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001437 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001438 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001439 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001440 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001441 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001442 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001443 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001444
Joe Onorato3a8820b2009-11-10 15:06:42 -08001445 final View v = getWindow().peekDecorView();
1446 if (v != null && v.getWindowToken() != null) {
1447 InputMethodManager imm = (InputMethodManager)getSystemService(
1448 INPUT_METHOD_SERVICE);
1449 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001451
1452 // Reset AllApps to it's initial state
1453 if (mAllAppsGrid != null) {
1454 mAllAppsGrid.reset();
1455 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001456 if (mAppsCustomizeContent != null) {
1457 mAppsCustomizeContent.reset();
1458 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 }
1460 }
1461
1462 @Override
1463 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1464 // Do not call super here
1465 mSavedInstanceState = savedInstanceState;
1466 }
1467
1468 @Override
1469 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001470 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471
1472 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1473 if (folders.size() > 0) {
1474 final int count = folders.size();
1475 long[] ids = new long[count];
1476 for (int i = 0; i < count; i++) {
1477 final FolderInfo info = folders.get(i).getInfo();
1478 ids[i] = info.id;
1479 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001480 outState.putLongArray(RUNTIME_STATE_FOLDERS, ids);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 } else {
1482 super.onSaveInstanceState(outState);
1483 }
1484
Michael Jurka883f55b2010-10-21 15:47:14 -07001485 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486
Michael Jurka0280c3b2010-09-17 15:00:07 -07001487 if (mAddScreen > -1 && mWaitingForResult) {
1488 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1489 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1490 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 }
1492
1493 if (mFolderInfo != null && mWaitingForResult) {
1494 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1495 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1496 }
Michael Jurka946ad472010-07-09 18:05:18 -07001497
Winson Chung43b119d2010-12-06 16:45:05 -08001498 // Save the current AllApps drawer tab
1499 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1500 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1501 String currentTabTag = tabhost.getCurrentTabTag();
1502 if (currentTabTag != null) {
1503 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001504 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001505 }
1506 }
Winson Chung43b119d2010-12-06 16:45:05 -08001507 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001508 if (mHomeCustomizationDrawer != null) {
1509 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1510 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001511 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001512 }
1513 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001514 // Save the current AppsCustomize tab
1515 if (mAppsCustomizeTabHost != null) {
1516 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1517 if (currentTabTag != null) {
1518 outState.putString("apps_customize_currentTab", currentTabTag);
1519 }
1520 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 }
1522
1523 @Override
1524 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001526
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001528 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001530 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001532 mAppWidgetHost = null;
1533
1534 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535
1536 TextKeyListener.getInstance().release();
1537
Joe Onorato9c1289c2009-08-17 11:03:03 -04001538 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539
Joe Onorato9c1289c2009-08-17 11:03:03 -04001540 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001541
1542 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001543
Patrick Dubroyab962b72010-07-26 12:10:10 -07001544 // Some launcher layouts don't have a previous and next view
1545 if (mPreviousView != null) {
1546 dismissPreview(mPreviousView);
1547 }
1548 if (mNextView != null) {
1549 dismissPreview(mNextView);
1550 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001551
1552 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001553
1554 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1555 mWorkspace.removeAllViews();
1556 mWorkspace = null;
1557 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001558
1559 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 }
1561
Adam Cohena9cf38f2011-05-02 15:36:58 -07001562 public DragController getDragController() {
1563 return mDragController;
1564 }
1565
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 @Override
1567 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001568 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 super.startActivityForResult(intent, requestCode);
1570 }
1571
1572 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001573 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001575
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001576 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001577
Karl Rosaen138a0412009-04-23 19:00:21 -07001578 if (initialQuery == null) {
1579 // Use any text typed in the launcher as the initial query
1580 initialQuery = getTypedText();
1581 clearTypedText();
1582 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 if (appSearchData == null) {
1584 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001585 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 }
Romain Guycbb89e42009-06-08 15:52:54 -07001587
Karl Rosaen138a0412009-04-23 19:00:21 -07001588 final SearchManager searchManager =
1589 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001590 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001591 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 }
1593
1594 @Override
1595 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001596 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001597 return false;
1598 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599
1600 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001601
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1603 .setIcon(android.R.drawable.ic_menu_add)
1604 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001605 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1606 .setIcon(android.R.drawable.ic_menu_manage)
1607 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001608 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 .setIcon(android.R.drawable.ic_menu_gallery)
1610 .setAlphabeticShortcut('W');
1611 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1612 .setIcon(android.R.drawable.ic_search_category_default)
1613 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1614 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1615 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1616 .setAlphabeticShortcut('N');
1617
1618 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001619 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1620 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621
1622 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1623 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1624 .setIntent(settings);
1625
1626 return true;
1627 }
1628
1629 @Override
1630 public boolean onPrepareOptionsMenu(Menu menu) {
1631 super.onPrepareOptionsMenu(menu);
1632
Winson Chung785d2eb2011-04-14 16:08:02 -07001633 if (mAllAppsGrid != null) {
1634 // If all apps is animating, don't show the menu, because we don't know
1635 // which one to show.
1636 if (mAllAppsGrid.isAnimating()) {
1637 return false;
1638 }
1639
1640 // Only show the add and wallpaper options when we're not in all apps.
1641 boolean visible = !mAllAppsGrid.isVisible();
1642 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1643 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1644
1645 // Disable add if the workspace is full.
1646 if (visible) {
1647 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1648 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
1649 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001650 }
1651
Winson Chung785d2eb2011-04-14 16:08:02 -07001652 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1653 // related code?
1654 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655
1656 return true;
1657 }
1658
1659 @Override
1660 public boolean onOptionsItemSelected(MenuItem item) {
1661 switch (item.getItemId()) {
1662 case MENU_ADD:
1663 addItems();
1664 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001665 case MENU_MANAGE_APPS:
1666 manageApps();
1667 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 case MENU_WALLPAPER_SETTINGS:
1669 startWallpaper();
1670 return true;
1671 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001672 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 return true;
1674 case MENU_NOTIFICATIONS:
1675 showNotifications();
1676 return true;
1677 }
1678
1679 return super.onOptionsItemSelected(item);
1680 }
Romain Guycbb89e42009-06-08 15:52:54 -07001681
Karl Rosaen138a0412009-04-23 19:00:21 -07001682 /**
1683 * Indicates that we want global search for this activity by setting the globalSearch
1684 * argument for {@link #startSearch} to true.
1685 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001687 @Override
1688 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001689 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001690 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001691 }
1692
Joe Onorato9c1289c2009-08-17 11:03:03 -04001693 public boolean isWorkspaceLocked() {
1694 return mWorkspaceLoading || mWaitingForResult;
1695 }
1696
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001697 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1698 private boolean isPreviewVisible() {
1699 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1700 (mNextView != null && mNextView.getTag() != null);
1701 }
1702
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 private void addItems() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07001704 if (LauncherApplication.isScreenLarge()) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001705 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001706 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001707 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001708 }
1709 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001710 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001711 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 }
1714
Michael Jurka0280c3b2010-09-17 15:00:07 -07001715 private void resetAddInfo() {
1716 mAddScreen = -1;
1717 mAddIntersectCellX = -1;
1718 mAddIntersectCellY = -1;
1719 mAddDropPosition = null;
1720 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001721
Winson Chung55cef262010-10-28 14:14:18 -07001722 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001723 resetAddInfo();
1724 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001725 mAddDropPosition = position;
1726
Michael Jurkaaf442092010-06-10 17:01:57 -07001727 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001728 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1729 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001730 }
1731
Winson Chung7ad01412010-09-27 11:33:03 -07001732 private void manageApps() {
1733 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1734 }
1735
Michael Jurkaaf442092010-06-10 17:01:57 -07001736 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001737 // TODO: catch bad widget exception when sent
1738 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001739 // TODO: Is this log message meaningful?
1740 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001741 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001742 }
1743
Winson Chung55cef262010-10-28 14:14:18 -07001744 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001745 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746
Bjorn Bringert7984c942009-12-09 15:38:25 +00001747 if (appWidget.configure != null) {
1748 // Launch over to configure widget, if needed
1749 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1750 intent.setComponent(appWidget.configure);
1751 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001752 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001753 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1754 intent.putExtra(
1755 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1756 info.mimeType);
1757
1758 final String mimeType = info.mimeType;
1759 final ClipData clipData = (ClipData) info.configurationData;
1760 final ClipDescription clipDesc = clipData.getDescription();
1761 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1762 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001763 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001764 final CharSequence stringData = item.getText();
1765 final Uri uriData = item.getUri();
1766 final Intent intentData = item.getIntent();
1767 final String key =
1768 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1769 if (uriData != null) {
1770 intent.putExtra(key, uriData);
1771 } else if (intentData != null) {
1772 intent.putExtra(key, intentData);
1773 } else if (stringData != null) {
1774 intent.putExtra(key, stringData);
1775 }
1776 break;
1777 }
1778 }
1779 }
Winson Chung55cef262010-10-28 14:14:18 -07001780 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001781
Romain Guy8e633c52010-03-04 12:51:36 -08001782 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001784 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001785 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 }
1787 }
Romain Guycbb89e42009-06-08 15:52:54 -07001788
Michael Jurka0280c3b2010-09-17 15:00:07 -07001789 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1790 resetAddInfo();
1791 mAddScreen = screen;
1792 mAddDropPosition = position;
1793
1794 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1795 createShortcutIntent.setComponent(componentName);
1796 processShortcut(createShortcutIntent);
1797 }
1798
Joe Onoratodeb98af2010-02-19 14:59:39 -08001799 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001800 // Handle case where user selected "Applications"
1801 String applicationName = getResources().getString(R.string.group_applications);
1802 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001803
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001804 if (applicationName != null && applicationName.equals(shortcutName)) {
1805 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1806 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001807
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001808 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1809 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001810 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001811 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001812 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001813 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001814 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 }
1816
Winson Chung24ab2f12010-09-16 14:10:47 -07001817 void processWallpaper(Intent intent) {
1818 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1819 }
1820
Adam Cohendf035382011-04-11 17:22:04 -07001821 FolderIcon addFolder(int screen, int intersectCellX, int intersectCellY) {
Adam Cohendf2cc412011-04-27 16:56:57 -07001822 FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 folderInfo.title = getText(R.string.folder_name);
1824
Michael Jurka0280c3b2010-09-17 15:00:07 -07001825 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1826 final int[] cellXY = mTmpAddItemCellCoordinates;
1827 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1828 showOutOfSpaceMessage();
Adam Cohendf035382011-04-11 17:22:04 -07001829 return null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001830 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831
1832 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001833 LauncherModel.addItemToDatabase(this, folderInfo,
1834 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001835 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001836 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837
1838 // Create the view
1839 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001840 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1841 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001842 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001843 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 }
Romain Guycbb89e42009-06-08 15:52:54 -07001845
Joe Onorato9c1289c2009-08-17 11:03:03 -04001846 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001847 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848 }
1849
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 private void showNotifications() {
1851 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1852 if (statusBar != null) {
1853 statusBar.expand();
1854 }
1855 }
1856
1857 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001858 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001860 Intent chooser = Intent.createChooser(pickWallpaper,
1861 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001862 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1863 // Removed in Eclair MR1
1864// WallpaperManager wm = (WallpaperManager)
1865// getSystemService(Context.WALLPAPER_SERVICE);
1866// WallpaperInfo wi = wm.getWallpaperInfo();
1867// if (wi != null && wi.getSettingsActivity() != null) {
1868// LabeledIntent li = new LabeledIntent(getPackageName(),
1869// R.string.configure_wallpaper, 0);
1870// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1871// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1872// }
Mike Clerona0618e42009-10-22 13:55:21 -07001873 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 }
1875
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001876 /**
1877 * Registers various content observers. The current implementation registers
1878 * only a favorites observer to keep track of the favorites applications.
1879 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001880 private void registerContentObservers() {
1881 ContentResolver resolver = getContentResolver();
1882 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1883 true, mWidgetObserver);
1884 }
1885
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 @Override
1887 public boolean dispatchKeyEvent(KeyEvent event) {
1888 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1889 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001891 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001892 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001893 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001894 dumpState();
1895 return true;
1896 }
1897 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001898 }
1899 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1900 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001901 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 return true;
1903 }
1904 }
1905
1906 return super.dispatchKeyEvent(event);
1907 }
1908
Joe Onorato88ec0992009-11-19 13:16:06 -08001909 @Override
1910 public void onBackPressed() {
Winson Chung785d2eb2011-04-14 16:08:02 -07001911 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001912 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001913 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08001914 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001915 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001916 dismissPreview(mPreviousView);
1917 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001918 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001919 mWorkspace.exitWidgetResizeMode();
1920
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001921 // Back button is a no-op here, but give at least some feedback for the button press
1922 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001923 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001924 }
1925
Adam Cohen2801caf2011-05-13 20:57:39 -07001926 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 Folder folder = mWorkspace.getOpenFolder();
1928 if (folder != null) {
1929 closeFolder(folder);
Adam Cohen2801caf2011-05-13 20:57:39 -07001930 mDragLayer.setCurrentFolder(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 }
1932 }
1933
1934 void closeFolder(Folder folder) {
1935 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001936
Michael Jurka8c920dd2011-01-20 14:16:56 -08001937 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001939 CellLayout cl = (CellLayout) parent;
Adam Cohen2801caf2011-05-13 20:57:39 -07001940 FolderIcon fi = (FolderIcon) cl.getChildAt(folder.mInfo.cellX, folder.mInfo.cellY);
1941 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001942 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001944
1945 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 folder.onClose();
1947 }
1948
1949 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001950 * Re-listen when widgets are reset.
1951 */
1952 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001953 if (mAppWidgetHost != null) {
1954 mAppWidgetHost.startListening();
1955 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001956 }
1957
1958 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001959 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1960 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001962 private void unbindDesktopItems() {
1963 for (ItemInfo item: mDesktopItems) {
1964 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001966 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 /**
1970 * Launches the intent referred by the clicked shortcut.
1971 *
1972 * @param v The view representing the clicked shortcut.
1973 */
1974 public void onClick(View v) {
1975 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001976 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001978 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001979 int[] pos = new int[2];
1980 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001981 intent.setSourceBounds(new Rect(pos[0], pos[1],
1982 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001983 boolean success = startActivitySafely(intent, tag);
1984
1985 if (success && v instanceof BubbleTextView) {
1986 mWaitingForResume = (BubbleTextView) v;
1987 mWaitingForResume.setStayPressed(true);
1988 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001990 if (v instanceof FolderIcon) {
1991 FolderIcon fi = (FolderIcon) v;
1992 handleFolderClick(fi);
1993 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001994 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001995 if (mState == State.ALL_APPS) {
1996 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001997 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001998 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001999 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 }
2001 }
2002
Michael Jurka0e260592010-06-30 17:07:39 -07002003 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002004 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002005 // clicking anywhere on the workspace causes the customization drawer to slide down
2006 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002007 return false;
2008 }
2009
Michael Jurkaaf442092010-06-10 17:01:57 -07002010 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002011 * Event handler for the search button
2012 *
2013 * @param v The view that was clicked.
2014 */
2015 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002016 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002017 // Use a custom animation for launching search
2018 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002019 }
2020
2021 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002022 * Event handler for the voice button
2023 *
2024 * @param v The view that was clicked.
2025 */
2026 public void onClickVoiceButton(View v) {
2027 startVoiceSearch();
2028 }
2029
2030 private void startVoiceSearch() {
2031 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2032 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2033 startActivity(intent);
2034 }
2035
2036 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002037 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002038 * enters home screen customization mode.
2039 *
2040 * @param v The view that was clicked.
2041 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002042 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002043 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002044 }
2045
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002046 /**
2047 * Event handler for the "grid" button that appears on the home screen, which
2048 * enters all apps mode.
2049 *
2050 * @param v The view that was clicked.
2051 */
2052 public void onClickAllAppsButton(View v) {
2053 showAllApps(true);
2054 }
2055
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002056 public void onClickAppMarketButton(View v) {
2057 if (mAppMarketIntent != null) {
2058 startActivitySafely(mAppMarketIntent, "app market");
2059 }
2060 }
2061
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002062 void startApplicationDetailsActivity(ComponentName componentName) {
2063 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002064 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2065 Uri.fromParts("package", packageName, null));
2066 startActivity(intent);
2067 }
2068
Patrick Dubroy5539af72010-09-07 15:22:01 -07002069 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2070 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2071 // System applications cannot be installed. For now, show a toast explaining that.
2072 // We may give them the option of disabling apps this way.
2073 int messageId = R.string.uninstall_system_app_text;
2074 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2075 } else {
2076 String packageName = appInfo.componentName.getPackageName();
2077 String className = appInfo.componentName.getClassName();
2078 Intent intent = new Intent(
2079 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2080 startActivity(intent);
2081 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002082 }
2083
Michael Jurkaddd62e92011-02-16 17:49:14 -08002084 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2086 try {
2087 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002088 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 } catch (ActivityNotFoundException e) {
2090 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002091 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 } catch (SecurityException e) {
2093 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002094 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002096 "or use the exported attribute for this activity. "
2097 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002099 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002101
Romain Guy8e633c52010-03-04 12:51:36 -08002102 void startActivityForResultSafely(Intent intent, int requestCode) {
2103 try {
2104 startActivityForResult(intent, requestCode);
2105 } catch (ActivityNotFoundException e) {
2106 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2107 } catch (SecurityException e) {
2108 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2109 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2110 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2111 "or use the exported attribute for this activity.", e);
2112 }
2113 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114
Adam Cohena9cf38f2011-05-02 15:36:58 -07002115 private void handleFolderClick(FolderIcon folderIcon) {
2116 final FolderInfo info = folderIcon.mInfo;
2117 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 // Close any open folder
2119 closeFolder();
2120 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002121 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 } else {
2123 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07002124 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 int folderScreen;
2126 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002127 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128 // .. and close it
2129 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002130 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131 // Close any folder open on the current screen
2132 closeFolder();
2133 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002134 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 }
2136 }
2137 }
2138 }
2139
Adam Cohen2801caf2011-05-13 20:57:39 -07002140 private void growAndFadeOutFolderIcon(FolderIcon fi) {
2141 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2142 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2143 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2144
2145 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2146 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2147 oa.start();
2148 }
2149
2150 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
2151 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2152 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2153 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2154
2155 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2156 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2157 oa.start();
2158 }
2159
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002161 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162 * is animated relative to the specified View. If the View is null, no animation
2163 * is played.
2164 *
2165 * @param folderInfo The FolderInfo describing the folder to open.
2166 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002167 public void openFolder(FolderIcon folderIcon) {
2168 Folder folder = folderIcon.mFolder;
2169 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170
Adam Cohen2801caf2011-05-13 20:57:39 -07002171 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002172 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173
Adam Cohena9cf38f2011-05-02 15:36:58 -07002174 mWorkspace.addInFullScreen(folder, info.screen);
Adam Cohen2801caf2011-05-13 20:57:39 -07002175 mDragLayer.setCurrentFolder(folder);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002176 folder.animateOpen();
2177 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 }
2179
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002181 if (mState != State.WORKSPACE) {
2182 return false;
2183 }
2184
Romain Guy1fbc1c82009-11-09 20:43:08 -08002185 switch (v.getId()) {
2186 case R.id.previous_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002187 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002188 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2189 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002190 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002191 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002192 return true;
2193 case R.id.next_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002194 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002195 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2196 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002197 showPreviews(v);
2198 }
2199 return true;
2200 case R.id.all_apps_button:
Winson Chung785d2eb2011-04-14 16:08:02 -07002201 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002202 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2203 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2204 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002205 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002206 return true;
2207 }
2208
Joe Onorato9c1289c2009-08-17 11:03:03 -04002209 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 return false;
2211 }
2212
2213 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002214 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 }
2216
Michael Jurka0280c3b2010-09-17 15:00:07 -07002217 resetAddInfo();
2218 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002220 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 return true;
2222 }
2223
Michael Jurka0280c3b2010-09-17 15:00:07 -07002224 final View itemUnderLongClick = longClickCellInfo.cell;
2225
Winson Chung304dcde2011-01-07 11:17:23 -08002226 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002227 if (itemUnderLongClick == null) {
2228 // User long pressed on empty space
2229 mWorkspace.setAllowLongPress(false);
2230 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2231 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002232 if (LauncherApplication.isScreenLarge()) {
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002233 addItems();
2234 } else {
Winson Chung4dbea792011-05-05 14:21:32 -07002235 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 }
2237 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002238 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002240 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2241 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002242 mAddIntersectCellX = longClickCellInfo.cellX;
2243 mAddIntersectCellY = longClickCellInfo.cellY;
2244 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002245 }
2246 }
2247 }
2248 return true;
2249 }
2250
Romain Guye6b8e2f2009-11-10 11:56:55 -08002251 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002252 private void dismissPreview(final View v) {
2253 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002254 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002255 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2256 public void onDismiss() {
2257 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2258 int count = group.getChildCount();
2259 for (int i = 0; i < count; i++) {
2260 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2261 }
Michael Jurka25913ca2011-05-04 17:45:33 -07002262 ArrayList<Bitmap> bitmaps =
2263 (ArrayList<Bitmap>) v.getTag(R.id.all_apps_button_cluster);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002264 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2265
2266 v.setTag(R.id.workspace, null);
Michael Jurka25913ca2011-05-04 17:45:33 -07002267 v.setTag(R.id.all_apps_button_cluster, null);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002268 window.setOnDismissListener(null);
2269 }
2270 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002271 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002272 }
2273 v.setTag(null);
2274 }
2275
Romain Guyf8e6a802009-12-07 17:48:02 -08002276 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002277 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002278 }
2279
Romain Guya6abce82009-11-10 02:54:41 -08002280 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002281 final Resources resources = getResources();
2282 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002283
Romain Guya6abce82009-11-10 02:54:41 -08002284 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002285
Romain Guy9d31e9f2009-11-11 18:54:28 -08002286 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002287
Romain Guyf8e6a802009-12-07 17:48:02 -08002288 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002289 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002290 int extraW = (int) ((r.left + r.right) * max);
2291 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002292
2293 int aW = cell.getWidth() - extraW;
2294 float w = aW / max;
2295
2296 int width = cell.getWidth();
2297 int height = cell.getHeight();
2298 int x = cell.getLeftPadding();
2299 int y = cell.getTopPadding();
2300 width -= (x + cell.getRightPadding());
2301 height -= (y + cell.getBottomPadding());
2302
2303 float scale = w / width;
2304
2305 int count = end - start;
2306
2307 final float sWidth = width * scale;
2308 float sHeight = height * scale;
2309
Romain Guye6b8e2f2009-11-10 11:56:55 -08002310 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002311
Romain Guye6b8e2f2009-11-10 11:56:55 -08002312 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2313 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002314
2315 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002316 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002317 cell = (CellLayout) workspace.getChildAt(i);
2318
Romain Guyf8e6a802009-12-07 17:48:02 -08002319 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002320 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002321
2322 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002323 c.scale(scale, scale);
2324 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002325 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002326
Romain Guy9d31e9f2009-11-11 18:54:28 -08002327 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002328 image.setImageBitmap(bitmap);
2329 image.setTag(i);
2330 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002331 image.setOnFocusChangeListener(handler);
2332 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002333 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002334
2335 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002336 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2337
Winson Chungaafa03c2010-06-11 17:34:16 -07002338 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002339 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002340
2341 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002342 p.setContentView(preview);
2343 p.setWidth((int) (sWidth * count + extraW));
2344 p.setHeight((int) (sHeight + extraH));
2345 p.setAnimationStyle(R.style.AnimationPreview);
2346 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002347 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002348 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002349 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002350
Romain Guye6b8e2f2009-11-10 11:56:55 -08002351 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2352 public void onDismiss() {
2353 dismissPreview(anchor);
2354 }
2355 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002356
2357 anchor.setTag(p);
2358 anchor.setTag(R.id.workspace, preview);
Michael Jurka25913ca2011-05-04 17:45:33 -07002359 anchor.setTag(R.id.all_apps_button_cluster, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002360 }
2361
Romain Guy9d31e9f2009-11-11 18:54:28 -08002362 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002363 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002364
Romain Guye6b8e2f2009-11-10 11:56:55 -08002365 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002366 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002367 }
2368
2369 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002370 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002371 v.post(this);
2372 }
2373
2374 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002375 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002376 }
2377
2378 public void onFocusChange(View v, boolean hasFocus) {
2379 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002380 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002381 }
Romain Guya6abce82009-11-10 02:54:41 -08002382 }
2383 }
2384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002385 Workspace getWorkspace() {
2386 return mWorkspace;
2387 }
2388
Michael Jurka3c38d562011-02-18 19:22:12 -08002389 TabHost getCustomizationDrawer() {
2390 return mHomeCustomizationDrawer;
2391 }
2392
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002393 @Override
2394 protected Dialog onCreateDialog(int id) {
2395 switch (id) {
2396 case DIALOG_CREATE_SHORTCUT:
2397 return new CreateShortcut().createDialog();
2398 case DIALOG_RENAME_FOLDER:
2399 return new RenameFolder().createDialog();
2400 }
2401
2402 return super.onCreateDialog(id);
2403 }
2404
2405 @Override
2406 protected void onPrepareDialog(int id, Dialog dialog) {
2407 switch (id) {
2408 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 break;
2410 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002411 if (mFolderInfo != null) {
2412 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2413 final CharSequence text = mFolderInfo.title;
2414 input.setText(text);
2415 input.setSelection(0, text.length());
2416 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 break;
2418 }
2419 }
2420
2421 void showRenameDialog(FolderInfo info) {
2422 mFolderInfo = info;
2423 mWaitingForResult = true;
2424 showDialog(DIALOG_RENAME_FOLDER);
2425 }
2426
Michael Jurka0280c3b2010-09-17 15:00:07 -07002427 private void showAddDialog(int intersectX, int intersectY) {
2428 resetAddInfo();
2429 mAddIntersectCellX = intersectX;
2430 mAddIntersectCellY = intersectY;
2431 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 mWaitingForResult = true;
2433 showDialog(DIALOG_CREATE_SHORTCUT);
2434 }
2435
Joe Onoratodeb98af2010-02-19 14:59:39 -08002436 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002437 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002438 Bundle bundle = new Bundle();
2439
2440 ArrayList<String> shortcutNames = new ArrayList<String>();
2441 shortcutNames.add(getString(R.string.group_applications));
2442 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2443
2444 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2445 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2446 R.drawable.ic_launcher_application));
2447 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2448
2449 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2450 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002451 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002452 pickIntent.putExtras(bundle);
2453
Joe Onoratodeb98af2010-02-19 14:59:39 -08002454 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002455 }
2456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002457 private class RenameFolder {
2458 private EditText mInput;
2459
2460 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002461 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2462 mInput = (EditText) layout.findViewById(R.id.folder_name);
2463
2464 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2465 builder.setIcon(0);
2466 builder.setTitle(getString(R.string.rename_folder_title));
2467 builder.setCancelable(true);
2468 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2469 public void onCancel(DialogInterface dialog) {
2470 cleanup();
2471 }
2472 });
2473 builder.setNegativeButton(getString(R.string.cancel_action),
2474 new Dialog.OnClickListener() {
2475 public void onClick(DialogInterface dialog, int which) {
2476 cleanup();
2477 }
2478 }
2479 );
2480 builder.setPositiveButton(getString(R.string.rename_action),
2481 new Dialog.OnClickListener() {
2482 public void onClick(DialogInterface dialog, int which) {
2483 changeFolderName();
2484 }
2485 }
2486 );
2487 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002488
2489 final AlertDialog dialog = builder.create();
2490 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2491 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002492 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002493 mInput.requestFocus();
2494 InputMethodManager inputManager = (InputMethodManager)
2495 getSystemService(Context.INPUT_METHOD_SERVICE);
2496 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002497 }
2498 });
2499
2500 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002501 }
2502
2503 private void changeFolderName() {
2504 final String name = mInput.getText().toString();
2505 if (!TextUtils.isEmpty(name)) {
2506 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002507 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 mFolderInfo.title = name;
2509 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2510
Joe Onorato9c1289c2009-08-17 11:03:03 -04002511 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002512 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002513 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002514 } else {
2515 final FolderIcon folderIcon = (FolderIcon)
2516 mWorkspace.getViewForTag(mFolderInfo);
2517 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002518 // TODO: At some point we'll probably want some version of setting
2519 // the text for a folder icon.
2520 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002521 getWorkspace().requestLayout();
2522 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002523 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002524 mWorkspaceLoading = true;
2525 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002526 }
2527 }
2528 }
2529 cleanup();
2530 }
2531
2532 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002533 dismissDialog(DIALOG_RENAME_FOLDER);
2534 mWaitingForResult = false;
2535 mFolderInfo = null;
2536 }
2537 }
2538
Daniel Sandler843e8602010-06-07 14:59:01 -04002539 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2540 public boolean isAllAppsVisible() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002541 return (mState == State.ALL_APPS) || (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002542 }
2543
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002544 // AllAppsView.Watcher
2545 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002546 // In XLarge view, we zoom down the workspace below all apps so it's still visible
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002547 if (zoom == 1.0f && !LauncherApplication.isScreenLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002548 mWorkspace.setVisibility(View.GONE);
2549 }
2550 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002551
Michael Jurkaafca9532011-02-16 14:50:35 -08002552 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002553 button.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002554 }
2555
2556 private void hideToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002557 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2558 button.setVisibility(View.INVISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002559 button.setAlpha(0.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002560 }
2561
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002562 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002563 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002564 *
2565 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2566 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002567 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002568 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002569 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002570 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002571 final boolean showing = show;
2572 final boolean hiding = !show;
2573
2574 final int duration = show ?
2575 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2576 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2577
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002578 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002579 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002580 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002581 anim.addUpdateListener(new AnimatorUpdateListener() {
2582 public void onAnimationUpdate(ValueAnimator animation) {
2583 view.setAlpha((Float) animation.getAnimatedValue());
2584 }
2585 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002586 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002587 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002588 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002589 if (showing) showAndEnableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002590 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002591 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002592 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002593 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002594 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002595 });
2596 seq.play(anim);
2597 } else {
2598 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002599 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002600 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002601 } else {
2602 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002603 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002604 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002605 }
2606
2607 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002608 * Show/hide the appropriate toolbar buttons for newState.
2609 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002610 *
2611 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002612 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2613 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002614 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002615 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002616 switch (newState) {
2617 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002618 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002619 mDeleteZone.setDragAndDropEnabled(true);
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002620 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002621 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
2622 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002623 break;
2624 case ALL_APPS:
Winson Chung785d2eb2011-04-14 16:08:02 -07002625 case APPS_CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002626 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002627 mDeleteZone.setDragAndDropEnabled(false);
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002628 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002629 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
2630 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002631 break;
2632 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002633 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002634 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002635 break;
2636 }
2637 }
2638
2639 /**
2640 * Helper method for the cameraZoomIn/cameraZoomOut animations
2641 * @param view The view being animated
2642 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2643 * @param scaleFactor The scale factor used for the zoom
2644 */
2645 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2646 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002647
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002648 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002649 // Set pivotY so that at the starting zoom factor, the view is partially
2650 // visible. Modifying initialHeightFactor changes how much of the view is
2651 // initially showing, and hence the perceived angle from which the view enters.
Winson Chung785d2eb2011-04-14 16:08:02 -07002652 if (state == State.ALL_APPS || state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002653 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002654 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002655 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002656 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002657 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002658 }
2659 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002660
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002661 /**
2662 * Zoom the camera out from the workspace to reveal 'toView'.
2663 * Assumes that the view to show is anchored at either the very top or very bottom
2664 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002665 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002666 */
Winson Chung20f71112011-04-06 14:22:04 -07002667 private void cameraZoomOut(State toState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002668 final Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002669 final boolean toAllApps = (toState == State.ALL_APPS)
2670 || (toState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002671
Winson Chung785d2eb2011-04-14 16:08:02 -07002672 final int duration = (toAllApps ?
2673 res.getInteger(R.integer.config_appsCustomizeZoomInTime) :
2674 res.getInteger(R.integer.config_customizeZoomInTime));
2675 final int fadeDuration = (toAllApps ?
2676 res.getInteger(R.integer.config_appsCustomizeFadeInTime) :
2677 res.getInteger(R.integer.config_customizeFadeInTime));
Adam Cohenf16e5712011-01-13 13:31:45 -08002678
2679 final float scale = toAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002680 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002681 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2682
Winson Chung785d2eb2011-04-14 16:08:02 -07002683 View tmpView;
2684 if (toAllApps) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002685 tmpView = (LauncherApplication.isScreenLarge())
Winson Chung785d2eb2011-04-14 16:08:02 -07002686 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2687 } else {
2688 tmpView = mHomeCustomizationDrawer;
2689 }
2690 final View toView = tmpView;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002691
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002692 setPivotsForZoom(toView, toState, scale);
2693
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002694 if (toAllApps) {
Winson Chung20f71112011-04-06 14:22:04 -07002695 if (!springLoaded) {
2696 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2697
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002698 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002699 // Everytime we launch into AllApps, we reset the successful drop flag which
2700 // controls when it should hide/show the mini workspaces
2701 mAllAppsPagedView.resetSuccessfulDropFlag();
2702 }
Winson Chung20f71112011-04-06 14:22:04 -07002703 } else {
2704 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
2705 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002706 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002707 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002708 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002709
2710 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002711 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002712 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002713 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2714 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002715 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002716 toView.setFastScaleX(a * scale + b * 1f);
2717 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002718 }
2719 });
Adam Cohen61033d32010-11-15 18:29:44 -08002720
Adam Cohenf16e5712011-01-13 13:31:45 -08002721 if (toAllApps) {
Winson Chung4e6a9762011-05-09 11:56:34 -07002722 toView.setVisibility(View.VISIBLE);
Michael Jurka2763be32011-02-24 11:19:57 -08002723 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002724 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002725 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002726 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2727 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002728 // don't need to invalidate because we do so above
2729 toView.setFastAlpha(a * 0f + b * 1f);
2730 }
2731 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002732 alphaAnim.start();
2733 }
2734
Michael Jurkaabded662011-03-04 12:06:57 -08002735 if (toView instanceof LauncherTransitionable) {
2736 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002737 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002738 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002739 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002740 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002741 // Prepare the position
2742 toView.setTranslationX(0.0f);
2743 toView.setTranslationY(0.0f);
2744 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002745 toView.bringToFront();
Adam Cohenf16e5712011-01-13 13:31:45 -08002746 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002747 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002748 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002749 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002750 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002751 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002752 // If we don't set the final scale values here, if this animation is cancelled
2753 // it will have the wrong scale value and subsequent cameraPan animations will
2754 // not fix that
2755 toView.setScaleX(1.0f);
2756 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002757 if (toView instanceof LauncherTransitionable) {
2758 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002759 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002760 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002761 });
2762
Chet Haaseb1254a62010-09-07 13:35:00 -07002763 AnimatorSet toolbarHideAnim = new AnimatorSet();
2764 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002765 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2766
2767 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002768 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002769
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002770 if (mStateAnimation != null) mStateAnimation.cancel();
2771 mStateAnimation = new AnimatorSet();
2772 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2773 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002774
2775 // Show the new toolbar buttons just as the main animation is ending
2776 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002777 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2778 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002779 } else {
2780 toView.setTranslationX(0.0f);
2781 toView.setTranslationY(0.0f);
2782 toView.setScaleX(1.0f);
2783 toView.setScaleY(1.0f);
2784 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002785 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002786 if (toView instanceof LauncherTransitionable) {
2787 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2788 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2789 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002790 hideAndShowToolbarButtons(toState, null, null);
2791 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002792 }
2793
2794 /**
2795 * Zoom the camera back into the workspace, hiding 'fromView'.
2796 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002797 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2798 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002799 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08002800 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002801 Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002802 final boolean fromAllApps = (fromState == State.ALL_APPS)
2803 || (fromState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002804
2805 int duration = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002806 res.getInteger(R.integer.config_appsCustomizeZoomOutTime) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002807 res.getInteger(R.integer.config_customizeZoomOutTime);
2808
Michael Jurka742574b2011-02-02 23:51:01 -08002809 final float scaleFactor = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002810 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002811 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2812
Winson Chung785d2eb2011-04-14 16:08:02 -07002813 View tmpView;
2814 if (fromAllApps) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002815 tmpView = (LauncherApplication.isScreenLarge())
Winson Chung785d2eb2011-04-14 16:08:02 -07002816 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2817 } else {
2818 tmpView = mHomeCustomizationDrawer;
2819 }
2820 final View fromView = tmpView;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002821
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002822 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002823 mCustomizePagedView.endChoiceMode();
2824 mAllAppsPagedView.endChoiceMode();
2825 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002826
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002827 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002828
Michael Jurkad3ef3062010-11-23 16:23:58 -08002829 if (!springLoaded) {
2830 mWorkspace.unshrink(animated);
2831 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002832 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002833 if (mStateAnimation != null) mStateAnimation.cancel();
2834 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002835
Michael Jurka742574b2011-02-02 23:51:01 -08002836 final float oldScaleX = fromView.getScaleX();
2837 final float oldScaleY = fromView.getScaleY();
2838
2839 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2840 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002841 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2842 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002843 ((View)fromView.getParent()).fastInvalidate();
2844 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2845 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2846 }
2847 });
Michael Jurkaabded662011-03-04 12:06:57 -08002848 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002849 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002850 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002851 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2852 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002853 // don't need to invalidate because we do so above
2854 fromView.setFastAlpha(a * 1f + b * 0f);
2855 }
2856 });
Michael Jurkaabded662011-03-04 12:06:57 -08002857 if (fromView instanceof LauncherTransitionable) {
2858 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002859 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002860 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002861 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002862 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002863 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002864 if (fromView instanceof LauncherTransitionable) {
2865 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002866 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002867 }
2868 });
2869
Chet Haaseb1254a62010-09-07 13:35:00 -07002870 AnimatorSet toolbarHideAnim = new AnimatorSet();
2871 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002872 if (!springLoaded) {
2873 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2874 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002875
Adam Cohen61033d32010-11-15 18:29:44 -08002876 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002877
2878 // Show the new toolbar buttons at the very end of the whole animation
2879 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2880 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002881 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2882 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002883 } else {
2884 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002885 if (fromView instanceof LauncherTransitionable) {
2886 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2887 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2888 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002889 if (!springLoaded) {
2890 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2891 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002892 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002893 }
2894
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002895 void showWorkspace(boolean animated) {
2896 showWorkspace(animated, null);
2897 }
2898
2899 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002900 if (layout != null) {
2901 // always animated, but that's ok since we never specify a layout and
2902 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002903 mWorkspace.unshrink(layout);
2904 } else {
2905 mWorkspace.unshrink(animated);
2906 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002907 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002908 closeAllApps(animated);
2909 } else if (mState == State.CUSTOMIZE) {
2910 hideCustomizationDrawer(animated);
2911 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002912
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002913 // Change the state *after* we've called all the transition code
2914 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002915
Winson Chung5fb63472011-02-02 17:03:37 -08002916 // Resume the auto-advance of widgets
2917 mUserPresent = true;
2918 updateRunning();
2919
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002920 // send an accessibility event to announce the context change
2921 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002922 }
2923
Michael Jurkad3ef3062010-11-23 16:23:58 -08002924 void enterSpringLoadedDragMode(CellLayout layout) {
2925 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07002926 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002927 mState = State.ALL_APPS_SPRING_LOADED;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002928 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002929 cameraZoomIn(State.ALL_APPS, true, true);
2930 } else {
2931 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
2932 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002933 } else if (mState == State.CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002934 mState = State.CUSTOMIZE_SPRING_LOADED;
Winson Chung20f71112011-04-06 14:22:04 -07002935 cameraZoomIn(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002936 }/* else {
2937 // we're already in spring loaded mode; don't do anything
2938 }*/
2939 }
2940
2941 void exitSpringLoadedDragMode() {
2942 if (mState == State.ALL_APPS_SPRING_LOADED) {
2943 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002944 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002945 cameraZoomOut(State.ALL_APPS, true, true);
2946 mState = State.ALL_APPS;
2947 } else {
2948 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2949 mState = State.APPS_CUSTOMIZE;
2950 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002951 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2952 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
Winson Chung20f71112011-04-06 14:22:04 -07002953 cameraZoomOut(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002954 mState = State.CUSTOMIZE;
2955 }/* else {
2956 // we're not in spring loaded mode; don't do anything
2957 }*/
2958 }
2959
Winson Chung785d2eb2011-04-14 16:08:02 -07002960 void showAllApps(boolean animated) {
2961 if (mState != State.WORKSPACE) return;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002962 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002963 cameraZoomOut(State.ALL_APPS, animated, false);
2964 ((View) mAllAppsGrid).requestFocus();
2965
2966 // TODO: fade these two too
2967 mDeleteZone.setVisibility(View.GONE);
2968
2969 // Change the state *after* we've called all the transition code
2970 mState = State.ALL_APPS;
2971 } else {
2972 View appsCustomizePane = findViewById(R.id.apps_customize_pane);
2973 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2974 appsCustomizePane.requestFocus();
2975
2976 // Change the state *after* we've called all the transition code
2977 mState = State.APPS_CUSTOMIZE;
2978 }
2979
2980 // Pause the auto-advance of widgets until we are out of AllApps
2981 mUserPresent = false;
2982 updateRunning();
2983
2984 // Send an accessibility event to announce the context change
2985 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2986 }
2987
Joe Onoratob2061212009-11-24 16:13:54 -05002988 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002989 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002990 * - Home from workspace
2991 * - from center screen
2992 * - from other screens
2993 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002994 * - from center screen
2995 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002996 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002997 * - from center screen
2998 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002999 * - Launch app from workspace and quit
3000 * - with back
3001 * - with home
3002 * - Launch app from all apps and quit
3003 * - with back
3004 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003005 * - Go to a screen that's not the default, then all
3006 * apps, and launch and app, and go back
3007 * - with back
3008 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003009 * - On workspace, long press power and go back
3010 * - with back
3011 * - with home
3012 * - On all apps, long press power and go back
3013 * - with back
3014 * - with home
3015 * - On workspace, power off
3016 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003017 * - Launch an app and turn off the screen while in that app
3018 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003019 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003020 * - From all apps
3021 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003022 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3023 * - From all apps
3024 * - From the center workspace
3025 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003026 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003027 void closeAllApps(boolean animated) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07003028 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003029 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
3030 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung20f71112011-04-06 14:22:04 -07003031 cameraZoomIn(State.ALL_APPS, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003032
3033 // Set focus to the AllApps button
3034 findViewById(R.id.all_apps_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003035 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003036 } else {
3037 if (mState == State.APPS_CUSTOMIZE || mState == State.ALL_APPS_SPRING_LOADED) {
3038 mWorkspace.setVisibility(View.VISIBLE);
3039 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
3040
3041 // Set focus to the AllApps button
3042 findViewById(R.id.all_apps_button).requestFocus();
3043 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003044 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003045 }
3046
Joe Onorato7c312c12009-08-13 21:36:53 -07003047 void lockAllApps() {
3048 // TODO
3049 }
3050
3051 void unlockAllApps() {
3052 // TODO
3053 }
3054
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003055 // Show the customization drawer (only exists in x-large configuration)
3056 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003057 if (mState != State.WORKSPACE) {
3058 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003059 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003060
Winson Chung20f71112011-04-06 14:22:04 -07003061 cameraZoomOut(State.CUSTOMIZE, animated, false);
Michael Jurkaafca9532011-02-16 14:50:35 -08003062
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003063 // Change the state *after* we've called all the transition code
3064 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003065
3066 // Pause the auto-advance of widgets until we are out of Customization drawer
3067 mUserPresent = false;
3068 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003069 }
3070
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003071 // Hide the customization drawer (only exists in x-large configuration)
3072 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003073 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Winson Chung20f71112011-04-06 14:22:04 -07003074 cameraZoomIn(State.CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003075
3076 // Set focus to the customize button
3077 findViewById(R.id.configure_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003078 }
3079 }
3080
Patrick Dubroy5f445422011-02-18 14:35:21 -08003081 /**
3082 * Add an item from all apps or customize onto the given workspace screen.
3083 * If layout is null, add to the current screen.
3084 */
3085 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003086 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3087 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003088 } else {
3089 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003090 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003091 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003092
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003093 void onWorkspaceClick(CellLayout layout) {
3094 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003095 }
3096
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003097 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003098 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003099 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003100 // Look for the toolbar icon specified in the activity meta-data
3101 Bundle metaData = packageManager.getActivityInfo(
3102 activityName, PackageManager.GET_META_DATA).metaData;
3103 if (metaData != null) {
3104 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3105 if (iconResId != 0) {
3106 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003107 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003108 }
3109 }
3110 } catch (NameNotFoundException e) {
3111 // Do nothing
3112 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003113 return null;
3114 }
3115
3116 // if successful in getting icon, return it; otherwise, set button to use default drawable
3117 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3118 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3119 TextView button = (TextView) findViewById(buttonId);
3120 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3121
3122 // If we were unable to find the icon via the meta-data, use a generic one
3123 if (toolbarIcon == null) {
3124 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3125 return null;
3126 } else {
3127 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3128 return toolbarIcon.getConstantState();
3129 }
3130 }
3131
3132 // if successful in getting icon, return it; otherwise, set button to use default drawable
3133 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3134 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3135 ImageView button = (ImageView) findViewById(buttonId);
3136 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3137
Michael Jurka0423dcf2010-10-05 14:56:18 -07003138 // If we were unable to find the icon via the meta-data, use a generic one
3139 if (toolbarIcon == null) {
3140 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003141 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003142 } else {
3143 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003144 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003145 }
3146 }
3147
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003148 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3149 TextView button = (TextView) findViewById(buttonId);
3150 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3151 }
3152
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003153 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003154 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003155 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003156 }
3157
Michael Jurka0423dcf2010-10-05 14:56:18 -07003158 private void updateGlobalSearchIcon() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07003159 if (LauncherApplication.isScreenLarge()) {
Winson Chung97d85d22011-04-13 11:27:36 -07003160 final View searchButton = findViewById(R.id.search_button);
3161 final View searchDivider = findViewById(R.id.search_divider);
3162
Michael Jurka0423dcf2010-10-05 14:56:18 -07003163 final SearchManager searchManager =
3164 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3165 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003166 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003167 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003168 R.id.search_button, activityName, R.drawable.ic_generic_search);
Winson Chung97d85d22011-04-13 11:27:36 -07003169 searchButton.setVisibility(View.VISIBLE);
3170 searchDivider.setVisibility(View.VISIBLE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003171 } else {
Winson Chung97d85d22011-04-13 11:27:36 -07003172 searchButton.setVisibility(View.GONE);
3173 searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003174 }
3175 }
3176 }
3177
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003178 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003179 updateButtonWithDrawable(R.id.search_button, d);
3180 }
3181
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003182 private void updateVoiceSearchIcon() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07003183 if (LauncherApplication.isScreenLarge()) {
Winson Chung97d85d22011-04-13 11:27:36 -07003184 final View searchDivider = findViewById(R.id.search_divider);
3185 final View voiceButton = findViewById(R.id.voice_button);
3186
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003187 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3188 ComponentName activityName = intent.resolveActivity(getPackageManager());
3189 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003190 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003191 R.id.voice_button, activityName, R.drawable.ic_voice_search);
Winson Chung97d85d22011-04-13 11:27:36 -07003192 searchDivider.setVisibility(View.VISIBLE);
3193 voiceButton.setVisibility(View.VISIBLE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003194 } else {
Winson Chung97d85d22011-04-13 11:27:36 -07003195 searchDivider.setVisibility(View.GONE);
3196 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003197 }
3198 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003199 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003200
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003201 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003202 updateButtonWithDrawable(R.id.voice_button, d);
3203 }
3204
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003205 /**
3206 * Sets the app market icon (shown when all apps is visible on x-large screens)
3207 */
3208 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003209 final View marketButton = findViewById(R.id.market_button);
3210 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3211 // Find the app market activity by resolving an intent.
3212 // (If multiple app markets are installed, it will return the ResolverActivity.)
3213 ComponentName activityName = intent.resolveActivity(getPackageManager());
3214 if (activityName != null) {
3215 mAppMarketIntent = intent;
3216 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
3217 R.id.market_button, activityName, R.drawable.app_market_generic);
3218 marketButton.setVisibility(View.VISIBLE);
3219 } else {
3220 // We should hide and disable the view so that we don't try and restore the visibility
3221 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3222 marketButton.setVisibility(View.GONE);
3223 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003224 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003225 }
3226
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003227 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003228 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003229 }
3230
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003231 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003232 * Displays the shortcut creation dialog and launches, if necessary, the
3233 * appropriate activity.
3234 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003235 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003236 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3237 DialogInterface.OnShowListener {
3238
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003239 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003241 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003242 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3245 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003246 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003248 builder.setInverseBackgroundForced(true);
3249
3250 AlertDialog dialog = builder.create();
3251 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003252 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003253 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003255 return dialog;
3256 }
3257
3258 public void onCancel(DialogInterface dialog) {
3259 mWaitingForResult = false;
3260 cleanup();
3261 }
3262
Romain Guycbb89e42009-06-08 15:52:54 -07003263 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003264 }
3265
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003266 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003267 try {
3268 dismissDialog(DIALOG_CREATE_SHORTCUT);
3269 } catch (Exception e) {
3270 // An exception is thrown if the dialog is not visible, which is fine
3271 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003272 }
3273
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003274 /**
3275 * Handle the action clicked in the "Add to home" dialog.
3276 */
3277 public void onClick(DialogInterface dialog, int which) {
3278 Resources res = getResources();
3279 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003280
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003281 switch (which) {
3282 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003283 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003284 break;
3285 }
Romain Guycbb89e42009-06-08 15:52:54 -07003286
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003287 case AddAdapter.ITEM_APPWIDGET: {
3288 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003289
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003290 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3291 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003292 // start the pick activity
3293 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3294 break;
3295 }
Romain Guycbb89e42009-06-08 15:52:54 -07003296
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003297 case AddAdapter.ITEM_WALLPAPER: {
3298 startWallpaper();
3299 break;
3300 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003301 }
3302 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003303
3304 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003305 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003306 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003307 }
3308
3309 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003310 * Receives notifications when applications are added/removed.
3311 */
3312 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3313 @Override
3314 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003315 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003316 String reason = intent.getStringExtra("reason");
3317 if (!"homekey".equals(reason)) {
3318 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003319 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003320 animate = false;
3321 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003322 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003323 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003324 }
3325 }
3326
3327 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003328 * Receives notifications whenever the appwidgets are reset.
3329 */
3330 private class AppWidgetResetObserver extends ContentObserver {
3331 public AppWidgetResetObserver() {
3332 super(new Handler());
3333 }
3334
3335 @Override
3336 public void onChange(boolean selfChange) {
3337 onAppWidgetReset();
3338 }
3339 }
3340
3341 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003342 * If the activity is currently paused, signal that we need to re-run the loader
3343 * in onResume.
3344 *
3345 * This needs to be called from incoming places where resources might have been loaded
3346 * while we are paused. That is becaues the Configuration might be wrong
3347 * when we're not running, and if it comes back to what it was when we
3348 * were paused, we are not restarted.
3349 *
3350 * Implementation of the method from LauncherModel.Callbacks.
3351 *
3352 * @return true if we are currently paused. The caller might be able to
3353 * skip some work in that case since we will come back again.
3354 */
3355 public boolean setLoadOnResume() {
3356 if (mPaused) {
3357 Log.i(TAG, "setLoadOnResume");
3358 mOnResumeNeedsLoad = true;
3359 return true;
3360 } else {
3361 return false;
3362 }
3363 }
3364
3365 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003366 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003367 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003368 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003369 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003370 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003371 } else {
3372 return SCREEN_COUNT / 2;
3373 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003374 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003375
Winson Chunga12a2502010-12-20 14:41:35 -08003376 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003377 mAllAppsPagedView = view;
3378 }
3379
Joe Onorato9c1289c2009-08-17 11:03:03 -04003380 /**
3381 * Refreshes the shortcuts shown on the workspace.
3382 *
3383 * Implementation of the method from LauncherModel.Callbacks.
3384 */
3385 public void startBinding() {
3386 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003387
3388 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003389 int count = workspace.getChildCount();
3390 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003391 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003392 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3393 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003394 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003395
Joe Onorato9c1289c2009-08-17 11:03:03 -04003396 if (DEBUG_USER_INTERFACE) {
3397 android.widget.Button finishButton = new android.widget.Button(this);
3398 finishButton.setText("Finish");
3399 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3400
3401 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3402 public void onClick(View v) {
3403 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003404 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003405 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003406 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003407
3408 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3409 // mDesktopItems -> AppWidgetInfo -> hostView).
3410 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003411 }
3412
3413 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003414 * Bind the items start-end from the list.
3415 *
3416 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003417 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003418 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3419
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003420 setLoadOnResume();
3421
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 final Workspace workspace = mWorkspace;
3423
3424 for (int i=start; i<end; i++) {
3425 final ItemInfo item = shortcuts.get(i);
3426 mDesktopItems.add(item);
3427 switch (item.itemType) {
3428 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3429 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003430 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003431 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3432 false);
3433 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003434 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003435 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003436 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003437 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003438 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3439 false);
3440 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003441 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003442 }
3443
Joe Onorato9c1289c2009-08-17 11:03:03 -04003444 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003445 }
3446
Joe Onorato9c1289c2009-08-17 11:03:03 -04003447 /**
3448 * Implementation of the method from LauncherModel.Callbacks.
3449 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003450 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003451 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003452 sFolders.clear();
3453 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003454 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003455
3456 /**
3457 * Add the views for a widget to the workspace.
3458 *
3459 * Implementation of the method from LauncherModel.Callbacks.
3460 */
3461 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003462 setLoadOnResume();
3463
Daniel Sandler843e8602010-06-07 14:59:01 -04003464 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3465 if (DEBUG_WIDGETS) {
3466 Log.d(TAG, "bindAppWidget: " + item);
3467 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003468 final Workspace workspace = mWorkspace;
3469
3470 final int appWidgetId = item.appWidgetId;
3471 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003472 if (DEBUG_WIDGETS) {
3473 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3474 }
3475
Joe Onorato9c1289c2009-08-17 11:03:03 -04003476 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3477
Joe Onorato9c1289c2009-08-17 11:03:03 -04003478 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3479 item.hostView.setTag(item);
3480
3481 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3482 item.cellY, item.spanX, item.spanY, false);
3483
Adam Cohended9f8d2010-11-03 13:25:16 -07003484 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3485
Joe Onorato9c1289c2009-08-17 11:03:03 -04003486 workspace.requestLayout();
3487
3488 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003489
3490 if (DEBUG_WIDGETS) {
3491 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3492 + (SystemClock.uptimeMillis()-start) + "ms");
3493 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003494 }
3495
3496 /**
3497 * Callback saying that there aren't any more items to bind.
3498 *
3499 * Implementation of the method from LauncherModel.Callbacks.
3500 */
3501 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003502 setLoadOnResume();
3503
Joe Onorato9c1289c2009-08-17 11:03:03 -04003504 if (mSavedState != null) {
3505 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003506 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003507 }
3508
Adam Cohendf2cc412011-04-27 16:56:57 -07003509 final long[] folders = mSavedState.getLongArray(RUNTIME_STATE_FOLDERS);
3510 if (folders != null) {
3511 for (long folderId : folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003512 final FolderInfo info = sFolders.get(folderId);
Adam Cohena9cf38f2011-05-02 15:36:58 -07003513 final FolderIcon folderIcon = (FolderIcon)
3514 mWorkspace.getViewForTag(info);
3515 if (folderIcon != null) {
3516 openFolder(folderIcon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003517 }
3518 }
3519 final Folder openFolder = mWorkspace.getOpenFolder();
3520 if (openFolder != null) {
3521 openFolder.requestFocus();
3522 }
3523 }
3524
Joe Onorato9c1289c2009-08-17 11:03:03 -04003525 mSavedState = null;
3526 }
3527
3528 if (mSavedInstanceState != null) {
3529 super.onRestoreInstanceState(mSavedInstanceState);
3530 mSavedInstanceState = null;
3531 }
3532
Winson Chung32bb5e52011-01-31 14:51:56 -08003533 // Workaround a bug that occurs when rotating the device while the customization mode is
3534 // open, we trigger a new layout on all the CellLayout children.
Michael Jurkaa2eb1702011-05-12 14:57:05 -07003535 if (LauncherApplication.isScreenLarge() && (mState == State.CUSTOMIZE)) {
Winson Chung32bb5e52011-01-31 14:51:56 -08003536 final int childCount = mWorkspace.getChildCount();
3537 for (int i = 0; i < childCount; ++i) {
3538 mWorkspace.getChildAt(i).requestLayout();
3539 }
3540 }
3541
Joe Onorato9c1289c2009-08-17 11:03:03 -04003542 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003543
3544 // If we received the result of any pending adds while the loader was running (e.g. the
3545 // widget configuration forced an orientation change), process them now.
3546 for (int i = 0; i < sPendingAddList.size(); i++) {
3547 completeAdd(sPendingAddList.get(i));
3548 }
3549 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003550 }
3551
3552 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003553 * Updates the icons on the launcher that are affected by changes to the package list
3554 * on the device.
3555 */
3556 private void updateIconsAffectedByPackageManagerChanges() {
3557 updateAppMarketIcon();
3558 updateGlobalSearchIcon();
3559 updateVoiceSearchIcon();
3560 }
3561
3562 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003563 * Add the icons for all apps.
3564 *
3565 * Implementation of the method from LauncherModel.Callbacks.
3566 */
3567 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003568 if (mAllAppsGrid != null) {
3569 mAllAppsGrid.setApps(apps);
3570 }
3571 if (mAppsCustomizeContent != null) {
3572 mAppsCustomizeContent.setApps(apps);
3573 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003574 if (mCustomizePagedView != null) {
3575 mCustomizePagedView.setApps(apps);
3576 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003577 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003578 }
3579
3580 /**
3581 * A package was installed.
3582 *
3583 * Implementation of the method from LauncherModel.Callbacks.
3584 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003585 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003586 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003587 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chung785d2eb2011-04-14 16:08:02 -07003588 if (mAllAppsGrid != null) {
3589 mAllAppsGrid.addApps(apps);
3590 }
3591 if (mAppsCustomizeContent != null) {
3592 mAppsCustomizeContent.addApps(apps);
3593 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003594 if (mCustomizePagedView != null) {
3595 mCustomizePagedView.addApps(apps);
3596 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003597 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598 }
3599
3600 /**
3601 * A package was updated.
3602 *
3603 * Implementation of the method from LauncherModel.Callbacks.
3604 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003605 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003606 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003607 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003608 if (mWorkspace != null) {
3609 mWorkspace.updateShortcuts(apps);
3610 }
3611 if (mAllAppsGrid != null) {
3612 mAllAppsGrid.updateApps(apps);
3613 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003614 if (mAppsCustomizeContent != null) {
3615 mAppsCustomizeContent.updateApps(apps);
3616 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003617 if (mCustomizePagedView != null) {
3618 mCustomizePagedView.updateApps(apps);
3619 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003620 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003621 }
3622
3623 /**
3624 * A package was uninstalled.
3625 *
3626 * Implementation of the method from LauncherModel.Callbacks.
3627 */
Joe Onorato36115782010-06-17 13:28:48 -04003628 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003629 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003630 if (permanent) {
3631 mWorkspace.removeItems(apps);
3632 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003633 if (mAllAppsGrid != null) {
3634 mAllAppsGrid.removeApps(apps);
3635 }
3636 if (mAppsCustomizeContent != null) {
3637 mAppsCustomizeContent.removeApps(apps);
3638 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003639 if (mCustomizePagedView != null) {
3640 mCustomizePagedView.removeApps(apps);
3641 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003642 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003643 }
Joe Onoratobe386092009-11-17 17:32:16 -08003644
3645 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003646 * A number of packages were updated.
3647 */
3648 public void bindPackagesUpdated() {
3649 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003650 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003651 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003652 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003653 if (mAppsCustomizeContent != null) {
3654 mAppsCustomizeContent.onPackagesUpdated();
3655 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003656 }
3657
Winson Chung400438b2011-01-16 17:53:48 -08003658 private int mapConfigurationOriActivityInfoOri(int configOri) {
3659 final Display d = getWindowManager().getDefaultDisplay();
3660 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3661 switch (d.getRotation()) {
3662 case Surface.ROTATION_0:
3663 case Surface.ROTATION_180:
3664 // We are currently in the same basic orientation as the natural orientation
3665 naturalOri = configOri;
3666 break;
3667 case Surface.ROTATION_90:
3668 case Surface.ROTATION_270:
3669 // We are currently in the other basic orientation to the natural orientation
3670 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3671 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3672 break;
3673 }
3674
3675 int[] oriMap = {
3676 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3677 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3678 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3679 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3680 };
3681 // Since the map starts at portrait, we need to offset if this device's natural orientation
3682 // is landscape.
3683 int indexOffset = 0;
3684 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3685 indexOffset = 1;
3686 }
3687 return oriMap[(d.getRotation() + indexOffset) % 4];
3688 }
3689 public void lockScreenOrientation() {
3690 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3691 .getConfiguration().orientation));
3692 }
3693 public void unlockScreenOrientation() {
3694 mHandler.postDelayed(new Runnable() {
3695 public void run() {
3696 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3697 }
3698 }, mRestoreScreenOrientationDelay);
3699 }
3700
Winson Chung80baf5a2010-08-09 16:03:15 -07003701 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003702 * Prints out out state for debugging.
3703 */
3704 public void dumpState() {
3705 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003706 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003707 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3708 Log.d(TAG, "mRestoring=" + mRestoring);
3709 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3710 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3711 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003712 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003713 mModel.dumpState();
Winson Chung785d2eb2011-04-14 16:08:02 -07003714 if (mAllAppsGrid != null) {
3715 mAllAppsGrid.dumpState();
3716 }
3717 if (mAppsCustomizeContent != null) {
3718 mAppsCustomizeContent.dumpState();
3719 }
Joe Onoratobe386092009-11-17 17:32:16 -08003720 Log.d(TAG, "END launcher2 dump state");
3721 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003722}
Michael Jurka2763be32011-02-24 11:19:57 -08003723
Michael Jurkaabded662011-03-04 12:06:57 -08003724interface LauncherTransitionable {
3725 void onLauncherTransitionStart(Animator animation);
3726 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003727}