blob: b2ec67c0141d0169a21640e895540e4496a5dd88 [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;
Michael Jurka8c920dd2011-01-20 14:16:56 -08002009 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07002011 CellLayout cl = (CellLayout) parent;
2012 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05002013 if (folder instanceof DropTarget) {
2014 // Live folders aren't DropTargets.
2015 mDragController.removeDropTarget((DropTarget)folder);
2016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 }
2018 folder.onClose();
2019 }
2020
2021 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002022 * Re-listen when widgets are reset.
2023 */
2024 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002025 if (mAppWidgetHost != null) {
2026 mAppWidgetHost.startListening();
2027 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002028 }
2029
2030 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002031 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2032 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002034 private void unbindDesktopItems() {
2035 for (ItemInfo item: mDesktopItems) {
2036 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002038 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 /**
2042 * Launches the intent referred by the clicked shortcut.
2043 *
2044 * @param v The view representing the clicked shortcut.
2045 */
2046 public void onClick(View v) {
2047 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002048 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002050 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002051 int[] pos = new int[2];
2052 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002053 intent.setSourceBounds(new Rect(pos[0], pos[1],
2054 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08002055 boolean success = startActivitySafely(intent, tag);
2056
2057 if (success && v instanceof BubbleTextView) {
2058 mWaitingForResume = (BubbleTextView) v;
2059 mWaitingForResume.setStayPressed(true);
2060 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 } else if (tag instanceof FolderInfo) {
2062 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002063 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002064 if (mState == State.ALL_APPS) {
2065 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002066 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002067 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002068 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 }
2070 }
2071
Michael Jurka0e260592010-06-30 17:07:39 -07002072 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002073 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002074 // clicking anywhere on the workspace causes the customization drawer to slide down
2075 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002076 return false;
2077 }
2078
Michael Jurkaaf442092010-06-10 17:01:57 -07002079 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002080 * Event handler for the search button
2081 *
2082 * @param v The view that was clicked.
2083 */
2084 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002085 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002086 // Use a custom animation for launching search
2087 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002088 }
2089
2090 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002091 * Event handler for the voice button
2092 *
2093 * @param v The view that was clicked.
2094 */
2095 public void onClickVoiceButton(View v) {
2096 startVoiceSearch();
2097 }
2098
2099 private void startVoiceSearch() {
2100 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2101 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2102 startActivity(intent);
2103 }
2104
2105 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002106 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002107 * enters home screen customization mode.
2108 *
2109 * @param v The view that was clicked.
2110 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002111 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002112 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002113 }
2114
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002115 /**
2116 * Event handler for the "grid" button that appears on the home screen, which
2117 * enters all apps mode.
2118 *
2119 * @param v The view that was clicked.
2120 */
2121 public void onClickAllAppsButton(View v) {
2122 showAllApps(true);
2123 }
2124
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002125 public void onClickAppMarketButton(View v) {
2126 if (mAppMarketIntent != null) {
2127 startActivitySafely(mAppMarketIntent, "app market");
2128 }
2129 }
2130
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002131 void startApplicationDetailsActivity(ComponentName componentName) {
2132 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002133 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2134 Uri.fromParts("package", packageName, null));
2135 startActivity(intent);
2136 }
2137
Patrick Dubroy5539af72010-09-07 15:22:01 -07002138 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2139 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2140 // System applications cannot be installed. For now, show a toast explaining that.
2141 // We may give them the option of disabling apps this way.
2142 int messageId = R.string.uninstall_system_app_text;
2143 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2144 } else {
2145 String packageName = appInfo.componentName.getPackageName();
2146 String className = appInfo.componentName.getClassName();
2147 Intent intent = new Intent(
2148 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2149 startActivity(intent);
2150 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002151 }
2152
Michael Jurkaddd62e92011-02-16 17:49:14 -08002153 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2155 try {
2156 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002157 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 } catch (ActivityNotFoundException e) {
2159 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002160 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 } catch (SecurityException e) {
2162 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002163 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002165 "or use the exported attribute for this activity. "
2166 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002168 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002170
Romain Guy8e633c52010-03-04 12:51:36 -08002171 void startActivityForResultSafely(Intent intent, int requestCode) {
2172 try {
2173 startActivityForResult(intent, requestCode);
2174 } catch (ActivityNotFoundException e) {
2175 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2176 } catch (SecurityException e) {
2177 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2178 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2179 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2180 "or use the exported attribute for this activity.", e);
2181 }
2182 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183
2184 private void handleFolderClick(FolderInfo folderInfo) {
2185 if (!folderInfo.opened) {
2186 // Close any open folder
2187 closeFolder();
2188 // Open the requested folder
2189 openFolder(folderInfo);
2190 } else {
2191 // Find the open folder...
2192 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2193 int folderScreen;
2194 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002195 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 // .. and close it
2197 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002198 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 // Close any folder open on the current screen
2200 closeFolder();
2201 // Pull the folder onto this screen
2202 openFolder(folderInfo);
2203 }
2204 }
2205 }
2206 }
2207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002209 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 * is animated relative to the specified View. If the View is null, no animation
2211 * is played.
2212 *
2213 * @param folderInfo The FolderInfo describing the folder to open.
2214 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002215 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 Folder openFolder;
2217
2218 if (folderInfo instanceof UserFolderInfo) {
2219 openFolder = UserFolder.fromXml(this);
2220 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002221 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 } else {
2223 return;
2224 }
2225
Joe Onorato00acb122009-08-04 16:04:30 -04002226 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 openFolder.setLauncher(this);
2228
2229 openFolder.bind(folderInfo);
2230 folderInfo.opened = true;
2231
Winson Chungaafa03c2010-06-11 17:34:16 -07002232 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2233
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 openFolder.onOpen();
2235 }
2236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002238 if (mState != State.WORKSPACE) {
2239 return false;
2240 }
2241
Romain Guy1fbc1c82009-11-09 20:43:08 -08002242 switch (v.getId()) {
2243 case R.id.previous_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002244 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002245 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2246 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002247 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002248 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002249 return true;
2250 case R.id.next_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002251 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002252 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2253 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002254 showPreviews(v);
2255 }
2256 return true;
2257 case R.id.all_apps_button:
Winson Chung785d2eb2011-04-14 16:08:02 -07002258 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002259 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2260 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2261 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002262 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002263 return true;
2264 }
2265
Joe Onorato9c1289c2009-08-17 11:03:03 -04002266 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 return false;
2268 }
2269
2270 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002271 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002272 }
2273
Michael Jurka0280c3b2010-09-17 15:00:07 -07002274 resetAddInfo();
2275 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002276 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002277 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 return true;
2279 }
2280
Michael Jurka0280c3b2010-09-17 15:00:07 -07002281 final View itemUnderLongClick = longClickCellInfo.cell;
2282
Winson Chung304dcde2011-01-07 11:17:23 -08002283 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002284 if (itemUnderLongClick == null) {
2285 // User long pressed on empty space
2286 mWorkspace.setAllowLongPress(false);
2287 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2288 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002289 if (LauncherApplication.isScreenXLarge()) {
2290 addItems();
2291 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002292 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002293 }
2294 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002295 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002296 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002297 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2298 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002299 mAddIntersectCellX = longClickCellInfo.cellX;
2300 mAddIntersectCellY = longClickCellInfo.cellY;
2301 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002302 }
2303 }
2304 }
2305 return true;
2306 }
2307
Romain Guye6b8e2f2009-11-10 11:56:55 -08002308 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002309 private void dismissPreview(final View v) {
2310 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002311 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002312 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2313 public void onDismiss() {
2314 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2315 int count = group.getChildCount();
2316 for (int i = 0; i < count; i++) {
2317 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2318 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002319 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2320 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2321
2322 v.setTag(R.id.workspace, null);
2323 v.setTag(R.id.icon, null);
2324 window.setOnDismissListener(null);
2325 }
2326 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002327 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002328 }
2329 v.setTag(null);
2330 }
2331
Romain Guyf8e6a802009-12-07 17:48:02 -08002332 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002333 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002334 }
2335
Romain Guya6abce82009-11-10 02:54:41 -08002336 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002337 final Resources resources = getResources();
2338 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002339
Romain Guya6abce82009-11-10 02:54:41 -08002340 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002341
Romain Guy9d31e9f2009-11-11 18:54:28 -08002342 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002343
Romain Guyf8e6a802009-12-07 17:48:02 -08002344 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002345 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002346 int extraW = (int) ((r.left + r.right) * max);
2347 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002348
2349 int aW = cell.getWidth() - extraW;
2350 float w = aW / max;
2351
2352 int width = cell.getWidth();
2353 int height = cell.getHeight();
2354 int x = cell.getLeftPadding();
2355 int y = cell.getTopPadding();
2356 width -= (x + cell.getRightPadding());
2357 height -= (y + cell.getBottomPadding());
2358
2359 float scale = w / width;
2360
2361 int count = end - start;
2362
2363 final float sWidth = width * scale;
2364 float sHeight = height * scale;
2365
Romain Guye6b8e2f2009-11-10 11:56:55 -08002366 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002367
Romain Guye6b8e2f2009-11-10 11:56:55 -08002368 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2369 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002370
2371 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002372 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002373 cell = (CellLayout) workspace.getChildAt(i);
2374
Romain Guyf8e6a802009-12-07 17:48:02 -08002375 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002376 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002377
2378 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002379 c.scale(scale, scale);
2380 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002381 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002382
Romain Guy9d31e9f2009-11-11 18:54:28 -08002383 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002384 image.setImageBitmap(bitmap);
2385 image.setTag(i);
2386 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002387 image.setOnFocusChangeListener(handler);
2388 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002389 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002390
2391 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002392 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2393
Winson Chungaafa03c2010-06-11 17:34:16 -07002394 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002395 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002396
2397 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002398 p.setContentView(preview);
2399 p.setWidth((int) (sWidth * count + extraW));
2400 p.setHeight((int) (sHeight + extraH));
2401 p.setAnimationStyle(R.style.AnimationPreview);
2402 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002403 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002404 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002405 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002406
Romain Guye6b8e2f2009-11-10 11:56:55 -08002407 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2408 public void onDismiss() {
2409 dismissPreview(anchor);
2410 }
2411 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002412
2413 anchor.setTag(p);
2414 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002415 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002416 }
2417
Romain Guy9d31e9f2009-11-11 18:54:28 -08002418 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002419 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002420
Romain Guye6b8e2f2009-11-10 11:56:55 -08002421 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002422 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002423 }
2424
2425 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002426 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002427 v.post(this);
2428 }
2429
2430 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002431 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002432 }
2433
2434 public void onFocusChange(View v, boolean hasFocus) {
2435 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002436 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002437 }
Romain Guya6abce82009-11-10 02:54:41 -08002438 }
2439 }
2440
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 Workspace getWorkspace() {
2442 return mWorkspace;
2443 }
2444
Michael Jurka3c38d562011-02-18 19:22:12 -08002445 TabHost getCustomizationDrawer() {
2446 return mHomeCustomizationDrawer;
2447 }
2448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449 @Override
2450 protected Dialog onCreateDialog(int id) {
2451 switch (id) {
2452 case DIALOG_CREATE_SHORTCUT:
2453 return new CreateShortcut().createDialog();
2454 case DIALOG_RENAME_FOLDER:
2455 return new RenameFolder().createDialog();
2456 }
2457
2458 return super.onCreateDialog(id);
2459 }
2460
2461 @Override
2462 protected void onPrepareDialog(int id, Dialog dialog) {
2463 switch (id) {
2464 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002465 break;
2466 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002467 if (mFolderInfo != null) {
2468 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2469 final CharSequence text = mFolderInfo.title;
2470 input.setText(text);
2471 input.setSelection(0, text.length());
2472 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002473 break;
2474 }
2475 }
2476
2477 void showRenameDialog(FolderInfo info) {
2478 mFolderInfo = info;
2479 mWaitingForResult = true;
2480 showDialog(DIALOG_RENAME_FOLDER);
2481 }
2482
Michael Jurka0280c3b2010-09-17 15:00:07 -07002483 private void showAddDialog(int intersectX, int intersectY) {
2484 resetAddInfo();
2485 mAddIntersectCellX = intersectX;
2486 mAddIntersectCellY = intersectY;
2487 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002488 mWaitingForResult = true;
2489 showDialog(DIALOG_CREATE_SHORTCUT);
2490 }
2491
Joe Onoratodeb98af2010-02-19 14:59:39 -08002492 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002493 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002494 Bundle bundle = new Bundle();
2495
2496 ArrayList<String> shortcutNames = new ArrayList<String>();
2497 shortcutNames.add(getString(R.string.group_applications));
2498 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2499
2500 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2501 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2502 R.drawable.ic_launcher_application));
2503 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2504
2505 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2506 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002507 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002508 pickIntent.putExtras(bundle);
2509
Joe Onoratodeb98af2010-02-19 14:59:39 -08002510 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002511 }
2512
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 private class RenameFolder {
2514 private EditText mInput;
2515
2516 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002517 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2518 mInput = (EditText) layout.findViewById(R.id.folder_name);
2519
2520 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2521 builder.setIcon(0);
2522 builder.setTitle(getString(R.string.rename_folder_title));
2523 builder.setCancelable(true);
2524 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2525 public void onCancel(DialogInterface dialog) {
2526 cleanup();
2527 }
2528 });
2529 builder.setNegativeButton(getString(R.string.cancel_action),
2530 new Dialog.OnClickListener() {
2531 public void onClick(DialogInterface dialog, int which) {
2532 cleanup();
2533 }
2534 }
2535 );
2536 builder.setPositiveButton(getString(R.string.rename_action),
2537 new Dialog.OnClickListener() {
2538 public void onClick(DialogInterface dialog, int which) {
2539 changeFolderName();
2540 }
2541 }
2542 );
2543 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002544
2545 final AlertDialog dialog = builder.create();
2546 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2547 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002548 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002549 mInput.requestFocus();
2550 InputMethodManager inputManager = (InputMethodManager)
2551 getSystemService(Context.INPUT_METHOD_SERVICE);
2552 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002553 }
2554 });
2555
2556 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 }
2558
2559 private void changeFolderName() {
2560 final String name = mInput.getText().toString();
2561 if (!TextUtils.isEmpty(name)) {
2562 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002563 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002564 mFolderInfo.title = name;
2565 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2566
Joe Onorato9c1289c2009-08-17 11:03:03 -04002567 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002568 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002569 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002570 } else {
2571 final FolderIcon folderIcon = (FolderIcon)
2572 mWorkspace.getViewForTag(mFolderInfo);
2573 if (folderIcon != null) {
2574 folderIcon.setText(name);
2575 getWorkspace().requestLayout();
2576 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002577 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002578 mWorkspaceLoading = true;
2579 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002580 }
2581 }
2582 }
2583 cleanup();
2584 }
2585
2586 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 dismissDialog(DIALOG_RENAME_FOLDER);
2588 mWaitingForResult = false;
2589 mFolderInfo = null;
2590 }
2591 }
2592
Daniel Sandler843e8602010-06-07 14:59:01 -04002593 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2594 public boolean isAllAppsVisible() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002595 return (mState == State.ALL_APPS) || (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002596 }
2597
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002598 // AllAppsView.Watcher
2599 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002600 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2601 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002602 mWorkspace.setVisibility(View.GONE);
2603 }
2604 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002605
Michael Jurkaafca9532011-02-16 14:50:35 -08002606 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002607 button.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002608 }
2609
2610 private void hideToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002611 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2612 button.setVisibility(View.INVISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002613 button.setAlpha(0.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002614 }
2615
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002616 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002617 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002618 *
2619 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2620 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002621 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002622 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002623 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002624 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002625 final boolean showing = show;
2626 final boolean hiding = !show;
2627
2628 final int duration = show ?
2629 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2630 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2631
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002632 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002633 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002634 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002635 anim.addUpdateListener(new AnimatorUpdateListener() {
2636 public void onAnimationUpdate(ValueAnimator animation) {
2637 view.setAlpha((Float) animation.getAnimatedValue());
2638 }
2639 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002640 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002641 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002642 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002643 if (showing) showAndEnableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002644 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002645 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002646 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002647 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002648 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002649 });
2650 seq.play(anim);
2651 } else {
2652 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002653 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002654 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002655 } else {
2656 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002657 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002658 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002659 }
2660
2661 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002662 * Show/hide the appropriate toolbar buttons for newState.
2663 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002664 *
2665 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002666 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2667 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002668 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002669 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002670 switch (newState) {
2671 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002672 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002673 mDeleteZone.setDragAndDropEnabled(true);
Winson Chung785d2eb2011-04-14 16:08:02 -07002674 if (LauncherApplication.isScreenXLarge()) {
2675 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
2676 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002677 break;
2678 case ALL_APPS:
Winson Chung785d2eb2011-04-14 16:08:02 -07002679 case APPS_CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002680 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002681 mDeleteZone.setDragAndDropEnabled(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002682 if (LauncherApplication.isScreenXLarge()) {
2683 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
2684 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002685 break;
2686 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002687 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002688 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002689 break;
2690 }
2691 }
2692
2693 /**
2694 * Helper method for the cameraZoomIn/cameraZoomOut animations
2695 * @param view The view being animated
2696 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2697 * @param scaleFactor The scale factor used for the zoom
2698 */
2699 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2700 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002701
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002702 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002703 // Set pivotY so that at the starting zoom factor, the view is partially
2704 // visible. Modifying initialHeightFactor changes how much of the view is
2705 // initially showing, and hence the perceived angle from which the view enters.
Winson Chung785d2eb2011-04-14 16:08:02 -07002706 if (state == State.ALL_APPS || state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002707 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002708 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002709 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002710 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002711 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002712 }
2713 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002714
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002715 /**
2716 * Zoom the camera out from the workspace to reveal 'toView'.
2717 * Assumes that the view to show is anchored at either the very top or very bottom
2718 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002719 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002720 */
Winson Chung20f71112011-04-06 14:22:04 -07002721 private void cameraZoomOut(State toState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002722 final Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002723 final boolean toAllApps = (toState == State.ALL_APPS)
2724 || (toState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002725
Winson Chung785d2eb2011-04-14 16:08:02 -07002726 final int duration = (toAllApps ?
2727 res.getInteger(R.integer.config_appsCustomizeZoomInTime) :
2728 res.getInteger(R.integer.config_customizeZoomInTime));
2729 final int fadeDuration = (toAllApps ?
2730 res.getInteger(R.integer.config_appsCustomizeFadeInTime) :
2731 res.getInteger(R.integer.config_customizeFadeInTime));
Adam Cohenf16e5712011-01-13 13:31:45 -08002732
2733 final float scale = toAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002734 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002735 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2736
Winson Chung785d2eb2011-04-14 16:08:02 -07002737 View tmpView;
2738 if (toAllApps) {
2739 tmpView = (LauncherApplication.isScreenXLarge())
2740 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2741 } else {
2742 tmpView = mHomeCustomizationDrawer;
2743 }
2744 final View toView = tmpView;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002745
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002746 setPivotsForZoom(toView, toState, scale);
2747
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002748 if (toAllApps) {
Winson Chung20f71112011-04-06 14:22:04 -07002749 if (!springLoaded) {
2750 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2751
Winson Chung785d2eb2011-04-14 16:08:02 -07002752 if (LauncherApplication.isScreenXLarge()) {
2753 // Everytime we launch into AllApps, we reset the successful drop flag which
2754 // controls when it should hide/show the mini workspaces
2755 mAllAppsPagedView.resetSuccessfulDropFlag();
2756 }
Winson Chung20f71112011-04-06 14:22:04 -07002757 } else {
2758 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
2759 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002760 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002761 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002762 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002763
2764 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002765 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002766 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002767 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2768 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002769 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002770 toView.setFastScaleX(a * scale + b * 1f);
2771 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002772 }
2773 });
Adam Cohen61033d32010-11-15 18:29:44 -08002774
Adam Cohenf16e5712011-01-13 13:31:45 -08002775 if (toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002776 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002777 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002778 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002779 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2780 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002781 // don't need to invalidate because we do so above
2782 toView.setFastAlpha(a * 0f + b * 1f);
2783 }
2784 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002785 alphaAnim.start();
2786 }
2787
Michael Jurkaabded662011-03-04 12:06:57 -08002788 if (toView instanceof LauncherTransitionable) {
2789 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002790 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002791 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002792 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002793 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002794 // Prepare the position
2795 toView.setTranslationX(0.0f);
2796 toView.setTranslationY(0.0f);
2797 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002798 toView.bringToFront();
Adam Cohenf16e5712011-01-13 13:31:45 -08002799 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002800 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002801 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002802 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002803 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002804 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002805 // If we don't set the final scale values here, if this animation is cancelled
2806 // it will have the wrong scale value and subsequent cameraPan animations will
2807 // not fix that
2808 toView.setScaleX(1.0f);
2809 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002810 if (toView instanceof LauncherTransitionable) {
2811 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002812 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002813 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002814 });
2815
Chet Haaseb1254a62010-09-07 13:35:00 -07002816 AnimatorSet toolbarHideAnim = new AnimatorSet();
2817 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002818 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2819
2820 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002821 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002822
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002823 if (mStateAnimation != null) mStateAnimation.cancel();
2824 mStateAnimation = new AnimatorSet();
2825 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2826 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002827
2828 // Show the new toolbar buttons just as the main animation is ending
2829 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002830 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2831 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002832 } else {
2833 toView.setTranslationX(0.0f);
2834 toView.setTranslationY(0.0f);
2835 toView.setScaleX(1.0f);
2836 toView.setScaleY(1.0f);
2837 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002838 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002839 if (toView instanceof LauncherTransitionable) {
2840 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2841 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2842 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002843 hideAndShowToolbarButtons(toState, null, null);
2844 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002845 }
2846
2847 /**
2848 * Zoom the camera back into the workspace, hiding 'fromView'.
2849 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002850 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2851 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002852 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08002853 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002854 Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002855 final boolean fromAllApps = (fromState == State.ALL_APPS)
2856 || (fromState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002857
2858 int duration = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002859 res.getInteger(R.integer.config_appsCustomizeZoomOutTime) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002860 res.getInteger(R.integer.config_customizeZoomOutTime);
2861
Michael Jurka742574b2011-02-02 23:51:01 -08002862 final float scaleFactor = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002863 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002864 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2865
Winson Chung785d2eb2011-04-14 16:08:02 -07002866 View tmpView;
2867 if (fromAllApps) {
2868 tmpView = (LauncherApplication.isScreenXLarge())
2869 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2870 } else {
2871 tmpView = mHomeCustomizationDrawer;
2872 }
2873 final View fromView = tmpView;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002874
Winson Chung785d2eb2011-04-14 16:08:02 -07002875 if (LauncherApplication.isScreenXLarge()) {
2876 mCustomizePagedView.endChoiceMode();
2877 mAllAppsPagedView.endChoiceMode();
2878 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002879
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002880 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002881
Michael Jurkad3ef3062010-11-23 16:23:58 -08002882 if (!springLoaded) {
2883 mWorkspace.unshrink(animated);
2884 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002885 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002886 if (mStateAnimation != null) mStateAnimation.cancel();
2887 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002888
Michael Jurka742574b2011-02-02 23:51:01 -08002889 final float oldScaleX = fromView.getScaleX();
2890 final float oldScaleY = fromView.getScaleY();
2891
2892 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2893 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002894 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2895 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002896 ((View)fromView.getParent()).fastInvalidate();
2897 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2898 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2899 }
2900 });
Michael Jurkaabded662011-03-04 12:06:57 -08002901 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002902 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002903 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002904 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2905 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002906 // don't need to invalidate because we do so above
2907 fromView.setFastAlpha(a * 1f + b * 0f);
2908 }
2909 });
Michael Jurkaabded662011-03-04 12:06:57 -08002910 if (fromView instanceof LauncherTransitionable) {
2911 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002912 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002913 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002914 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002915 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002916 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002917 if (fromView instanceof LauncherTransitionable) {
2918 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002919 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002920 }
2921 });
2922
Chet Haaseb1254a62010-09-07 13:35:00 -07002923 AnimatorSet toolbarHideAnim = new AnimatorSet();
2924 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002925 if (!springLoaded) {
2926 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2927 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002928
Adam Cohen61033d32010-11-15 18:29:44 -08002929 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002930
2931 // Show the new toolbar buttons at the very end of the whole animation
2932 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2933 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002934 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2935 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002936 } else {
2937 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002938 if (fromView instanceof LauncherTransitionable) {
2939 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2940 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2941 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002942 if (!springLoaded) {
2943 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2944 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002945 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002946 }
2947
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002948 void showWorkspace(boolean animated) {
2949 showWorkspace(animated, null);
2950 }
2951
2952 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002953 if (layout != null) {
2954 // always animated, but that's ok since we never specify a layout and
2955 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002956 mWorkspace.unshrink(layout);
2957 } else {
2958 mWorkspace.unshrink(animated);
2959 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002960 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002961 closeAllApps(animated);
2962 } else if (mState == State.CUSTOMIZE) {
2963 hideCustomizationDrawer(animated);
2964 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002965
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002966 // Change the state *after* we've called all the transition code
2967 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002968
Winson Chung5fb63472011-02-02 17:03:37 -08002969 // Resume the auto-advance of widgets
2970 mUserPresent = true;
2971 updateRunning();
2972
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002973 // send an accessibility event to announce the context change
2974 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002975 }
2976
Michael Jurkad3ef3062010-11-23 16:23:58 -08002977 void enterSpringLoadedDragMode(CellLayout layout) {
2978 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07002979 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002980 mState = State.ALL_APPS_SPRING_LOADED;
Winson Chung785d2eb2011-04-14 16:08:02 -07002981 if (LauncherApplication.isScreenXLarge()) {
2982 cameraZoomIn(State.ALL_APPS, true, true);
2983 } else {
2984 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
2985 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002986 } else if (mState == State.CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002987 mState = State.CUSTOMIZE_SPRING_LOADED;
Winson Chung20f71112011-04-06 14:22:04 -07002988 cameraZoomIn(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002989 }/* else {
2990 // we're already in spring loaded mode; don't do anything
2991 }*/
2992 }
2993
2994 void exitSpringLoadedDragMode() {
2995 if (mState == State.ALL_APPS_SPRING_LOADED) {
2996 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002997 if (LauncherApplication.isScreenXLarge()) {
2998 cameraZoomOut(State.ALL_APPS, true, true);
2999 mState = State.ALL_APPS;
3000 } else {
3001 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
3002 mState = State.APPS_CUSTOMIZE;
3003 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003004 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
3005 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
Winson Chung20f71112011-04-06 14:22:04 -07003006 cameraZoomOut(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08003007 mState = State.CUSTOMIZE;
3008 }/* else {
3009 // we're not in spring loaded mode; don't do anything
3010 }*/
3011 }
3012
Winson Chung785d2eb2011-04-14 16:08:02 -07003013 void showAllApps(boolean animated) {
3014 if (mState != State.WORKSPACE) return;
3015 if (LauncherApplication.isScreenXLarge()) {
3016 cameraZoomOut(State.ALL_APPS, animated, false);
3017 ((View) mAllAppsGrid).requestFocus();
3018
3019 // TODO: fade these two too
3020 mDeleteZone.setVisibility(View.GONE);
3021
3022 // Change the state *after* we've called all the transition code
3023 mState = State.ALL_APPS;
3024 } else {
3025 View appsCustomizePane = findViewById(R.id.apps_customize_pane);
3026 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
3027 appsCustomizePane.requestFocus();
3028
3029 // Change the state *after* we've called all the transition code
3030 mState = State.APPS_CUSTOMIZE;
3031 }
3032
3033 // Pause the auto-advance of widgets until we are out of AllApps
3034 mUserPresent = false;
3035 updateRunning();
3036
3037 // Send an accessibility event to announce the context change
3038 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
3039 }
3040
Joe Onoratob2061212009-11-24 16:13:54 -05003041 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08003042 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05003043 * - Home from workspace
3044 * - from center screen
3045 * - from other screens
3046 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003047 * - from center screen
3048 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003049 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003050 * - from center screen
3051 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003052 * - Launch app from workspace and quit
3053 * - with back
3054 * - with home
3055 * - Launch app from all apps and quit
3056 * - with back
3057 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003058 * - Go to a screen that's not the default, then all
3059 * apps, and launch and app, and go back
3060 * - with back
3061 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003062 * - On workspace, long press power and go back
3063 * - with back
3064 * - with home
3065 * - On all apps, long press power and go back
3066 * - with back
3067 * - with home
3068 * - On workspace, power off
3069 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003070 * - Launch an app and turn off the screen while in that app
3071 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003072 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003073 * - From all apps
3074 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003075 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3076 * - From all apps
3077 * - From the center workspace
3078 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003079 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003080 void closeAllApps(boolean animated) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003081 if (LauncherApplication.isScreenXLarge()) {
3082 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
3083 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung20f71112011-04-06 14:22:04 -07003084 cameraZoomIn(State.ALL_APPS, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003085
3086 // Set focus to the AllApps button
3087 findViewById(R.id.all_apps_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003088 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003089 } else {
3090 if (mState == State.APPS_CUSTOMIZE || mState == State.ALL_APPS_SPRING_LOADED) {
3091 mWorkspace.setVisibility(View.VISIBLE);
3092 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
3093
3094 // Set focus to the AllApps button
3095 findViewById(R.id.all_apps_button).requestFocus();
3096 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003097 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003098 }
3099
Joe Onorato7c312c12009-08-13 21:36:53 -07003100 void lockAllApps() {
3101 // TODO
3102 }
3103
3104 void unlockAllApps() {
3105 // TODO
3106 }
3107
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003108 // Show the customization drawer (only exists in x-large configuration)
3109 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003110 if (mState != State.WORKSPACE) {
3111 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003112 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003113
Winson Chung20f71112011-04-06 14:22:04 -07003114 cameraZoomOut(State.CUSTOMIZE, animated, false);
Michael Jurkaafca9532011-02-16 14:50:35 -08003115
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003116 // Change the state *after* we've called all the transition code
3117 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003118
3119 // Pause the auto-advance of widgets until we are out of Customization drawer
3120 mUserPresent = false;
3121 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003122 }
3123
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003124 // Hide the customization drawer (only exists in x-large configuration)
3125 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003126 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Winson Chung20f71112011-04-06 14:22:04 -07003127 cameraZoomIn(State.CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003128
3129 // Set focus to the customize button
3130 findViewById(R.id.configure_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003131 }
3132 }
3133
Patrick Dubroy5f445422011-02-18 14:35:21 -08003134 /**
3135 * Add an item from all apps or customize onto the given workspace screen.
3136 * If layout is null, add to the current screen.
3137 */
3138 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003139 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3140 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003141 } else {
3142 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003143 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003144 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003145
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003146 void onWorkspaceClick(CellLayout layout) {
3147 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003148 }
3149
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003150 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003151 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003152 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003153 // Look for the toolbar icon specified in the activity meta-data
3154 Bundle metaData = packageManager.getActivityInfo(
3155 activityName, PackageManager.GET_META_DATA).metaData;
3156 if (metaData != null) {
3157 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3158 if (iconResId != 0) {
3159 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003160 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003161 }
3162 }
3163 } catch (NameNotFoundException e) {
3164 // Do nothing
3165 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003166 return null;
3167 }
3168
3169 // if successful in getting icon, return it; otherwise, set button to use default drawable
3170 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3171 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3172 TextView button = (TextView) findViewById(buttonId);
3173 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3174
3175 // If we were unable to find the icon via the meta-data, use a generic one
3176 if (toolbarIcon == null) {
3177 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3178 return null;
3179 } else {
3180 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3181 return toolbarIcon.getConstantState();
3182 }
3183 }
3184
3185 // if successful in getting icon, return it; otherwise, set button to use default drawable
3186 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3187 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3188 ImageView button = (ImageView) findViewById(buttonId);
3189 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3190
Michael Jurka0423dcf2010-10-05 14:56:18 -07003191 // If we were unable to find the icon via the meta-data, use a generic one
3192 if (toolbarIcon == null) {
3193 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003194 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003195 } else {
3196 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003197 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003198 }
3199 }
3200
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003201 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3202 TextView button = (TextView) findViewById(buttonId);
3203 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3204 }
3205
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003206 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003207 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003208 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003209 }
3210
Michael Jurka0423dcf2010-10-05 14:56:18 -07003211 private void updateGlobalSearchIcon() {
3212 if (LauncherApplication.isScreenXLarge()) {
Winson Chung97d85d22011-04-13 11:27:36 -07003213 final View searchButton = findViewById(R.id.search_button);
3214 final View searchDivider = findViewById(R.id.search_divider);
3215
Michael Jurka0423dcf2010-10-05 14:56:18 -07003216 final SearchManager searchManager =
3217 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3218 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003219 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003220 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003221 R.id.search_button, activityName, R.drawable.ic_generic_search);
Winson Chung97d85d22011-04-13 11:27:36 -07003222 searchButton.setVisibility(View.VISIBLE);
3223 searchDivider.setVisibility(View.VISIBLE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003224 } else {
Winson Chung97d85d22011-04-13 11:27:36 -07003225 searchButton.setVisibility(View.GONE);
3226 searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003227 }
3228 }
3229 }
3230
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003231 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003232 updateButtonWithDrawable(R.id.search_button, d);
3233 }
3234
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003235 private void updateVoiceSearchIcon() {
3236 if (LauncherApplication.isScreenXLarge()) {
Winson Chung97d85d22011-04-13 11:27:36 -07003237 final View searchDivider = findViewById(R.id.search_divider);
3238 final View voiceButton = findViewById(R.id.voice_button);
3239
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003240 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3241 ComponentName activityName = intent.resolveActivity(getPackageManager());
3242 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003243 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003244 R.id.voice_button, activityName, R.drawable.ic_voice_search);
Winson Chung97d85d22011-04-13 11:27:36 -07003245 searchDivider.setVisibility(View.VISIBLE);
3246 voiceButton.setVisibility(View.VISIBLE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003247 } else {
Winson Chung97d85d22011-04-13 11:27:36 -07003248 searchDivider.setVisibility(View.GONE);
3249 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003250 }
3251 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003252 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003253
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003254 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003255 updateButtonWithDrawable(R.id.voice_button, d);
3256 }
3257
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003258 /**
3259 * Sets the app market icon (shown when all apps is visible on x-large screens)
3260 */
3261 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003262 final View marketButton = findViewById(R.id.market_button);
3263 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3264 // Find the app market activity by resolving an intent.
3265 // (If multiple app markets are installed, it will return the ResolverActivity.)
3266 ComponentName activityName = intent.resolveActivity(getPackageManager());
3267 if (activityName != null) {
3268 mAppMarketIntent = intent;
3269 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
3270 R.id.market_button, activityName, R.drawable.app_market_generic);
3271 marketButton.setVisibility(View.VISIBLE);
3272 } else {
3273 // We should hide and disable the view so that we don't try and restore the visibility
3274 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3275 marketButton.setVisibility(View.GONE);
3276 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003277 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003278 }
3279
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003280 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003281 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003282 }
3283
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003284 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003285 * Displays the shortcut creation dialog and launches, if necessary, the
3286 * appropriate activity.
3287 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003288 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003289 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3290 DialogInterface.OnShowListener {
3291
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003292 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003293
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003294 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003295 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003296
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003297 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3298 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003299 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003300
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003301 builder.setInverseBackgroundForced(true);
3302
3303 AlertDialog dialog = builder.create();
3304 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003305 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003306 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003307
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003308 return dialog;
3309 }
3310
3311 public void onCancel(DialogInterface dialog) {
3312 mWaitingForResult = false;
3313 cleanup();
3314 }
3315
Romain Guycbb89e42009-06-08 15:52:54 -07003316 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003317 }
3318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003319 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003320 try {
3321 dismissDialog(DIALOG_CREATE_SHORTCUT);
3322 } catch (Exception e) {
3323 // An exception is thrown if the dialog is not visible, which is fine
3324 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003325 }
3326
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003327 /**
3328 * Handle the action clicked in the "Add to home" dialog.
3329 */
3330 public void onClick(DialogInterface dialog, int which) {
3331 Resources res = getResources();
3332 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003333
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003334 switch (which) {
3335 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003336 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003337 break;
3338 }
Romain Guycbb89e42009-06-08 15:52:54 -07003339
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003340 case AddAdapter.ITEM_APPWIDGET: {
3341 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003342
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003343 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3344 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003345 // start the pick activity
3346 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3347 break;
3348 }
Romain Guycbb89e42009-06-08 15:52:54 -07003349
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003350 case AddAdapter.ITEM_LIVE_FOLDER: {
3351 // Insert extra item to handle inserting folder
3352 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003353
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003354 ArrayList<String> shortcutNames = new ArrayList<String>();
3355 shortcutNames.add(res.getString(R.string.group_folder));
3356 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003357
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003358 ArrayList<ShortcutIconResource> shortcutIcons =
3359 new ArrayList<ShortcutIconResource>();
3360 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3361 R.drawable.ic_launcher_folder));
3362 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3363
3364 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3365 pickIntent.putExtra(Intent.EXTRA_INTENT,
3366 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3367 pickIntent.putExtra(Intent.EXTRA_TITLE,
3368 getText(R.string.title_select_live_folder));
3369 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003370
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003371 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3372 break;
3373 }
3374
3375 case AddAdapter.ITEM_WALLPAPER: {
3376 startWallpaper();
3377 break;
3378 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003379 }
3380 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003381
3382 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003383 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003384 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003385 }
3386
3387 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003388 * Receives notifications when applications are added/removed.
3389 */
3390 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3391 @Override
3392 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003393 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003394 String reason = intent.getStringExtra("reason");
3395 if (!"homekey".equals(reason)) {
3396 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003397 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003398 animate = false;
3399 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003400 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003401 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003402 }
3403 }
3404
3405 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003406 * Receives notifications whenever the appwidgets are reset.
3407 */
3408 private class AppWidgetResetObserver extends ContentObserver {
3409 public AppWidgetResetObserver() {
3410 super(new Handler());
3411 }
3412
3413 @Override
3414 public void onChange(boolean selfChange) {
3415 onAppWidgetReset();
3416 }
3417 }
3418
3419 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003420 * If the activity is currently paused, signal that we need to re-run the loader
3421 * in onResume.
3422 *
3423 * This needs to be called from incoming places where resources might have been loaded
3424 * while we are paused. That is becaues the Configuration might be wrong
3425 * when we're not running, and if it comes back to what it was when we
3426 * were paused, we are not restarted.
3427 *
3428 * Implementation of the method from LauncherModel.Callbacks.
3429 *
3430 * @return true if we are currently paused. The caller might be able to
3431 * skip some work in that case since we will come back again.
3432 */
3433 public boolean setLoadOnResume() {
3434 if (mPaused) {
3435 Log.i(TAG, "setLoadOnResume");
3436 mOnResumeNeedsLoad = true;
3437 return true;
3438 } else {
3439 return false;
3440 }
3441 }
3442
3443 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003444 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003445 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003446 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003447 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003448 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003449 } else {
3450 return SCREEN_COUNT / 2;
3451 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003452 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003453
Winson Chunga12a2502010-12-20 14:41:35 -08003454 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003455 mAllAppsPagedView = view;
3456 }
3457
Joe Onorato9c1289c2009-08-17 11:03:03 -04003458 /**
3459 * Refreshes the shortcuts shown on the workspace.
3460 *
3461 * Implementation of the method from LauncherModel.Callbacks.
3462 */
3463 public void startBinding() {
3464 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003465
3466 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003467 int count = workspace.getChildCount();
3468 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003469 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003470 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3471 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003472 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003473
Joe Onorato9c1289c2009-08-17 11:03:03 -04003474 if (DEBUG_USER_INTERFACE) {
3475 android.widget.Button finishButton = new android.widget.Button(this);
3476 finishButton.setText("Finish");
3477 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3478
3479 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3480 public void onClick(View v) {
3481 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003482 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003483 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003484 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003485
3486 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3487 // mDesktopItems -> AppWidgetInfo -> hostView).
3488 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003489 }
3490
3491 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003492 * Bind the items start-end from the list.
3493 *
3494 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003495 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003496 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3497
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003498 setLoadOnResume();
3499
Joe Onorato9c1289c2009-08-17 11:03:03 -04003500 final Workspace workspace = mWorkspace;
3501
3502 for (int i=start; i<end; i++) {
3503 final ItemInfo item = shortcuts.get(i);
3504 mDesktopItems.add(item);
3505 switch (item.itemType) {
3506 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3507 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003508 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003509 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3510 false);
3511 break;
3512 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3513 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003514 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003515 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003516 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3517 false);
3518 break;
3519 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3520 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3521 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003522 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003523 (LiveFolderInfo) item);
3524 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3525 false);
3526 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003528 }
3529
Joe Onorato9c1289c2009-08-17 11:03:03 -04003530 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003531 }
3532
Joe Onorato9c1289c2009-08-17 11:03:03 -04003533 /**
3534 * Implementation of the method from LauncherModel.Callbacks.
3535 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003536 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003537 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003538 sFolders.clear();
3539 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003540 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003541
3542 /**
3543 * Add the views for a widget to the workspace.
3544 *
3545 * Implementation of the method from LauncherModel.Callbacks.
3546 */
3547 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003548 setLoadOnResume();
3549
Daniel Sandler843e8602010-06-07 14:59:01 -04003550 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3551 if (DEBUG_WIDGETS) {
3552 Log.d(TAG, "bindAppWidget: " + item);
3553 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003554 final Workspace workspace = mWorkspace;
3555
3556 final int appWidgetId = item.appWidgetId;
3557 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003558 if (DEBUG_WIDGETS) {
3559 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3560 }
3561
Joe Onorato9c1289c2009-08-17 11:03:03 -04003562 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3563
Joe Onorato9c1289c2009-08-17 11:03:03 -04003564 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3565 item.hostView.setTag(item);
3566
3567 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3568 item.cellY, item.spanX, item.spanY, false);
3569
Adam Cohended9f8d2010-11-03 13:25:16 -07003570 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3571
Joe Onorato9c1289c2009-08-17 11:03:03 -04003572 workspace.requestLayout();
3573
3574 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003575
3576 if (DEBUG_WIDGETS) {
3577 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3578 + (SystemClock.uptimeMillis()-start) + "ms");
3579 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003580 }
3581
3582 /**
3583 * Callback saying that there aren't any more items to bind.
3584 *
3585 * Implementation of the method from LauncherModel.Callbacks.
3586 */
3587 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003588 setLoadOnResume();
3589
Joe Onorato9c1289c2009-08-17 11:03:03 -04003590 if (mSavedState != null) {
3591 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003592 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003593 }
3594
3595 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3596 if (userFolders != null) {
3597 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003598 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003599 if (info != null) {
3600 openFolder(info);
3601 }
3602 }
3603 final Folder openFolder = mWorkspace.getOpenFolder();
3604 if (openFolder != null) {
3605 openFolder.requestFocus();
3606 }
3607 }
3608
Joe Onorato9c1289c2009-08-17 11:03:03 -04003609 mSavedState = null;
3610 }
3611
3612 if (mSavedInstanceState != null) {
3613 super.onRestoreInstanceState(mSavedInstanceState);
3614 mSavedInstanceState = null;
3615 }
3616
Winson Chung32bb5e52011-01-31 14:51:56 -08003617 // Workaround a bug that occurs when rotating the device while the customization mode is
3618 // open, we trigger a new layout on all the CellLayout children.
3619 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3620 final int childCount = mWorkspace.getChildCount();
3621 for (int i = 0; i < childCount; ++i) {
3622 mWorkspace.getChildAt(i).requestLayout();
3623 }
3624 }
3625
Joe Onorato9c1289c2009-08-17 11:03:03 -04003626 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003627
3628 // If we received the result of any pending adds while the loader was running (e.g. the
3629 // widget configuration forced an orientation change), process them now.
3630 for (int i = 0; i < sPendingAddList.size(); i++) {
3631 completeAdd(sPendingAddList.get(i));
3632 }
3633 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003634 }
3635
3636 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003637 * Updates the icons on the launcher that are affected by changes to the package list
3638 * on the device.
3639 */
3640 private void updateIconsAffectedByPackageManagerChanges() {
3641 updateAppMarketIcon();
3642 updateGlobalSearchIcon();
3643 updateVoiceSearchIcon();
3644 }
3645
3646 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003647 * Add the icons for all apps.
3648 *
3649 * Implementation of the method from LauncherModel.Callbacks.
3650 */
3651 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003652 if (mAllAppsGrid != null) {
3653 mAllAppsGrid.setApps(apps);
3654 }
3655 if (mAppsCustomizeContent != null) {
3656 mAppsCustomizeContent.setApps(apps);
3657 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003658 if (mCustomizePagedView != null) {
3659 mCustomizePagedView.setApps(apps);
3660 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003661 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003662 }
3663
3664 /**
3665 * A package was installed.
3666 *
3667 * Implementation of the method from LauncherModel.Callbacks.
3668 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003669 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003670 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003671 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chung785d2eb2011-04-14 16:08:02 -07003672 if (mAllAppsGrid != null) {
3673 mAllAppsGrid.addApps(apps);
3674 }
3675 if (mAppsCustomizeContent != null) {
3676 mAppsCustomizeContent.addApps(apps);
3677 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003678 if (mCustomizePagedView != null) {
3679 mCustomizePagedView.addApps(apps);
3680 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003681 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003682 }
3683
3684 /**
3685 * A package was updated.
3686 *
3687 * Implementation of the method from LauncherModel.Callbacks.
3688 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003689 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003690 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003691 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003692 if (mWorkspace != null) {
3693 mWorkspace.updateShortcuts(apps);
3694 }
3695 if (mAllAppsGrid != null) {
3696 mAllAppsGrid.updateApps(apps);
3697 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003698 if (mAppsCustomizeContent != null) {
3699 mAppsCustomizeContent.updateApps(apps);
3700 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003701 if (mCustomizePagedView != null) {
3702 mCustomizePagedView.updateApps(apps);
3703 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003704 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003705 }
3706
3707 /**
3708 * A package was uninstalled.
3709 *
3710 * Implementation of the method from LauncherModel.Callbacks.
3711 */
Joe Onorato36115782010-06-17 13:28:48 -04003712 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003713 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003714 if (permanent) {
3715 mWorkspace.removeItems(apps);
3716 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003717 if (mAllAppsGrid != null) {
3718 mAllAppsGrid.removeApps(apps);
3719 }
3720 if (mAppsCustomizeContent != null) {
3721 mAppsCustomizeContent.removeApps(apps);
3722 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003723 if (mCustomizePagedView != null) {
3724 mCustomizePagedView.removeApps(apps);
3725 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003726 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 }
Joe Onoratobe386092009-11-17 17:32:16 -08003728
3729 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003730 * A number of packages were updated.
3731 */
3732 public void bindPackagesUpdated() {
3733 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003734 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003735 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003736 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003737 if (mAppsCustomizeContent != null) {
3738 mAppsCustomizeContent.onPackagesUpdated();
3739 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003740 }
3741
Winson Chung400438b2011-01-16 17:53:48 -08003742 private int mapConfigurationOriActivityInfoOri(int configOri) {
3743 final Display d = getWindowManager().getDefaultDisplay();
3744 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3745 switch (d.getRotation()) {
3746 case Surface.ROTATION_0:
3747 case Surface.ROTATION_180:
3748 // We are currently in the same basic orientation as the natural orientation
3749 naturalOri = configOri;
3750 break;
3751 case Surface.ROTATION_90:
3752 case Surface.ROTATION_270:
3753 // We are currently in the other basic orientation to the natural orientation
3754 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3755 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3756 break;
3757 }
3758
3759 int[] oriMap = {
3760 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3761 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3762 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3763 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3764 };
3765 // Since the map starts at portrait, we need to offset if this device's natural orientation
3766 // is landscape.
3767 int indexOffset = 0;
3768 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3769 indexOffset = 1;
3770 }
3771 return oriMap[(d.getRotation() + indexOffset) % 4];
3772 }
3773 public void lockScreenOrientation() {
3774 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3775 .getConfiguration().orientation));
3776 }
3777 public void unlockScreenOrientation() {
3778 mHandler.postDelayed(new Runnable() {
3779 public void run() {
3780 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3781 }
3782 }, mRestoreScreenOrientationDelay);
3783 }
3784
Winson Chung80baf5a2010-08-09 16:03:15 -07003785 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003786 * Prints out out state for debugging.
3787 */
3788 public void dumpState() {
3789 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003790 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003791 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3792 Log.d(TAG, "mRestoring=" + mRestoring);
3793 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3794 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3795 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003796 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003797 mModel.dumpState();
Winson Chung785d2eb2011-04-14 16:08:02 -07003798 if (mAllAppsGrid != null) {
3799 mAllAppsGrid.dumpState();
3800 }
3801 if (mAppsCustomizeContent != null) {
3802 mAppsCustomizeContent.dumpState();
3803 }
Joe Onoratobe386092009-11-17 17:32:16 -08003804 Log.d(TAG, "END launcher2 dump state");
3805 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003806}
Michael Jurka2763be32011-02-24 11:19:57 -08003807
Michael Jurkaabded662011-03-04 12:06:57 -08003808interface LauncherTransitionable {
3809 void onLauncherTransitionStart(Animator animation);
3810 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003811}