blob: 52263cf38c97e3add0cb814587a19ed067814b7d [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka742574b2011-02-02 23:51:01 -080020import com.android.common.Search;
21import com.android.launcher.R;
Michael Jurka742574b2011-02-02 23:51:01 -080022import com.android.launcher2.Workspace.ShrinkState;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070023
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080025import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.AnimatorSet;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.Dialog;
32import android.app.SearchManager;
33import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070038import android.content.ClipData;
39import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040040import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080041import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Context;
43import android.content.DialogInterface;
44import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080045import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080046import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080049import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080050import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070052import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040053import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080054import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080056import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070065import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040067import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080068import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070069import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070070import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080071import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.Selection;
73import android.text.SpannableStringBuilder;
74import android.text.TextUtils;
75import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070076import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080077import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080078import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.KeyEvent;
80import android.view.LayoutInflater;
81import android.view.Menu;
82import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070083import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080084import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.view.View;
86import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070087import android.view.WindowManager;
Michael Jurkae0f5a612011-02-07 16:45:41 -080088import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080089import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080091import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070093import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070095import android.widget.ImageView;
96import android.widget.LinearLayout;
97import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070098import android.widget.TabHost;
Winson Chung68846fd2010-10-29 11:00:27 -070099import android.widget.TextView;
100import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700101
Michael Jurka742574b2011-02-02 23:51:01 -0800102import java.io.DataInputStream;
103import java.io.DataOutputStream;
104import java.io.FileNotFoundException;
105import java.io.IOException;
106import java.util.ArrayList;
107import java.util.HashMap;
108import java.util.List;
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;
134 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700135 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 private static final int REQUEST_PICK_APPLICATION = 6;
137 private static final int REQUEST_PICK_SHORTCUT = 7;
138 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
142 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
143
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800144 static final int SCREEN_COUNT = 5;
145 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Joe Onorato7c312c12009-08-13 21:36:53 -0700147 static final int DIALOG_CREATE_SHORTCUT = 1;
148 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Romain Guy98d01652009-06-30 16:21:04 -0700150 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
152 // Type: int
153 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700154 // Type: int
155 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: long
157 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
158 // Type: int
159 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
160 // Type: int
161 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
162 // Type: int
163 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 // Type: boolean
165 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
166 // Type: long
167 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
168
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700169 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
170
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700171 /** The different states that Launcher can be in. */
Winson Chung785d2eb2011-04-14 16:08:02 -0700172 private enum State { WORKSPACE, APPS_CUSTOMIZE, ALL_APPS, CUSTOMIZE,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800173 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700174 private State mState = State.WORKSPACE;
175 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700176
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800180 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800182 private final BroadcastReceiver mCloseSystemDialogsReceiver
183 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800184 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private LayoutInflater mInflater;
187
Joe Onorato00acb122009-08-04 16:04:30 -0400188 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700190
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700191 private AppWidgetManager mAppWidgetManager;
192 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700193
Michael Jurka0280c3b2010-09-17 15:00:07 -0700194 private int mAddScreen = -1;
195 private int mAddIntersectCellX = -1;
196 private int mAddIntersectCellY = -1;
197 private int[] mAddDropPosition;
198 private int[] mTmpAddItemCellCoordinates = new int[2];
199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private FolderInfo mFolderInfo;
201
Joe Onorato7c312c12009-08-13 21:36:53 -0700202 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700204 private AllAppsView mAllAppsGrid;
Winson Chung785d2eb2011-04-14 16:08:02 -0700205 private AppsCustomizeTabHost mAppsCustomizeTabHost;
206 private AppsCustomizePagedView mAppsCustomizeContent;
Michael Jurka7ef959b2011-02-23 11:48:32 -0800207 private CustomizeTrayTabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700208 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700209
Winson Chung97d85d22011-04-13 11:27:36 -0700210 private ViewGroup mButtonCluster;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800211 private View mAllAppsButton;
212 private View mDivider;
213 private View mConfigureButton;
214
Winson Chunga12a2502010-12-20 14:41:35 -0800215 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700216 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 private Bundle mSavedState;
219
220 private SpannableStringBuilder mDefaultKeySsb = null;
221
Joe Onorato9c1289c2009-08-17 11:03:03 -0400222 private boolean mWorkspaceLoading = true;
223
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800224 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 private boolean mRestoring;
226 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700227 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228
229 private Bundle mSavedInstanceState;
230
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800232 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800233 private boolean mUserPresent = true;
234 private boolean mVisible = false;
235 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700237 private static LocaleConfiguration sLocaleConfiguration = null;
238
Romain Guy84f296c2009-11-04 15:00:44 -0800239 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700240
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700241 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700242
Patrick Dubroy94f78a52011-02-28 17:39:16 -0800243 // The "signpost" images along the bottom of the screen (only in some layouts)
Romain Guy1fbc1c82009-11-09 20:43:08 -0800244 private ImageView mPreviousView;
245 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500246
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400247 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400248 private String[] mHotseatConfig = null;
249 private Intent[] mHotseats = null;
250 private Drawable[] mHotseatIcons = null;
251 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400252
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700253 private Intent mAppMarketIntent = null;
254
Adam Cohended9f8d2010-11-03 13:25:16 -0700255 // Related to the auto-advancing of widgets
256 private final int ADVANCE_MSG = 1;
257 private final int mAdvanceInterval = 20000;
258 private final int mAdvanceStagger = 250;
259 private long mAutoAdvanceSentTime;
260 private long mAutoAdvanceTimeLeft = -1;
261 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
262 new HashMap<View, AppWidgetProviderInfo>();
263
Winson Chung400438b2011-01-16 17:53:48 -0800264 // Determines how long to wait after a rotation before restoring the screen orientation to
265 // match the sensor state.
266 private final int mRestoreScreenOrientationDelay = 500;
267
Michael Jurka4ef207b2010-11-29 17:05:45 -0800268 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800269 private static Drawable.ConstantState sGlobalSearchIcon;
270 private static Drawable.ConstantState sVoiceSearchIcon;
271 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800272
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
353 if (LauncherApplication.isScreenXLarge()) {
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;
639 case REQUEST_PICK_LIVE_FOLDER:
640 addLiveFolder(args.intent);
641 break;
642 case REQUEST_CREATE_LIVE_FOLDER:
643 completeAddLiveFolder(args.intent, args.screen, args.cellX, args.cellY);
644 break;
645 case REQUEST_PICK_APPWIDGET:
646 addAppWidgetFromPick(args.intent);
647 break;
648 case REQUEST_CREATE_APPWIDGET:
649 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
650 completeAddAppWidget(appWidgetId, args.screen);
651 break;
652 case REQUEST_PICK_WALLPAPER:
653 // We just wanted the activity result here so we can clear mWaitingForResult
654 break;
655 }
656 }
657
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800659 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700660 mWaitingForResult = false;
661
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 // The pattern used here is that a user PICKs a specific application,
663 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700664
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
666 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700667
Michael Jurka0280c3b2010-09-17 15:00:07 -0700668 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800669 final PendingAddArguments args = new PendingAddArguments();
670 args.requestCode = requestCode;
671 args.intent = data;
672 args.screen = mAddScreen;
673 args.cellX = mAddIntersectCellX;
674 args.cellY = mAddIntersectCellY;
675
676 // If the loader is still running, defer the add until it is done.
677 if (isWorkspaceLocked()) {
678 sPendingAddList.add(args);
679 } else {
680 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
Romain Guy18042c82009-11-06 11:44:55 -0800682 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
683 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
684 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700685 // Clean up the appWidgetId if we canceled
686 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
687 if (appWidgetId != -1) {
688 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 }
690 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 }
692
693 @Override
694 protected void onResume() {
695 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800696 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700697 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400698 mWorkspaceLoading = true;
699 mModel.startLoader(this, true);
700 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700701 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800703 if (mWaitingForResume != null) {
704 mWaitingForResume.setStayPressed(false);
705 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700706 // When we resume Launcher, a different Activity might be responsible for the app
707 // market intent, so refresh the icon
708 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 }
710
711 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700712 protected void onPause() {
713 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700714 // Some launcher layouts don't have a previous and next view
715 if (mPreviousView != null) {
716 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700717 }
718 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700719 dismissPreview(mNextView);
720 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700721 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800722 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700723 }
Romain Guycbb89e42009-06-08 15:52:54 -0700724
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700725 @Override
726 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400727 // Flag the loader to stop early before switching
728 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700729 if (mAllAppsGrid != null) {
730 mAllAppsGrid.surrender();
731 }
732 if (mAppsCustomizeContent != null) {
733 mAppsCustomizeContent.surrender();
734 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800735 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700736 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700737
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800738 // We can't hide the IME if it was forced open. So don't bother
739 /*
740 @Override
741 public void onWindowFocusChanged(boolean hasFocus) {
742 super.onWindowFocusChanged(hasFocus);
743
744 if (hasFocus) {
745 final InputMethodManager inputManager = (InputMethodManager)
746 getSystemService(Context.INPUT_METHOD_SERVICE);
747 WindowManager.LayoutParams lp = getWindow().getAttributes();
748 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
749 android.os.Handler()) {
750 protected void onReceiveResult(int resultCode, Bundle resultData) {
751 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
752 }
753 });
754 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
755 }
756 }
757 */
758
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 private boolean acceptFilter() {
760 final InputMethodManager inputManager = (InputMethodManager)
761 getSystemService(Context.INPUT_METHOD_SERVICE);
762 return !inputManager.isFullscreenMode();
763 }
764
765 @Override
766 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700767 final int uniChar = event.getUnicodeChar();
768 final boolean handled = super.onKeyDown(keyCode, event);
769 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
770 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
772 keyCode, event);
773 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700774 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700775 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700776 // showSearchDialog()
777 // If there are multiple keystrokes before the search dialog takes focus,
778 // onSearchRequested() will be called for every keystroke,
779 // but it is idempotent, so it's fine.
780 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 }
782 }
783
Joe Onorato8a9625e2010-01-28 15:55:35 -0800784 // Eat the long press event so the keyboard doesn't come up.
785 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
786 return true;
787 }
788
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789 return handled;
790 }
791
Karl Rosaen138a0412009-04-23 19:00:21 -0700792 private String getTypedText() {
793 return mDefaultKeySsb.toString();
794 }
795
796 private void clearTypedText() {
797 mDefaultKeySsb.clear();
798 mDefaultKeySsb.clearSpans();
799 Selection.setSelection(mDefaultKeySsb, 0);
800 }
801
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700803 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
804 * State
805 */
806 private static State intToState(int stateOrdinal) {
807 State state = State.WORKSPACE;
808 final State[] stateValues = State.values();
809 for (int i = 0; i < stateValues.length; i++) {
810 if (stateValues[i].ordinal() == stateOrdinal) {
811 state = stateValues[i];
812 break;
813 }
814 }
815 return state;
816 }
817
818 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 * Restores the previous state, if it exists.
820 *
821 * @param savedState The previous state.
822 */
823 private void restoreState(Bundle savedState) {
824 if (savedState == null) {
825 return;
826 }
827
Michael Jurka883f55b2010-10-21 15:47:14 -0700828 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
829
Winson Chung785d2eb2011-04-14 16:08:02 -0700830 if (state == State.ALL_APPS || state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800831 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700832 } else if (state == State.CUSTOMIZE) {
833 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800834 }
835
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
837 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700838 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 }
840
841 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700842
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700844 mAddScreen = addScreen;
845 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
846 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 mRestoring = true;
848 }
849
850 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
851 if (renameFolder) {
852 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700853 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 mRestoring = true;
855 }
Winson Chunga12a2502010-12-20 14:41:35 -0800856
857 // Restore the current AllApps drawer tab
858 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
859 String curTab = savedState.getString("allapps_currentTab");
860 if (curTab != null) {
861 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
862 tabhost.setCurrentTabByTag(curTab);
863 }
864 int curPage = savedState.getInt("allapps_currentPage", -1);
865 if (curPage > -1) {
866 mAllAppsPagedView.setRestorePage(curPage);
867 }
868 }
869
870 // Restore the current customization drawer tab
871 if (mHomeCustomizationDrawer != null) {
872 String curTab = savedState.getString("customize_currentTab");
873 if (curTab != null) {
874 // We set this directly so that there is no delay before the tab is set
Michael Jurka7ef959b2011-02-23 11:48:32 -0800875 mCustomizePagedView.setCustomizationFilter(
876 mHomeCustomizationDrawer.getCustomizeFilterForTabTag(curTab));
Winson Chunga12a2502010-12-20 14:41:35 -0800877 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
878 }
879
880 // Note: currently we do not restore the page for the customization tray because unlike
881 // AllApps, the page content can change drastically
882 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700883
884 // Restore the AppsCustomize tab
885 if (mAppsCustomizeTabHost != null) {
886 String curTab = savedState.getString("apps_customize_currentTab");
887 if (curTab != null) {
888 // We set this directly so that there is no delay before the tab is set
889 mAppsCustomizeContent.setContentType(
890 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
891 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
892 }
893
894 // Note: currently we do not restore the page for the AppsCustomize pane because the
895 // change in layout can drastically affect the saved page index
896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 }
898
899 /**
900 * Finds all the views we need and configure them properly.
901 */
902 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700903 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400904
Romain Guyb1b69f52009-08-10 15:10:15 -0700905 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400906 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700908 if (LauncherApplication.isScreenXLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700909 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
910 mAllAppsGrid.setup(this, dragController);
911 // We don't want a hole punched in our window.
912 ((View) mAllAppsGrid).setWillNotDraw(false);
913 } else {
914 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
915 findViewById(R.id.apps_customize_pane);
916 mAppsCustomizeContent = (AppsCustomizePagedView)
917 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
918 mAppsCustomizeContent.setup(this, dragController);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700919 }
920
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700922
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500924 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700925
Joe Onorato7c312c12009-08-13 21:36:53 -0700926 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
927 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700929 View handleView = findViewById(R.id.all_apps_button);
930 if (handleView != null && handleView instanceof HandleView) {
931 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700932 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700933 mHandleView.setLauncher(this);
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700934 mHandleView.setOnLongClickListener(this);
935 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800936
Winson Chung80baf5a2010-08-09 16:03:15 -0700937 if (mCustomizePagedView != null) {
938 mCustomizePagedView.setLauncher(this);
939 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700940 } else {
941 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
942 hotseatLeft.setContentDescription(mHotseatLabels[0]);
943 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
944 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
945 hotseatRight.setContentDescription(mHotseatLabels[1]);
946 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400947
Michael Jurkaaf442092010-06-10 17:01:57 -0700948 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
949 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800950
Michael Jurkaaf442092010-06-10 17:01:57 -0700951 Drawable previous = mPreviousView.getDrawable();
952 Drawable next = mNextView.getDrawable();
953 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954
Michael Jurkaaf442092010-06-10 17:01:57 -0700955 mPreviousView.setHapticFeedbackEnabled(false);
956 mPreviousView.setOnLongClickListener(this);
957 mNextView.setHapticFeedbackEnabled(false);
958 mNextView.setOnLongClickListener(this);
959 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800960
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400962 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -0800964 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965
966 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400967 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -0800968
969 final View allAppsButton = findViewById(R.id.all_apps_button);
Winson Chung97d85d22011-04-13 11:27:36 -0700970 final View divider = findViewById(R.id.all_apps_divider);
Michael Jurkab8e14472010-12-20 16:06:10 -0800971 final View configureButton = findViewById(R.id.configure_button);
972
Michael Jurka54f7ac32010-08-02 13:56:46 -0700973 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800974 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -0700975 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800976 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -0700977 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700978 dragController.addDragListener(deleteZone);
979
Adam Cohencdc30d52010-12-01 15:09:47 -0800980 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
981 if (allAppsDeleteZone != null) {
982 allAppsDeleteZone.setLauncher(this);
983 allAppsDeleteZone.setDragController(dragController);
984 allAppsDeleteZone.setDragAndDropEnabled(false);
985 dragController.addDragListener(allAppsDeleteZone);
986 dragController.addDropTarget(allAppsDeleteZone);
987 }
988
989 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
990 findViewById(R.id.all_apps_info_target);
991 if (allAppsInfoTarget != null) {
992 allAppsInfoTarget.setLauncher(this);
993 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -0800994 allAppsInfoTarget.setDragAndDropEnabled(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700995 }
996 View marketButton = findViewById(R.id.market_button);
997 if (marketButton != null) {
998 if (allAppsInfoTarget != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -0800999 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -08001000 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001001 marketButton.setOnClickListener(new OnClickListener() {
1002 public void onClick(View v) {
1003 onClickAppMarketButton(v);
1004 }
1005 });
Adam Cohencdc30d52010-12-01 15:09:47 -08001006 }
1007
Joe Onorato00acb122009-08-04 16:04:30 -04001008 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001009 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001010 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001011
Joe Onorato00acb122009-08-04 16:04:30 -04001012 // The order here is bottom to top.
1013 dragController.addDropTarget(workspace);
1014 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001015 if (allAppsInfoTarget != null) {
1016 dragController.addDropTarget(allAppsInfoTarget);
1017 }
1018 if (allAppsDeleteZone != null) {
1019 dragController.addDropTarget(allAppsDeleteZone);
1020 }
Winson Chung97d85d22011-04-13 11:27:36 -07001021 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
1022 View.OnKeyListener listener = new ButtonBarKeyEventListener();
1023 int buttonCount = mButtonCluster.getChildCount();
1024 for (int i = 0; i < buttonCount; ++i) {
1025 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
1026 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001027
1028 mAllAppsButton = findViewById(R.id.all_apps_button);
Winson Chung97d85d22011-04-13 11:27:36 -07001029 mDivider = findViewById(R.id.all_apps_divider);
Michael Jurka838a4ca2011-02-07 13:33:06 -08001030 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001031
1032 // We had previously set these click handlers in XML, but the first time we launched
1033 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1034 // found the right handler. Setting the handlers directly here eliminates that cost.
1035 if (mConfigureButton != null) {
1036 mConfigureButton.setOnClickListener(new OnClickListener() {
1037 public void onClick(View v) {
1038 onClickConfigureButton(v);
1039 }
1040 });
1041 }
1042 if (mDivider != null) {
1043 mDivider.setOnClickListener(new OnClickListener() {
1044 public void onClick(View v) {
1045 onClickAllAppsButton(v);
1046 }
1047 });
1048 }
1049 if (mAllAppsButton != null) {
1050 mAllAppsButton.setOnClickListener(new OnClickListener() {
1051 public void onClick(View v) {
1052 onClickAllAppsButton(v);
1053 }
1054 });
1055 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 }
1057
Romain Guy8a73c512009-11-09 19:19:59 -08001058 @SuppressWarnings({"UnusedDeclaration"})
1059 public void previousScreen(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001060 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001061 mWorkspace.scrollLeft();
1062 }
Romain Guy8a73c512009-11-09 19:19:59 -08001063 }
1064
1065 @SuppressWarnings({"UnusedDeclaration"})
1066 public void nextScreen(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001067 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001068 mWorkspace.scrollRight();
1069 }
Romain Guy8a73c512009-11-09 19:19:59 -08001070 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001071
1072 @SuppressWarnings({"UnusedDeclaration"})
1073 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001074 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001075
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001076 int index = -1;
1077 if (v.getId() == R.id.hotseat_left) {
1078 index = 0;
1079 } else if (v.getId() == R.id.hotseat_right) {
1080 index = 1;
1081 }
1082
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001083 // reload these every tap; you never know when they might change
1084 loadHotseats();
1085 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001086 startActivitySafely(
1087 mHotseats[index],
1088 "hotseat"
1089 );
1090 }
1091 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 /**
1094 * Creates a view representing a shortcut.
1095 *
1096 * @param info The data structure describing the shortcut.
1097 *
1098 * @return A View inflated from R.layout.application.
1099 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001100 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001102 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 }
1104
1105 /**
1106 * Creates a view representing a shortcut inflated from the specified resource.
1107 *
1108 * @param layoutResId The id of the XML layout used to create the shortcut.
1109 * @param parent The group the shortcut belongs to.
1110 * @param info The data structure describing the shortcut.
1111 *
1112 * @return A View inflated from layoutResId.
1113 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001114 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001115 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1116 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 return favorite;
1119 }
1120
1121 /**
1122 * Add an application shortcut to the workspace.
1123 *
1124 * @param data The intent describing the application.
1125 * @param cellInfo The position on screen where to create the shortcut.
1126 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001127 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001128 int intersectCellX, int intersectCellY) {
1129 final int[] cellXY = mTmpAddItemCellCoordinates;
1130 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1131
1132 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1133 showOutOfSpaceMessage();
1134 return;
1135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001137 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001138
Romain Guy73b979d2009-06-09 12:57:21 -07001139 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001140 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001142 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001143 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1144 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001145 } else {
1146 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 }
1148 }
Romain Guycbb89e42009-06-08 15:52:54 -07001149
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 /**
1151 * Add a shortcut to the workspace.
1152 *
1153 * @param data The intent describing the shortcut.
1154 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001156 private void completeAddShortcut(Intent data, int screen,
1157 int intersectCellX, int intersectCellY) {
1158 final int[] cellXY = mTmpAddItemCellCoordinates;
1159 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001160
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001161 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001162 boolean foundCellSpan = false;
1163 if (touchXY != null) {
1164 // when dragging and dropping, just find the closest free spot
1165 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1166 int[] result = screenLayout.findNearestVacantArea(
1167 touchXY[0], touchXY[1], 1, 1, cellXY);
1168 foundCellSpan = (result != null);
1169 } else {
1170 foundCellSpan = layout.findCellForSpanThatIntersects(
1171 cellXY, 1, 1, intersectCellX, intersectCellY);
1172 }
1173
1174 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001175 showOutOfSpaceMessage();
1176 return;
1177 }
1178
1179 final ShortcutInfo info = mModel.addShortcut(
1180 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181
1182 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001184 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 }
1186 }
1187
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001189 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001191 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001192 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 */
Winson Chungf7640c82011-02-28 13:47:29 -08001194 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001195 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001196
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001197 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001198 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1199 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001200
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001202 // We have saved the position to which the widget was dragged-- this really only matters
1203 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001204 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001205
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001206 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001207 boolean foundCellSpan = false;
1208 if (touchXY != null) {
1209 // when dragging and dropping, just find the closest free spot
1210 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1211 int[] result = screenLayout.findNearestVacantArea(
1212 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001213 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001214 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001215 // if we long pressed on an empty cell to bring up a menu,
1216 // make sure we intersect the empty cell
1217 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1218 // findCellForSpanThatIntersects will just ignore them
1219 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1220 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001221 }
1222
Michael Jurka0280c3b2010-09-17 15:00:07 -07001223 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001224 if (appWidgetId != -1) {
1225 // Deleting an app widget ID is a void call but writes to disk before returning
1226 // to the caller...
1227 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1228 new Thread("deleteAppWidgetId") {
1229 public void run() {
1230 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1231 }
1232 }.start();
1233 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001234 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001235 return;
1236 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001238 // Build Launcher-specific widget info and save to database
1239 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001240 launcherInfo.spanX = spanXY[0];
1241 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 LauncherModel.addItemToDatabase(this, launcherInfo,
1244 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001245 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246
1247 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001248 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001251 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001252
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001253 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001255
Michael Jurka0280c3b2010-09-17 15:00:07 -07001256 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001257 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001258
1259 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 }
1261 }
Romain Guycbb89e42009-06-08 15:52:54 -07001262
Adam Cohended9f8d2010-11-03 13:25:16 -07001263 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1264 @Override
1265 public void onReceive(Context context, Intent intent) {
1266 final String action = intent.getAction();
1267 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1268 mUserPresent = false;
1269 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001270
1271 // Reset AllApps to it's initial state
1272 if (mAllAppsGrid != null) {
1273 mAllAppsGrid.reset();
1274 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001275 if (mAppsCustomizeContent != null) {
1276 mAppsCustomizeContent.reset();
1277 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001278 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1279 mUserPresent = true;
1280 updateRunning();
1281 }
1282 }
1283 };
1284
1285 @Override
1286 public void onAttachedToWindow() {
1287 super.onAttachedToWindow();
1288
1289 // Listen for broadcasts related to user-presence
1290 final IntentFilter filter = new IntentFilter();
1291 filter.addAction(Intent.ACTION_SCREEN_OFF);
1292 filter.addAction(Intent.ACTION_USER_PRESENT);
1293 registerReceiver(mReceiver, filter);
1294
Adam Cohend113e0c2010-11-11 10:48:05 -08001295 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001296 mVisible = true;
1297 }
1298
1299 @Override
1300 public void onDetachedFromWindow() {
1301 super.onDetachedFromWindow();
1302 mVisible = false;
1303
Adam Cohend113e0c2010-11-11 10:48:05 -08001304 if (mAttached) {
1305 unregisterReceiver(mReceiver);
1306 mAttached = false;
1307 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001308 updateRunning();
1309 }
1310
1311 public void onWindowVisibilityChanged(int visibility) {
1312 mVisible = visibility == View.VISIBLE;
1313 updateRunning();
1314 }
1315
1316 private void sendAdvanceMessage(long delay) {
1317 mHandler.removeMessages(ADVANCE_MSG);
1318 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1319 mHandler.sendMessageDelayed(msg, delay);
1320 mAutoAdvanceSentTime = System.currentTimeMillis();
1321 }
1322
1323 private void updateRunning() {
1324 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1325 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1326 mAutoAdvanceRunning = autoAdvanceRunning;
1327 if (autoAdvanceRunning) {
1328 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1329 sendAdvanceMessage(delay);
1330 } else {
1331 if (!mWidgetsToAdvance.isEmpty()) {
1332 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1333 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1334 }
1335 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001336 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001337 }
1338 }
1339 }
1340
1341 private final Handler mHandler = new Handler() {
1342 @Override
1343 public void handleMessage(Message msg) {
1344 if (msg.what == ADVANCE_MSG) {
1345 int i = 0;
1346 for (View key: mWidgetsToAdvance.keySet()) {
1347 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1348 final int delay = mAdvanceStagger * i;
1349 if (v instanceof Advanceable) {
1350 postDelayed(new Runnable() {
1351 public void run() {
1352 ((Advanceable) v).advance();
1353 }
1354 }, delay);
1355 }
1356 i++;
1357 }
1358 sendAdvanceMessage(mAdvanceInterval);
1359 }
1360 }
1361 };
1362
1363 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1364 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1365 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1366 if (v instanceof Advanceable) {
1367 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001368 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001369 updateRunning();
1370 }
1371 }
1372
1373 void removeWidgetToAutoAdvance(View hostView) {
1374 if (mWidgetsToAdvance.containsKey(hostView)) {
1375 mWidgetsToAdvance.remove(hostView);
1376 updateRunning();
1377 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001378 }
1379
Joe Onorato9c1289c2009-08-17 11:03:03 -04001380 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1381 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001382 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001383 launcherInfo.hostView = null;
1384 }
1385
Adam Cohended9f8d2010-11-03 13:25:16 -07001386 void showOutOfSpaceMessage() {
1387 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1388 }
1389
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001390 public LauncherAppWidgetHost getAppWidgetHost() {
1391 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 }
Romain Guycbb89e42009-06-08 15:52:54 -07001393
Winson Chunga9abd0e2010-10-27 17:18:37 -07001394 public LauncherModel getModel() {
1395 return mModel;
1396 }
1397
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001398 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001399 getWindow().closeAllPanels();
1400
1401 try {
1402 dismissDialog(DIALOG_CREATE_SHORTCUT);
1403 // Unlock the workspace if the dialog was showing
1404 } catch (Exception e) {
1405 // An exception is thrown if the dialog is not visible, which is fine
1406 }
1407
1408 try {
1409 dismissDialog(DIALOG_RENAME_FOLDER);
1410 // Unlock the workspace if the dialog was showing
1411 } catch (Exception e) {
1412 // An exception is thrown if the dialog is not visible, which is fine
1413 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001414
1415 // Whatever we were doing is hereby canceled.
1416 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001417 }
1418
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 @Override
1420 protected void onNewIntent(Intent intent) {
1421 super.onNewIntent(intent);
1422
1423 // Close the menu
1424 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001425 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001426 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001427
Joe Onorato14f122b2009-11-19 14:06:36 -08001428 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1429 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001430
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001431 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001432 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001433 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001434 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001435 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001436 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001437 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001438
Joe Onorato3a8820b2009-11-10 15:06:42 -08001439 final View v = getWindow().peekDecorView();
1440 if (v != null && v.getWindowToken() != null) {
1441 InputMethodManager imm = (InputMethodManager)getSystemService(
1442 INPUT_METHOD_SERVICE);
1443 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001445
1446 // Reset AllApps to it's initial state
1447 if (mAllAppsGrid != null) {
1448 mAllAppsGrid.reset();
1449 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001450 if (mAppsCustomizeContent != null) {
1451 mAppsCustomizeContent.reset();
1452 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 }
1454 }
1455
1456 @Override
1457 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1458 // Do not call super here
1459 mSavedInstanceState = savedInstanceState;
1460 }
1461
1462 @Override
1463 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001464 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465
1466 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1467 if (folders.size() > 0) {
1468 final int count = folders.size();
1469 long[] ids = new long[count];
1470 for (int i = 0; i < count; i++) {
1471 final FolderInfo info = folders.get(i).getInfo();
1472 ids[i] = info.id;
1473 }
1474 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1475 } else {
1476 super.onSaveInstanceState(outState);
1477 }
1478
Michael Jurka883f55b2010-10-21 15:47:14 -07001479 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480
Michael Jurka0280c3b2010-09-17 15:00:07 -07001481 if (mAddScreen > -1 && mWaitingForResult) {
1482 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1483 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1484 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 }
1486
1487 if (mFolderInfo != null && mWaitingForResult) {
1488 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1489 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1490 }
Michael Jurka946ad472010-07-09 18:05:18 -07001491
Winson Chung43b119d2010-12-06 16:45:05 -08001492 // Save the current AllApps drawer tab
1493 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1494 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1495 String currentTabTag = tabhost.getCurrentTabTag();
1496 if (currentTabTag != null) {
1497 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001498 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001499 }
1500 }
Winson Chung43b119d2010-12-06 16:45:05 -08001501 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001502 if (mHomeCustomizationDrawer != null) {
1503 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1504 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001505 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001506 }
1507 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001508 // Save the current AppsCustomize tab
1509 if (mAppsCustomizeTabHost != null) {
1510 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1511 if (currentTabTag != null) {
1512 outState.putString("apps_customize_currentTab", currentTabTag);
1513 }
1514 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 }
1516
1517 @Override
1518 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001520
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001522 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001524 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001526 mAppWidgetHost = null;
1527
1528 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529
1530 TextKeyListener.getInstance().release();
1531
Joe Onorato9c1289c2009-08-17 11:03:03 -04001532 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533
Joe Onorato9c1289c2009-08-17 11:03:03 -04001534 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001535
1536 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001537
Patrick Dubroyab962b72010-07-26 12:10:10 -07001538 // Some launcher layouts don't have a previous and next view
1539 if (mPreviousView != null) {
1540 dismissPreview(mPreviousView);
1541 }
1542 if (mNextView != null) {
1543 dismissPreview(mNextView);
1544 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001545
1546 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001547
1548 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1549 mWorkspace.removeAllViews();
1550 mWorkspace = null;
1551 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001552
1553 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 }
1555
1556 @Override
1557 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001558 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 super.startActivityForResult(intent, requestCode);
1560 }
1561
1562 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001563 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001565
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001566 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001567
Karl Rosaen138a0412009-04-23 19:00:21 -07001568 if (initialQuery == null) {
1569 // Use any text typed in the launcher as the initial query
1570 initialQuery = getTypedText();
1571 clearTypedText();
1572 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 if (appSearchData == null) {
1574 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001575 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 }
Romain Guycbb89e42009-06-08 15:52:54 -07001577
Karl Rosaen138a0412009-04-23 19:00:21 -07001578 final SearchManager searchManager =
1579 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001580 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001581 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 }
1583
1584 @Override
1585 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001586 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001587 return false;
1588 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589
1590 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001591
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1593 .setIcon(android.R.drawable.ic_menu_add)
1594 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001595 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1596 .setIcon(android.R.drawable.ic_menu_manage)
1597 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001598 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 .setIcon(android.R.drawable.ic_menu_gallery)
1600 .setAlphabeticShortcut('W');
1601 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1602 .setIcon(android.R.drawable.ic_search_category_default)
1603 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1604 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1605 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1606 .setAlphabeticShortcut('N');
1607
1608 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001609 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1610 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611
1612 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1613 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1614 .setIntent(settings);
1615
1616 return true;
1617 }
1618
1619 @Override
1620 public boolean onPrepareOptionsMenu(Menu menu) {
1621 super.onPrepareOptionsMenu(menu);
1622
Winson Chung785d2eb2011-04-14 16:08:02 -07001623 if (mAllAppsGrid != null) {
1624 // If all apps is animating, don't show the menu, because we don't know
1625 // which one to show.
1626 if (mAllAppsGrid.isAnimating()) {
1627 return false;
1628 }
1629
1630 // Only show the add and wallpaper options when we're not in all apps.
1631 boolean visible = !mAllAppsGrid.isVisible();
1632 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1633 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1634
1635 // Disable add if the workspace is full.
1636 if (visible) {
1637 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1638 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
1639 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001640 }
1641
Winson Chung785d2eb2011-04-14 16:08:02 -07001642 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1643 // related code?
1644 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645
1646 return true;
1647 }
1648
1649 @Override
1650 public boolean onOptionsItemSelected(MenuItem item) {
1651 switch (item.getItemId()) {
1652 case MENU_ADD:
1653 addItems();
1654 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001655 case MENU_MANAGE_APPS:
1656 manageApps();
1657 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 case MENU_WALLPAPER_SETTINGS:
1659 startWallpaper();
1660 return true;
1661 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001662 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 return true;
1664 case MENU_NOTIFICATIONS:
1665 showNotifications();
1666 return true;
1667 }
1668
1669 return super.onOptionsItemSelected(item);
1670 }
Romain Guycbb89e42009-06-08 15:52:54 -07001671
Karl Rosaen138a0412009-04-23 19:00:21 -07001672 /**
1673 * Indicates that we want global search for this activity by setting the globalSearch
1674 * argument for {@link #startSearch} to true.
1675 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001677 @Override
1678 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001679 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001680 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001681 }
1682
Joe Onorato9c1289c2009-08-17 11:03:03 -04001683 public boolean isWorkspaceLocked() {
1684 return mWorkspaceLoading || mWaitingForResult;
1685 }
1686
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001687 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1688 private boolean isPreviewVisible() {
1689 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1690 (mNextView != null && mNextView.getTag() != null);
1691 }
1692
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001694 if (LauncherApplication.isScreenXLarge()) {
1695 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001696 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001697 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001698 }
1699 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001700 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001701 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001702 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 }
1704
Michael Jurka0280c3b2010-09-17 15:00:07 -07001705 private void resetAddInfo() {
1706 mAddScreen = -1;
1707 mAddIntersectCellX = -1;
1708 mAddIntersectCellY = -1;
1709 mAddDropPosition = null;
1710 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001711
Winson Chung55cef262010-10-28 14:14:18 -07001712 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001713 resetAddInfo();
1714 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001715 mAddDropPosition = position;
1716
Michael Jurkaaf442092010-06-10 17:01:57 -07001717 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001718 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1719 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001720 }
1721
Winson Chung7ad01412010-09-27 11:33:03 -07001722 private void manageApps() {
1723 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1724 }
1725
Michael Jurkaaf442092010-06-10 17:01:57 -07001726 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001727 // TODO: catch bad widget exception when sent
1728 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001729 // TODO: Is this log message meaningful?
1730 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001731 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001732 }
1733
Winson Chung55cef262010-10-28 14:14:18 -07001734 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001735 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736
Bjorn Bringert7984c942009-12-09 15:38:25 +00001737 if (appWidget.configure != null) {
1738 // Launch over to configure widget, if needed
1739 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1740 intent.setComponent(appWidget.configure);
1741 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001742 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001743 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1744 intent.putExtra(
1745 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1746 info.mimeType);
1747
1748 final String mimeType = info.mimeType;
1749 final ClipData clipData = (ClipData) info.configurationData;
1750 final ClipDescription clipDesc = clipData.getDescription();
1751 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1752 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001753 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001754 final CharSequence stringData = item.getText();
1755 final Uri uriData = item.getUri();
1756 final Intent intentData = item.getIntent();
1757 final String key =
1758 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1759 if (uriData != null) {
1760 intent.putExtra(key, uriData);
1761 } else if (intentData != null) {
1762 intent.putExtra(key, intentData);
1763 } else if (stringData != null) {
1764 intent.putExtra(key, stringData);
1765 }
1766 break;
1767 }
1768 }
1769 }
Winson Chung55cef262010-10-28 14:14:18 -07001770 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001771
Romain Guy8e633c52010-03-04 12:51:36 -08001772 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001774 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001775 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 }
1777 }
Romain Guycbb89e42009-06-08 15:52:54 -07001778
Michael Jurka0280c3b2010-09-17 15:00:07 -07001779 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1780 resetAddInfo();
1781 mAddScreen = screen;
1782 mAddDropPosition = position;
1783
1784 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1785 createShortcutIntent.setComponent(componentName);
1786 processShortcut(createShortcutIntent);
1787 }
1788
Joe Onoratodeb98af2010-02-19 14:59:39 -08001789 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001790 // Handle case where user selected "Applications"
1791 String applicationName = getResources().getString(R.string.group_applications);
1792 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001793
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001794 if (applicationName != null && applicationName.equals(shortcutName)) {
1795 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1796 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001797
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001798 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1799 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001800 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001801 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001802 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001803 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001804 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 }
1806
Winson Chung24ab2f12010-09-16 14:10:47 -07001807 void processWallpaper(Intent intent) {
1808 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1809 }
1810
Michael Jurka0280c3b2010-09-17 15:00:07 -07001811 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1812 resetAddInfo();
1813 mAddScreen = screen;
1814 mAddDropPosition = position;
1815
1816 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1817 createFolderIntent.setComponent(componentName);
1818
1819 addLiveFolder(createFolderIntent);
1820 }
1821
1822 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001823 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001824 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001825 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001826
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001827 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001828 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001829 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001830 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 }
1833
Adam Cohendf035382011-04-11 17:22:04 -07001834 FolderIcon addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 UserFolderInfo folderInfo = new UserFolderInfo();
1836 folderInfo.title = getText(R.string.folder_name);
1837
Michael Jurka0280c3b2010-09-17 15:00:07 -07001838 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1839 final int[] cellXY = mTmpAddItemCellCoordinates;
1840 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1841 showOutOfSpaceMessage();
Adam Cohendf035382011-04-11 17:22:04 -07001842 return null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001843 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844
1845 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001846 LauncherModel.addItemToDatabase(this, folderInfo,
1847 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001848 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001849 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850
1851 // Create the view
1852 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001853 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1854 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001855 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001856 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 }
Romain Guycbb89e42009-06-08 15:52:54 -07001858
Joe Onorato9c1289c2009-08-17 11:03:03 -04001859 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001860 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001861 }
1862
Michael Jurka28750fb2010-09-24 17:43:49 -07001863 private void completeAddLiveFolder(
1864 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001865 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1866 final int[] cellXY = mTmpAddItemCellCoordinates;
1867 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1868 showOutOfSpaceMessage();
1869 return;
1870 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871
Michael Jurka0280c3b2010-09-17 15:00:07 -07001872 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873
1874 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001876 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001877 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 }
1879 }
1880
1881 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001882 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883
1884 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1885 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1886
1887 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 Intent.ShortcutIconResource iconResource = null;
1889
1890 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1891 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1892 try {
1893 iconResource = (Intent.ShortcutIconResource) extra;
1894 final PackageManager packageManager = context.getPackageManager();
1895 Resources resources = packageManager.getResourcesForApplication(
1896 iconResource.packageName);
1897 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1898 icon = resources.getDrawable(id);
1899 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001900 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 }
1902 }
1903
1904 if (icon == null) {
1905 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1906 }
1907
1908 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001909 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 info.title = name;
1911 info.iconResource = iconResource;
1912 info.uri = data.getData();
1913 info.baseIntent = baseIntent;
1914 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1915 LiveFolders.DISPLAY_MODE_GRID);
1916
1917 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001918 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001919 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920
1921 return info;
1922 }
1923
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 private void showNotifications() {
1925 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1926 if (statusBar != null) {
1927 statusBar.expand();
1928 }
1929 }
1930
1931 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001932 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001934 Intent chooser = Intent.createChooser(pickWallpaper,
1935 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001936 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1937 // Removed in Eclair MR1
1938// WallpaperManager wm = (WallpaperManager)
1939// getSystemService(Context.WALLPAPER_SERVICE);
1940// WallpaperInfo wi = wm.getWallpaperInfo();
1941// if (wi != null && wi.getSettingsActivity() != null) {
1942// LabeledIntent li = new LabeledIntent(getPackageName(),
1943// R.string.configure_wallpaper, 0);
1944// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1945// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1946// }
Mike Clerona0618e42009-10-22 13:55:21 -07001947 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
1949
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001950 /**
1951 * Registers various content observers. The current implementation registers
1952 * only a favorites observer to keep track of the favorites applications.
1953 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001954 private void registerContentObservers() {
1955 ContentResolver resolver = getContentResolver();
1956 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1957 true, mWidgetObserver);
1958 }
1959
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 @Override
1961 public boolean dispatchKeyEvent(KeyEvent event) {
1962 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1963 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001965 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001966 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001967 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001968 dumpState();
1969 return true;
1970 }
1971 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001972 }
1973 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1974 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001975 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 return true;
1977 }
1978 }
1979
1980 return super.dispatchKeyEvent(event);
1981 }
1982
Joe Onorato88ec0992009-11-19 13:16:06 -08001983 @Override
1984 public void onBackPressed() {
Winson Chung785d2eb2011-04-14 16:08:02 -07001985 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001986 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001987 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08001988 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001989 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001990 dismissPreview(mPreviousView);
1991 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001992 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001993 mWorkspace.exitWidgetResizeMode();
1994
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001995 // Back button is a no-op here, but give at least some feedback for the button press
1996 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001997 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001998 }
1999
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 private void closeFolder() {
2001 Folder folder = mWorkspace.getOpenFolder();
2002 if (folder != null) {
2003 closeFolder(folder);
2004 }
2005 }
2006
2007 void closeFolder(Folder folder) {
2008 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002009
Michael Jurka8c920dd2011-01-20 14:16:56 -08002010 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07002012 CellLayout cl = (CellLayout) parent;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002013 if (!(folder instanceof UserFolder)) {
2014 // User folders will remove themselves
2015 cl.removeViewWithoutMarkingCells(folder);
2016 }
Joe Onoratob6341e92009-11-02 10:41:48 -05002017 if (folder instanceof DropTarget) {
2018 // Live folders aren't DropTargets.
2019 mDragController.removeDropTarget((DropTarget)folder);
2020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002022 if (folder instanceof UserFolder) {
2023 UserFolder uf = (UserFolder) folder;
2024 uf.animateClosed();
2025 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 folder.onClose();
2027 }
2028
2029 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002030 * Re-listen when widgets are reset.
2031 */
2032 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002033 if (mAppWidgetHost != null) {
2034 mAppWidgetHost.startListening();
2035 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002036 }
2037
2038 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002039 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2040 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002042 private void unbindDesktopItems() {
2043 for (ItemInfo item: mDesktopItems) {
2044 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002046 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002048
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 /**
2050 * Launches the intent referred by the clicked shortcut.
2051 *
2052 * @param v The view representing the clicked shortcut.
2053 */
2054 public void onClick(View v) {
2055 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002056 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002058 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002059 int[] pos = new int[2];
2060 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002061 intent.setSourceBounds(new Rect(pos[0], pos[1],
2062 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08002063 boolean success = startActivitySafely(intent, tag);
2064
2065 if (success && v instanceof BubbleTextView) {
2066 mWaitingForResume = (BubbleTextView) v;
2067 mWaitingForResume.setStayPressed(true);
2068 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 } else if (tag instanceof FolderInfo) {
2070 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002071 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002072 if (mState == State.ALL_APPS) {
2073 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002074 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002075 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002076 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 }
2078 }
2079
Michael Jurka0e260592010-06-30 17:07:39 -07002080 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002081 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002082 // clicking anywhere on the workspace causes the customization drawer to slide down
2083 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002084 return false;
2085 }
2086
Michael Jurkaaf442092010-06-10 17:01:57 -07002087 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002088 * Event handler for the search button
2089 *
2090 * @param v The view that was clicked.
2091 */
2092 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002093 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002094 // Use a custom animation for launching search
2095 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002096 }
2097
2098 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002099 * Event handler for the voice button
2100 *
2101 * @param v The view that was clicked.
2102 */
2103 public void onClickVoiceButton(View v) {
2104 startVoiceSearch();
2105 }
2106
2107 private void startVoiceSearch() {
2108 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2109 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2110 startActivity(intent);
2111 }
2112
2113 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002114 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002115 * enters home screen customization mode.
2116 *
2117 * @param v The view that was clicked.
2118 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002119 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002120 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002121 }
2122
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002123 /**
2124 * Event handler for the "grid" button that appears on the home screen, which
2125 * enters all apps mode.
2126 *
2127 * @param v The view that was clicked.
2128 */
2129 public void onClickAllAppsButton(View v) {
2130 showAllApps(true);
2131 }
2132
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002133 public void onClickAppMarketButton(View v) {
2134 if (mAppMarketIntent != null) {
2135 startActivitySafely(mAppMarketIntent, "app market");
2136 }
2137 }
2138
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002139 void startApplicationDetailsActivity(ComponentName componentName) {
2140 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002141 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2142 Uri.fromParts("package", packageName, null));
2143 startActivity(intent);
2144 }
2145
Patrick Dubroy5539af72010-09-07 15:22:01 -07002146 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2147 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2148 // System applications cannot be installed. For now, show a toast explaining that.
2149 // We may give them the option of disabling apps this way.
2150 int messageId = R.string.uninstall_system_app_text;
2151 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2152 } else {
2153 String packageName = appInfo.componentName.getPackageName();
2154 String className = appInfo.componentName.getClassName();
2155 Intent intent = new Intent(
2156 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2157 startActivity(intent);
2158 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002159 }
2160
Michael Jurkaddd62e92011-02-16 17:49:14 -08002161 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2163 try {
2164 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002165 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 } catch (ActivityNotFoundException e) {
2167 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002168 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 } catch (SecurityException e) {
2170 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002171 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002173 "or use the exported attribute for this activity. "
2174 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002176 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002178
Romain Guy8e633c52010-03-04 12:51:36 -08002179 void startActivityForResultSafely(Intent intent, int requestCode) {
2180 try {
2181 startActivityForResult(intent, requestCode);
2182 } catch (ActivityNotFoundException e) {
2183 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2184 } catch (SecurityException e) {
2185 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2186 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2187 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2188 "or use the exported attribute for this activity.", e);
2189 }
2190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191
2192 private void handleFolderClick(FolderInfo folderInfo) {
2193 if (!folderInfo.opened) {
2194 // Close any open folder
2195 closeFolder();
2196 // Open the requested folder
2197 openFolder(folderInfo);
2198 } else {
2199 // Find the open folder...
2200 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2201 int folderScreen;
2202 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002203 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 // .. and close it
2205 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002206 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 // Close any folder open on the current screen
2208 closeFolder();
2209 // Pull the folder onto this screen
2210 openFolder(folderInfo);
2211 }
2212 }
2213 }
2214 }
2215
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002217 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 * is animated relative to the specified View. If the View is null, no animation
2219 * is played.
2220 *
2221 * @param folderInfo The FolderInfo describing the folder to open.
2222 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002223 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 Folder openFolder;
2225
2226 if (folderInfo instanceof UserFolderInfo) {
2227 openFolder = UserFolder.fromXml(this);
2228 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002229 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 } else {
2231 return;
2232 }
2233
Joe Onorato00acb122009-08-04 16:04:30 -04002234 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 openFolder.setLauncher(this);
2236
2237 openFolder.bind(folderInfo);
2238 folderInfo.opened = true;
2239
Winson Chungaafa03c2010-06-11 17:34:16 -07002240 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002241 if (openFolder instanceof UserFolder) {
2242 UserFolder uf = (UserFolder) openFolder;
2243 uf.animateOpen();
2244 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 openFolder.onOpen();
2247 }
2248
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002250 if (mState != State.WORKSPACE) {
2251 return false;
2252 }
2253
Romain Guy1fbc1c82009-11-09 20:43:08 -08002254 switch (v.getId()) {
2255 case R.id.previous_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002256 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002257 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2258 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002259 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002260 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002261 return true;
2262 case R.id.next_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002263 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002264 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2265 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002266 showPreviews(v);
2267 }
2268 return true;
2269 case R.id.all_apps_button:
Winson Chung785d2eb2011-04-14 16:08:02 -07002270 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002271 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2272 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2273 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002274 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002275 return true;
2276 }
2277
Joe Onorato9c1289c2009-08-17 11:03:03 -04002278 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002279 return false;
2280 }
2281
2282 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002283 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002284 }
2285
Michael Jurka0280c3b2010-09-17 15:00:07 -07002286 resetAddInfo();
2287 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002288 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002289 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002290 return true;
2291 }
2292
Michael Jurka0280c3b2010-09-17 15:00:07 -07002293 final View itemUnderLongClick = longClickCellInfo.cell;
2294
Winson Chung304dcde2011-01-07 11:17:23 -08002295 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002296 if (itemUnderLongClick == null) {
2297 // User long pressed on empty space
2298 mWorkspace.setAllowLongPress(false);
2299 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2300 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002301 if (LauncherApplication.isScreenXLarge()) {
2302 addItems();
2303 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002304 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002305 }
2306 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002307 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002309 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2310 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002311 mAddIntersectCellX = longClickCellInfo.cellX;
2312 mAddIntersectCellY = longClickCellInfo.cellY;
2313 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002314 }
2315 }
2316 }
2317 return true;
2318 }
2319
Romain Guye6b8e2f2009-11-10 11:56:55 -08002320 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002321 private void dismissPreview(final View v) {
2322 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002323 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002324 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2325 public void onDismiss() {
2326 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2327 int count = group.getChildCount();
2328 for (int i = 0; i < count; i++) {
2329 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2330 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002331 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2332 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2333
2334 v.setTag(R.id.workspace, null);
2335 v.setTag(R.id.icon, null);
2336 window.setOnDismissListener(null);
2337 }
2338 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002339 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002340 }
2341 v.setTag(null);
2342 }
2343
Romain Guyf8e6a802009-12-07 17:48:02 -08002344 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002345 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002346 }
2347
Romain Guya6abce82009-11-10 02:54:41 -08002348 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002349 final Resources resources = getResources();
2350 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002351
Romain Guya6abce82009-11-10 02:54:41 -08002352 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002353
Romain Guy9d31e9f2009-11-11 18:54:28 -08002354 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002355
Romain Guyf8e6a802009-12-07 17:48:02 -08002356 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002357 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002358 int extraW = (int) ((r.left + r.right) * max);
2359 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002360
2361 int aW = cell.getWidth() - extraW;
2362 float w = aW / max;
2363
2364 int width = cell.getWidth();
2365 int height = cell.getHeight();
2366 int x = cell.getLeftPadding();
2367 int y = cell.getTopPadding();
2368 width -= (x + cell.getRightPadding());
2369 height -= (y + cell.getBottomPadding());
2370
2371 float scale = w / width;
2372
2373 int count = end - start;
2374
2375 final float sWidth = width * scale;
2376 float sHeight = height * scale;
2377
Romain Guye6b8e2f2009-11-10 11:56:55 -08002378 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002379
Romain Guye6b8e2f2009-11-10 11:56:55 -08002380 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2381 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002382
2383 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002384 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002385 cell = (CellLayout) workspace.getChildAt(i);
2386
Romain Guyf8e6a802009-12-07 17:48:02 -08002387 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002388 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002389
2390 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002391 c.scale(scale, scale);
2392 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002393 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002394
Romain Guy9d31e9f2009-11-11 18:54:28 -08002395 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002396 image.setImageBitmap(bitmap);
2397 image.setTag(i);
2398 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002399 image.setOnFocusChangeListener(handler);
2400 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002401 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002402
2403 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002404 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2405
Winson Chungaafa03c2010-06-11 17:34:16 -07002406 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002407 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002408
2409 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002410 p.setContentView(preview);
2411 p.setWidth((int) (sWidth * count + extraW));
2412 p.setHeight((int) (sHeight + extraH));
2413 p.setAnimationStyle(R.style.AnimationPreview);
2414 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002415 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002416 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002417 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002418
Romain Guye6b8e2f2009-11-10 11:56:55 -08002419 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2420 public void onDismiss() {
2421 dismissPreview(anchor);
2422 }
2423 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002424
2425 anchor.setTag(p);
2426 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002427 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002428 }
2429
Romain Guy9d31e9f2009-11-11 18:54:28 -08002430 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002431 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002432
Romain Guye6b8e2f2009-11-10 11:56:55 -08002433 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002434 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002435 }
2436
2437 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002438 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002439 v.post(this);
2440 }
2441
2442 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002443 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002444 }
2445
2446 public void onFocusChange(View v, boolean hasFocus) {
2447 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002448 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002449 }
Romain Guya6abce82009-11-10 02:54:41 -08002450 }
2451 }
2452
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 Workspace getWorkspace() {
2454 return mWorkspace;
2455 }
2456
Michael Jurka3c38d562011-02-18 19:22:12 -08002457 TabHost getCustomizationDrawer() {
2458 return mHomeCustomizationDrawer;
2459 }
2460
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002461 @Override
2462 protected Dialog onCreateDialog(int id) {
2463 switch (id) {
2464 case DIALOG_CREATE_SHORTCUT:
2465 return new CreateShortcut().createDialog();
2466 case DIALOG_RENAME_FOLDER:
2467 return new RenameFolder().createDialog();
2468 }
2469
2470 return super.onCreateDialog(id);
2471 }
2472
2473 @Override
2474 protected void onPrepareDialog(int id, Dialog dialog) {
2475 switch (id) {
2476 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002477 break;
2478 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002479 if (mFolderInfo != null) {
2480 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2481 final CharSequence text = mFolderInfo.title;
2482 input.setText(text);
2483 input.setSelection(0, text.length());
2484 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 break;
2486 }
2487 }
2488
2489 void showRenameDialog(FolderInfo info) {
2490 mFolderInfo = info;
2491 mWaitingForResult = true;
2492 showDialog(DIALOG_RENAME_FOLDER);
2493 }
2494
Michael Jurka0280c3b2010-09-17 15:00:07 -07002495 private void showAddDialog(int intersectX, int intersectY) {
2496 resetAddInfo();
2497 mAddIntersectCellX = intersectX;
2498 mAddIntersectCellY = intersectY;
2499 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 mWaitingForResult = true;
2501 showDialog(DIALOG_CREATE_SHORTCUT);
2502 }
2503
Joe Onoratodeb98af2010-02-19 14:59:39 -08002504 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002505 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002506 Bundle bundle = new Bundle();
2507
2508 ArrayList<String> shortcutNames = new ArrayList<String>();
2509 shortcutNames.add(getString(R.string.group_applications));
2510 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2511
2512 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2513 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2514 R.drawable.ic_launcher_application));
2515 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2516
2517 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2518 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002519 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002520 pickIntent.putExtras(bundle);
2521
Joe Onoratodeb98af2010-02-19 14:59:39 -08002522 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002523 }
2524
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 private class RenameFolder {
2526 private EditText mInput;
2527
2528 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002529 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2530 mInput = (EditText) layout.findViewById(R.id.folder_name);
2531
2532 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2533 builder.setIcon(0);
2534 builder.setTitle(getString(R.string.rename_folder_title));
2535 builder.setCancelable(true);
2536 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2537 public void onCancel(DialogInterface dialog) {
2538 cleanup();
2539 }
2540 });
2541 builder.setNegativeButton(getString(R.string.cancel_action),
2542 new Dialog.OnClickListener() {
2543 public void onClick(DialogInterface dialog, int which) {
2544 cleanup();
2545 }
2546 }
2547 );
2548 builder.setPositiveButton(getString(R.string.rename_action),
2549 new Dialog.OnClickListener() {
2550 public void onClick(DialogInterface dialog, int which) {
2551 changeFolderName();
2552 }
2553 }
2554 );
2555 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002556
2557 final AlertDialog dialog = builder.create();
2558 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2559 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002560 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002561 mInput.requestFocus();
2562 InputMethodManager inputManager = (InputMethodManager)
2563 getSystemService(Context.INPUT_METHOD_SERVICE);
2564 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002565 }
2566 });
2567
2568 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002569 }
2570
2571 private void changeFolderName() {
2572 final String name = mInput.getText().toString();
2573 if (!TextUtils.isEmpty(name)) {
2574 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002575 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002576 mFolderInfo.title = name;
2577 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2578
Joe Onorato9c1289c2009-08-17 11:03:03 -04002579 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002580 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002581 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002582 } else {
2583 final FolderIcon folderIcon = (FolderIcon)
2584 mWorkspace.getViewForTag(mFolderInfo);
2585 if (folderIcon != null) {
2586 folderIcon.setText(name);
2587 getWorkspace().requestLayout();
2588 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002589 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002590 mWorkspaceLoading = true;
2591 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002592 }
2593 }
2594 }
2595 cleanup();
2596 }
2597
2598 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002599 dismissDialog(DIALOG_RENAME_FOLDER);
2600 mWaitingForResult = false;
2601 mFolderInfo = null;
2602 }
2603 }
2604
Daniel Sandler843e8602010-06-07 14:59:01 -04002605 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2606 public boolean isAllAppsVisible() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002607 return (mState == State.ALL_APPS) || (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002608 }
2609
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002610 // AllAppsView.Watcher
2611 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002612 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2613 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002614 mWorkspace.setVisibility(View.GONE);
2615 }
2616 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002617
Michael Jurkaafca9532011-02-16 14:50:35 -08002618 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002619 button.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002620 }
2621
2622 private void hideToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002623 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2624 button.setVisibility(View.INVISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002625 button.setAlpha(0.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002626 }
2627
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002628 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002629 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002630 *
2631 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2632 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002633 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002634 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002635 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002636 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002637 final boolean showing = show;
2638 final boolean hiding = !show;
2639
2640 final int duration = show ?
2641 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2642 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2643
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002644 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002645 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002646 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002647 anim.addUpdateListener(new AnimatorUpdateListener() {
2648 public void onAnimationUpdate(ValueAnimator animation) {
2649 view.setAlpha((Float) animation.getAnimatedValue());
2650 }
2651 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002652 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002653 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002654 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002655 if (showing) showAndEnableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002656 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002657 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002658 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002659 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002660 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002661 });
2662 seq.play(anim);
2663 } else {
2664 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002665 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002666 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002667 } else {
2668 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002669 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002670 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002671 }
2672
2673 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002674 * Show/hide the appropriate toolbar buttons for newState.
2675 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002676 *
2677 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002678 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2679 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002680 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002681 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002682 switch (newState) {
2683 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002684 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002685 mDeleteZone.setDragAndDropEnabled(true);
Winson Chung785d2eb2011-04-14 16:08:02 -07002686 if (LauncherApplication.isScreenXLarge()) {
2687 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
2688 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002689 break;
2690 case ALL_APPS:
Winson Chung785d2eb2011-04-14 16:08:02 -07002691 case APPS_CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002692 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002693 mDeleteZone.setDragAndDropEnabled(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002694 if (LauncherApplication.isScreenXLarge()) {
2695 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
2696 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002697 break;
2698 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002699 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002700 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002701 break;
2702 }
2703 }
2704
2705 /**
2706 * Helper method for the cameraZoomIn/cameraZoomOut animations
2707 * @param view The view being animated
2708 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2709 * @param scaleFactor The scale factor used for the zoom
2710 */
2711 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2712 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002713
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002714 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002715 // Set pivotY so that at the starting zoom factor, the view is partially
2716 // visible. Modifying initialHeightFactor changes how much of the view is
2717 // initially showing, and hence the perceived angle from which the view enters.
Winson Chung785d2eb2011-04-14 16:08:02 -07002718 if (state == State.ALL_APPS || state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002719 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002720 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002721 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002722 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002723 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002724 }
2725 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002726
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002727 /**
2728 * Zoom the camera out from the workspace to reveal 'toView'.
2729 * Assumes that the view to show is anchored at either the very top or very bottom
2730 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002731 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002732 */
Winson Chung20f71112011-04-06 14:22:04 -07002733 private void cameraZoomOut(State toState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002734 final Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002735 final boolean toAllApps = (toState == State.ALL_APPS)
2736 || (toState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002737
Winson Chung785d2eb2011-04-14 16:08:02 -07002738 final int duration = (toAllApps ?
2739 res.getInteger(R.integer.config_appsCustomizeZoomInTime) :
2740 res.getInteger(R.integer.config_customizeZoomInTime));
2741 final int fadeDuration = (toAllApps ?
2742 res.getInteger(R.integer.config_appsCustomizeFadeInTime) :
2743 res.getInteger(R.integer.config_customizeFadeInTime));
Adam Cohenf16e5712011-01-13 13:31:45 -08002744
2745 final float scale = toAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002746 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002747 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2748
Winson Chung785d2eb2011-04-14 16:08:02 -07002749 View tmpView;
2750 if (toAllApps) {
2751 tmpView = (LauncherApplication.isScreenXLarge())
2752 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2753 } else {
2754 tmpView = mHomeCustomizationDrawer;
2755 }
2756 final View toView = tmpView;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002757
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002758 setPivotsForZoom(toView, toState, scale);
2759
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002760 if (toAllApps) {
Winson Chung20f71112011-04-06 14:22:04 -07002761 if (!springLoaded) {
2762 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2763
Winson Chung785d2eb2011-04-14 16:08:02 -07002764 if (LauncherApplication.isScreenXLarge()) {
2765 // Everytime we launch into AllApps, we reset the successful drop flag which
2766 // controls when it should hide/show the mini workspaces
2767 mAllAppsPagedView.resetSuccessfulDropFlag();
2768 }
Winson Chung20f71112011-04-06 14:22:04 -07002769 } else {
2770 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
2771 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002772 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002773 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002774 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002775
2776 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002777 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002778 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002779 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2780 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002781 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002782 toView.setFastScaleX(a * scale + b * 1f);
2783 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002784 }
2785 });
Adam Cohen61033d32010-11-15 18:29:44 -08002786
Adam Cohenf16e5712011-01-13 13:31:45 -08002787 if (toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002788 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002789 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002790 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002791 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2792 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002793 // don't need to invalidate because we do so above
2794 toView.setFastAlpha(a * 0f + b * 1f);
2795 }
2796 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002797 alphaAnim.start();
2798 }
2799
Michael Jurkaabded662011-03-04 12:06:57 -08002800 if (toView instanceof LauncherTransitionable) {
2801 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002802 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002803 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002804 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002805 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002806 // Prepare the position
2807 toView.setTranslationX(0.0f);
2808 toView.setTranslationY(0.0f);
2809 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002810 toView.bringToFront();
Adam Cohenf16e5712011-01-13 13:31:45 -08002811 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002812 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002813 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002814 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002815 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002816 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002817 // If we don't set the final scale values here, if this animation is cancelled
2818 // it will have the wrong scale value and subsequent cameraPan animations will
2819 // not fix that
2820 toView.setScaleX(1.0f);
2821 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002822 if (toView instanceof LauncherTransitionable) {
2823 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002824 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002825 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002826 });
2827
Chet Haaseb1254a62010-09-07 13:35:00 -07002828 AnimatorSet toolbarHideAnim = new AnimatorSet();
2829 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002830 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2831
2832 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002833 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002834
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002835 if (mStateAnimation != null) mStateAnimation.cancel();
2836 mStateAnimation = new AnimatorSet();
2837 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2838 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002839
2840 // Show the new toolbar buttons just as the main animation is ending
2841 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002842 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2843 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002844 } else {
2845 toView.setTranslationX(0.0f);
2846 toView.setTranslationY(0.0f);
2847 toView.setScaleX(1.0f);
2848 toView.setScaleY(1.0f);
2849 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002850 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002851 if (toView instanceof LauncherTransitionable) {
2852 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2853 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2854 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002855 hideAndShowToolbarButtons(toState, null, null);
2856 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002857 }
2858
2859 /**
2860 * Zoom the camera back into the workspace, hiding 'fromView'.
2861 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002862 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2863 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002864 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08002865 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002866 Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002867 final boolean fromAllApps = (fromState == State.ALL_APPS)
2868 || (fromState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002869
2870 int duration = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002871 res.getInteger(R.integer.config_appsCustomizeZoomOutTime) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002872 res.getInteger(R.integer.config_customizeZoomOutTime);
2873
Michael Jurka742574b2011-02-02 23:51:01 -08002874 final float scaleFactor = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002875 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002876 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2877
Winson Chung785d2eb2011-04-14 16:08:02 -07002878 View tmpView;
2879 if (fromAllApps) {
2880 tmpView = (LauncherApplication.isScreenXLarge())
2881 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2882 } else {
2883 tmpView = mHomeCustomizationDrawer;
2884 }
2885 final View fromView = tmpView;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002886
Winson Chung785d2eb2011-04-14 16:08:02 -07002887 if (LauncherApplication.isScreenXLarge()) {
2888 mCustomizePagedView.endChoiceMode();
2889 mAllAppsPagedView.endChoiceMode();
2890 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002891
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002892 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002893
Michael Jurkad3ef3062010-11-23 16:23:58 -08002894 if (!springLoaded) {
2895 mWorkspace.unshrink(animated);
2896 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002897 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002898 if (mStateAnimation != null) mStateAnimation.cancel();
2899 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002900
Michael Jurka742574b2011-02-02 23:51:01 -08002901 final float oldScaleX = fromView.getScaleX();
2902 final float oldScaleY = fromView.getScaleY();
2903
2904 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2905 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002906 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2907 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002908 ((View)fromView.getParent()).fastInvalidate();
2909 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2910 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2911 }
2912 });
Michael Jurkaabded662011-03-04 12:06:57 -08002913 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002914 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002915 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002916 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2917 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002918 // don't need to invalidate because we do so above
2919 fromView.setFastAlpha(a * 1f + b * 0f);
2920 }
2921 });
Michael Jurkaabded662011-03-04 12:06:57 -08002922 if (fromView instanceof LauncherTransitionable) {
2923 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002924 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002925 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002926 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002927 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002928 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002929 if (fromView instanceof LauncherTransitionable) {
2930 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002931 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002932 }
2933 });
2934
Chet Haaseb1254a62010-09-07 13:35:00 -07002935 AnimatorSet toolbarHideAnim = new AnimatorSet();
2936 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002937 if (!springLoaded) {
2938 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2939 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002940
Adam Cohen61033d32010-11-15 18:29:44 -08002941 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002942
2943 // Show the new toolbar buttons at the very end of the whole animation
2944 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2945 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002946 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2947 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002948 } else {
2949 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002950 if (fromView instanceof LauncherTransitionable) {
2951 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2952 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2953 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002954 if (!springLoaded) {
2955 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2956 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002957 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002958 }
2959
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002960 void showWorkspace(boolean animated) {
2961 showWorkspace(animated, null);
2962 }
2963
2964 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002965 if (layout != null) {
2966 // always animated, but that's ok since we never specify a layout and
2967 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002968 mWorkspace.unshrink(layout);
2969 } else {
2970 mWorkspace.unshrink(animated);
2971 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002972 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002973 closeAllApps(animated);
2974 } else if (mState == State.CUSTOMIZE) {
2975 hideCustomizationDrawer(animated);
2976 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002977
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002978 // Change the state *after* we've called all the transition code
2979 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002980
Winson Chung5fb63472011-02-02 17:03:37 -08002981 // Resume the auto-advance of widgets
2982 mUserPresent = true;
2983 updateRunning();
2984
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002985 // send an accessibility event to announce the context change
2986 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002987 }
2988
Michael Jurkad3ef3062010-11-23 16:23:58 -08002989 void enterSpringLoadedDragMode(CellLayout layout) {
2990 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07002991 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002992 mState = State.ALL_APPS_SPRING_LOADED;
Winson Chung785d2eb2011-04-14 16:08:02 -07002993 if (LauncherApplication.isScreenXLarge()) {
2994 cameraZoomIn(State.ALL_APPS, true, true);
2995 } else {
2996 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
2997 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002998 } else if (mState == State.CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002999 mState = State.CUSTOMIZE_SPRING_LOADED;
Winson Chung20f71112011-04-06 14:22:04 -07003000 cameraZoomIn(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08003001 }/* else {
3002 // we're already in spring loaded mode; don't do anything
3003 }*/
3004 }
3005
3006 void exitSpringLoadedDragMode() {
3007 if (mState == State.ALL_APPS_SPRING_LOADED) {
3008 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003009 if (LauncherApplication.isScreenXLarge()) {
3010 cameraZoomOut(State.ALL_APPS, true, true);
3011 mState = State.ALL_APPS;
3012 } else {
3013 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
3014 mState = State.APPS_CUSTOMIZE;
3015 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003016 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
3017 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
Winson Chung20f71112011-04-06 14:22:04 -07003018 cameraZoomOut(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08003019 mState = State.CUSTOMIZE;
3020 }/* else {
3021 // we're not in spring loaded mode; don't do anything
3022 }*/
3023 }
3024
Winson Chung785d2eb2011-04-14 16:08:02 -07003025 void showAllApps(boolean animated) {
3026 if (mState != State.WORKSPACE) return;
3027 if (LauncherApplication.isScreenXLarge()) {
3028 cameraZoomOut(State.ALL_APPS, animated, false);
3029 ((View) mAllAppsGrid).requestFocus();
3030
3031 // TODO: fade these two too
3032 mDeleteZone.setVisibility(View.GONE);
3033
3034 // Change the state *after* we've called all the transition code
3035 mState = State.ALL_APPS;
3036 } else {
3037 View appsCustomizePane = findViewById(R.id.apps_customize_pane);
3038 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
3039 appsCustomizePane.requestFocus();
3040
3041 // Change the state *after* we've called all the transition code
3042 mState = State.APPS_CUSTOMIZE;
3043 }
3044
3045 // Pause the auto-advance of widgets until we are out of AllApps
3046 mUserPresent = false;
3047 updateRunning();
3048
3049 // Send an accessibility event to announce the context change
3050 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
3051 }
3052
Joe Onoratob2061212009-11-24 16:13:54 -05003053 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08003054 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05003055 * - Home from workspace
3056 * - from center screen
3057 * - from other screens
3058 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003059 * - from center screen
3060 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003061 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003062 * - from center screen
3063 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003064 * - Launch app from workspace and quit
3065 * - with back
3066 * - with home
3067 * - Launch app from all apps and quit
3068 * - with back
3069 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003070 * - Go to a screen that's not the default, then all
3071 * apps, and launch and app, and go back
3072 * - with back
3073 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003074 * - On workspace, long press power and go back
3075 * - with back
3076 * - with home
3077 * - On all apps, long press power and go back
3078 * - with back
3079 * - with home
3080 * - On workspace, power off
3081 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003082 * - Launch an app and turn off the screen while in that app
3083 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003084 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003085 * - From all apps
3086 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003087 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3088 * - From all apps
3089 * - From the center workspace
3090 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003091 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003092 void closeAllApps(boolean animated) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003093 if (LauncherApplication.isScreenXLarge()) {
3094 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
3095 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung20f71112011-04-06 14:22:04 -07003096 cameraZoomIn(State.ALL_APPS, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003097
3098 // Set focus to the AllApps button
3099 findViewById(R.id.all_apps_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003100 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003101 } else {
3102 if (mState == State.APPS_CUSTOMIZE || mState == State.ALL_APPS_SPRING_LOADED) {
3103 mWorkspace.setVisibility(View.VISIBLE);
3104 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
3105
3106 // Set focus to the AllApps button
3107 findViewById(R.id.all_apps_button).requestFocus();
3108 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003109 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003110 }
3111
Joe Onorato7c312c12009-08-13 21:36:53 -07003112 void lockAllApps() {
3113 // TODO
3114 }
3115
3116 void unlockAllApps() {
3117 // TODO
3118 }
3119
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003120 // Show the customization drawer (only exists in x-large configuration)
3121 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003122 if (mState != State.WORKSPACE) {
3123 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003124 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003125
Winson Chung20f71112011-04-06 14:22:04 -07003126 cameraZoomOut(State.CUSTOMIZE, animated, false);
Michael Jurkaafca9532011-02-16 14:50:35 -08003127
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003128 // Change the state *after* we've called all the transition code
3129 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003130
3131 // Pause the auto-advance of widgets until we are out of Customization drawer
3132 mUserPresent = false;
3133 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003134 }
3135
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003136 // Hide the customization drawer (only exists in x-large configuration)
3137 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003138 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Winson Chung20f71112011-04-06 14:22:04 -07003139 cameraZoomIn(State.CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003140
3141 // Set focus to the customize button
3142 findViewById(R.id.configure_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003143 }
3144 }
3145
Patrick Dubroy5f445422011-02-18 14:35:21 -08003146 /**
3147 * Add an item from all apps or customize onto the given workspace screen.
3148 * If layout is null, add to the current screen.
3149 */
3150 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003151 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3152 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003153 } else {
3154 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003155 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003156 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003157
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003158 void onWorkspaceClick(CellLayout layout) {
3159 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003160 }
3161
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003162 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003163 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003164 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003165 // Look for the toolbar icon specified in the activity meta-data
3166 Bundle metaData = packageManager.getActivityInfo(
3167 activityName, PackageManager.GET_META_DATA).metaData;
3168 if (metaData != null) {
3169 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3170 if (iconResId != 0) {
3171 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003172 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003173 }
3174 }
3175 } catch (NameNotFoundException e) {
3176 // Do nothing
3177 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003178 return null;
3179 }
3180
3181 // if successful in getting icon, return it; otherwise, set button to use default drawable
3182 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3183 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3184 TextView button = (TextView) findViewById(buttonId);
3185 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3186
3187 // If we were unable to find the icon via the meta-data, use a generic one
3188 if (toolbarIcon == null) {
3189 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3190 return null;
3191 } else {
3192 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3193 return toolbarIcon.getConstantState();
3194 }
3195 }
3196
3197 // if successful in getting icon, return it; otherwise, set button to use default drawable
3198 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3199 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3200 ImageView button = (ImageView) findViewById(buttonId);
3201 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3202
Michael Jurka0423dcf2010-10-05 14:56:18 -07003203 // If we were unable to find the icon via the meta-data, use a generic one
3204 if (toolbarIcon == null) {
3205 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003206 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003207 } else {
3208 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003209 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003210 }
3211 }
3212
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003213 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3214 TextView button = (TextView) findViewById(buttonId);
3215 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3216 }
3217
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003218 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003219 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003220 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003221 }
3222
Michael Jurka0423dcf2010-10-05 14:56:18 -07003223 private void updateGlobalSearchIcon() {
3224 if (LauncherApplication.isScreenXLarge()) {
Winson Chung97d85d22011-04-13 11:27:36 -07003225 final View searchButton = findViewById(R.id.search_button);
3226 final View searchDivider = findViewById(R.id.search_divider);
3227
Michael Jurka0423dcf2010-10-05 14:56:18 -07003228 final SearchManager searchManager =
3229 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3230 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003231 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003232 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003233 R.id.search_button, activityName, R.drawable.ic_generic_search);
Winson Chung97d85d22011-04-13 11:27:36 -07003234 searchButton.setVisibility(View.VISIBLE);
3235 searchDivider.setVisibility(View.VISIBLE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003236 } else {
Winson Chung97d85d22011-04-13 11:27:36 -07003237 searchButton.setVisibility(View.GONE);
3238 searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003239 }
3240 }
3241 }
3242
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003243 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003244 updateButtonWithDrawable(R.id.search_button, d);
3245 }
3246
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003247 private void updateVoiceSearchIcon() {
3248 if (LauncherApplication.isScreenXLarge()) {
Winson Chung97d85d22011-04-13 11:27:36 -07003249 final View searchDivider = findViewById(R.id.search_divider);
3250 final View voiceButton = findViewById(R.id.voice_button);
3251
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003252 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3253 ComponentName activityName = intent.resolveActivity(getPackageManager());
3254 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003255 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003256 R.id.voice_button, activityName, R.drawable.ic_voice_search);
Winson Chung97d85d22011-04-13 11:27:36 -07003257 searchDivider.setVisibility(View.VISIBLE);
3258 voiceButton.setVisibility(View.VISIBLE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003259 } else {
Winson Chung97d85d22011-04-13 11:27:36 -07003260 searchDivider.setVisibility(View.GONE);
3261 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003262 }
3263 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003264 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003265
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003266 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003267 updateButtonWithDrawable(R.id.voice_button, d);
3268 }
3269
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003270 /**
3271 * Sets the app market icon (shown when all apps is visible on x-large screens)
3272 */
3273 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003274 final View marketButton = findViewById(R.id.market_button);
3275 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3276 // Find the app market activity by resolving an intent.
3277 // (If multiple app markets are installed, it will return the ResolverActivity.)
3278 ComponentName activityName = intent.resolveActivity(getPackageManager());
3279 if (activityName != null) {
3280 mAppMarketIntent = intent;
3281 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
3282 R.id.market_button, activityName, R.drawable.app_market_generic);
3283 marketButton.setVisibility(View.VISIBLE);
3284 } else {
3285 // We should hide and disable the view so that we don't try and restore the visibility
3286 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3287 marketButton.setVisibility(View.GONE);
3288 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003289 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003290 }
3291
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003292 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003293 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003294 }
3295
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003296 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003297 * Displays the shortcut creation dialog and launches, if necessary, the
3298 * appropriate activity.
3299 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003300 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003301 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3302 DialogInterface.OnShowListener {
3303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003304 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003305
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003306 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003307 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003308
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003309 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3310 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003311 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003312
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003313 builder.setInverseBackgroundForced(true);
3314
3315 AlertDialog dialog = builder.create();
3316 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003317 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003318 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003319
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003320 return dialog;
3321 }
3322
3323 public void onCancel(DialogInterface dialog) {
3324 mWaitingForResult = false;
3325 cleanup();
3326 }
3327
Romain Guycbb89e42009-06-08 15:52:54 -07003328 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003329 }
3330
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003331 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003332 try {
3333 dismissDialog(DIALOG_CREATE_SHORTCUT);
3334 } catch (Exception e) {
3335 // An exception is thrown if the dialog is not visible, which is fine
3336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003337 }
3338
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003339 /**
3340 * Handle the action clicked in the "Add to home" dialog.
3341 */
3342 public void onClick(DialogInterface dialog, int which) {
3343 Resources res = getResources();
3344 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003345
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003346 switch (which) {
3347 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003348 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003349 break;
3350 }
Romain Guycbb89e42009-06-08 15:52:54 -07003351
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003352 case AddAdapter.ITEM_APPWIDGET: {
3353 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003354
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003355 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3356 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003357 // start the pick activity
3358 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3359 break;
3360 }
Romain Guycbb89e42009-06-08 15:52:54 -07003361
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003362 case AddAdapter.ITEM_LIVE_FOLDER: {
3363 // Insert extra item to handle inserting folder
3364 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003365
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003366 ArrayList<String> shortcutNames = new ArrayList<String>();
3367 shortcutNames.add(res.getString(R.string.group_folder));
3368 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003369
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003370 ArrayList<ShortcutIconResource> shortcutIcons =
3371 new ArrayList<ShortcutIconResource>();
3372 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3373 R.drawable.ic_launcher_folder));
3374 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3375
3376 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3377 pickIntent.putExtra(Intent.EXTRA_INTENT,
3378 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3379 pickIntent.putExtra(Intent.EXTRA_TITLE,
3380 getText(R.string.title_select_live_folder));
3381 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003382
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003383 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3384 break;
3385 }
3386
3387 case AddAdapter.ITEM_WALLPAPER: {
3388 startWallpaper();
3389 break;
3390 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003391 }
3392 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003393
3394 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003395 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003396 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003397 }
3398
3399 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003400 * Receives notifications when applications are added/removed.
3401 */
3402 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3403 @Override
3404 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003405 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003406 String reason = intent.getStringExtra("reason");
3407 if (!"homekey".equals(reason)) {
3408 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003409 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003410 animate = false;
3411 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003412 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003413 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003414 }
3415 }
3416
3417 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003418 * Receives notifications whenever the appwidgets are reset.
3419 */
3420 private class AppWidgetResetObserver extends ContentObserver {
3421 public AppWidgetResetObserver() {
3422 super(new Handler());
3423 }
3424
3425 @Override
3426 public void onChange(boolean selfChange) {
3427 onAppWidgetReset();
3428 }
3429 }
3430
3431 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003432 * If the activity is currently paused, signal that we need to re-run the loader
3433 * in onResume.
3434 *
3435 * This needs to be called from incoming places where resources might have been loaded
3436 * while we are paused. That is becaues the Configuration might be wrong
3437 * when we're not running, and if it comes back to what it was when we
3438 * were paused, we are not restarted.
3439 *
3440 * Implementation of the method from LauncherModel.Callbacks.
3441 *
3442 * @return true if we are currently paused. The caller might be able to
3443 * skip some work in that case since we will come back again.
3444 */
3445 public boolean setLoadOnResume() {
3446 if (mPaused) {
3447 Log.i(TAG, "setLoadOnResume");
3448 mOnResumeNeedsLoad = true;
3449 return true;
3450 } else {
3451 return false;
3452 }
3453 }
3454
3455 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003456 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003457 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003458 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003459 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003460 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003461 } else {
3462 return SCREEN_COUNT / 2;
3463 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003464 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003465
Winson Chunga12a2502010-12-20 14:41:35 -08003466 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003467 mAllAppsPagedView = view;
3468 }
3469
Joe Onorato9c1289c2009-08-17 11:03:03 -04003470 /**
3471 * Refreshes the shortcuts shown on the workspace.
3472 *
3473 * Implementation of the method from LauncherModel.Callbacks.
3474 */
3475 public void startBinding() {
3476 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003477
3478 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003479 int count = workspace.getChildCount();
3480 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003481 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003482 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3483 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003484 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003485
Joe Onorato9c1289c2009-08-17 11:03:03 -04003486 if (DEBUG_USER_INTERFACE) {
3487 android.widget.Button finishButton = new android.widget.Button(this);
3488 finishButton.setText("Finish");
3489 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3490
3491 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3492 public void onClick(View v) {
3493 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003494 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003495 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003496 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003497
3498 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3499 // mDesktopItems -> AppWidgetInfo -> hostView).
3500 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003501 }
3502
3503 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003504 * Bind the items start-end from the list.
3505 *
3506 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003507 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003508 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3509
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003510 setLoadOnResume();
3511
Joe Onorato9c1289c2009-08-17 11:03:03 -04003512 final Workspace workspace = mWorkspace;
3513
3514 for (int i=start; i<end; i++) {
3515 final ItemInfo item = shortcuts.get(i);
3516 mDesktopItems.add(item);
3517 switch (item.itemType) {
3518 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3519 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003520 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003521 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3522 false);
3523 break;
3524 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3525 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003526 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003527 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003528 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3529 false);
3530 break;
3531 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3532 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3533 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003534 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003535 (LiveFolderInfo) item);
3536 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3537 false);
3538 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003539 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003540 }
3541
Joe Onorato9c1289c2009-08-17 11:03:03 -04003542 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003543 }
3544
Joe Onorato9c1289c2009-08-17 11:03:03 -04003545 /**
3546 * Implementation of the method from LauncherModel.Callbacks.
3547 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003548 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003549 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003550 sFolders.clear();
3551 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003552 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003553
3554 /**
3555 * Add the views for a widget to the workspace.
3556 *
3557 * Implementation of the method from LauncherModel.Callbacks.
3558 */
3559 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003560 setLoadOnResume();
3561
Daniel Sandler843e8602010-06-07 14:59:01 -04003562 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3563 if (DEBUG_WIDGETS) {
3564 Log.d(TAG, "bindAppWidget: " + item);
3565 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003566 final Workspace workspace = mWorkspace;
3567
3568 final int appWidgetId = item.appWidgetId;
3569 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003570 if (DEBUG_WIDGETS) {
3571 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3572 }
3573
Joe Onorato9c1289c2009-08-17 11:03:03 -04003574 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3575
Joe Onorato9c1289c2009-08-17 11:03:03 -04003576 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3577 item.hostView.setTag(item);
3578
3579 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3580 item.cellY, item.spanX, item.spanY, false);
3581
Adam Cohended9f8d2010-11-03 13:25:16 -07003582 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3583
Joe Onorato9c1289c2009-08-17 11:03:03 -04003584 workspace.requestLayout();
3585
3586 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003587
3588 if (DEBUG_WIDGETS) {
3589 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3590 + (SystemClock.uptimeMillis()-start) + "ms");
3591 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003592 }
3593
3594 /**
3595 * Callback saying that there aren't any more items to bind.
3596 *
3597 * Implementation of the method from LauncherModel.Callbacks.
3598 */
3599 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003600 setLoadOnResume();
3601
Joe Onorato9c1289c2009-08-17 11:03:03 -04003602 if (mSavedState != null) {
3603 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003604 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003605 }
3606
3607 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3608 if (userFolders != null) {
3609 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003610 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 if (info != null) {
3612 openFolder(info);
3613 }
3614 }
3615 final Folder openFolder = mWorkspace.getOpenFolder();
3616 if (openFolder != null) {
3617 openFolder.requestFocus();
3618 }
3619 }
3620
Joe Onorato9c1289c2009-08-17 11:03:03 -04003621 mSavedState = null;
3622 }
3623
3624 if (mSavedInstanceState != null) {
3625 super.onRestoreInstanceState(mSavedInstanceState);
3626 mSavedInstanceState = null;
3627 }
3628
Winson Chung32bb5e52011-01-31 14:51:56 -08003629 // Workaround a bug that occurs when rotating the device while the customization mode is
3630 // open, we trigger a new layout on all the CellLayout children.
3631 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3632 final int childCount = mWorkspace.getChildCount();
3633 for (int i = 0; i < childCount; ++i) {
3634 mWorkspace.getChildAt(i).requestLayout();
3635 }
3636 }
3637
Joe Onorato9c1289c2009-08-17 11:03:03 -04003638 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003639
3640 // If we received the result of any pending adds while the loader was running (e.g. the
3641 // widget configuration forced an orientation change), process them now.
3642 for (int i = 0; i < sPendingAddList.size(); i++) {
3643 completeAdd(sPendingAddList.get(i));
3644 }
3645 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003646 }
3647
3648 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003649 * Updates the icons on the launcher that are affected by changes to the package list
3650 * on the device.
3651 */
3652 private void updateIconsAffectedByPackageManagerChanges() {
3653 updateAppMarketIcon();
3654 updateGlobalSearchIcon();
3655 updateVoiceSearchIcon();
3656 }
3657
3658 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003659 * Add the icons for all apps.
3660 *
3661 * Implementation of the method from LauncherModel.Callbacks.
3662 */
3663 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003664 if (mAllAppsGrid != null) {
3665 mAllAppsGrid.setApps(apps);
3666 }
3667 if (mAppsCustomizeContent != null) {
3668 mAppsCustomizeContent.setApps(apps);
3669 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003670 if (mCustomizePagedView != null) {
3671 mCustomizePagedView.setApps(apps);
3672 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003673 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003674 }
3675
3676 /**
3677 * A package was installed.
3678 *
3679 * Implementation of the method from LauncherModel.Callbacks.
3680 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003681 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003682 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003683 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chung785d2eb2011-04-14 16:08:02 -07003684 if (mAllAppsGrid != null) {
3685 mAllAppsGrid.addApps(apps);
3686 }
3687 if (mAppsCustomizeContent != null) {
3688 mAppsCustomizeContent.addApps(apps);
3689 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003690 if (mCustomizePagedView != null) {
3691 mCustomizePagedView.addApps(apps);
3692 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003693 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003694 }
3695
3696 /**
3697 * A package was updated.
3698 *
3699 * Implementation of the method from LauncherModel.Callbacks.
3700 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003701 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003702 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003703 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003704 if (mWorkspace != null) {
3705 mWorkspace.updateShortcuts(apps);
3706 }
3707 if (mAllAppsGrid != null) {
3708 mAllAppsGrid.updateApps(apps);
3709 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003710 if (mAppsCustomizeContent != null) {
3711 mAppsCustomizeContent.updateApps(apps);
3712 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003713 if (mCustomizePagedView != null) {
3714 mCustomizePagedView.updateApps(apps);
3715 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003716 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003717 }
3718
3719 /**
3720 * A package was uninstalled.
3721 *
3722 * Implementation of the method from LauncherModel.Callbacks.
3723 */
Joe Onorato36115782010-06-17 13:28:48 -04003724 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003725 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003726 if (permanent) {
3727 mWorkspace.removeItems(apps);
3728 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003729 if (mAllAppsGrid != null) {
3730 mAllAppsGrid.removeApps(apps);
3731 }
3732 if (mAppsCustomizeContent != null) {
3733 mAppsCustomizeContent.removeApps(apps);
3734 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003735 if (mCustomizePagedView != null) {
3736 mCustomizePagedView.removeApps(apps);
3737 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003738 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003739 }
Joe Onoratobe386092009-11-17 17:32:16 -08003740
3741 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003742 * A number of packages were updated.
3743 */
3744 public void bindPackagesUpdated() {
3745 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003746 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003747 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003748 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003749 if (mAppsCustomizeContent != null) {
3750 mAppsCustomizeContent.onPackagesUpdated();
3751 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003752 }
3753
Winson Chung400438b2011-01-16 17:53:48 -08003754 private int mapConfigurationOriActivityInfoOri(int configOri) {
3755 final Display d = getWindowManager().getDefaultDisplay();
3756 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3757 switch (d.getRotation()) {
3758 case Surface.ROTATION_0:
3759 case Surface.ROTATION_180:
3760 // We are currently in the same basic orientation as the natural orientation
3761 naturalOri = configOri;
3762 break;
3763 case Surface.ROTATION_90:
3764 case Surface.ROTATION_270:
3765 // We are currently in the other basic orientation to the natural orientation
3766 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3767 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3768 break;
3769 }
3770
3771 int[] oriMap = {
3772 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3773 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3774 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3775 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3776 };
3777 // Since the map starts at portrait, we need to offset if this device's natural orientation
3778 // is landscape.
3779 int indexOffset = 0;
3780 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3781 indexOffset = 1;
3782 }
3783 return oriMap[(d.getRotation() + indexOffset) % 4];
3784 }
3785 public void lockScreenOrientation() {
3786 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3787 .getConfiguration().orientation));
3788 }
3789 public void unlockScreenOrientation() {
3790 mHandler.postDelayed(new Runnable() {
3791 public void run() {
3792 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3793 }
3794 }, mRestoreScreenOrientationDelay);
3795 }
3796
Winson Chung80baf5a2010-08-09 16:03:15 -07003797 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003798 * Prints out out state for debugging.
3799 */
3800 public void dumpState() {
3801 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003802 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003803 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3804 Log.d(TAG, "mRestoring=" + mRestoring);
3805 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3806 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3807 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003808 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003809 mModel.dumpState();
Winson Chung785d2eb2011-04-14 16:08:02 -07003810 if (mAllAppsGrid != null) {
3811 mAllAppsGrid.dumpState();
3812 }
3813 if (mAppsCustomizeContent != null) {
3814 mAppsCustomizeContent.dumpState();
3815 }
Joe Onoratobe386092009-11-17 17:32:16 -08003816 Log.d(TAG, "END launcher2 dump state");
3817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003818}
Michael Jurka2763be32011-02-24 11:19:57 -08003819
Michael Jurkaabded662011-03-04 12:06:57 -08003820interface LauncherTransitionable {
3821 void onLauncherTransitionStart(Animator animation);
3822 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003823}