blob: 2d63b3f4b64d6ea43cf113a243e9f60dc6c2057d [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Adam Cohendf2cc412011-04-27 16:56:57 -070020import java.io.DataInputStream;
21import java.io.DataOutputStream;
22import java.io.FileNotFoundException;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.List;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070027
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080029import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070031import android.animation.ObjectAnimator;
32import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070033import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080034import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070035import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.app.Dialog;
38import android.app.SearchManager;
39import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070040import android.appwidget.AppWidgetManager;
41import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080043import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070044import android.content.ClipData;
45import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040046import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080047import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Context;
49import android.content.DialogInterface;
50import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080051import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080052import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070053import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080055import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080056import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070058import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040059import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080062import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070063import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080064import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070065import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040066import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070067import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020069import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080070import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070071import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040072import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080073import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070074import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080075import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.text.Selection;
77import android.text.SpannableStringBuilder;
78import android.text.TextUtils;
79import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070080import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080081import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080082import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.KeyEvent;
84import android.view.LayoutInflater;
85import android.view.Menu;
86import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070087import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080088import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.View;
90import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070091import android.view.WindowManager;
Michael Jurkae0f5a612011-02-07 16:45:41 -080092import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080093import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080094import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080095import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070097import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070099import android.widget.ImageView;
100import android.widget.LinearLayout;
101import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700102import android.widget.TabHost;
Winson Chung68846fd2010-10-29 11:00:27 -0700103import android.widget.TextView;
104import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700105
Adam Cohendf2cc412011-04-27 16:56:57 -0700106import com.android.common.Search;
107import com.android.launcher.R;
108import com.android.launcher2.Workspace.ShrinkState;
Romain Guyedcce092010-03-04 13:03:17 -0800109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110/**
111 * Default launcher application.
112 */
Michael Jurka946ad472010-07-09 18:05:18 -0700113public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700114 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
115 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800116 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700117 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400120 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400121 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800124 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700127 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
128 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
130 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700131 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_PICK_APPLICATION = 6;
136 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700138 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
140 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
141
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800142 static final int SCREEN_COUNT = 5;
143 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Joe Onorato7c312c12009-08-13 21:36:53 -0700145 static final int DIALOG_CREATE_SHORTCUT = 1;
146 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Romain Guy98d01652009-06-30 16:21:04 -0700148 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
150 // Type: int
151 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700152 // Type: int
153 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 // Type: long
Adam Cohendf2cc412011-04-27 16:56:57 -0700155 private static final String RUNTIME_STATE_FOLDERS = "launcher.folder";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: int
157 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
158 // Type: int
159 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
160 // Type: int
161 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: boolean
163 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
164 // Type: long
165 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
166
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700167 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
168
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700169 /** The different states that Launcher can be in. */
Winson Chung785d2eb2011-04-14 16:08:02 -0700170 private enum State { WORKSPACE, APPS_CUSTOMIZE, ALL_APPS, CUSTOMIZE,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800171 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700172 private State mState = State.WORKSPACE;
173 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700174
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800178 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800180 private final BroadcastReceiver mCloseSystemDialogsReceiver
181 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800182 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private LayoutInflater mInflater;
185
Joe Onorato00acb122009-08-04 16:04:30 -0400186 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700188
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700189 private AppWidgetManager mAppWidgetManager;
190 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700191
Michael Jurka0280c3b2010-09-17 15:00:07 -0700192 private int mAddScreen = -1;
193 private int mAddIntersectCellX = -1;
194 private int mAddIntersectCellY = -1;
195 private int[] mAddDropPosition;
196 private int[] mTmpAddItemCellCoordinates = new int[2];
197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private FolderInfo mFolderInfo;
199
Joe Onorato7c312c12009-08-13 21:36:53 -0700200 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700201 private HandleView mHandleView;
Winson Chung4c98d922011-05-31 16:50:48 -0700202 private SearchDropTargetBar mSearchDeleteBar;
Joe Onorato7c312c12009-08-13 21:36:53 -0700203 private AllAppsView mAllAppsGrid;
Winson Chung785d2eb2011-04-14 16:08:02 -0700204 private AppsCustomizeTabHost mAppsCustomizeTabHost;
205 private AppsCustomizePagedView mAppsCustomizeContent;
Michael Jurka7ef959b2011-02-23 11:48:32 -0800206 private CustomizeTrayTabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700207 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700208
Winson Chung97d85d22011-04-13 11:27:36 -0700209 private ViewGroup mButtonCluster;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800210 private View mAllAppsButton;
211 private View mDivider;
212 private View mConfigureButton;
213
Winson Chunga12a2502010-12-20 14:41:35 -0800214 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700215 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private Bundle mSavedState;
218
219 private SpannableStringBuilder mDefaultKeySsb = null;
220
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221 private boolean mWorkspaceLoading = true;
222
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800223 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 private boolean mRestoring;
225 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700226 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
228 private Bundle mSavedInstanceState;
229
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800231 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800232 private boolean mUserPresent = true;
233 private boolean mVisible = false;
234 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400235
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700236 private static LocaleConfiguration sLocaleConfiguration = null;
237
Romain Guy84f296c2009-11-04 15:00:44 -0800238 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700239
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700240 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Patrick Dubroy94f78a52011-02-28 17:39:16 -0800242 // The "signpost" images along the bottom of the screen (only in some layouts)
Romain Guy1fbc1c82009-11-09 20:43:08 -0800243 private ImageView mPreviousView;
244 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500245
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400246 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400247 private String[] mHotseatConfig = null;
248 private Intent[] mHotseats = null;
249 private Drawable[] mHotseatIcons = null;
250 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400251
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700252 private Intent mAppMarketIntent = null;
253
Adam Cohended9f8d2010-11-03 13:25:16 -0700254 // Related to the auto-advancing of widgets
255 private final int ADVANCE_MSG = 1;
256 private final int mAdvanceInterval = 20000;
257 private final int mAdvanceStagger = 250;
258 private long mAutoAdvanceSentTime;
259 private long mAutoAdvanceTimeLeft = -1;
260 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
261 new HashMap<View, AppWidgetProviderInfo>();
262
Winson Chung400438b2011-01-16 17:53:48 -0800263 // Determines how long to wait after a rotation before restoring the screen orientation to
264 // match the sensor state.
265 private final int mRestoreScreenOrientationDelay = 500;
266
Michael Jurka4ef207b2010-11-29 17:05:45 -0800267 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800268 private static Drawable.ConstantState sGlobalSearchIcon;
269 private static Drawable.ConstantState sVoiceSearchIcon;
270 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800271
Adam Cohen2801caf2011-05-13 20:57:39 -0700272 private DragLayer mDragLayer;
273
Michael Jurkaddd62e92011-02-16 17:49:14 -0800274 private BubbleTextView mWaitingForResume;
275
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800276 private static ArrayList<PendingAddArguments> sPendingAddList
277 = new ArrayList<PendingAddArguments>();
278
279 private static class PendingAddArguments {
280 int requestCode;
281 Intent intent;
282 int screen;
283 int cellX;
284 int cellY;
285 }
286
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 @Override
288 protected void onCreate(Bundle savedInstanceState) {
289 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700290
Winson Chungaafa03c2010-06-11 17:34:16 -0700291 if (LauncherApplication.isInPlaceRotationEnabled()) {
292 // hide the status bar (temporary until we get the status bar design figured out)
293 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
294 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
295 }
296
Joe Onorato0589f0f2010-02-08 13:44:00 -0800297 LauncherApplication app = ((LauncherApplication)getApplication());
298 mModel = app.setLauncher(this);
299 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400300 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700302
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700303 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700304 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
305 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700306
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200308 android.os.Debug.startMethodTracing(
309 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 }
311
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400312 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800313 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314 setContentView(R.layout.launcher);
Michael Jurka7ef959b2011-02-23 11:48:32 -0800315 mHomeCustomizationDrawer = (CustomizeTrayTabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700316 if (mHomeCustomizationDrawer != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700317 // share the same customization workspace across all the tabs
Michael Jurka7ef959b2011-02-23 11:48:32 -0800318 mCustomizePagedView = (CustomizePagedView) findViewById(
319 R.id.customization_drawer_tab_contents);
Michael Jurka946ad472010-07-09 18:05:18 -0700320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 setupViews();
322
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800323 registerContentObservers();
324
Joe Onorato7c312c12009-08-13 21:36:53 -0700325 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700326
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327 mSavedState = savedInstanceState;
328 restoreState(mSavedState);
329
Winson Chunga12a2502010-12-20 14:41:35 -0800330 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800331 if (mCustomizePagedView != null) {
332 mCustomizePagedView.update();
333 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700334 if (mAppsCustomizeContent != null) {
335 mAppsCustomizeContent.onPackagesUpdated();
336 }
Winson Chunga12a2502010-12-20 14:41:35 -0800337
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 if (PROFILE_STARTUP) {
339 android.os.Debug.stopMethodTracing();
340 }
341
342 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400343 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 }
345
346 // For handling default keys
347 mDefaultKeySsb = new SpannableStringBuilder();
348 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800349
350 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
351 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800352
353 // If we have a saved version of these external icons, we load them up immediately
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700354 if (LauncherApplication.isScreenLarge()) {
Winson Chungc6d20002011-03-02 11:57:29 -0800355 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
356 updateIconsAffectedByPackageManagerChanges();
Michael Jurka4ef207b2010-11-29 17:05:45 -0800357 }
Michael Jurka1c3f8ec2011-03-07 16:41:21 -0800358 if (sGlobalSearchIcon != null) {
359 updateGlobalSearchIcon(sGlobalSearchIcon);
360 }
361 if (sVoiceSearchIcon != null) {
362 updateVoiceSearchIcon(sVoiceSearchIcon);
363 }
364 if (sAppMarketIcon != null) {
365 updateAppMarketIcon(sAppMarketIcon);
366 }
Michael Jurka4ef207b2010-11-29 17:05:45 -0800367 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 }
Romain Guycbb89e42009-06-08 15:52:54 -0700369
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700371 if (sLocaleConfiguration == null) {
372 new AsyncTask<Void, Void, LocaleConfiguration>() {
373 @Override
374 protected LocaleConfiguration doInBackground(Void... unused) {
375 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
376 readConfiguration(Launcher.this, localeConfiguration);
377 return localeConfiguration;
378 }
379
380 @Override
381 protected void onPostExecute(LocaleConfiguration result) {
382 sLocaleConfiguration = result;
383 checkForLocaleChange(); // recursive, but now with a locale configuration
384 }
385 }.execute();
386 return;
387 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 final Configuration configuration = getResources().getConfiguration();
390
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700391 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 final String locale = configuration.locale.toString();
393
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700394 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 final int mcc = configuration.mcc;
396
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700397 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 final int mnc = configuration.mnc;
399
Romain Guy84f296c2009-11-04 15:00:44 -0800400 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401
Romain Guy84f296c2009-11-04 15:00:44 -0800402 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700403 sLocaleConfiguration.locale = locale;
404 sLocaleConfiguration.mcc = mcc;
405 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700406
Joe Onorato0589f0f2010-02-08 13:44:00 -0800407 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400408 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700409
410 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
411 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700412 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700413 public void run() {
414 writeConfiguration(Launcher.this, localeConfiguration);
415 }
416 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700417 }
418 }
419
420 private static class LocaleConfiguration {
421 public String locale;
422 public int mcc = -1;
423 public int mnc = -1;
424 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700425
Romain Guy98d01652009-06-30 16:21:04 -0700426 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
427 DataInputStream in = null;
428 try {
429 in = new DataInputStream(context.openFileInput(PREFERENCES));
430 configuration.locale = in.readUTF();
431 configuration.mcc = in.readInt();
432 configuration.mnc = in.readInt();
433 } catch (FileNotFoundException e) {
434 // Ignore
435 } catch (IOException e) {
436 // Ignore
437 } finally {
438 if (in != null) {
439 try {
440 in.close();
441 } catch (IOException e) {
442 // Ignore
443 }
444 }
445 }
446 }
447
448 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
449 DataOutputStream out = null;
450 try {
451 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
452 out.writeUTF(configuration.locale);
453 out.writeInt(configuration.mcc);
454 out.writeInt(configuration.mnc);
455 out.flush();
456 } catch (FileNotFoundException e) {
457 // Ignore
458 } catch (IOException e) {
459 //noinspection ResultOfMethodCallIgnored
460 context.getFileStreamPath(PREFERENCES).delete();
461 } finally {
462 if (out != null) {
463 try {
464 out.close();
465 } catch (IOException e) {
466 // Ignore
467 }
468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 }
470 }
471
472 static int getScreen() {
473 synchronized (sLock) {
474 return sScreen;
475 }
476 }
477
478 static void setScreen(int screen) {
479 synchronized (sLock) {
480 sScreen = screen;
481 }
482 }
483
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400484 // Note: This doesn't do all the client-id magic that BrowserProvider does
485 // in Browser. (http://b/2425179)
486 private Uri getDefaultBrowserUri() {
487 String url = getString(R.string.default_browser_url);
488 if (url.indexOf("{CID}") != -1) {
489 url = url.replace("{CID}", "android-google");
490 }
491 return Uri.parse(url);
492 }
493
494 // Load the Intent templates from arrays.xml to populate the hotseats. For
495 // each Intent, if it resolves to a single app, use that as the launch
496 // intent & use that app's label as the contentDescription. Otherwise,
497 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400498 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400499 if (mHotseatConfig == null) {
500 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
501 if (mHotseatConfig.length > 0) {
502 mHotseats = new Intent[mHotseatConfig.length];
503 mHotseatLabels = new CharSequence[mHotseatConfig.length];
504 mHotseatIcons = new Drawable[mHotseatConfig.length];
505 } else {
506 mHotseats = null;
507 mHotseatIcons = null;
508 mHotseatLabels = null;
509 }
510
511 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
512 for (int i=0; i<mHotseatConfig.length; i++) {
513 // load icon for this slot; currently unrelated to the actual activity
514 try {
515 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
516 } catch (ArrayIndexOutOfBoundsException ex) {
517 Log.w(TAG, "Missing hotseat_icons array item #" + i);
518 mHotseatIcons[i] = null;
519 }
520 }
521 hotseatIconDrawables.recycle();
522 }
523
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400524 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400525 for (int i=0; i<mHotseatConfig.length; i++) {
526 Intent intent = null;
527 if (mHotseatConfig[i].equals("*BROWSER*")) {
528 // magic value meaning "launch user's default web browser"
529 // replace it with a generic web request so we can see if there is indeed a default
530 String defaultUri = getString(R.string.default_browser_url);
531 intent = new Intent(
532 Intent.ACTION_VIEW,
533 ((defaultUri != null)
534 ? Uri.parse(defaultUri)
535 : getDefaultBrowserUri())
536 ).addCategory(Intent.CATEGORY_BROWSABLE);
537 // note: if the user launches this without a default set, she
538 // will always be taken to the default URL above; this is
539 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700540 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400541 // URL is unavoidably sent to the chosen app.
542 } else {
543 try {
544 intent = Intent.parseUri(mHotseatConfig[i], 0);
545 } catch (java.net.URISyntaxException ex) {
546 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
547 // bogus; leave intent=null
548 }
549 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700550
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400551 if (intent == null) {
552 mHotseats[i] = null;
553 mHotseatLabels[i] = getText(R.string.activity_not_found);
554 continue;
555 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400556
557 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700558 Log.d(TAG, "loadHotseats: hotseat " + i
559 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400560 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400561 + "]");
562 }
563
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400564 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
565 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700566 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400567 Log.d(TAG, "Best match for intent: " + bestMatch);
568 Log.d(TAG, "All matches: ");
569 for (ResolveInfo ri : allMatches) {
570 Log.d(TAG, " --> " + ri);
571 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400572 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400573 // did this resolve to a single app, or the resolver?
574 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700575 // can't find any activity to handle this. let's leave the
576 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400577 // to launch.
578 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400579
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400580 // set accessibility text to "Not installed"
581 mHotseatLabels[i] = getText(R.string.activity_not_found);
582 } else {
583 boolean found = false;
584 for (ResolveInfo ri : allMatches) {
585 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
586 && bestMatch.activityInfo.applicationInfo.packageName
587 .equals(ri.activityInfo.applicationInfo.packageName)) {
588 found = true;
589 break;
590 }
591 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700592
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400593 if (!found) {
594 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
595 // the bestMatch is probably the ResolveActivity, meaning the
596 // user has not yet selected a default
597 // so: we'll keep the original intent for now
598 mHotseats[i] = intent;
599
600 // set the accessibility text to "Select shortcut"
601 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
602 } else {
603 // we have an app!
604 // now reconstruct the intent to launch it through the front
605 // door
606 ComponentName com = new ComponentName(
607 bestMatch.activityInfo.applicationInfo.packageName,
608 bestMatch.activityInfo.name);
609 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
610
611 // load the app label for accessibility
612 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
613 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400614 }
615
616 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700617 Log.d(TAG, "loadHotseats: hotseat " + i
618 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400619 + ((mHotseats[i] == null)
620 ? "null"
621 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400622 + "] label=[" + mHotseatLabels[i]
623 + "]"
624 );
625 }
626 }
627 }
628
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800629 private void completeAdd(PendingAddArguments args) {
630 switch (args.requestCode) {
631 case REQUEST_PICK_APPLICATION:
632 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
633 break;
634 case REQUEST_PICK_SHORTCUT:
635 processShortcut(args.intent);
636 break;
637 case REQUEST_CREATE_SHORTCUT:
638 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
639 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800640 case REQUEST_PICK_APPWIDGET:
641 addAppWidgetFromPick(args.intent);
642 break;
643 case REQUEST_CREATE_APPWIDGET:
644 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
645 completeAddAppWidget(appWidgetId, args.screen);
646 break;
647 case REQUEST_PICK_WALLPAPER:
648 // We just wanted the activity result here so we can clear mWaitingForResult
649 break;
650 }
651 }
652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800654 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700655 mWaitingForResult = false;
656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 // The pattern used here is that a user PICKs a specific application,
658 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700659
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
661 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700662
Michael Jurka0280c3b2010-09-17 15:00:07 -0700663 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800664 final PendingAddArguments args = new PendingAddArguments();
665 args.requestCode = requestCode;
666 args.intent = data;
667 args.screen = mAddScreen;
668 args.cellX = mAddIntersectCellX;
669 args.cellY = mAddIntersectCellY;
670
671 // If the loader is still running, defer the add until it is done.
672 if (isWorkspaceLocked()) {
673 sPendingAddList.add(args);
674 } else {
675 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
Romain Guy18042c82009-11-06 11:44:55 -0800677 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
678 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
679 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700680 // Clean up the appWidgetId if we canceled
681 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
682 if (appWidgetId != -1) {
683 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
685 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 }
687
688 @Override
689 protected void onResume() {
690 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800691 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700692 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400693 mWorkspaceLoading = true;
694 mModel.startLoader(this, true);
695 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700696 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800698 if (mWaitingForResume != null) {
699 mWaitingForResume.setStayPressed(false);
700 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700701 // When we resume Launcher, a different Activity might be responsible for the app
702 // market intent, so refresh the icon
703 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 }
705
706 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700707 protected void onPause() {
708 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700709 // Some launcher layouts don't have a previous and next view
710 if (mPreviousView != null) {
711 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700712 }
713 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700714 dismissPreview(mNextView);
715 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700716 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800717 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700718 }
Romain Guycbb89e42009-06-08 15:52:54 -0700719
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700720 @Override
721 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722 // Flag the loader to stop early before switching
723 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700724 if (mAllAppsGrid != null) {
725 mAllAppsGrid.surrender();
726 }
727 if (mAppsCustomizeContent != null) {
728 mAppsCustomizeContent.surrender();
729 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800730 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700731 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700732
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800733 // We can't hide the IME if it was forced open. So don't bother
734 /*
735 @Override
736 public void onWindowFocusChanged(boolean hasFocus) {
737 super.onWindowFocusChanged(hasFocus);
738
739 if (hasFocus) {
740 final InputMethodManager inputManager = (InputMethodManager)
741 getSystemService(Context.INPUT_METHOD_SERVICE);
742 WindowManager.LayoutParams lp = getWindow().getAttributes();
743 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
744 android.os.Handler()) {
745 protected void onReceiveResult(int resultCode, Bundle resultData) {
746 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
747 }
748 });
749 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
750 }
751 }
752 */
753
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 private boolean acceptFilter() {
755 final InputMethodManager inputManager = (InputMethodManager)
756 getSystemService(Context.INPUT_METHOD_SERVICE);
757 return !inputManager.isFullscreenMode();
758 }
759
760 @Override
761 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700762 final int uniChar = event.getUnicodeChar();
763 final boolean handled = super.onKeyDown(keyCode, event);
764 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
765 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
767 keyCode, event);
768 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700769 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700770 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700771 // showSearchDialog()
772 // If there are multiple keystrokes before the search dialog takes focus,
773 // onSearchRequested() will be called for every keystroke,
774 // but it is idempotent, so it's fine.
775 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 }
777 }
778
Joe Onorato8a9625e2010-01-28 15:55:35 -0800779 // Eat the long press event so the keyboard doesn't come up.
780 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
781 return true;
782 }
783
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 return handled;
785 }
786
Karl Rosaen138a0412009-04-23 19:00:21 -0700787 private String getTypedText() {
788 return mDefaultKeySsb.toString();
789 }
790
791 private void clearTypedText() {
792 mDefaultKeySsb.clear();
793 mDefaultKeySsb.clearSpans();
794 Selection.setSelection(mDefaultKeySsb, 0);
795 }
796
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700798 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
799 * State
800 */
801 private static State intToState(int stateOrdinal) {
802 State state = State.WORKSPACE;
803 final State[] stateValues = State.values();
804 for (int i = 0; i < stateValues.length; i++) {
805 if (stateValues[i].ordinal() == stateOrdinal) {
806 state = stateValues[i];
807 break;
808 }
809 }
810 return state;
811 }
812
813 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 * Restores the previous state, if it exists.
815 *
816 * @param savedState The previous state.
817 */
818 private void restoreState(Bundle savedState) {
819 if (savedState == null) {
820 return;
821 }
822
Michael Jurka883f55b2010-10-21 15:47:14 -0700823 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
824
Winson Chung785d2eb2011-04-14 16:08:02 -0700825 if (state == State.ALL_APPS || state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800826 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700827 } else if (state == State.CUSTOMIZE) {
828 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800829 }
830
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
832 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700833 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 }
835
836 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700837
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700839 mAddScreen = addScreen;
840 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
841 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 mRestoring = true;
843 }
844
845 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
846 if (renameFolder) {
847 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700848 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 mRestoring = true;
850 }
Winson Chunga12a2502010-12-20 14:41:35 -0800851
852 // Restore the current AllApps drawer tab
853 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
854 String curTab = savedState.getString("allapps_currentTab");
855 if (curTab != null) {
856 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
857 tabhost.setCurrentTabByTag(curTab);
858 }
859 int curPage = savedState.getInt("allapps_currentPage", -1);
860 if (curPage > -1) {
861 mAllAppsPagedView.setRestorePage(curPage);
862 }
863 }
864
865 // Restore the current customization drawer tab
866 if (mHomeCustomizationDrawer != null) {
867 String curTab = savedState.getString("customize_currentTab");
868 if (curTab != null) {
869 // We set this directly so that there is no delay before the tab is set
Michael Jurka7ef959b2011-02-23 11:48:32 -0800870 mCustomizePagedView.setCustomizationFilter(
871 mHomeCustomizationDrawer.getCustomizeFilterForTabTag(curTab));
Winson Chunga12a2502010-12-20 14:41:35 -0800872 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
873 }
874
875 // Note: currently we do not restore the page for the customization tray because unlike
876 // AllApps, the page content can change drastically
877 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700878
879 // Restore the AppsCustomize tab
880 if (mAppsCustomizeTabHost != null) {
881 String curTab = savedState.getString("apps_customize_currentTab");
882 if (curTab != null) {
883 // We set this directly so that there is no delay before the tab is set
884 mAppsCustomizeContent.setContentType(
885 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
886 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
887 }
888
889 // Note: currently we do not restore the page for the AppsCustomize pane because the
890 // change in layout can drastically affect the saved page index
891 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 }
893
894 /**
895 * Finds all the views we need and configure them properly.
896 */
897 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700898 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400899
Winson Chung4c98d922011-05-31 16:50:48 -0700900 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
901 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902
Winson Chung4c98d922011-05-31 16:50:48 -0700903 // Setup the drag layer
904 mDragLayer.setup(this, dragController);
905
906 // Setup the workspace
907 mWorkspace.setHapticFeedbackEnabled(false);
908 mWorkspace.setOnLongClickListener(this);
909 mWorkspace.setup(this, dragController);
910 mWorkspace.setWallpaperDimension();
911
912 // Setup the different configurations
913 DeleteZone allAppsDeleteZone = null;
914 ApplicationInfoDropTarget allAppsInfoTarget = null;
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700915 if (LauncherApplication.isScreenLarge()) {
Winson Chung4c98d922011-05-31 16:50:48 -0700916 // Setup AllApps
917 mAllAppsGrid = (AllAppsView) mDragLayer.findViewById(R.id.all_apps_view);
Winson Chung785d2eb2011-04-14 16:08:02 -0700918 mAllAppsGrid.setup(this, dragController);
919 // We don't want a hole punched in our window.
920 ((View) mAllAppsGrid).setWillNotDraw(false);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700921
Winson Chung4c98d922011-05-31 16:50:48 -0700922 // Setup Customize
Winson Chung80baf5a2010-08-09 16:03:15 -0700923 mCustomizePagedView.setLauncher(this);
924 mCustomizePagedView.setDragController(dragController);
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700925 mCustomizePagedView.setAllAppsPagedView(mAllAppsPagedView);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400926
Winson Chung4c98d922011-05-31 16:50:48 -0700927 // Setup DeleteZone
928 mDeleteZone = (DeleteZone) mDragLayer.findViewById(R.id.delete_zone);
929 mDeleteZone.setLauncher(this);
930 mDeleteZone.setDragController(dragController);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800931
Winson Chung4c98d922011-05-31 16:50:48 -0700932 // Setup the top-right Apps/Customize buttons
933 mAllAppsButton = findViewById(R.id.all_apps_button);
934 mDivider = findViewById(R.id.all_apps_divider);
935 mConfigureButton = findViewById(R.id.configure_button);
936 mDeleteZone.setOverlappingViews(new View[] { mAllAppsButton, mDivider,
937 mConfigureButton });
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938
Winson Chung4c98d922011-05-31 16:50:48 -0700939 // Setup the AllApps Delete toolbar button
940 allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
Adam Cohencdc30d52010-12-01 15:09:47 -0800941 allAppsDeleteZone.setLauncher(this);
942 allAppsDeleteZone.setDragController(dragController);
943 allAppsDeleteZone.setDragAndDropEnabled(false);
Adam Cohencdc30d52010-12-01 15:09:47 -0800944
Winson Chung4c98d922011-05-31 16:50:48 -0700945 // Setup the AllApps Info toolbar button
946 allAppsInfoTarget = (ApplicationInfoDropTarget) findViewById(R.id.all_apps_info_target);
Adam Cohencdc30d52010-12-01 15:09:47 -0800947 allAppsInfoTarget.setLauncher(this);
Adam Cohencdc30d52010-12-01 15:09:47 -0800948 allAppsInfoTarget.setDragAndDropEnabled(false);
Winson Chung4c98d922011-05-31 16:50:48 -0700949
950 // Setup the AllApps Market toolbar button
951 View marketButton = findViewById(R.id.market_button);
952 allAppsInfoTarget.setOverlappingView(marketButton);
Winson Chung4c98d922011-05-31 16:50:48 -0700953 } else {
954 // Get the search/delete bar
955 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
956
957 // Setup AppsCustomize
958 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
959 findViewById(R.id.apps_customize_pane);
960 mAppsCustomizeContent = (AppsCustomizePagedView)
961 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
962 mAppsCustomizeContent.setup(this, dragController);
963
964 // Setup AppsCustomize button
965 mHandleView = (HandleView) mDragLayer.findViewById(R.id.all_apps_button);
966 mHandleView.setLauncher(this);
967 mHandleView.setOnLongClickListener(this);
968 mHandleView.setOnClickListener(new OnClickListener() {
969 public void onClick(View v) {
970 onClickAllAppsButton(v);
971 }
972 });
973
974 // Setup Hotseat
975 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
976 hotseatLeft.setContentDescription(mHotseatLabels[0]);
977 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
978 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
979 hotseatRight.setContentDescription(mHotseatLabels[1]);
980 hotseatRight.setImageDrawable(mHotseatIcons[1]);
981
982 View.OnKeyListener listener = new IndicatorKeyEventListener();
983 mPreviousView = (ImageView) mDragLayer.findViewById(R.id.previous_screen);
984 mPreviousView.setOnKeyListener(listener);
985 mNextView = (ImageView) mDragLayer.findViewById(R.id.next_screen);
986 mNextView.setOnKeyListener(listener);
987
988 Drawable previous = mPreviousView.getDrawable();
989 Drawable next = mNextView.getDrawable();
990 mWorkspace.setIndicators(previous, next);
991
992 mPreviousView.setHapticFeedbackEnabled(false);
993 mPreviousView.setOnLongClickListener(this);
994 mNextView.setHapticFeedbackEnabled(false);
995 mNextView.setOnLongClickListener(this);
Adam Cohencdc30d52010-12-01 15:09:47 -0800996 }
997
Winson Chung4c98d922011-05-31 16:50:48 -0700998 // Setup keylistener for button cluster
Winson Chung97d85d22011-04-13 11:27:36 -0700999 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
Winson Chung4e6a9762011-05-09 11:56:34 -07001000 View.OnKeyListener listener = null;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07001001 if (LauncherApplication.isScreenLarge()) {
Winson Chung4e6a9762011-05-09 11:56:34 -07001002 // For tablets, AllApps lives in the button bar at the top
1003 listener = new ButtonBarKeyEventListener();
1004 } else {
1005 // For phones, AppsCustomize lives in the "dock" at the bottom
1006 listener = new DockKeyEventListener();
1007 }
Winson Chung97d85d22011-04-13 11:27:36 -07001008 int buttonCount = mButtonCluster.getChildCount();
1009 for (int i = 0; i < buttonCount; ++i) {
1010 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
1011 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001012
Winson Chung4c98d922011-05-31 16:50:48 -07001013 // Setup the drag controller (the drop targets have to be added in reverse order)
1014 dragController.setDragScoller(mWorkspace);
1015 dragController.setScrollView(mDragLayer);
1016 dragController.setMoveTarget(mWorkspace);
1017 dragController.addDropTarget(mWorkspace);
1018 if (mDeleteZone != null) {
1019 dragController.addDragListener(mDeleteZone);
1020 dragController.addDropTarget(mDeleteZone);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001021 }
Winson Chung4c98d922011-05-31 16:50:48 -07001022 if (mSearchDeleteBar != null) {
1023 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001024 }
Winson Chung4c98d922011-05-31 16:50:48 -07001025 if (allAppsDeleteZone != null) {
1026 dragController.addDragListener(allAppsDeleteZone);
1027 dragController.addDropTarget(allAppsDeleteZone);
1028 }
1029 if (allAppsInfoTarget != null) {
1030 dragController.addDragListener(allAppsInfoTarget);
1031 dragController.addDropTarget(allAppsInfoTarget);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001032 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 }
1034
Romain Guy8a73c512009-11-09 19:19:59 -08001035 @SuppressWarnings({"UnusedDeclaration"})
1036 public void previousScreen(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001037 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001038 mWorkspace.scrollLeft();
1039 }
Romain Guy8a73c512009-11-09 19:19:59 -08001040 }
1041
1042 @SuppressWarnings({"UnusedDeclaration"})
1043 public void nextScreen(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001044 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001045 mWorkspace.scrollRight();
1046 }
Romain Guy8a73c512009-11-09 19:19:59 -08001047 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001048
1049 @SuppressWarnings({"UnusedDeclaration"})
1050 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001051 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001052
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001053 int index = -1;
1054 if (v.getId() == R.id.hotseat_left) {
1055 index = 0;
1056 } else if (v.getId() == R.id.hotseat_right) {
1057 index = 1;
1058 }
1059
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001060 // reload these every tap; you never know when they might change
1061 loadHotseats();
1062 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001063 startActivitySafely(
1064 mHotseats[index],
1065 "hotseat"
1066 );
1067 }
1068 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 /**
1071 * Creates a view representing a shortcut.
1072 *
1073 * @param info The data structure describing the shortcut.
1074 *
1075 * @return A View inflated from R.layout.application.
1076 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001077 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001079 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 }
1081
1082 /**
1083 * Creates a view representing a shortcut inflated from the specified resource.
1084 *
1085 * @param layoutResId The id of the XML layout used to create the shortcut.
1086 * @param parent The group the shortcut belongs to.
1087 * @param info The data structure describing the shortcut.
1088 *
1089 * @return A View inflated from layoutResId.
1090 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001091 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001092 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1093 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 return favorite;
1096 }
1097
1098 /**
1099 * Add an application shortcut to the workspace.
1100 *
1101 * @param data The intent describing the application.
1102 * @param cellInfo The position on screen where to create the shortcut.
1103 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001104 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001105 int intersectCellX, int intersectCellY) {
1106 final int[] cellXY = mTmpAddItemCellCoordinates;
1107 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1108
1109 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1110 showOutOfSpaceMessage();
1111 return;
1112 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001114 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001115
Romain Guy73b979d2009-06-09 12:57:21 -07001116 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001117 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001119 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001120 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1121 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001122 } else {
1123 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 }
1125 }
Romain Guycbb89e42009-06-08 15:52:54 -07001126
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 /**
1128 * Add a shortcut to the workspace.
1129 *
1130 * @param data The intent describing the shortcut.
1131 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001133 private void completeAddShortcut(Intent data, int screen,
1134 int intersectCellX, int intersectCellY) {
1135 final int[] cellXY = mTmpAddItemCellCoordinates;
1136 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001137
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001138 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001139 boolean foundCellSpan = false;
1140 if (touchXY != null) {
1141 // when dragging and dropping, just find the closest free spot
1142 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1143 int[] result = screenLayout.findNearestVacantArea(
1144 touchXY[0], touchXY[1], 1, 1, cellXY);
1145 foundCellSpan = (result != null);
1146 } else {
1147 foundCellSpan = layout.findCellForSpanThatIntersects(
1148 cellXY, 1, 1, intersectCellX, intersectCellY);
1149 }
1150
1151 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001152 showOutOfSpaceMessage();
1153 return;
1154 }
1155
1156 final ShortcutInfo info = mModel.addShortcut(
1157 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158
1159 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001161 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 }
1163 }
1164
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001166 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001168 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001169 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 */
Winson Chungf7640c82011-02-28 13:47:29 -08001171 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001172 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001173
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001174 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001175 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1176 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001179 // We have saved the position to which the widget was dragged-- this really only matters
1180 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001181 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001182
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001183 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001184 boolean foundCellSpan = false;
1185 if (touchXY != null) {
1186 // when dragging and dropping, just find the closest free spot
1187 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1188 int[] result = screenLayout.findNearestVacantArea(
1189 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001190 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001191 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001192 // if we long pressed on an empty cell to bring up a menu,
1193 // make sure we intersect the empty cell
1194 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1195 // findCellForSpanThatIntersects will just ignore them
1196 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1197 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001198 }
1199
Michael Jurka0280c3b2010-09-17 15:00:07 -07001200 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001201 if (appWidgetId != -1) {
1202 // Deleting an app widget ID is a void call but writes to disk before returning
1203 // to the caller...
1204 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1205 new Thread("deleteAppWidgetId") {
1206 public void run() {
1207 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1208 }
1209 }.start();
1210 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001211 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001212 return;
1213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001215 // Build Launcher-specific widget info and save to database
1216 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001217 launcherInfo.spanX = spanXY[0];
1218 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 LauncherModel.addItemToDatabase(this, launcherInfo,
1221 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001222 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223
1224 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001225 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001228 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001229
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001230 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001232
Michael Jurka0280c3b2010-09-17 15:00:07 -07001233 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001234 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001235
1236 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 }
1238 }
Romain Guycbb89e42009-06-08 15:52:54 -07001239
Adam Cohended9f8d2010-11-03 13:25:16 -07001240 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1241 @Override
1242 public void onReceive(Context context, Intent intent) {
1243 final String action = intent.getAction();
1244 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1245 mUserPresent = false;
1246 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001247
1248 // Reset AllApps to it's initial state
1249 if (mAllAppsGrid != null) {
1250 mAllAppsGrid.reset();
1251 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001252 if (mAppsCustomizeContent != null) {
1253 mAppsCustomizeContent.reset();
1254 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001255 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1256 mUserPresent = true;
1257 updateRunning();
1258 }
1259 }
1260 };
1261
1262 @Override
1263 public void onAttachedToWindow() {
1264 super.onAttachedToWindow();
1265
1266 // Listen for broadcasts related to user-presence
1267 final IntentFilter filter = new IntentFilter();
1268 filter.addAction(Intent.ACTION_SCREEN_OFF);
1269 filter.addAction(Intent.ACTION_USER_PRESENT);
1270 registerReceiver(mReceiver, filter);
1271
Adam Cohend113e0c2010-11-11 10:48:05 -08001272 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001273 mVisible = true;
1274 }
1275
1276 @Override
1277 public void onDetachedFromWindow() {
1278 super.onDetachedFromWindow();
1279 mVisible = false;
1280
Adam Cohend113e0c2010-11-11 10:48:05 -08001281 if (mAttached) {
1282 unregisterReceiver(mReceiver);
1283 mAttached = false;
1284 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001285 updateRunning();
1286 }
1287
1288 public void onWindowVisibilityChanged(int visibility) {
1289 mVisible = visibility == View.VISIBLE;
1290 updateRunning();
1291 }
1292
1293 private void sendAdvanceMessage(long delay) {
1294 mHandler.removeMessages(ADVANCE_MSG);
1295 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1296 mHandler.sendMessageDelayed(msg, delay);
1297 mAutoAdvanceSentTime = System.currentTimeMillis();
1298 }
1299
1300 private void updateRunning() {
1301 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1302 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1303 mAutoAdvanceRunning = autoAdvanceRunning;
1304 if (autoAdvanceRunning) {
1305 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1306 sendAdvanceMessage(delay);
1307 } else {
1308 if (!mWidgetsToAdvance.isEmpty()) {
1309 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1310 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1311 }
1312 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001313 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001314 }
1315 }
1316 }
1317
1318 private final Handler mHandler = new Handler() {
1319 @Override
1320 public void handleMessage(Message msg) {
1321 if (msg.what == ADVANCE_MSG) {
1322 int i = 0;
1323 for (View key: mWidgetsToAdvance.keySet()) {
1324 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1325 final int delay = mAdvanceStagger * i;
1326 if (v instanceof Advanceable) {
1327 postDelayed(new Runnable() {
1328 public void run() {
1329 ((Advanceable) v).advance();
1330 }
1331 }, delay);
1332 }
1333 i++;
1334 }
1335 sendAdvanceMessage(mAdvanceInterval);
1336 }
1337 }
1338 };
1339
1340 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1341 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1342 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1343 if (v instanceof Advanceable) {
1344 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001345 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001346 updateRunning();
1347 }
1348 }
1349
1350 void removeWidgetToAutoAdvance(View hostView) {
1351 if (mWidgetsToAdvance.containsKey(hostView)) {
1352 mWidgetsToAdvance.remove(hostView);
1353 updateRunning();
1354 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001355 }
1356
Joe Onorato9c1289c2009-08-17 11:03:03 -04001357 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1358 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001359 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001360 launcherInfo.hostView = null;
1361 }
1362
Adam Cohended9f8d2010-11-03 13:25:16 -07001363 void showOutOfSpaceMessage() {
1364 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1365 }
1366
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001367 public LauncherAppWidgetHost getAppWidgetHost() {
1368 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 }
Romain Guycbb89e42009-06-08 15:52:54 -07001370
Winson Chunga9abd0e2010-10-27 17:18:37 -07001371 public LauncherModel getModel() {
1372 return mModel;
1373 }
1374
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001375 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001376 getWindow().closeAllPanels();
1377
1378 try {
1379 dismissDialog(DIALOG_CREATE_SHORTCUT);
1380 // Unlock the workspace if the dialog was showing
1381 } catch (Exception e) {
1382 // An exception is thrown if the dialog is not visible, which is fine
1383 }
1384
1385 try {
1386 dismissDialog(DIALOG_RENAME_FOLDER);
1387 // Unlock the workspace if the dialog was showing
1388 } catch (Exception e) {
1389 // An exception is thrown if the dialog is not visible, which is fine
1390 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001391
1392 // Whatever we were doing is hereby canceled.
1393 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001394 }
1395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 @Override
1397 protected void onNewIntent(Intent intent) {
1398 super.onNewIntent(intent);
1399
1400 // Close the menu
1401 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001402 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001403 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001404
Joe Onorato14f122b2009-11-19 14:06:36 -08001405 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1406 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001407
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001408 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001409 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001410 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001411 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001412 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001413 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001414 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001415
Joe Onorato3a8820b2009-11-10 15:06:42 -08001416 final View v = getWindow().peekDecorView();
1417 if (v != null && v.getWindowToken() != null) {
1418 InputMethodManager imm = (InputMethodManager)getSystemService(
1419 INPUT_METHOD_SERVICE);
1420 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001422
1423 // Reset AllApps to it's initial state
1424 if (mAllAppsGrid != null) {
1425 mAllAppsGrid.reset();
1426 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001427 if (mAppsCustomizeContent != null) {
1428 mAppsCustomizeContent.reset();
1429 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 }
1431 }
1432
1433 @Override
1434 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1435 // Do not call super here
1436 mSavedInstanceState = savedInstanceState;
1437 }
1438
1439 @Override
1440 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001441 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442
1443 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1444 if (folders.size() > 0) {
1445 final int count = folders.size();
1446 long[] ids = new long[count];
1447 for (int i = 0; i < count; i++) {
1448 final FolderInfo info = folders.get(i).getInfo();
1449 ids[i] = info.id;
1450 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001451 outState.putLongArray(RUNTIME_STATE_FOLDERS, ids);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 } else {
1453 super.onSaveInstanceState(outState);
1454 }
1455
Michael Jurka883f55b2010-10-21 15:47:14 -07001456 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457
Michael Jurka0280c3b2010-09-17 15:00:07 -07001458 if (mAddScreen > -1 && mWaitingForResult) {
1459 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1460 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1461 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 }
1463
1464 if (mFolderInfo != null && mWaitingForResult) {
1465 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1466 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1467 }
Michael Jurka946ad472010-07-09 18:05:18 -07001468
Winson Chung43b119d2010-12-06 16:45:05 -08001469 // Save the current AllApps drawer tab
1470 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1471 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1472 String currentTabTag = tabhost.getCurrentTabTag();
1473 if (currentTabTag != null) {
1474 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001475 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001476 }
1477 }
Winson Chung43b119d2010-12-06 16:45:05 -08001478 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001479 if (mHomeCustomizationDrawer != null) {
1480 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1481 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001482 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001483 }
1484 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001485 // Save the current AppsCustomize tab
1486 if (mAppsCustomizeTabHost != null) {
1487 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1488 if (currentTabTag != null) {
1489 outState.putString("apps_customize_currentTab", currentTabTag);
1490 }
1491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 }
1493
1494 @Override
1495 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001497
Winson Chungcd2b0142011-06-08 16:02:26 -07001498 // Stop callbacks from LauncherModel
1499 LauncherApplication app = ((LauncherApplication) getApplication());
1500 mModel.stopLoader();
1501 app.setLauncher(null);
1502
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001504 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001506 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001508 mAppWidgetHost = null;
1509
1510 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511
1512 TextKeyListener.getInstance().release();
1513
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514
Joe Onorato9c1289c2009-08-17 11:03:03 -04001515 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001516
1517 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001518
Patrick Dubroyab962b72010-07-26 12:10:10 -07001519 // Some launcher layouts don't have a previous and next view
1520 if (mPreviousView != null) {
1521 dismissPreview(mPreviousView);
1522 }
1523 if (mNextView != null) {
1524 dismissPreview(mNextView);
1525 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001526
1527 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001528
1529 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1530 mWorkspace.removeAllViews();
1531 mWorkspace = null;
1532 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001533
1534 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 }
1536
Adam Cohena9cf38f2011-05-02 15:36:58 -07001537 public DragController getDragController() {
1538 return mDragController;
1539 }
1540
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 @Override
1542 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001543 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 super.startActivityForResult(intent, requestCode);
1545 }
1546
1547 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001548 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001550
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001551 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001552
Karl Rosaen138a0412009-04-23 19:00:21 -07001553 if (initialQuery == null) {
1554 // Use any text typed in the launcher as the initial query
1555 initialQuery = getTypedText();
1556 clearTypedText();
1557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 if (appSearchData == null) {
1559 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001560 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 }
Romain Guycbb89e42009-06-08 15:52:54 -07001562
Karl Rosaen138a0412009-04-23 19:00:21 -07001563 final SearchManager searchManager =
1564 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001565 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001566 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
1568
1569 @Override
1570 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001571 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001572 return false;
1573 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574
1575 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001576
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1578 .setIcon(android.R.drawable.ic_menu_add)
1579 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001580 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1581 .setIcon(android.R.drawable.ic_menu_manage)
1582 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001583 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 .setIcon(android.R.drawable.ic_menu_gallery)
1585 .setAlphabeticShortcut('W');
1586 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1587 .setIcon(android.R.drawable.ic_search_category_default)
1588 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1589 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1590 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1591 .setAlphabeticShortcut('N');
1592
1593 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001594 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1595 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596
1597 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1598 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1599 .setIntent(settings);
1600
1601 return true;
1602 }
1603
1604 @Override
1605 public boolean onPrepareOptionsMenu(Menu menu) {
1606 super.onPrepareOptionsMenu(menu);
1607
Winson Chung785d2eb2011-04-14 16:08:02 -07001608 if (mAllAppsGrid != null) {
1609 // If all apps is animating, don't show the menu, because we don't know
1610 // which one to show.
1611 if (mAllAppsGrid.isAnimating()) {
1612 return false;
1613 }
1614
1615 // Only show the add and wallpaper options when we're not in all apps.
1616 boolean visible = !mAllAppsGrid.isVisible();
1617 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1618 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1619
1620 // Disable add if the workspace is full.
1621 if (visible) {
1622 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1623 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
1624 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001625 }
1626
Winson Chung785d2eb2011-04-14 16:08:02 -07001627 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1628 // related code?
1629 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630
1631 return true;
1632 }
1633
1634 @Override
1635 public boolean onOptionsItemSelected(MenuItem item) {
1636 switch (item.getItemId()) {
1637 case MENU_ADD:
1638 addItems();
1639 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001640 case MENU_MANAGE_APPS:
1641 manageApps();
1642 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 case MENU_WALLPAPER_SETTINGS:
1644 startWallpaper();
1645 return true;
1646 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001647 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 return true;
1649 case MENU_NOTIFICATIONS:
1650 showNotifications();
1651 return true;
1652 }
1653
1654 return super.onOptionsItemSelected(item);
1655 }
Romain Guycbb89e42009-06-08 15:52:54 -07001656
Karl Rosaen138a0412009-04-23 19:00:21 -07001657 /**
1658 * Indicates that we want global search for this activity by setting the globalSearch
1659 * argument for {@link #startSearch} to true.
1660 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001662 @Override
1663 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001664 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001665 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001666 }
1667
Joe Onorato9c1289c2009-08-17 11:03:03 -04001668 public boolean isWorkspaceLocked() {
1669 return mWorkspaceLoading || mWaitingForResult;
1670 }
1671
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001672 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1673 private boolean isPreviewVisible() {
1674 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1675 (mNextView != null && mNextView.getTag() != null);
1676 }
1677
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 private void addItems() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07001679 if (LauncherApplication.isScreenLarge()) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001680 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001681 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001682 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001683 }
1684 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001685 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001686 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001687 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 }
1689
Michael Jurka0280c3b2010-09-17 15:00:07 -07001690 private void resetAddInfo() {
1691 mAddScreen = -1;
1692 mAddIntersectCellX = -1;
1693 mAddIntersectCellY = -1;
1694 mAddDropPosition = null;
1695 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001696
Winson Chung55cef262010-10-28 14:14:18 -07001697 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001698 resetAddInfo();
1699 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001700 mAddDropPosition = position;
1701
Michael Jurkaaf442092010-06-10 17:01:57 -07001702 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001703 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1704 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001705 }
1706
Winson Chung7ad01412010-09-27 11:33:03 -07001707 private void manageApps() {
1708 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1709 }
1710
Michael Jurkaaf442092010-06-10 17:01:57 -07001711 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001712 // TODO: catch bad widget exception when sent
1713 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001714 // TODO: Is this log message meaningful?
1715 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001716 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001717 }
1718
Winson Chung55cef262010-10-28 14:14:18 -07001719 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001720 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721
Bjorn Bringert7984c942009-12-09 15:38:25 +00001722 if (appWidget.configure != null) {
1723 // Launch over to configure widget, if needed
1724 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1725 intent.setComponent(appWidget.configure);
1726 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001727 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001728 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1729 intent.putExtra(
1730 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1731 info.mimeType);
1732
1733 final String mimeType = info.mimeType;
1734 final ClipData clipData = (ClipData) info.configurationData;
1735 final ClipDescription clipDesc = clipData.getDescription();
1736 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1737 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001738 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001739 final CharSequence stringData = item.getText();
1740 final Uri uriData = item.getUri();
1741 final Intent intentData = item.getIntent();
1742 final String key =
1743 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1744 if (uriData != null) {
1745 intent.putExtra(key, uriData);
1746 } else if (intentData != null) {
1747 intent.putExtra(key, intentData);
1748 } else if (stringData != null) {
1749 intent.putExtra(key, stringData);
1750 }
1751 break;
1752 }
1753 }
1754 }
Winson Chung55cef262010-10-28 14:14:18 -07001755 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001756
Romain Guy8e633c52010-03-04 12:51:36 -08001757 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001759 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001760 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 }
1762 }
Romain Guycbb89e42009-06-08 15:52:54 -07001763
Michael Jurka0280c3b2010-09-17 15:00:07 -07001764 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1765 resetAddInfo();
1766 mAddScreen = screen;
1767 mAddDropPosition = position;
1768
1769 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1770 createShortcutIntent.setComponent(componentName);
1771 processShortcut(createShortcutIntent);
1772 }
1773
Joe Onoratodeb98af2010-02-19 14:59:39 -08001774 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001775 // Handle case where user selected "Applications"
1776 String applicationName = getResources().getString(R.string.group_applications);
1777 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001778
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001779 if (applicationName != null && applicationName.equals(shortcutName)) {
1780 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1781 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001782
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001783 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1784 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001785 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001786 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001787 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001788 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001789 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 }
1791
Winson Chung24ab2f12010-09-16 14:10:47 -07001792 void processWallpaper(Intent intent) {
1793 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1794 }
1795
Adam Cohendf035382011-04-11 17:22:04 -07001796 FolderIcon addFolder(int screen, int intersectCellX, int intersectCellY) {
Adam Cohendf2cc412011-04-27 16:56:57 -07001797 FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 folderInfo.title = getText(R.string.folder_name);
1799
Michael Jurka0280c3b2010-09-17 15:00:07 -07001800 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1801 final int[] cellXY = mTmpAddItemCellCoordinates;
1802 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1803 showOutOfSpaceMessage();
Adam Cohendf035382011-04-11 17:22:04 -07001804 return null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001805 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806
1807 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001808 LauncherModel.addItemToDatabase(this, folderInfo,
1809 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001810 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001811 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812
1813 // Create the view
1814 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001815 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1816 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001817 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001818 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 }
Romain Guycbb89e42009-06-08 15:52:54 -07001820
Joe Onorato9c1289c2009-08-17 11:03:03 -04001821 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001822 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001823 }
1824
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 private void showNotifications() {
1826 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1827 if (statusBar != null) {
1828 statusBar.expand();
1829 }
1830 }
1831
1832 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001833 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001835 Intent chooser = Intent.createChooser(pickWallpaper,
1836 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001837 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1838 // Removed in Eclair MR1
1839// WallpaperManager wm = (WallpaperManager)
1840// getSystemService(Context.WALLPAPER_SERVICE);
1841// WallpaperInfo wi = wm.getWallpaperInfo();
1842// if (wi != null && wi.getSettingsActivity() != null) {
1843// LabeledIntent li = new LabeledIntent(getPackageName(),
1844// R.string.configure_wallpaper, 0);
1845// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1846// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1847// }
Mike Clerona0618e42009-10-22 13:55:21 -07001848 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 }
1850
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001851 /**
1852 * Registers various content observers. The current implementation registers
1853 * only a favorites observer to keep track of the favorites applications.
1854 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001855 private void registerContentObservers() {
1856 ContentResolver resolver = getContentResolver();
1857 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1858 true, mWidgetObserver);
1859 }
1860
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 @Override
1862 public boolean dispatchKeyEvent(KeyEvent event) {
1863 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1864 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001866 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001867 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001868 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001869 dumpState();
1870 return true;
1871 }
1872 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001873 }
1874 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1875 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001876 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 return true;
1878 }
1879 }
1880
1881 return super.dispatchKeyEvent(event);
1882 }
1883
Joe Onorato88ec0992009-11-19 13:16:06 -08001884 @Override
1885 public void onBackPressed() {
Winson Chung785d2eb2011-04-14 16:08:02 -07001886 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001887 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001888 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08001889 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001890 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001891 dismissPreview(mPreviousView);
1892 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001893 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001894 mWorkspace.exitWidgetResizeMode();
1895
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001896 // Back button is a no-op here, but give at least some feedback for the button press
1897 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001898 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001899 }
1900
Adam Cohen2801caf2011-05-13 20:57:39 -07001901 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 Folder folder = mWorkspace.getOpenFolder();
1903 if (folder != null) {
1904 closeFolder(folder);
Adam Cohen2801caf2011-05-13 20:57:39 -07001905 mDragLayer.setCurrentFolder(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 }
1907 }
1908
1909 void closeFolder(Folder folder) {
1910 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001911
Michael Jurka8c920dd2011-01-20 14:16:56 -08001912 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001914 CellLayout cl = (CellLayout) parent;
Adam Cohen2801caf2011-05-13 20:57:39 -07001915 FolderIcon fi = (FolderIcon) cl.getChildAt(folder.mInfo.cellX, folder.mInfo.cellY);
1916 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001917 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001919
1920 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 folder.onClose();
1922 }
1923
1924 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001925 * Re-listen when widgets are reset.
1926 */
1927 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001928 if (mAppWidgetHost != null) {
1929 mAppWidgetHost.startListening();
1930 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001931 }
1932
1933 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001934 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1935 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001937 private void unbindDesktopItems() {
1938 for (ItemInfo item: mDesktopItems) {
1939 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001941 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001943
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 /**
1945 * Launches the intent referred by the clicked shortcut.
1946 *
1947 * @param v The view representing the clicked shortcut.
1948 */
1949 public void onClick(View v) {
1950 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001951 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001953 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001954 int[] pos = new int[2];
1955 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001956 intent.setSourceBounds(new Rect(pos[0], pos[1],
1957 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001958 boolean success = startActivitySafely(intent, tag);
1959
1960 if (success && v instanceof BubbleTextView) {
1961 mWaitingForResume = (BubbleTextView) v;
1962 mWaitingForResume.setStayPressed(true);
1963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001965 if (v instanceof FolderIcon) {
1966 FolderIcon fi = (FolderIcon) v;
1967 handleFolderClick(fi);
1968 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001969 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001970 if (mState == State.ALL_APPS) {
1971 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001972 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001973 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001974 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 }
1976 }
1977
Michael Jurka0e260592010-06-30 17:07:39 -07001978 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001979 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001980 // clicking anywhere on the workspace causes the customization drawer to slide down
1981 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001982 return false;
1983 }
1984
Michael Jurkaaf442092010-06-10 17:01:57 -07001985 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001986 * Event handler for the search button
1987 *
1988 * @param v The view that was clicked.
1989 */
1990 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001991 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001992 // Use a custom animation for launching search
1993 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001994 }
1995
1996 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001997 * Event handler for the voice button
1998 *
1999 * @param v The view that was clicked.
2000 */
2001 public void onClickVoiceButton(View v) {
2002 startVoiceSearch();
2003 }
2004
2005 private void startVoiceSearch() {
2006 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2007 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2008 startActivity(intent);
2009 }
2010
2011 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002012 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002013 * enters home screen customization mode.
2014 *
2015 * @param v The view that was clicked.
2016 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002017 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002018 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002019 }
2020
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002021 /**
2022 * Event handler for the "grid" button that appears on the home screen, which
2023 * enters all apps mode.
2024 *
2025 * @param v The view that was clicked.
2026 */
2027 public void onClickAllAppsButton(View v) {
2028 showAllApps(true);
2029 }
2030
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002031 public void onClickAppMarketButton(View v) {
2032 if (mAppMarketIntent != null) {
2033 startActivitySafely(mAppMarketIntent, "app market");
2034 }
2035 }
2036
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002037 void startApplicationDetailsActivity(ComponentName componentName) {
2038 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002039 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2040 Uri.fromParts("package", packageName, null));
2041 startActivity(intent);
2042 }
2043
Patrick Dubroy5539af72010-09-07 15:22:01 -07002044 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2045 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2046 // System applications cannot be installed. For now, show a toast explaining that.
2047 // We may give them the option of disabling apps this way.
2048 int messageId = R.string.uninstall_system_app_text;
2049 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2050 } else {
2051 String packageName = appInfo.componentName.getPackageName();
2052 String className = appInfo.componentName.getClassName();
2053 Intent intent = new Intent(
2054 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2055 startActivity(intent);
2056 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002057 }
2058
Michael Jurkaddd62e92011-02-16 17:49:14 -08002059 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2061 try {
2062 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002063 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 } catch (ActivityNotFoundException e) {
2065 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002066 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 } catch (SecurityException e) {
2068 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002069 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002071 "or use the exported attribute for this activity. "
2072 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002074 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002076
Romain Guy8e633c52010-03-04 12:51:36 -08002077 void startActivityForResultSafely(Intent intent, int requestCode) {
2078 try {
2079 startActivityForResult(intent, requestCode);
2080 } catch (ActivityNotFoundException e) {
2081 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2082 } catch (SecurityException e) {
2083 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2084 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2085 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2086 "or use the exported attribute for this activity.", e);
2087 }
2088 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089
Adam Cohena9cf38f2011-05-02 15:36:58 -07002090 private void handleFolderClick(FolderIcon folderIcon) {
2091 final FolderInfo info = folderIcon.mInfo;
2092 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 // Close any open folder
2094 closeFolder();
2095 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002096 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 } else {
2098 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07002099 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 int folderScreen;
2101 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002102 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 // .. and close it
2104 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002105 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 // Close any folder open on the current screen
2107 closeFolder();
2108 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002109 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 }
2111 }
2112 }
2113 }
2114
Adam Cohen2801caf2011-05-13 20:57:39 -07002115 private void growAndFadeOutFolderIcon(FolderIcon fi) {
2116 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2117 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2118 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2119
2120 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2121 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2122 oa.start();
2123 }
2124
2125 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
2126 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2127 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2128 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2129
2130 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2131 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2132 oa.start();
2133 }
2134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002136 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 * is animated relative to the specified View. If the View is null, no animation
2138 * is played.
2139 *
2140 * @param folderInfo The FolderInfo describing the folder to open.
2141 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002142 public void openFolder(FolderIcon folderIcon) {
2143 Folder folder = folderIcon.mFolder;
2144 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002145
Adam Cohen2801caf2011-05-13 20:57:39 -07002146 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002147 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148
Adam Cohena9cf38f2011-05-02 15:36:58 -07002149 mWorkspace.addInFullScreen(folder, info.screen);
Adam Cohen2801caf2011-05-13 20:57:39 -07002150 mDragLayer.setCurrentFolder(folder);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002151 folder.animateOpen();
2152 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153 }
2154
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002156 if (mState != State.WORKSPACE) {
2157 return false;
2158 }
2159
Romain Guy1fbc1c82009-11-09 20:43:08 -08002160 switch (v.getId()) {
2161 case R.id.previous_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002162 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002163 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2164 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002165 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002166 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002167 return true;
2168 case R.id.next_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002169 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002170 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2171 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002172 showPreviews(v);
2173 }
2174 return true;
2175 case R.id.all_apps_button:
Winson Chung785d2eb2011-04-14 16:08:02 -07002176 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002177 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2178 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2179 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002180 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002181 return true;
2182 }
2183
Joe Onorato9c1289c2009-08-17 11:03:03 -04002184 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 return false;
2186 }
2187
2188 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002189 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 }
2191
Michael Jurka0280c3b2010-09-17 15:00:07 -07002192 resetAddInfo();
2193 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002195 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 return true;
2197 }
2198
Michael Jurka0280c3b2010-09-17 15:00:07 -07002199 final View itemUnderLongClick = longClickCellInfo.cell;
2200
Winson Chung304dcde2011-01-07 11:17:23 -08002201 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002202 if (itemUnderLongClick == null) {
2203 // User long pressed on empty space
2204 mWorkspace.setAllowLongPress(false);
2205 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2206 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07002207 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002209 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002211 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2212 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002213 mAddIntersectCellX = longClickCellInfo.cellX;
2214 mAddIntersectCellY = longClickCellInfo.cellY;
2215 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 }
2217 }
2218 }
2219 return true;
2220 }
2221
Romain Guye6b8e2f2009-11-10 11:56:55 -08002222 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002223 private void dismissPreview(final View v) {
2224 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002225 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002226 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2227 public void onDismiss() {
2228 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2229 int count = group.getChildCount();
2230 for (int i = 0; i < count; i++) {
2231 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2232 }
Michael Jurka25913ca2011-05-04 17:45:33 -07002233 ArrayList<Bitmap> bitmaps =
2234 (ArrayList<Bitmap>) v.getTag(R.id.all_apps_button_cluster);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002235 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2236
2237 v.setTag(R.id.workspace, null);
Michael Jurka25913ca2011-05-04 17:45:33 -07002238 v.setTag(R.id.all_apps_button_cluster, null);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002239 window.setOnDismissListener(null);
2240 }
2241 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002242 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002243 }
2244 v.setTag(null);
2245 }
2246
Romain Guyf8e6a802009-12-07 17:48:02 -08002247 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002248 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002249 }
2250
Romain Guya6abce82009-11-10 02:54:41 -08002251 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002252 final Resources resources = getResources();
2253 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002254
Romain Guya6abce82009-11-10 02:54:41 -08002255 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002256
Romain Guy9d31e9f2009-11-11 18:54:28 -08002257 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002258
Romain Guyf8e6a802009-12-07 17:48:02 -08002259 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002260 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002261 int extraW = (int) ((r.left + r.right) * max);
2262 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002263
2264 int aW = cell.getWidth() - extraW;
2265 float w = aW / max;
2266
2267 int width = cell.getWidth();
2268 int height = cell.getHeight();
2269 int x = cell.getLeftPadding();
2270 int y = cell.getTopPadding();
2271 width -= (x + cell.getRightPadding());
2272 height -= (y + cell.getBottomPadding());
2273
2274 float scale = w / width;
2275
2276 int count = end - start;
2277
2278 final float sWidth = width * scale;
2279 float sHeight = height * scale;
2280
Romain Guye6b8e2f2009-11-10 11:56:55 -08002281 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002282
Romain Guye6b8e2f2009-11-10 11:56:55 -08002283 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2284 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002285
2286 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002287 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002288 cell = (CellLayout) workspace.getChildAt(i);
2289
Romain Guyf8e6a802009-12-07 17:48:02 -08002290 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002291 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002292
2293 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002294 c.scale(scale, scale);
2295 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002296 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002297
Romain Guy9d31e9f2009-11-11 18:54:28 -08002298 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002299 image.setImageBitmap(bitmap);
2300 image.setTag(i);
2301 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002302 image.setOnFocusChangeListener(handler);
2303 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002304 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002305
2306 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002307 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2308
Winson Chungaafa03c2010-06-11 17:34:16 -07002309 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002310 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002311
2312 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002313 p.setContentView(preview);
2314 p.setWidth((int) (sWidth * count + extraW));
2315 p.setHeight((int) (sHeight + extraH));
2316 p.setAnimationStyle(R.style.AnimationPreview);
2317 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002318 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002319 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002320 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002321
Romain Guye6b8e2f2009-11-10 11:56:55 -08002322 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2323 public void onDismiss() {
2324 dismissPreview(anchor);
2325 }
2326 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002327
2328 anchor.setTag(p);
2329 anchor.setTag(R.id.workspace, preview);
Michael Jurka25913ca2011-05-04 17:45:33 -07002330 anchor.setTag(R.id.all_apps_button_cluster, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002331 }
2332
Romain Guy9d31e9f2009-11-11 18:54:28 -08002333 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002334 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002335
Romain Guye6b8e2f2009-11-10 11:56:55 -08002336 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002337 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002338 }
2339
2340 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002341 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002342 v.post(this);
2343 }
2344
2345 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002346 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002347 }
2348
2349 public void onFocusChange(View v, boolean hasFocus) {
2350 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002351 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002352 }
Romain Guya6abce82009-11-10 02:54:41 -08002353 }
2354 }
2355
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356 Workspace getWorkspace() {
2357 return mWorkspace;
2358 }
2359
Michael Jurka3c38d562011-02-18 19:22:12 -08002360 TabHost getCustomizationDrawer() {
2361 return mHomeCustomizationDrawer;
2362 }
2363
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002364 @Override
2365 protected Dialog onCreateDialog(int id) {
2366 switch (id) {
2367 case DIALOG_CREATE_SHORTCUT:
2368 return new CreateShortcut().createDialog();
2369 case DIALOG_RENAME_FOLDER:
2370 return new RenameFolder().createDialog();
2371 }
2372
2373 return super.onCreateDialog(id);
2374 }
2375
2376 @Override
2377 protected void onPrepareDialog(int id, Dialog dialog) {
2378 switch (id) {
2379 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002380 break;
2381 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002382 if (mFolderInfo != null) {
2383 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2384 final CharSequence text = mFolderInfo.title;
2385 input.setText(text);
2386 input.setSelection(0, text.length());
2387 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388 break;
2389 }
2390 }
2391
2392 void showRenameDialog(FolderInfo info) {
2393 mFolderInfo = info;
2394 mWaitingForResult = true;
2395 showDialog(DIALOG_RENAME_FOLDER);
2396 }
2397
Michael Jurka0280c3b2010-09-17 15:00:07 -07002398 private void showAddDialog(int intersectX, int intersectY) {
2399 resetAddInfo();
2400 mAddIntersectCellX = intersectX;
2401 mAddIntersectCellY = intersectY;
2402 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 mWaitingForResult = true;
2404 showDialog(DIALOG_CREATE_SHORTCUT);
2405 }
2406
Joe Onoratodeb98af2010-02-19 14:59:39 -08002407 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002408 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002409 Bundle bundle = new Bundle();
2410
2411 ArrayList<String> shortcutNames = new ArrayList<String>();
2412 shortcutNames.add(getString(R.string.group_applications));
2413 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2414
2415 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2416 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2417 R.drawable.ic_launcher_application));
2418 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2419
2420 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2421 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002422 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002423 pickIntent.putExtras(bundle);
2424
Joe Onoratodeb98af2010-02-19 14:59:39 -08002425 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002426 }
2427
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 private class RenameFolder {
2429 private EditText mInput;
2430
2431 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2433 mInput = (EditText) layout.findViewById(R.id.folder_name);
2434
2435 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2436 builder.setIcon(0);
2437 builder.setTitle(getString(R.string.rename_folder_title));
2438 builder.setCancelable(true);
2439 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2440 public void onCancel(DialogInterface dialog) {
2441 cleanup();
2442 }
2443 });
2444 builder.setNegativeButton(getString(R.string.cancel_action),
2445 new Dialog.OnClickListener() {
2446 public void onClick(DialogInterface dialog, int which) {
2447 cleanup();
2448 }
2449 }
2450 );
2451 builder.setPositiveButton(getString(R.string.rename_action),
2452 new Dialog.OnClickListener() {
2453 public void onClick(DialogInterface dialog, int which) {
2454 changeFolderName();
2455 }
2456 }
2457 );
2458 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002459
2460 final AlertDialog dialog = builder.create();
2461 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2462 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002463 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002464 mInput.requestFocus();
2465 InputMethodManager inputManager = (InputMethodManager)
2466 getSystemService(Context.INPUT_METHOD_SERVICE);
2467 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002468 }
2469 });
2470
2471 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002472 }
2473
2474 private void changeFolderName() {
2475 final String name = mInput.getText().toString();
2476 if (!TextUtils.isEmpty(name)) {
2477 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002478 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002479 mFolderInfo.title = name;
2480 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2481
Joe Onorato9c1289c2009-08-17 11:03:03 -04002482 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002483 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002484 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 } else {
2486 final FolderIcon folderIcon = (FolderIcon)
2487 mWorkspace.getViewForTag(mFolderInfo);
2488 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002489 // TODO: At some point we'll probably want some version of setting
2490 // the text for a folder icon.
2491 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002492 getWorkspace().requestLayout();
2493 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002494 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002495 mWorkspaceLoading = true;
2496 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 }
2498 }
2499 }
2500 cleanup();
2501 }
2502
2503 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002504 dismissDialog(DIALOG_RENAME_FOLDER);
2505 mWaitingForResult = false;
2506 mFolderInfo = null;
2507 }
2508 }
2509
Daniel Sandler843e8602010-06-07 14:59:01 -04002510 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2511 public boolean isAllAppsVisible() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002512 return (mState == State.ALL_APPS) || (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002513 }
2514
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002515 // AllAppsView.Watcher
2516 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002517 // In XLarge view, we zoom down the workspace below all apps so it's still visible
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002518 if (zoom == 1.0f && !LauncherApplication.isScreenLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002519 mWorkspace.setVisibility(View.GONE);
2520 }
2521 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002522
Michael Jurkaafca9532011-02-16 14:50:35 -08002523 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002524 button.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002525 }
2526
2527 private void hideToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002528 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2529 button.setVisibility(View.INVISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002530 button.setAlpha(0.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002531 }
2532
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002533 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002534 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002535 *
2536 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2537 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002538 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002539 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002540 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002541 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002542 final boolean showing = show;
2543 final boolean hiding = !show;
2544
2545 final int duration = show ?
2546 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2547 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2548
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002549 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002550 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002551 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002552 anim.addUpdateListener(new AnimatorUpdateListener() {
2553 public void onAnimationUpdate(ValueAnimator animation) {
2554 view.setAlpha((Float) animation.getAnimatedValue());
2555 }
2556 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002557 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002558 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002559 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002560 if (showing) showAndEnableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002561 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002562 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002563 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002564 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002565 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002566 });
2567 seq.play(anim);
2568 } else {
2569 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002570 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002571 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002572 } else {
2573 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002574 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002575 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002576 }
2577
2578 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002579 * Show/hide the appropriate toolbar buttons for newState.
2580 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002581 *
2582 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002583 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2584 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002585 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002586 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002587 switch (newState) {
2588 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002589 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
Winson Chung4c98d922011-05-31 16:50:48 -07002590 if (mDeleteZone != null) {
2591 mDeleteZone.setDragAndDropEnabled(true);
2592 if (LauncherApplication.isScreenLarge()) {
2593 mDeleteZone.setText(
2594 getResources().getString(R.string.delete_zone_label_workspace));
2595 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002596 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002597 break;
2598 case ALL_APPS:
Winson Chung785d2eb2011-04-14 16:08:02 -07002599 case APPS_CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002600 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Winson Chung4c98d922011-05-31 16:50:48 -07002601 if (mDeleteZone != null) {
2602 mDeleteZone.setDragAndDropEnabled(false);
2603 if (LauncherApplication.isScreenLarge()) {
2604 mDeleteZone.setText(
2605 getResources().getString(R.string.delete_zone_label_all_apps));
2606 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002607 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002608 break;
2609 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002610 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Winson Chung4c98d922011-05-31 16:50:48 -07002611 if (mDeleteZone != null) {
2612 mDeleteZone.setDragAndDropEnabled(false);
2613 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002614 break;
2615 }
2616 }
2617
2618 /**
2619 * Helper method for the cameraZoomIn/cameraZoomOut animations
2620 * @param view The view being animated
2621 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2622 * @param scaleFactor The scale factor used for the zoom
2623 */
2624 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2625 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002626
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002627 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002628 // Set pivotY so that at the starting zoom factor, the view is partially
2629 // visible. Modifying initialHeightFactor changes how much of the view is
2630 // initially showing, and hence the perceived angle from which the view enters.
Winson Chung785d2eb2011-04-14 16:08:02 -07002631 if (state == State.ALL_APPS || state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002632 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002633 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002634 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002635 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002636 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002637 }
2638 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002639
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002640 /**
2641 * Zoom the camera out from the workspace to reveal 'toView'.
2642 * Assumes that the view to show is anchored at either the very top or very bottom
2643 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002644 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002645 */
Winson Chung20f71112011-04-06 14:22:04 -07002646 private void cameraZoomOut(State toState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002647 final Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002648 final boolean toAllApps = (toState == State.ALL_APPS)
2649 || (toState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002650
Winson Chung785d2eb2011-04-14 16:08:02 -07002651 final int duration = (toAllApps ?
2652 res.getInteger(R.integer.config_appsCustomizeZoomInTime) :
2653 res.getInteger(R.integer.config_customizeZoomInTime));
2654 final int fadeDuration = (toAllApps ?
2655 res.getInteger(R.integer.config_appsCustomizeFadeInTime) :
2656 res.getInteger(R.integer.config_customizeFadeInTime));
Adam Cohenf16e5712011-01-13 13:31:45 -08002657
2658 final float scale = toAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002659 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002660 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2661
Winson Chung785d2eb2011-04-14 16:08:02 -07002662 View tmpView;
2663 if (toAllApps) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002664 tmpView = (LauncherApplication.isScreenLarge())
Winson Chung785d2eb2011-04-14 16:08:02 -07002665 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2666 } else {
2667 tmpView = mHomeCustomizationDrawer;
2668 }
2669 final View toView = tmpView;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002670
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002671 setPivotsForZoom(toView, toState, scale);
2672
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002673 if (toAllApps) {
Winson Chung20f71112011-04-06 14:22:04 -07002674 if (!springLoaded) {
2675 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2676
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002677 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002678 // Everytime we launch into AllApps, we reset the successful drop flag which
2679 // controls when it should hide/show the mini workspaces
2680 mAllAppsPagedView.resetSuccessfulDropFlag();
2681 }
Winson Chung20f71112011-04-06 14:22:04 -07002682 } else {
2683 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
2684 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002685 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002686 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002687 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002688
2689 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002690 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002691 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002692 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2693 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002694 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002695 toView.setFastScaleX(a * scale + b * 1f);
2696 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002697 }
2698 });
Adam Cohen61033d32010-11-15 18:29:44 -08002699
Adam Cohenf16e5712011-01-13 13:31:45 -08002700 if (toAllApps) {
Winson Chung4e6a9762011-05-09 11:56:34 -07002701 toView.setVisibility(View.VISIBLE);
Michael Jurka2763be32011-02-24 11:19:57 -08002702 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002703 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002704 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002705 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2706 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002707 // don't need to invalidate because we do so above
2708 toView.setFastAlpha(a * 0f + b * 1f);
2709 }
2710 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002711 alphaAnim.start();
2712 }
2713
Michael Jurkaabded662011-03-04 12:06:57 -08002714 if (toView instanceof LauncherTransitionable) {
2715 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002716 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002717 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002718 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002719 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002720 // Prepare the position
2721 toView.setTranslationX(0.0f);
2722 toView.setTranslationY(0.0f);
2723 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002724 toView.bringToFront();
Adam Cohenf16e5712011-01-13 13:31:45 -08002725 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002726 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002727 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002728 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002729 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002730 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002731 // If we don't set the final scale values here, if this animation is cancelled
2732 // it will have the wrong scale value and subsequent cameraPan animations will
2733 // not fix that
2734 toView.setScaleX(1.0f);
2735 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002736 if (toView instanceof LauncherTransitionable) {
2737 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002738 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002739 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002740 });
2741
Chet Haaseb1254a62010-09-07 13:35:00 -07002742 AnimatorSet toolbarHideAnim = new AnimatorSet();
2743 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002744 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2745
2746 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002747 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002748
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002749 if (mStateAnimation != null) mStateAnimation.cancel();
2750 mStateAnimation = new AnimatorSet();
2751 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2752 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002753
2754 // Show the new toolbar buttons just as the main animation is ending
2755 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002756 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2757 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002758 } else {
2759 toView.setTranslationX(0.0f);
2760 toView.setTranslationY(0.0f);
2761 toView.setScaleX(1.0f);
2762 toView.setScaleY(1.0f);
2763 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002764 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002765 if (toView instanceof LauncherTransitionable) {
2766 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2767 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2768 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002769 hideAndShowToolbarButtons(toState, null, null);
2770 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002771 }
2772
2773 /**
2774 * Zoom the camera back into the workspace, hiding 'fromView'.
2775 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002776 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2777 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002778 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08002779 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002780 Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002781 final boolean fromAllApps = (fromState == State.ALL_APPS)
2782 || (fromState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002783
2784 int duration = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002785 res.getInteger(R.integer.config_appsCustomizeZoomOutTime) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002786 res.getInteger(R.integer.config_customizeZoomOutTime);
2787
Michael Jurka742574b2011-02-02 23:51:01 -08002788 final float scaleFactor = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002789 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002790 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2791
Winson Chung785d2eb2011-04-14 16:08:02 -07002792 View tmpView;
2793 if (fromAllApps) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002794 tmpView = (LauncherApplication.isScreenLarge())
Winson Chung785d2eb2011-04-14 16:08:02 -07002795 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2796 } else {
2797 tmpView = mHomeCustomizationDrawer;
2798 }
2799 final View fromView = tmpView;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002800
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002801 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002802 mCustomizePagedView.endChoiceMode();
2803 mAllAppsPagedView.endChoiceMode();
2804 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002805
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002806 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002807
Michael Jurkad3ef3062010-11-23 16:23:58 -08002808 if (!springLoaded) {
2809 mWorkspace.unshrink(animated);
2810 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002811 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002812 if (mStateAnimation != null) mStateAnimation.cancel();
2813 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002814
Michael Jurka742574b2011-02-02 23:51:01 -08002815 final float oldScaleX = fromView.getScaleX();
2816 final float oldScaleY = fromView.getScaleY();
2817
2818 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2819 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002820 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2821 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002822 ((View)fromView.getParent()).fastInvalidate();
2823 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2824 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2825 }
2826 });
Michael Jurkaabded662011-03-04 12:06:57 -08002827 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002828 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002829 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002830 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2831 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002832 // don't need to invalidate because we do so above
2833 fromView.setFastAlpha(a * 1f + b * 0f);
2834 }
2835 });
Michael Jurkaabded662011-03-04 12:06:57 -08002836 if (fromView instanceof LauncherTransitionable) {
2837 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002838 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002839 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002840 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002841 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002842 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002843 if (fromView instanceof LauncherTransitionable) {
2844 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002845 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002846 }
2847 });
2848
Chet Haaseb1254a62010-09-07 13:35:00 -07002849 AnimatorSet toolbarHideAnim = new AnimatorSet();
2850 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002851 if (!springLoaded) {
2852 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2853 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002854
Adam Cohen61033d32010-11-15 18:29:44 -08002855 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002856
2857 // Show the new toolbar buttons at the very end of the whole animation
2858 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2859 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002860 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2861 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002862 } else {
2863 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002864 if (fromView instanceof LauncherTransitionable) {
2865 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2866 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2867 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002868 if (!springLoaded) {
2869 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2870 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002871 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002872 }
2873
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002874 void showWorkspace(boolean animated) {
2875 showWorkspace(animated, null);
2876 }
2877
2878 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002879 if (layout != null) {
2880 // always animated, but that's ok since we never specify a layout and
2881 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002882 mWorkspace.unshrink(layout);
2883 } else {
2884 mWorkspace.unshrink(animated);
2885 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002886 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002887 closeAllApps(animated);
2888 } else if (mState == State.CUSTOMIZE) {
2889 hideCustomizationDrawer(animated);
2890 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002891
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002892 // Change the state *after* we've called all the transition code
2893 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002894
Winson Chung5fb63472011-02-02 17:03:37 -08002895 // Resume the auto-advance of widgets
2896 mUserPresent = true;
2897 updateRunning();
2898
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002899 // send an accessibility event to announce the context change
2900 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002901 }
2902
Michael Jurkad3ef3062010-11-23 16:23:58 -08002903 void enterSpringLoadedDragMode(CellLayout layout) {
2904 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07002905 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002906 mState = State.ALL_APPS_SPRING_LOADED;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002907 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002908 cameraZoomIn(State.ALL_APPS, true, true);
2909 } else {
2910 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
2911 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002912 } else if (mState == State.CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002913 mState = State.CUSTOMIZE_SPRING_LOADED;
Winson Chung20f71112011-04-06 14:22:04 -07002914 cameraZoomIn(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002915 }/* else {
2916 // we're already in spring loaded mode; don't do anything
2917 }*/
2918 }
2919
2920 void exitSpringLoadedDragMode() {
2921 if (mState == State.ALL_APPS_SPRING_LOADED) {
2922 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002923 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002924 cameraZoomOut(State.ALL_APPS, true, true);
2925 mState = State.ALL_APPS;
2926 } else {
2927 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2928 mState = State.APPS_CUSTOMIZE;
2929 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002930 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2931 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
Winson Chung20f71112011-04-06 14:22:04 -07002932 cameraZoomOut(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002933 mState = State.CUSTOMIZE;
2934 }/* else {
2935 // we're not in spring loaded mode; don't do anything
2936 }*/
2937 }
2938
Winson Chung785d2eb2011-04-14 16:08:02 -07002939 void showAllApps(boolean animated) {
2940 if (mState != State.WORKSPACE) return;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002941 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002942 cameraZoomOut(State.ALL_APPS, animated, false);
2943 ((View) mAllAppsGrid).requestFocus();
2944
Winson Chung4c98d922011-05-31 16:50:48 -07002945 if (mDeleteZone != null) {
2946 mDeleteZone.setVisibility(View.GONE);
2947 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002948
2949 // Change the state *after* we've called all the transition code
2950 mState = State.ALL_APPS;
2951 } else {
Winson Chung785d2eb2011-04-14 16:08:02 -07002952 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
Winson Chung55b65502011-05-26 12:03:43 -07002953 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002954
2955 // Change the state *after* we've called all the transition code
2956 mState = State.APPS_CUSTOMIZE;
2957 }
2958
2959 // Pause the auto-advance of widgets until we are out of AllApps
2960 mUserPresent = false;
2961 updateRunning();
2962
2963 // Send an accessibility event to announce the context change
2964 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2965 }
2966
Joe Onoratob2061212009-11-24 16:13:54 -05002967 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002968 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002969 * - Home from workspace
2970 * - from center screen
2971 * - from other screens
2972 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002973 * - from center screen
2974 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002975 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002976 * - from center screen
2977 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002978 * - Launch app from workspace and quit
2979 * - with back
2980 * - with home
2981 * - Launch app from all apps and quit
2982 * - with back
2983 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002984 * - Go to a screen that's not the default, then all
2985 * apps, and launch and app, and go back
2986 * - with back
2987 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002988 * - On workspace, long press power and go back
2989 * - with back
2990 * - with home
2991 * - On all apps, long press power and go back
2992 * - with back
2993 * - with home
2994 * - On workspace, power off
2995 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002996 * - Launch an app and turn off the screen while in that app
2997 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002998 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002999 * - From all apps
3000 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003001 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3002 * - From all apps
3003 * - From the center workspace
3004 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003005 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003006 void closeAllApps(boolean animated) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07003007 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003008 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
3009 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung20f71112011-04-06 14:22:04 -07003010 cameraZoomIn(State.ALL_APPS, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003011
3012 // Set focus to the AllApps button
3013 findViewById(R.id.all_apps_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003014 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003015 } else {
3016 if (mState == State.APPS_CUSTOMIZE || mState == State.ALL_APPS_SPRING_LOADED) {
3017 mWorkspace.setVisibility(View.VISIBLE);
3018 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
3019
3020 // Set focus to the AllApps button
3021 findViewById(R.id.all_apps_button).requestFocus();
3022 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003023 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003024 }
3025
Joe Onorato7c312c12009-08-13 21:36:53 -07003026 void lockAllApps() {
3027 // TODO
3028 }
3029
3030 void unlockAllApps() {
3031 // TODO
3032 }
3033
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003034 // Show the customization drawer (only exists in x-large configuration)
3035 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003036 if (mState != State.WORKSPACE) {
3037 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003038 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003039
Winson Chung20f71112011-04-06 14:22:04 -07003040 cameraZoomOut(State.CUSTOMIZE, animated, false);
Michael Jurkaafca9532011-02-16 14:50:35 -08003041
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003042 // Change the state *after* we've called all the transition code
3043 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003044
3045 // Pause the auto-advance of widgets until we are out of Customization drawer
3046 mUserPresent = false;
3047 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003048 }
3049
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003050 // Hide the customization drawer (only exists in x-large configuration)
3051 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003052 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Winson Chung20f71112011-04-06 14:22:04 -07003053 cameraZoomIn(State.CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003054
3055 // Set focus to the customize button
3056 findViewById(R.id.configure_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003057 }
3058 }
3059
Patrick Dubroy5f445422011-02-18 14:35:21 -08003060 /**
3061 * Add an item from all apps or customize onto the given workspace screen.
3062 * If layout is null, add to the current screen.
3063 */
3064 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003065 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3066 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003067 } else {
3068 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003069 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003070 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003071
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003072 void onWorkspaceClick(CellLayout layout) {
3073 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003074 }
3075
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003076 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003077 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003078 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003079 // Look for the toolbar icon specified in the activity meta-data
3080 Bundle metaData = packageManager.getActivityInfo(
3081 activityName, PackageManager.GET_META_DATA).metaData;
3082 if (metaData != null) {
3083 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3084 if (iconResId != 0) {
3085 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003086 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003087 }
3088 }
3089 } catch (NameNotFoundException e) {
3090 // Do nothing
3091 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003092 return null;
3093 }
3094
3095 // if successful in getting icon, return it; otherwise, set button to use default drawable
3096 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3097 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3098 TextView button = (TextView) findViewById(buttonId);
3099 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3100
3101 // If we were unable to find the icon via the meta-data, use a generic one
3102 if (toolbarIcon == null) {
3103 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3104 return null;
3105 } else {
3106 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3107 return toolbarIcon.getConstantState();
3108 }
3109 }
3110
3111 // if successful in getting icon, return it; otherwise, set button to use default drawable
3112 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3113 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3114 ImageView button = (ImageView) findViewById(buttonId);
3115 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3116
Michael Jurka0423dcf2010-10-05 14:56:18 -07003117 // If we were unable to find the icon via the meta-data, use a generic one
3118 if (toolbarIcon == null) {
3119 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003120 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003121 } else {
3122 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003123 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003124 }
3125 }
3126
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003127 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3128 TextView button = (TextView) findViewById(buttonId);
3129 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3130 }
3131
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003132 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003133 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003134 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003135 }
3136
Michael Jurka0423dcf2010-10-05 14:56:18 -07003137 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07003138 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
3139 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07003140
Winson Chung1cad91e2011-05-25 17:41:01 -07003141 final SearchManager searchManager =
3142 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3143 ComponentName activityName = searchManager.getGlobalSearchActivity();
3144 if (activityName != null) {
3145 // In landscape mode on the Phone UI, we only have enough space to show the magnifying
3146 // glass icon
3147 boolean iconLoaded = false;
3148 if (!LauncherApplication.isScreenLarge()) {
3149 // TODO-APPS_CUSTOMIZE: Remove when the QSB fixes itself?
3150 int orientation = getResources().getConfiguration().orientation;
3151 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
3152 searchButton.setImageResource(R.drawable.ic_generic_search);
3153 iconLoaded = true;
3154 sGlobalSearchIcon = null;
3155 }
3156 }
3157 if (!iconLoaded) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003158 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003159 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003160 }
Winson Chung1cad91e2011-05-25 17:41:01 -07003161 searchButton.setVisibility(View.VISIBLE);
3162 searchDivider.setVisibility(View.VISIBLE);
3163 } else {
3164 searchButton.setVisibility(View.GONE);
3165 searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003166 }
3167 }
3168
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003169 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003170 updateButtonWithDrawable(R.id.search_button, d);
3171 }
3172
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003173 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07003174 final View searchDivider = findViewById(R.id.search_divider);
3175 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003176
Winson Chung1cad91e2011-05-25 17:41:01 -07003177 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3178 ComponentName activityName = intent.resolveActivity(getPackageManager());
3179 if (activityName != null) {
3180 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
3181 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3182 searchDivider.setVisibility(View.VISIBLE);
3183 voiceButton.setVisibility(View.VISIBLE);
3184 } else {
3185 searchDivider.setVisibility(View.GONE);
3186 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003187 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003188 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003189
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003190 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003191 updateButtonWithDrawable(R.id.voice_button, d);
3192 }
3193
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003194 /**
3195 * Sets the app market icon (shown when all apps is visible on x-large screens)
3196 */
3197 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003198 final View marketButton = findViewById(R.id.market_button);
3199 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3200 // Find the app market activity by resolving an intent.
3201 // (If multiple app markets are installed, it will return the ResolverActivity.)
3202 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003203 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003204 mAppMarketIntent = intent;
3205 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
3206 R.id.market_button, activityName, R.drawable.app_market_generic);
3207 marketButton.setVisibility(View.VISIBLE);
3208 } else {
3209 // We should hide and disable the view so that we don't try and restore the visibility
3210 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3211 marketButton.setVisibility(View.GONE);
3212 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003213 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003214 }
3215
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003216 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003217 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003218 }
3219
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003220 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003221 * Displays the shortcut creation dialog and launches, if necessary, the
3222 * appropriate activity.
3223 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003224 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003225 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3226 DialogInterface.OnShowListener {
3227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003228 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003229
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003230 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003231 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003232
Winson Chung55b65502011-05-26 12:03:43 -07003233 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
3234 AlertDialog.THEME_HOLO_DARK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003235 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003236 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003238 AlertDialog dialog = builder.create();
3239 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003240 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003241 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003243 return dialog;
3244 }
3245
3246 public void onCancel(DialogInterface dialog) {
3247 mWaitingForResult = false;
3248 cleanup();
3249 }
3250
Romain Guycbb89e42009-06-08 15:52:54 -07003251 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07003252 mWaitingForResult = false;
3253 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003254 }
3255
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003256 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003257 try {
3258 dismissDialog(DIALOG_CREATE_SHORTCUT);
3259 } catch (Exception e) {
3260 // An exception is thrown if the dialog is not visible, which is fine
3261 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003262 }
3263
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003264 /**
3265 * Handle the action clicked in the "Add to home" dialog.
3266 */
3267 public void onClick(DialogInterface dialog, int which) {
3268 Resources res = getResources();
3269 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003270
Winson Chung55b65502011-05-26 12:03:43 -07003271 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
3272 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003273 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003274 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003275 break;
3276 }
Winson Chung55b65502011-05-26 12:03:43 -07003277 case AddAdapter.ITEM_APPLICATION: {
3278 if (mAppsCustomizeTabHost != null) {
3279 mAppsCustomizeTabHost.selectAppsTab();
3280 }
3281 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003282 break;
3283 }
Winson Chung55b65502011-05-26 12:03:43 -07003284 case AddAdapter.ITEM_APPWIDGET: {
3285 if (mAppsCustomizeTabHost != null) {
3286 mAppsCustomizeTabHost.selectWidgetsTab();
3287 }
3288 showAllApps(true);
3289 break;
3290 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003291 case AddAdapter.ITEM_WALLPAPER: {
3292 startWallpaper();
3293 break;
3294 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003295 }
3296 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003297
3298 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003299 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003300 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003301 }
3302
3303 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003304 * Receives notifications when applications are added/removed.
3305 */
3306 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3307 @Override
3308 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003309 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003310 String reason = intent.getStringExtra("reason");
3311 if (!"homekey".equals(reason)) {
3312 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003313 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003314 animate = false;
3315 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003316 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003317 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003318 }
3319 }
3320
3321 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003322 * Receives notifications whenever the appwidgets are reset.
3323 */
3324 private class AppWidgetResetObserver extends ContentObserver {
3325 public AppWidgetResetObserver() {
3326 super(new Handler());
3327 }
3328
3329 @Override
3330 public void onChange(boolean selfChange) {
3331 onAppWidgetReset();
3332 }
3333 }
3334
3335 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003336 * If the activity is currently paused, signal that we need to re-run the loader
3337 * in onResume.
3338 *
3339 * This needs to be called from incoming places where resources might have been loaded
3340 * while we are paused. That is becaues the Configuration might be wrong
3341 * when we're not running, and if it comes back to what it was when we
3342 * were paused, we are not restarted.
3343 *
3344 * Implementation of the method from LauncherModel.Callbacks.
3345 *
3346 * @return true if we are currently paused. The caller might be able to
3347 * skip some work in that case since we will come back again.
3348 */
3349 public boolean setLoadOnResume() {
3350 if (mPaused) {
3351 Log.i(TAG, "setLoadOnResume");
3352 mOnResumeNeedsLoad = true;
3353 return true;
3354 } else {
3355 return false;
3356 }
3357 }
3358
3359 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003360 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003361 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003362 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003363 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003364 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003365 } else {
3366 return SCREEN_COUNT / 2;
3367 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003368 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003369
Winson Chunga12a2502010-12-20 14:41:35 -08003370 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003371 mAllAppsPagedView = view;
3372 }
3373
Joe Onorato9c1289c2009-08-17 11:03:03 -04003374 /**
3375 * Refreshes the shortcuts shown on the workspace.
3376 *
3377 * Implementation of the method from LauncherModel.Callbacks.
3378 */
3379 public void startBinding() {
3380 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003381
3382 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003383 int count = workspace.getChildCount();
3384 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003385 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003386 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3387 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003388 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003389
Joe Onorato9c1289c2009-08-17 11:03:03 -04003390 if (DEBUG_USER_INTERFACE) {
3391 android.widget.Button finishButton = new android.widget.Button(this);
3392 finishButton.setText("Finish");
3393 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3394
3395 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3396 public void onClick(View v) {
3397 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003398 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003399 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003400 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003401
3402 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3403 // mDesktopItems -> AppWidgetInfo -> hostView).
3404 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003405 }
3406
3407 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003408 * Bind the items start-end from the list.
3409 *
3410 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003411 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003412 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3413
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003414 setLoadOnResume();
3415
Joe Onorato9c1289c2009-08-17 11:03:03 -04003416 final Workspace workspace = mWorkspace;
3417
3418 for (int i=start; i<end; i++) {
3419 final ItemInfo item = shortcuts.get(i);
3420 mDesktopItems.add(item);
3421 switch (item.itemType) {
3422 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3423 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003424 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003425 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3426 false);
3427 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003428 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003429 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003430 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003431 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003432 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3433 false);
3434 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003435 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003436 }
3437
Joe Onorato9c1289c2009-08-17 11:03:03 -04003438 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003439 }
3440
Joe Onorato9c1289c2009-08-17 11:03:03 -04003441 /**
3442 * Implementation of the method from LauncherModel.Callbacks.
3443 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003444 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003445 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003446 sFolders.clear();
3447 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003448 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003449
3450 /**
3451 * Add the views for a widget to the workspace.
3452 *
3453 * Implementation of the method from LauncherModel.Callbacks.
3454 */
3455 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003456 setLoadOnResume();
3457
Daniel Sandler843e8602010-06-07 14:59:01 -04003458 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3459 if (DEBUG_WIDGETS) {
3460 Log.d(TAG, "bindAppWidget: " + item);
3461 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003462 final Workspace workspace = mWorkspace;
3463
3464 final int appWidgetId = item.appWidgetId;
3465 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003466 if (DEBUG_WIDGETS) {
3467 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3468 }
3469
Joe Onorato9c1289c2009-08-17 11:03:03 -04003470 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3471
Joe Onorato9c1289c2009-08-17 11:03:03 -04003472 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3473 item.hostView.setTag(item);
3474
3475 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3476 item.cellY, item.spanX, item.spanY, false);
3477
Adam Cohended9f8d2010-11-03 13:25:16 -07003478 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3479
Joe Onorato9c1289c2009-08-17 11:03:03 -04003480 workspace.requestLayout();
3481
3482 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003483
3484 if (DEBUG_WIDGETS) {
3485 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3486 + (SystemClock.uptimeMillis()-start) + "ms");
3487 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003488 }
3489
3490 /**
3491 * Callback saying that there aren't any more items to bind.
3492 *
3493 * Implementation of the method from LauncherModel.Callbacks.
3494 */
3495 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003496 setLoadOnResume();
3497
Joe Onorato9c1289c2009-08-17 11:03:03 -04003498 if (mSavedState != null) {
3499 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003500 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003501 }
3502
Adam Cohendf2cc412011-04-27 16:56:57 -07003503 final long[] folders = mSavedState.getLongArray(RUNTIME_STATE_FOLDERS);
3504 if (folders != null) {
3505 for (long folderId : folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003506 final FolderInfo info = sFolders.get(folderId);
Adam Cohena9cf38f2011-05-02 15:36:58 -07003507 final FolderIcon folderIcon = (FolderIcon)
3508 mWorkspace.getViewForTag(info);
3509 if (folderIcon != null) {
3510 openFolder(folderIcon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003511 }
3512 }
3513 final Folder openFolder = mWorkspace.getOpenFolder();
3514 if (openFolder != null) {
3515 openFolder.requestFocus();
3516 }
3517 }
3518
Joe Onorato9c1289c2009-08-17 11:03:03 -04003519 mSavedState = null;
3520 }
3521
3522 if (mSavedInstanceState != null) {
3523 super.onRestoreInstanceState(mSavedInstanceState);
3524 mSavedInstanceState = null;
3525 }
3526
Winson Chung32bb5e52011-01-31 14:51:56 -08003527 // Workaround a bug that occurs when rotating the device while the customization mode is
3528 // open, we trigger a new layout on all the CellLayout children.
Michael Jurkaa2eb1702011-05-12 14:57:05 -07003529 if (LauncherApplication.isScreenLarge() && (mState == State.CUSTOMIZE)) {
Winson Chung32bb5e52011-01-31 14:51:56 -08003530 final int childCount = mWorkspace.getChildCount();
3531 for (int i = 0; i < childCount; ++i) {
3532 mWorkspace.getChildAt(i).requestLayout();
3533 }
3534 }
3535
Joe Onorato9c1289c2009-08-17 11:03:03 -04003536 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003537
3538 // If we received the result of any pending adds while the loader was running (e.g. the
3539 // widget configuration forced an orientation change), process them now.
3540 for (int i = 0; i < sPendingAddList.size(); i++) {
3541 completeAdd(sPendingAddList.get(i));
3542 }
3543 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003544 }
3545
3546 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003547 * Updates the icons on the launcher that are affected by changes to the package list
3548 * on the device.
3549 */
3550 private void updateIconsAffectedByPackageManagerChanges() {
3551 updateAppMarketIcon();
3552 updateGlobalSearchIcon();
3553 updateVoiceSearchIcon();
3554 }
3555
3556 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003557 * Add the icons for all apps.
3558 *
3559 * Implementation of the method from LauncherModel.Callbacks.
3560 */
3561 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003562 if (mAllAppsGrid != null) {
3563 mAllAppsGrid.setApps(apps);
3564 }
3565 if (mAppsCustomizeContent != null) {
3566 mAppsCustomizeContent.setApps(apps);
3567 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003568 if (mCustomizePagedView != null) {
3569 mCustomizePagedView.setApps(apps);
3570 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003571 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003572 }
3573
3574 /**
3575 * A package was installed.
3576 *
3577 * Implementation of the method from LauncherModel.Callbacks.
3578 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003579 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003580 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003581 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chung785d2eb2011-04-14 16:08:02 -07003582 if (mAllAppsGrid != null) {
3583 mAllAppsGrid.addApps(apps);
3584 }
3585 if (mAppsCustomizeContent != null) {
3586 mAppsCustomizeContent.addApps(apps);
3587 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003588 if (mCustomizePagedView != null) {
3589 mCustomizePagedView.addApps(apps);
3590 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003591 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003592 }
3593
3594 /**
3595 * A package was updated.
3596 *
3597 * Implementation of the method from LauncherModel.Callbacks.
3598 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003599 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003600 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003601 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003602 if (mWorkspace != null) {
3603 mWorkspace.updateShortcuts(apps);
3604 }
3605 if (mAllAppsGrid != null) {
3606 mAllAppsGrid.updateApps(apps);
3607 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003608 if (mAppsCustomizeContent != null) {
3609 mAppsCustomizeContent.updateApps(apps);
3610 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003611 if (mCustomizePagedView != null) {
3612 mCustomizePagedView.updateApps(apps);
3613 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003614 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003615 }
3616
3617 /**
3618 * A package was uninstalled.
3619 *
3620 * Implementation of the method from LauncherModel.Callbacks.
3621 */
Joe Onorato36115782010-06-17 13:28:48 -04003622 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003623 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003624 if (permanent) {
3625 mWorkspace.removeItems(apps);
3626 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003627 if (mAllAppsGrid != null) {
3628 mAllAppsGrid.removeApps(apps);
3629 }
3630 if (mAppsCustomizeContent != null) {
3631 mAppsCustomizeContent.removeApps(apps);
3632 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003633 if (mCustomizePagedView != null) {
3634 mCustomizePagedView.removeApps(apps);
3635 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003636 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003637 }
Joe Onoratobe386092009-11-17 17:32:16 -08003638
3639 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003640 * A number of packages were updated.
3641 */
3642 public void bindPackagesUpdated() {
3643 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003644 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003645 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003646 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003647 if (mAppsCustomizeContent != null) {
3648 mAppsCustomizeContent.onPackagesUpdated();
3649 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003650 }
3651
Winson Chung400438b2011-01-16 17:53:48 -08003652 private int mapConfigurationOriActivityInfoOri(int configOri) {
3653 final Display d = getWindowManager().getDefaultDisplay();
3654 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3655 switch (d.getRotation()) {
3656 case Surface.ROTATION_0:
3657 case Surface.ROTATION_180:
3658 // We are currently in the same basic orientation as the natural orientation
3659 naturalOri = configOri;
3660 break;
3661 case Surface.ROTATION_90:
3662 case Surface.ROTATION_270:
3663 // We are currently in the other basic orientation to the natural orientation
3664 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3665 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3666 break;
3667 }
3668
3669 int[] oriMap = {
3670 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3671 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3672 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3673 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3674 };
3675 // Since the map starts at portrait, we need to offset if this device's natural orientation
3676 // is landscape.
3677 int indexOffset = 0;
3678 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3679 indexOffset = 1;
3680 }
3681 return oriMap[(d.getRotation() + indexOffset) % 4];
3682 }
3683 public void lockScreenOrientation() {
3684 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3685 .getConfiguration().orientation));
3686 }
3687 public void unlockScreenOrientation() {
3688 mHandler.postDelayed(new Runnable() {
3689 public void run() {
3690 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3691 }
3692 }, mRestoreScreenOrientationDelay);
3693 }
3694
Winson Chung80baf5a2010-08-09 16:03:15 -07003695 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003696 * Prints out out state for debugging.
3697 */
3698 public void dumpState() {
3699 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003700 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003701 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3702 Log.d(TAG, "mRestoring=" + mRestoring);
3703 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3704 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3705 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003706 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003707 mModel.dumpState();
Winson Chung785d2eb2011-04-14 16:08:02 -07003708 if (mAllAppsGrid != null) {
3709 mAllAppsGrid.dumpState();
3710 }
3711 if (mAppsCustomizeContent != null) {
3712 mAppsCustomizeContent.dumpState();
3713 }
Joe Onoratobe386092009-11-17 17:32:16 -08003714 Log.d(TAG, "END launcher2 dump state");
3715 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003716}
Michael Jurka2763be32011-02-24 11:19:57 -08003717
Michael Jurkaabded662011-03-04 12:06:57 -08003718interface LauncherTransitionable {
3719 void onLauncherTransitionStart(Animator animation);
3720 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003721}