blob: eb98b4d12ba0b947421b8fb9dad839acde466f27 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Adam Cohendf2cc412011-04-27 16:56:57 -070020import java.io.DataInputStream;
21import java.io.DataOutputStream;
22import java.io.FileNotFoundException;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.List;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070027
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080029import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070031import android.animation.ObjectAnimator;
32import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070033import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080034import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070035import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.app.Dialog;
38import android.app.SearchManager;
39import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070040import android.appwidget.AppWidgetManager;
41import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080043import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070044import android.content.ClipData;
45import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040046import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080047import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.Context;
49import android.content.DialogInterface;
50import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080051import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080052import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070053import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080055import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080056import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070058import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040059import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080062import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070063import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080064import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070065import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040066import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070067import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020069import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080070import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070071import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040072import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080073import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070074import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080075import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.text.Selection;
77import android.text.SpannableStringBuilder;
78import android.text.TextUtils;
79import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070080import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080081import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080082import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.KeyEvent;
84import android.view.LayoutInflater;
85import android.view.Menu;
86import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070087import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080088import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.View;
90import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070091import android.view.WindowManager;
Michael Jurkae0f5a612011-02-07 16:45:41 -080092import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080093import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080094import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080095import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070097import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070099import android.widget.ImageView;
100import android.widget.LinearLayout;
101import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700102import android.widget.TabHost;
Winson Chung68846fd2010-10-29 11:00:27 -0700103import android.widget.TextView;
104import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700105
Adam Cohendf2cc412011-04-27 16:56:57 -0700106import com.android.common.Search;
107import com.android.launcher.R;
108import com.android.launcher2.Workspace.ShrinkState;
Romain Guyedcce092010-03-04 13:03:17 -0800109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110/**
111 * Default launcher application.
112 */
Michael Jurka946ad472010-07-09 18:05:18 -0700113public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700114 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
115 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800116 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700117 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400120 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400121 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800124 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700127 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
128 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
130 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700131 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_PICK_APPLICATION = 6;
136 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700138 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
140 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
141
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800142 static final int SCREEN_COUNT = 5;
143 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Joe Onorato7c312c12009-08-13 21:36:53 -0700145 static final int DIALOG_CREATE_SHORTCUT = 1;
146 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Romain Guy98d01652009-06-30 16:21:04 -0700148 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
150 // Type: int
151 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700152 // Type: int
153 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 // Type: long
Adam Cohendf2cc412011-04-27 16:56:57 -0700155 private static final String RUNTIME_STATE_FOLDERS = "launcher.folder";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: int
157 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
158 // Type: int
159 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
160 // Type: int
161 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: boolean
163 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
164 // Type: long
165 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
166
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700167 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
168
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700169 /** The different states that Launcher can be in. */
Winson Chung785d2eb2011-04-14 16:08:02 -0700170 private enum State { WORKSPACE, APPS_CUSTOMIZE, ALL_APPS, CUSTOMIZE,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800171 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700172 private State mState = State.WORKSPACE;
173 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700174
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800178 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800180 private final BroadcastReceiver mCloseSystemDialogsReceiver
181 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800182 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private LayoutInflater mInflater;
185
Joe Onorato00acb122009-08-04 16:04:30 -0400186 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700188
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700189 private AppWidgetManager mAppWidgetManager;
190 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700191
Michael Jurka0280c3b2010-09-17 15:00:07 -0700192 private int mAddScreen = -1;
193 private int mAddIntersectCellX = -1;
194 private int mAddIntersectCellY = -1;
195 private int[] mAddDropPosition;
196 private int[] mTmpAddItemCellCoordinates = new int[2];
197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private FolderInfo mFolderInfo;
199
Joe Onorato7c312c12009-08-13 21:36:53 -0700200 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700201 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700202 private AllAppsView mAllAppsGrid;
Winson Chung785d2eb2011-04-14 16:08:02 -0700203 private AppsCustomizeTabHost mAppsCustomizeTabHost;
204 private AppsCustomizePagedView mAppsCustomizeContent;
Michael Jurka7ef959b2011-02-23 11:48:32 -0800205 private CustomizeTrayTabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700206 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700207
Winson Chung97d85d22011-04-13 11:27:36 -0700208 private ViewGroup mButtonCluster;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800209 private View mAllAppsButton;
210 private View mDivider;
211 private View mConfigureButton;
212
Winson Chunga12a2502010-12-20 14:41:35 -0800213 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700214 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private Bundle mSavedState;
217
218 private SpannableStringBuilder mDefaultKeySsb = null;
219
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220 private boolean mWorkspaceLoading = true;
221
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800222 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private boolean mRestoring;
224 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700225 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226
227 private Bundle mSavedInstanceState;
228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800230 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800231 private boolean mUserPresent = true;
232 private boolean mVisible = false;
233 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700235 private static LocaleConfiguration sLocaleConfiguration = null;
236
Romain Guy84f296c2009-11-04 15:00:44 -0800237 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700238
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700239 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700240
Patrick Dubroy94f78a52011-02-28 17:39:16 -0800241 // The "signpost" images along the bottom of the screen (only in some layouts)
Romain Guy1fbc1c82009-11-09 20:43:08 -0800242 private ImageView mPreviousView;
243 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500244
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400245 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400246 private String[] mHotseatConfig = null;
247 private Intent[] mHotseats = null;
248 private Drawable[] mHotseatIcons = null;
249 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400250
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700251 private Intent mAppMarketIntent = null;
252
Adam Cohended9f8d2010-11-03 13:25:16 -0700253 // Related to the auto-advancing of widgets
254 private final int ADVANCE_MSG = 1;
255 private final int mAdvanceInterval = 20000;
256 private final int mAdvanceStagger = 250;
257 private long mAutoAdvanceSentTime;
258 private long mAutoAdvanceTimeLeft = -1;
259 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
260 new HashMap<View, AppWidgetProviderInfo>();
261
Winson Chung400438b2011-01-16 17:53:48 -0800262 // Determines how long to wait after a rotation before restoring the screen orientation to
263 // match the sensor state.
264 private final int mRestoreScreenOrientationDelay = 500;
265
Michael Jurka4ef207b2010-11-29 17:05:45 -0800266 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800267 private static Drawable.ConstantState sGlobalSearchIcon;
268 private static Drawable.ConstantState sVoiceSearchIcon;
269 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800270
Adam Cohen2801caf2011-05-13 20:57:39 -0700271 private DragLayer mDragLayer;
272
Michael Jurkaddd62e92011-02-16 17:49:14 -0800273 private BubbleTextView mWaitingForResume;
274
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800275 private static ArrayList<PendingAddArguments> sPendingAddList
276 = new ArrayList<PendingAddArguments>();
277
278 private static class PendingAddArguments {
279 int requestCode;
280 Intent intent;
281 int screen;
282 int cellX;
283 int cellY;
284 }
285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 @Override
287 protected void onCreate(Bundle savedInstanceState) {
288 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700289
Winson Chungaafa03c2010-06-11 17:34:16 -0700290 if (LauncherApplication.isInPlaceRotationEnabled()) {
291 // hide the status bar (temporary until we get the status bar design figured out)
292 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
293 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
294 }
295
Joe Onorato0589f0f2010-02-08 13:44:00 -0800296 LauncherApplication app = ((LauncherApplication)getApplication());
297 mModel = app.setLauncher(this);
298 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400299 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700301
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700302 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700303 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
304 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700305
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800306 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200307 android.os.Debug.startMethodTracing(
308 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 }
310
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400311 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800313 setContentView(R.layout.launcher);
Michael Jurka7ef959b2011-02-23 11:48:32 -0800314 mHomeCustomizationDrawer = (CustomizeTrayTabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700315 if (mHomeCustomizationDrawer != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700316 // share the same customization workspace across all the tabs
Michael Jurka7ef959b2011-02-23 11:48:32 -0800317 mCustomizePagedView = (CustomizePagedView) findViewById(
318 R.id.customization_drawer_tab_contents);
Michael Jurka946ad472010-07-09 18:05:18 -0700319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 setupViews();
321
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800322 registerContentObservers();
323
Joe Onorato7c312c12009-08-13 21:36:53 -0700324 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700325
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 mSavedState = savedInstanceState;
327 restoreState(mSavedState);
328
Winson Chunga12a2502010-12-20 14:41:35 -0800329 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800330 if (mCustomizePagedView != null) {
331 mCustomizePagedView.update();
332 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700333 if (mAppsCustomizeContent != null) {
334 mAppsCustomizeContent.onPackagesUpdated();
335 }
Winson Chunga12a2502010-12-20 14:41:35 -0800336
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 if (PROFILE_STARTUP) {
338 android.os.Debug.stopMethodTracing();
339 }
340
341 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400342 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 }
344
345 // For handling default keys
346 mDefaultKeySsb = new SpannableStringBuilder();
347 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800348
349 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
350 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800351
352 // If we have a saved version of these external icons, we load them up immediately
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700353 if (LauncherApplication.isScreenLarge()) {
Winson Chungc6d20002011-03-02 11:57:29 -0800354 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
355 updateIconsAffectedByPackageManagerChanges();
Michael Jurka4ef207b2010-11-29 17:05:45 -0800356 }
Michael Jurka1c3f8ec2011-03-07 16:41:21 -0800357 if (sGlobalSearchIcon != null) {
358 updateGlobalSearchIcon(sGlobalSearchIcon);
359 }
360 if (sVoiceSearchIcon != null) {
361 updateVoiceSearchIcon(sVoiceSearchIcon);
362 }
363 if (sAppMarketIcon != null) {
364 updateAppMarketIcon(sAppMarketIcon);
365 }
Michael Jurka4ef207b2010-11-29 17:05:45 -0800366 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 }
Romain Guycbb89e42009-06-08 15:52:54 -0700368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700370 if (sLocaleConfiguration == null) {
371 new AsyncTask<Void, Void, LocaleConfiguration>() {
372 @Override
373 protected LocaleConfiguration doInBackground(Void... unused) {
374 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
375 readConfiguration(Launcher.this, localeConfiguration);
376 return localeConfiguration;
377 }
378
379 @Override
380 protected void onPostExecute(LocaleConfiguration result) {
381 sLocaleConfiguration = result;
382 checkForLocaleChange(); // recursive, but now with a locale configuration
383 }
384 }.execute();
385 return;
386 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700387
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 final Configuration configuration = getResources().getConfiguration();
389
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700390 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 final String locale = configuration.locale.toString();
392
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700393 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 final int mcc = configuration.mcc;
395
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700396 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 final int mnc = configuration.mnc;
398
Romain Guy84f296c2009-11-04 15:00:44 -0800399 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400
Romain Guy84f296c2009-11-04 15:00:44 -0800401 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700402 sLocaleConfiguration.locale = locale;
403 sLocaleConfiguration.mcc = mcc;
404 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700405
Joe Onorato0589f0f2010-02-08 13:44:00 -0800406 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400407 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700408
409 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
410 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700411 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700412 public void run() {
413 writeConfiguration(Launcher.this, localeConfiguration);
414 }
415 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700416 }
417 }
418
419 private static class LocaleConfiguration {
420 public String locale;
421 public int mcc = -1;
422 public int mnc = -1;
423 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700424
Romain Guy98d01652009-06-30 16:21:04 -0700425 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
426 DataInputStream in = null;
427 try {
428 in = new DataInputStream(context.openFileInput(PREFERENCES));
429 configuration.locale = in.readUTF();
430 configuration.mcc = in.readInt();
431 configuration.mnc = in.readInt();
432 } catch (FileNotFoundException e) {
433 // Ignore
434 } catch (IOException e) {
435 // Ignore
436 } finally {
437 if (in != null) {
438 try {
439 in.close();
440 } catch (IOException e) {
441 // Ignore
442 }
443 }
444 }
445 }
446
447 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
448 DataOutputStream out = null;
449 try {
450 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
451 out.writeUTF(configuration.locale);
452 out.writeInt(configuration.mcc);
453 out.writeInt(configuration.mnc);
454 out.flush();
455 } catch (FileNotFoundException e) {
456 // Ignore
457 } catch (IOException e) {
458 //noinspection ResultOfMethodCallIgnored
459 context.getFileStreamPath(PREFERENCES).delete();
460 } finally {
461 if (out != null) {
462 try {
463 out.close();
464 } catch (IOException e) {
465 // Ignore
466 }
467 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468 }
469 }
470
471 static int getScreen() {
472 synchronized (sLock) {
473 return sScreen;
474 }
475 }
476
477 static void setScreen(int screen) {
478 synchronized (sLock) {
479 sScreen = screen;
480 }
481 }
482
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400483 // Note: This doesn't do all the client-id magic that BrowserProvider does
484 // in Browser. (http://b/2425179)
485 private Uri getDefaultBrowserUri() {
486 String url = getString(R.string.default_browser_url);
487 if (url.indexOf("{CID}") != -1) {
488 url = url.replace("{CID}", "android-google");
489 }
490 return Uri.parse(url);
491 }
492
493 // Load the Intent templates from arrays.xml to populate the hotseats. For
494 // each Intent, if it resolves to a single app, use that as the launch
495 // intent & use that app's label as the contentDescription. Otherwise,
496 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400497 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400498 if (mHotseatConfig == null) {
499 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
500 if (mHotseatConfig.length > 0) {
501 mHotseats = new Intent[mHotseatConfig.length];
502 mHotseatLabels = new CharSequence[mHotseatConfig.length];
503 mHotseatIcons = new Drawable[mHotseatConfig.length];
504 } else {
505 mHotseats = null;
506 mHotseatIcons = null;
507 mHotseatLabels = null;
508 }
509
510 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
511 for (int i=0; i<mHotseatConfig.length; i++) {
512 // load icon for this slot; currently unrelated to the actual activity
513 try {
514 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
515 } catch (ArrayIndexOutOfBoundsException ex) {
516 Log.w(TAG, "Missing hotseat_icons array item #" + i);
517 mHotseatIcons[i] = null;
518 }
519 }
520 hotseatIconDrawables.recycle();
521 }
522
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400523 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400524 for (int i=0; i<mHotseatConfig.length; i++) {
525 Intent intent = null;
526 if (mHotseatConfig[i].equals("*BROWSER*")) {
527 // magic value meaning "launch user's default web browser"
528 // replace it with a generic web request so we can see if there is indeed a default
529 String defaultUri = getString(R.string.default_browser_url);
530 intent = new Intent(
531 Intent.ACTION_VIEW,
532 ((defaultUri != null)
533 ? Uri.parse(defaultUri)
534 : getDefaultBrowserUri())
535 ).addCategory(Intent.CATEGORY_BROWSABLE);
536 // note: if the user launches this without a default set, she
537 // will always be taken to the default URL above; this is
538 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700539 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400540 // URL is unavoidably sent to the chosen app.
541 } else {
542 try {
543 intent = Intent.parseUri(mHotseatConfig[i], 0);
544 } catch (java.net.URISyntaxException ex) {
545 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
546 // bogus; leave intent=null
547 }
548 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700549
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400550 if (intent == null) {
551 mHotseats[i] = null;
552 mHotseatLabels[i] = getText(R.string.activity_not_found);
553 continue;
554 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400555
556 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700557 Log.d(TAG, "loadHotseats: hotseat " + i
558 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400559 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400560 + "]");
561 }
562
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400563 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
564 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700565 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400566 Log.d(TAG, "Best match for intent: " + bestMatch);
567 Log.d(TAG, "All matches: ");
568 for (ResolveInfo ri : allMatches) {
569 Log.d(TAG, " --> " + ri);
570 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400571 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400572 // did this resolve to a single app, or the resolver?
573 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700574 // can't find any activity to handle this. let's leave the
575 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400576 // to launch.
577 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400578
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400579 // set accessibility text to "Not installed"
580 mHotseatLabels[i] = getText(R.string.activity_not_found);
581 } else {
582 boolean found = false;
583 for (ResolveInfo ri : allMatches) {
584 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
585 && bestMatch.activityInfo.applicationInfo.packageName
586 .equals(ri.activityInfo.applicationInfo.packageName)) {
587 found = true;
588 break;
589 }
590 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700591
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400592 if (!found) {
593 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
594 // the bestMatch is probably the ResolveActivity, meaning the
595 // user has not yet selected a default
596 // so: we'll keep the original intent for now
597 mHotseats[i] = intent;
598
599 // set the accessibility text to "Select shortcut"
600 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
601 } else {
602 // we have an app!
603 // now reconstruct the intent to launch it through the front
604 // door
605 ComponentName com = new ComponentName(
606 bestMatch.activityInfo.applicationInfo.packageName,
607 bestMatch.activityInfo.name);
608 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
609
610 // load the app label for accessibility
611 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
612 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400613 }
614
615 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700616 Log.d(TAG, "loadHotseats: hotseat " + i
617 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400618 + ((mHotseats[i] == null)
619 ? "null"
620 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400621 + "] label=[" + mHotseatLabels[i]
622 + "]"
623 );
624 }
625 }
626 }
627
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800628 private void completeAdd(PendingAddArguments args) {
629 switch (args.requestCode) {
630 case REQUEST_PICK_APPLICATION:
631 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
632 break;
633 case REQUEST_PICK_SHORTCUT:
634 processShortcut(args.intent);
635 break;
636 case REQUEST_CREATE_SHORTCUT:
637 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
638 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800639 case REQUEST_PICK_APPWIDGET:
640 addAppWidgetFromPick(args.intent);
641 break;
642 case REQUEST_CREATE_APPWIDGET:
643 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
644 completeAddAppWidget(appWidgetId, args.screen);
645 break;
646 case REQUEST_PICK_WALLPAPER:
647 // We just wanted the activity result here so we can clear mWaitingForResult
648 break;
649 }
650 }
651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800653 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700654 mWaitingForResult = false;
655
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 // The pattern used here is that a user PICKs a specific application,
657 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700658
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
660 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700661
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800663 final PendingAddArguments args = new PendingAddArguments();
664 args.requestCode = requestCode;
665 args.intent = data;
666 args.screen = mAddScreen;
667 args.cellX = mAddIntersectCellX;
668 args.cellY = mAddIntersectCellY;
669
670 // If the loader is still running, defer the add until it is done.
671 if (isWorkspaceLocked()) {
672 sPendingAddList.add(args);
673 } else {
674 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 }
Romain Guy18042c82009-11-06 11:44:55 -0800676 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
677 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
678 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700679 // Clean up the appWidgetId if we canceled
680 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
681 if (appWidgetId != -1) {
682 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 }
684 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 }
686
687 @Override
688 protected void onResume() {
689 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800690 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700691 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400692 mWorkspaceLoading = true;
693 mModel.startLoader(this, true);
694 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700695 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800697 if (mWaitingForResume != null) {
698 mWaitingForResume.setStayPressed(false);
699 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700700 // When we resume Launcher, a different Activity might be responsible for the app
701 // market intent, so refresh the icon
702 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 }
704
705 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706 protected void onPause() {
707 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700708 // Some launcher layouts don't have a previous and next view
709 if (mPreviousView != null) {
710 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700711 }
712 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700713 dismissPreview(mNextView);
714 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700715 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800716 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700717 }
Romain Guycbb89e42009-06-08 15:52:54 -0700718
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700719 @Override
720 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400721 // Flag the loader to stop early before switching
722 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700723 if (mAllAppsGrid != null) {
724 mAllAppsGrid.surrender();
725 }
726 if (mAppsCustomizeContent != null) {
727 mAppsCustomizeContent.surrender();
728 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800729 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700730 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700731
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800732 // We can't hide the IME if it was forced open. So don't bother
733 /*
734 @Override
735 public void onWindowFocusChanged(boolean hasFocus) {
736 super.onWindowFocusChanged(hasFocus);
737
738 if (hasFocus) {
739 final InputMethodManager inputManager = (InputMethodManager)
740 getSystemService(Context.INPUT_METHOD_SERVICE);
741 WindowManager.LayoutParams lp = getWindow().getAttributes();
742 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
743 android.os.Handler()) {
744 protected void onReceiveResult(int resultCode, Bundle resultData) {
745 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
746 }
747 });
748 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
749 }
750 }
751 */
752
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 private boolean acceptFilter() {
754 final InputMethodManager inputManager = (InputMethodManager)
755 getSystemService(Context.INPUT_METHOD_SERVICE);
756 return !inputManager.isFullscreenMode();
757 }
758
759 @Override
760 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700761 final int uniChar = event.getUnicodeChar();
762 final boolean handled = super.onKeyDown(keyCode, event);
763 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
764 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
766 keyCode, event);
767 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700768 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700769 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700770 // showSearchDialog()
771 // If there are multiple keystrokes before the search dialog takes focus,
772 // onSearchRequested() will be called for every keystroke,
773 // but it is idempotent, so it's fine.
774 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775 }
776 }
777
Joe Onorato8a9625e2010-01-28 15:55:35 -0800778 // Eat the long press event so the keyboard doesn't come up.
779 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
780 return true;
781 }
782
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 return handled;
784 }
785
Karl Rosaen138a0412009-04-23 19:00:21 -0700786 private String getTypedText() {
787 return mDefaultKeySsb.toString();
788 }
789
790 private void clearTypedText() {
791 mDefaultKeySsb.clear();
792 mDefaultKeySsb.clearSpans();
793 Selection.setSelection(mDefaultKeySsb, 0);
794 }
795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700797 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
798 * State
799 */
800 private static State intToState(int stateOrdinal) {
801 State state = State.WORKSPACE;
802 final State[] stateValues = State.values();
803 for (int i = 0; i < stateValues.length; i++) {
804 if (stateValues[i].ordinal() == stateOrdinal) {
805 state = stateValues[i];
806 break;
807 }
808 }
809 return state;
810 }
811
812 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 * Restores the previous state, if it exists.
814 *
815 * @param savedState The previous state.
816 */
817 private void restoreState(Bundle savedState) {
818 if (savedState == null) {
819 return;
820 }
821
Michael Jurka883f55b2010-10-21 15:47:14 -0700822 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
823
Winson Chung785d2eb2011-04-14 16:08:02 -0700824 if (state == State.ALL_APPS || state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800825 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700826 } else if (state == State.CUSTOMIZE) {
827 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800828 }
829
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
831 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700832 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 }
834
835 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700836
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700838 mAddScreen = addScreen;
839 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
840 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 mRestoring = true;
842 }
843
844 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
845 if (renameFolder) {
846 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700847 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 mRestoring = true;
849 }
Winson Chunga12a2502010-12-20 14:41:35 -0800850
851 // Restore the current AllApps drawer tab
852 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
853 String curTab = savedState.getString("allapps_currentTab");
854 if (curTab != null) {
855 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
856 tabhost.setCurrentTabByTag(curTab);
857 }
858 int curPage = savedState.getInt("allapps_currentPage", -1);
859 if (curPage > -1) {
860 mAllAppsPagedView.setRestorePage(curPage);
861 }
862 }
863
864 // Restore the current customization drawer tab
865 if (mHomeCustomizationDrawer != null) {
866 String curTab = savedState.getString("customize_currentTab");
867 if (curTab != null) {
868 // We set this directly so that there is no delay before the tab is set
Michael Jurka7ef959b2011-02-23 11:48:32 -0800869 mCustomizePagedView.setCustomizationFilter(
870 mHomeCustomizationDrawer.getCustomizeFilterForTabTag(curTab));
Winson Chunga12a2502010-12-20 14:41:35 -0800871 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
872 }
873
874 // Note: currently we do not restore the page for the customization tray because unlike
875 // AllApps, the page content can change drastically
876 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700877
878 // Restore the AppsCustomize tab
879 if (mAppsCustomizeTabHost != null) {
880 String curTab = savedState.getString("apps_customize_currentTab");
881 if (curTab != null) {
882 // We set this directly so that there is no delay before the tab is set
883 mAppsCustomizeContent.setContentType(
884 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
885 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
886 }
887
888 // Note: currently we do not restore the page for the AppsCustomize pane because the
889 // change in layout can drastically affect the saved page index
890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 }
892
893 /**
894 * Finds all the views we need and configure them properly.
895 */
896 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700897 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400898
Romain Guyb1b69f52009-08-10 15:10:15 -0700899 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400900 dragLayer.setDragController(dragController);
Adam Cohen2801caf2011-05-13 20:57:39 -0700901 dragLayer.setLauncher(this);
902 mDragLayer = dragLayer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700904 if (LauncherApplication.isScreenLarge()) {
Winson Chung63257c12011-05-05 17:06:13 -0700905 mAllAppsGrid = (AllAppsView) dragLayer.findViewById(R.id.all_apps_view);
Winson Chung785d2eb2011-04-14 16:08:02 -0700906 mAllAppsGrid.setup(this, dragController);
907 // We don't want a hole punched in our window.
908 ((View) mAllAppsGrid).setWillNotDraw(false);
909 } else {
910 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
911 findViewById(R.id.apps_customize_pane);
912 mAppsCustomizeContent = (AppsCustomizePagedView)
913 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
914 mAppsCustomizeContent.setup(this, dragController);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700915 }
916
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700918
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500920 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700921
Joe Onorato7c312c12009-08-13 21:36:53 -0700922 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
923 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700925 View handleView = findViewById(R.id.all_apps_button);
926 if (handleView != null && handleView instanceof HandleView) {
927 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700928 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700929 mHandleView.setLauncher(this);
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700930 mHandleView.setOnLongClickListener(this);
931 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800932
Winson Chung80baf5a2010-08-09 16:03:15 -0700933 if (mCustomizePagedView != null) {
934 mCustomizePagedView.setLauncher(this);
935 mCustomizePagedView.setDragController(dragController);
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700936 mCustomizePagedView.setAllAppsPagedView(mAllAppsPagedView);
Michael Jurkaaf442092010-06-10 17:01:57 -0700937 } else {
938 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
939 hotseatLeft.setContentDescription(mHotseatLabels[0]);
940 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
941 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
942 hotseatRight.setContentDescription(mHotseatLabels[1]);
943 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400944
Winson Chung4e6a9762011-05-09 11:56:34 -0700945 View.OnKeyListener listener = new IndicatorKeyEventListener();
Michael Jurkaaf442092010-06-10 17:01:57 -0700946 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
Winson Chung4e6a9762011-05-09 11:56:34 -0700947 mPreviousView.setOnKeyListener(listener);
Michael Jurkaaf442092010-06-10 17:01:57 -0700948 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Winson Chung4e6a9762011-05-09 11:56:34 -0700949 mNextView.setOnKeyListener(listener);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800950
Michael Jurkaaf442092010-06-10 17:01:57 -0700951 Drawable previous = mPreviousView.getDrawable();
952 Drawable next = mNextView.getDrawable();
953 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954
Michael Jurkaaf442092010-06-10 17:01:57 -0700955 mPreviousView.setHapticFeedbackEnabled(false);
956 mPreviousView.setOnLongClickListener(this);
957 mNextView.setHapticFeedbackEnabled(false);
958 mNextView.setOnLongClickListener(this);
959 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800960
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400962 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -0800964 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965
966 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400967 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -0800968
969 final View allAppsButton = findViewById(R.id.all_apps_button);
Winson Chung97d85d22011-04-13 11:27:36 -0700970 final View divider = findViewById(R.id.all_apps_divider);
Michael Jurkab8e14472010-12-20 16:06:10 -0800971 final View configureButton = findViewById(R.id.configure_button);
972
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700973 if (LauncherApplication.isScreenLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800974 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -0700975 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800976 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -0700977 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700978 dragController.addDragListener(deleteZone);
979
Adam Cohencdc30d52010-12-01 15:09:47 -0800980 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
981 if (allAppsDeleteZone != null) {
982 allAppsDeleteZone.setLauncher(this);
983 allAppsDeleteZone.setDragController(dragController);
984 allAppsDeleteZone.setDragAndDropEnabled(false);
985 dragController.addDragListener(allAppsDeleteZone);
986 dragController.addDropTarget(allAppsDeleteZone);
987 }
988
989 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
990 findViewById(R.id.all_apps_info_target);
991 if (allAppsInfoTarget != null) {
992 allAppsInfoTarget.setLauncher(this);
993 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -0800994 allAppsInfoTarget.setDragAndDropEnabled(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700995 }
996 View marketButton = findViewById(R.id.market_button);
997 if (marketButton != null) {
998 if (allAppsInfoTarget != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -0800999 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -08001000 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001001 marketButton.setOnClickListener(new OnClickListener() {
1002 public void onClick(View v) {
1003 onClickAppMarketButton(v);
1004 }
1005 });
Adam Cohencdc30d52010-12-01 15:09:47 -08001006 }
1007
Joe Onorato00acb122009-08-04 16:04:30 -04001008 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001009 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001010 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001011
Joe Onorato00acb122009-08-04 16:04:30 -04001012 // The order here is bottom to top.
1013 dragController.addDropTarget(workspace);
1014 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001015 if (allAppsInfoTarget != null) {
1016 dragController.addDropTarget(allAppsInfoTarget);
1017 }
1018 if (allAppsDeleteZone != null) {
1019 dragController.addDropTarget(allAppsDeleteZone);
1020 }
Winson Chung97d85d22011-04-13 11:27:36 -07001021 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
Winson Chung4e6a9762011-05-09 11:56:34 -07001022 View.OnKeyListener listener = null;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07001023 if (LauncherApplication.isScreenLarge()) {
Winson Chung4e6a9762011-05-09 11:56:34 -07001024 // For tablets, AllApps lives in the button bar at the top
1025 listener = new ButtonBarKeyEventListener();
1026 } else {
1027 // For phones, AppsCustomize lives in the "dock" at the bottom
1028 listener = new DockKeyEventListener();
1029 }
Winson Chung97d85d22011-04-13 11:27:36 -07001030 int buttonCount = mButtonCluster.getChildCount();
1031 for (int i = 0; i < buttonCount; ++i) {
1032 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
1033 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001034
1035 mAllAppsButton = findViewById(R.id.all_apps_button);
Winson Chung97d85d22011-04-13 11:27:36 -07001036 mDivider = findViewById(R.id.all_apps_divider);
Michael Jurka838a4ca2011-02-07 13:33:06 -08001037 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001038
1039 // We had previously set these click handlers in XML, but the first time we launched
1040 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1041 // found the right handler. Setting the handlers directly here eliminates that cost.
1042 if (mConfigureButton != null) {
1043 mConfigureButton.setOnClickListener(new OnClickListener() {
1044 public void onClick(View v) {
1045 onClickConfigureButton(v);
1046 }
1047 });
1048 }
1049 if (mDivider != null) {
1050 mDivider.setOnClickListener(new OnClickListener() {
1051 public void onClick(View v) {
1052 onClickAllAppsButton(v);
1053 }
1054 });
1055 }
1056 if (mAllAppsButton != null) {
1057 mAllAppsButton.setOnClickListener(new OnClickListener() {
1058 public void onClick(View v) {
1059 onClickAllAppsButton(v);
1060 }
1061 });
1062 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 }
1064
Romain Guy8a73c512009-11-09 19:19:59 -08001065 @SuppressWarnings({"UnusedDeclaration"})
1066 public void previousScreen(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001067 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001068 mWorkspace.scrollLeft();
1069 }
Romain Guy8a73c512009-11-09 19:19:59 -08001070 }
1071
1072 @SuppressWarnings({"UnusedDeclaration"})
1073 public void nextScreen(View v) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001074 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001075 mWorkspace.scrollRight();
1076 }
Romain Guy8a73c512009-11-09 19:19:59 -08001077 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001078
1079 @SuppressWarnings({"UnusedDeclaration"})
1080 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001081 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001082
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001083 int index = -1;
1084 if (v.getId() == R.id.hotseat_left) {
1085 index = 0;
1086 } else if (v.getId() == R.id.hotseat_right) {
1087 index = 1;
1088 }
1089
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001090 // reload these every tap; you never know when they might change
1091 loadHotseats();
1092 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001093 startActivitySafely(
1094 mHotseats[index],
1095 "hotseat"
1096 );
1097 }
1098 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001099
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 /**
1101 * Creates a view representing a shortcut.
1102 *
1103 * @param info The data structure describing the shortcut.
1104 *
1105 * @return A View inflated from R.layout.application.
1106 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001107 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001109 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 }
1111
1112 /**
1113 * Creates a view representing a shortcut inflated from the specified resource.
1114 *
1115 * @param layoutResId The id of the XML layout used to create the shortcut.
1116 * @param parent The group the shortcut belongs to.
1117 * @param info The data structure describing the shortcut.
1118 *
1119 * @return A View inflated from layoutResId.
1120 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001121 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001122 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1123 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 return favorite;
1126 }
1127
1128 /**
1129 * Add an application shortcut to the workspace.
1130 *
1131 * @param data The intent describing the application.
1132 * @param cellInfo The position on screen where to create the shortcut.
1133 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001134 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001135 int intersectCellX, int intersectCellY) {
1136 final int[] cellXY = mTmpAddItemCellCoordinates;
1137 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1138
1139 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1140 showOutOfSpaceMessage();
1141 return;
1142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001144 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001145
Romain Guy73b979d2009-06-09 12:57:21 -07001146 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001147 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001149 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001150 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1151 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001152 } else {
1153 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 }
1155 }
Romain Guycbb89e42009-06-08 15:52:54 -07001156
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 /**
1158 * Add a shortcut to the workspace.
1159 *
1160 * @param data The intent describing the shortcut.
1161 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001163 private void completeAddShortcut(Intent data, int screen,
1164 int intersectCellX, int intersectCellY) {
1165 final int[] cellXY = mTmpAddItemCellCoordinates;
1166 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001167
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001168 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001169 boolean foundCellSpan = false;
1170 if (touchXY != null) {
1171 // when dragging and dropping, just find the closest free spot
1172 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1173 int[] result = screenLayout.findNearestVacantArea(
1174 touchXY[0], touchXY[1], 1, 1, cellXY);
1175 foundCellSpan = (result != null);
1176 } else {
1177 foundCellSpan = layout.findCellForSpanThatIntersects(
1178 cellXY, 1, 1, intersectCellX, intersectCellY);
1179 }
1180
1181 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001182 showOutOfSpaceMessage();
1183 return;
1184 }
1185
1186 final ShortcutInfo info = mModel.addShortcut(
1187 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188
1189 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001191 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 }
1193 }
1194
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001196 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001198 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001199 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 */
Winson Chungf7640c82011-02-28 13:47:29 -08001201 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001202 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001203
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001204 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001205 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1206 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001209 // We have saved the position to which the widget was dragged-- this really only matters
1210 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001211 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001212
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001213 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001214 boolean foundCellSpan = false;
1215 if (touchXY != null) {
1216 // when dragging and dropping, just find the closest free spot
1217 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1218 int[] result = screenLayout.findNearestVacantArea(
1219 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001220 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001221 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001222 // if we long pressed on an empty cell to bring up a menu,
1223 // make sure we intersect the empty cell
1224 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1225 // findCellForSpanThatIntersects will just ignore them
1226 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1227 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001228 }
1229
Michael Jurka0280c3b2010-09-17 15:00:07 -07001230 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001231 if (appWidgetId != -1) {
1232 // Deleting an app widget ID is a void call but writes to disk before returning
1233 // to the caller...
1234 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1235 new Thread("deleteAppWidgetId") {
1236 public void run() {
1237 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1238 }
1239 }.start();
1240 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001241 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001242 return;
1243 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001245 // Build Launcher-specific widget info and save to database
1246 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001247 launcherInfo.spanX = spanXY[0];
1248 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 LauncherModel.addItemToDatabase(this, launcherInfo,
1251 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001252 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253
1254 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001255 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001256
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001258 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001259
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001260 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001262
Michael Jurka0280c3b2010-09-17 15:00:07 -07001263 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001264 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001265
1266 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 }
1268 }
Romain Guycbb89e42009-06-08 15:52:54 -07001269
Adam Cohended9f8d2010-11-03 13:25:16 -07001270 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1271 @Override
1272 public void onReceive(Context context, Intent intent) {
1273 final String action = intent.getAction();
1274 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1275 mUserPresent = false;
1276 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001277
1278 // Reset AllApps to it's initial state
1279 if (mAllAppsGrid != null) {
1280 mAllAppsGrid.reset();
1281 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001282 if (mAppsCustomizeContent != null) {
1283 mAppsCustomizeContent.reset();
1284 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001285 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1286 mUserPresent = true;
1287 updateRunning();
1288 }
1289 }
1290 };
1291
1292 @Override
1293 public void onAttachedToWindow() {
1294 super.onAttachedToWindow();
1295
1296 // Listen for broadcasts related to user-presence
1297 final IntentFilter filter = new IntentFilter();
1298 filter.addAction(Intent.ACTION_SCREEN_OFF);
1299 filter.addAction(Intent.ACTION_USER_PRESENT);
1300 registerReceiver(mReceiver, filter);
1301
Adam Cohend113e0c2010-11-11 10:48:05 -08001302 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001303 mVisible = true;
1304 }
1305
1306 @Override
1307 public void onDetachedFromWindow() {
1308 super.onDetachedFromWindow();
1309 mVisible = false;
1310
Adam Cohend113e0c2010-11-11 10:48:05 -08001311 if (mAttached) {
1312 unregisterReceiver(mReceiver);
1313 mAttached = false;
1314 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001315 updateRunning();
1316 }
1317
1318 public void onWindowVisibilityChanged(int visibility) {
1319 mVisible = visibility == View.VISIBLE;
1320 updateRunning();
1321 }
1322
1323 private void sendAdvanceMessage(long delay) {
1324 mHandler.removeMessages(ADVANCE_MSG);
1325 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1326 mHandler.sendMessageDelayed(msg, delay);
1327 mAutoAdvanceSentTime = System.currentTimeMillis();
1328 }
1329
1330 private void updateRunning() {
1331 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1332 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1333 mAutoAdvanceRunning = autoAdvanceRunning;
1334 if (autoAdvanceRunning) {
1335 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1336 sendAdvanceMessage(delay);
1337 } else {
1338 if (!mWidgetsToAdvance.isEmpty()) {
1339 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1340 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1341 }
1342 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001343 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001344 }
1345 }
1346 }
1347
1348 private final Handler mHandler = new Handler() {
1349 @Override
1350 public void handleMessage(Message msg) {
1351 if (msg.what == ADVANCE_MSG) {
1352 int i = 0;
1353 for (View key: mWidgetsToAdvance.keySet()) {
1354 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1355 final int delay = mAdvanceStagger * i;
1356 if (v instanceof Advanceable) {
1357 postDelayed(new Runnable() {
1358 public void run() {
1359 ((Advanceable) v).advance();
1360 }
1361 }, delay);
1362 }
1363 i++;
1364 }
1365 sendAdvanceMessage(mAdvanceInterval);
1366 }
1367 }
1368 };
1369
1370 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1371 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1372 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1373 if (v instanceof Advanceable) {
1374 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001375 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001376 updateRunning();
1377 }
1378 }
1379
1380 void removeWidgetToAutoAdvance(View hostView) {
1381 if (mWidgetsToAdvance.containsKey(hostView)) {
1382 mWidgetsToAdvance.remove(hostView);
1383 updateRunning();
1384 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001385 }
1386
Joe Onorato9c1289c2009-08-17 11:03:03 -04001387 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1388 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001389 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001390 launcherInfo.hostView = null;
1391 }
1392
Adam Cohended9f8d2010-11-03 13:25:16 -07001393 void showOutOfSpaceMessage() {
1394 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1395 }
1396
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001397 public LauncherAppWidgetHost getAppWidgetHost() {
1398 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 }
Romain Guycbb89e42009-06-08 15:52:54 -07001400
Winson Chunga9abd0e2010-10-27 17:18:37 -07001401 public LauncherModel getModel() {
1402 return mModel;
1403 }
1404
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001405 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001406 getWindow().closeAllPanels();
1407
1408 try {
1409 dismissDialog(DIALOG_CREATE_SHORTCUT);
1410 // Unlock the workspace if the dialog was showing
1411 } catch (Exception e) {
1412 // An exception is thrown if the dialog is not visible, which is fine
1413 }
1414
1415 try {
1416 dismissDialog(DIALOG_RENAME_FOLDER);
1417 // Unlock the workspace if the dialog was showing
1418 } catch (Exception e) {
1419 // An exception is thrown if the dialog is not visible, which is fine
1420 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001421
1422 // Whatever we were doing is hereby canceled.
1423 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001424 }
1425
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 @Override
1427 protected void onNewIntent(Intent intent) {
1428 super.onNewIntent(intent);
1429
1430 // Close the menu
1431 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001432 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001433 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001434
Joe Onorato14f122b2009-11-19 14:06:36 -08001435 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1436 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001437
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001438 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001439 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001440 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001441 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001442 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001443 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001444 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001445
Joe Onorato3a8820b2009-11-10 15:06:42 -08001446 final View v = getWindow().peekDecorView();
1447 if (v != null && v.getWindowToken() != null) {
1448 InputMethodManager imm = (InputMethodManager)getSystemService(
1449 INPUT_METHOD_SERVICE);
1450 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001452
1453 // Reset AllApps to it's initial state
1454 if (mAllAppsGrid != null) {
1455 mAllAppsGrid.reset();
1456 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001457 if (mAppsCustomizeContent != null) {
1458 mAppsCustomizeContent.reset();
1459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 }
1461 }
1462
1463 @Override
1464 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1465 // Do not call super here
1466 mSavedInstanceState = savedInstanceState;
1467 }
1468
1469 @Override
1470 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001471 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472
1473 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1474 if (folders.size() > 0) {
1475 final int count = folders.size();
1476 long[] ids = new long[count];
1477 for (int i = 0; i < count; i++) {
1478 final FolderInfo info = folders.get(i).getInfo();
1479 ids[i] = info.id;
1480 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001481 outState.putLongArray(RUNTIME_STATE_FOLDERS, ids);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 } else {
1483 super.onSaveInstanceState(outState);
1484 }
1485
Michael Jurka883f55b2010-10-21 15:47:14 -07001486 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487
Michael Jurka0280c3b2010-09-17 15:00:07 -07001488 if (mAddScreen > -1 && mWaitingForResult) {
1489 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1490 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1491 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 }
1493
1494 if (mFolderInfo != null && mWaitingForResult) {
1495 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1496 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1497 }
Michael Jurka946ad472010-07-09 18:05:18 -07001498
Winson Chung43b119d2010-12-06 16:45:05 -08001499 // Save the current AllApps drawer tab
1500 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1501 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1502 String currentTabTag = tabhost.getCurrentTabTag();
1503 if (currentTabTag != null) {
1504 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001505 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001506 }
1507 }
Winson Chung43b119d2010-12-06 16:45:05 -08001508 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001509 if (mHomeCustomizationDrawer != null) {
1510 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1511 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001512 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001513 }
1514 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001515 // Save the current AppsCustomize tab
1516 if (mAppsCustomizeTabHost != null) {
1517 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1518 if (currentTabTag != null) {
1519 outState.putString("apps_customize_currentTab", currentTabTag);
1520 }
1521 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 }
1523
1524 @Override
1525 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001527
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001529 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001531 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001533 mAppWidgetHost = null;
1534
1535 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536
1537 TextKeyListener.getInstance().release();
1538
Joe Onorato9c1289c2009-08-17 11:03:03 -04001539 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540
Joe Onorato9c1289c2009-08-17 11:03:03 -04001541 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001542
1543 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001544
Patrick Dubroyab962b72010-07-26 12:10:10 -07001545 // Some launcher layouts don't have a previous and next view
1546 if (mPreviousView != null) {
1547 dismissPreview(mPreviousView);
1548 }
1549 if (mNextView != null) {
1550 dismissPreview(mNextView);
1551 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001552
1553 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001554
1555 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1556 mWorkspace.removeAllViews();
1557 mWorkspace = null;
1558 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001559
1560 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 }
1562
Adam Cohena9cf38f2011-05-02 15:36:58 -07001563 public DragController getDragController() {
1564 return mDragController;
1565 }
1566
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 @Override
1568 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001569 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 super.startActivityForResult(intent, requestCode);
1571 }
1572
1573 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001574 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001576
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001577 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001578
Karl Rosaen138a0412009-04-23 19:00:21 -07001579 if (initialQuery == null) {
1580 // Use any text typed in the launcher as the initial query
1581 initialQuery = getTypedText();
1582 clearTypedText();
1583 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 if (appSearchData == null) {
1585 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001586 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 }
Romain Guycbb89e42009-06-08 15:52:54 -07001588
Karl Rosaen138a0412009-04-23 19:00:21 -07001589 final SearchManager searchManager =
1590 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001591 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001592 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 }
1594
1595 @Override
1596 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001597 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001598 return false;
1599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600
1601 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001602
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1604 .setIcon(android.R.drawable.ic_menu_add)
1605 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001606 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1607 .setIcon(android.R.drawable.ic_menu_manage)
1608 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001609 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 .setIcon(android.R.drawable.ic_menu_gallery)
1611 .setAlphabeticShortcut('W');
1612 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1613 .setIcon(android.R.drawable.ic_search_category_default)
1614 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1615 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1616 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1617 .setAlphabeticShortcut('N');
1618
1619 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001620 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1621 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622
1623 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1624 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1625 .setIntent(settings);
1626
1627 return true;
1628 }
1629
1630 @Override
1631 public boolean onPrepareOptionsMenu(Menu menu) {
1632 super.onPrepareOptionsMenu(menu);
1633
Winson Chung785d2eb2011-04-14 16:08:02 -07001634 if (mAllAppsGrid != null) {
1635 // If all apps is animating, don't show the menu, because we don't know
1636 // which one to show.
1637 if (mAllAppsGrid.isAnimating()) {
1638 return false;
1639 }
1640
1641 // Only show the add and wallpaper options when we're not in all apps.
1642 boolean visible = !mAllAppsGrid.isVisible();
1643 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1644 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1645
1646 // Disable add if the workspace is full.
1647 if (visible) {
1648 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1649 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
1650 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001651 }
1652
Winson Chung785d2eb2011-04-14 16:08:02 -07001653 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1654 // related code?
1655 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656
1657 return true;
1658 }
1659
1660 @Override
1661 public boolean onOptionsItemSelected(MenuItem item) {
1662 switch (item.getItemId()) {
1663 case MENU_ADD:
1664 addItems();
1665 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001666 case MENU_MANAGE_APPS:
1667 manageApps();
1668 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 case MENU_WALLPAPER_SETTINGS:
1670 startWallpaper();
1671 return true;
1672 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001673 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 return true;
1675 case MENU_NOTIFICATIONS:
1676 showNotifications();
1677 return true;
1678 }
1679
1680 return super.onOptionsItemSelected(item);
1681 }
Romain Guycbb89e42009-06-08 15:52:54 -07001682
Karl Rosaen138a0412009-04-23 19:00:21 -07001683 /**
1684 * Indicates that we want global search for this activity by setting the globalSearch
1685 * argument for {@link #startSearch} to true.
1686 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001688 @Override
1689 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001690 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001691 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001692 }
1693
Joe Onorato9c1289c2009-08-17 11:03:03 -04001694 public boolean isWorkspaceLocked() {
1695 return mWorkspaceLoading || mWaitingForResult;
1696 }
1697
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001698 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1699 private boolean isPreviewVisible() {
1700 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1701 (mNextView != null && mNextView.getTag() != null);
1702 }
1703
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 private void addItems() {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07001705 if (LauncherApplication.isScreenLarge()) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001706 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001707 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001708 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001709 }
1710 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001711 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001712 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 }
1715
Michael Jurka0280c3b2010-09-17 15:00:07 -07001716 private void resetAddInfo() {
1717 mAddScreen = -1;
1718 mAddIntersectCellX = -1;
1719 mAddIntersectCellY = -1;
1720 mAddDropPosition = null;
1721 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001722
Winson Chung55cef262010-10-28 14:14:18 -07001723 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001724 resetAddInfo();
1725 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001726 mAddDropPosition = position;
1727
Michael Jurkaaf442092010-06-10 17:01:57 -07001728 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001729 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1730 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001731 }
1732
Winson Chung7ad01412010-09-27 11:33:03 -07001733 private void manageApps() {
1734 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1735 }
1736
Michael Jurkaaf442092010-06-10 17:01:57 -07001737 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001738 // TODO: catch bad widget exception when sent
1739 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001740 // TODO: Is this log message meaningful?
1741 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001742 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001743 }
1744
Winson Chung55cef262010-10-28 14:14:18 -07001745 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001746 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747
Bjorn Bringert7984c942009-12-09 15:38:25 +00001748 if (appWidget.configure != null) {
1749 // Launch over to configure widget, if needed
1750 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1751 intent.setComponent(appWidget.configure);
1752 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001753 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001754 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1755 intent.putExtra(
1756 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1757 info.mimeType);
1758
1759 final String mimeType = info.mimeType;
1760 final ClipData clipData = (ClipData) info.configurationData;
1761 final ClipDescription clipDesc = clipData.getDescription();
1762 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1763 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001764 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001765 final CharSequence stringData = item.getText();
1766 final Uri uriData = item.getUri();
1767 final Intent intentData = item.getIntent();
1768 final String key =
1769 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1770 if (uriData != null) {
1771 intent.putExtra(key, uriData);
1772 } else if (intentData != null) {
1773 intent.putExtra(key, intentData);
1774 } else if (stringData != null) {
1775 intent.putExtra(key, stringData);
1776 }
1777 break;
1778 }
1779 }
1780 }
Winson Chung55cef262010-10-28 14:14:18 -07001781 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001782
Romain Guy8e633c52010-03-04 12:51:36 -08001783 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001785 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001786 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 }
1788 }
Romain Guycbb89e42009-06-08 15:52:54 -07001789
Michael Jurka0280c3b2010-09-17 15:00:07 -07001790 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1791 resetAddInfo();
1792 mAddScreen = screen;
1793 mAddDropPosition = position;
1794
1795 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1796 createShortcutIntent.setComponent(componentName);
1797 processShortcut(createShortcutIntent);
1798 }
1799
Joe Onoratodeb98af2010-02-19 14:59:39 -08001800 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001801 // Handle case where user selected "Applications"
1802 String applicationName = getResources().getString(R.string.group_applications);
1803 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001804
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001805 if (applicationName != null && applicationName.equals(shortcutName)) {
1806 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1807 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001808
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001809 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1810 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001811 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001812 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001813 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001814 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001815 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 }
1817
Winson Chung24ab2f12010-09-16 14:10:47 -07001818 void processWallpaper(Intent intent) {
1819 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1820 }
1821
Adam Cohendf035382011-04-11 17:22:04 -07001822 FolderIcon addFolder(int screen, int intersectCellX, int intersectCellY) {
Adam Cohendf2cc412011-04-27 16:56:57 -07001823 FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 folderInfo.title = getText(R.string.folder_name);
1825
Michael Jurka0280c3b2010-09-17 15:00:07 -07001826 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1827 final int[] cellXY = mTmpAddItemCellCoordinates;
1828 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1829 showOutOfSpaceMessage();
Adam Cohendf035382011-04-11 17:22:04 -07001830 return null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832
1833 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001834 LauncherModel.addItemToDatabase(this, folderInfo,
1835 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001836 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001837 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838
1839 // Create the view
1840 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001841 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1842 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001843 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001844 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 }
Romain Guycbb89e42009-06-08 15:52:54 -07001846
Joe Onorato9c1289c2009-08-17 11:03:03 -04001847 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001848 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001849 }
1850
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 private void showNotifications() {
1852 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1853 if (statusBar != null) {
1854 statusBar.expand();
1855 }
1856 }
1857
1858 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001859 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001861 Intent chooser = Intent.createChooser(pickWallpaper,
1862 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001863 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1864 // Removed in Eclair MR1
1865// WallpaperManager wm = (WallpaperManager)
1866// getSystemService(Context.WALLPAPER_SERVICE);
1867// WallpaperInfo wi = wm.getWallpaperInfo();
1868// if (wi != null && wi.getSettingsActivity() != null) {
1869// LabeledIntent li = new LabeledIntent(getPackageName(),
1870// R.string.configure_wallpaper, 0);
1871// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1872// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1873// }
Mike Clerona0618e42009-10-22 13:55:21 -07001874 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 }
1876
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001877 /**
1878 * Registers various content observers. The current implementation registers
1879 * only a favorites observer to keep track of the favorites applications.
1880 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001881 private void registerContentObservers() {
1882 ContentResolver resolver = getContentResolver();
1883 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1884 true, mWidgetObserver);
1885 }
1886
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 @Override
1888 public boolean dispatchKeyEvent(KeyEvent event) {
1889 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1890 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001892 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001893 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001894 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001895 dumpState();
1896 return true;
1897 }
1898 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001899 }
1900 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1901 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001902 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 return true;
1904 }
1905 }
1906
1907 return super.dispatchKeyEvent(event);
1908 }
1909
Joe Onorato88ec0992009-11-19 13:16:06 -08001910 @Override
1911 public void onBackPressed() {
Winson Chung785d2eb2011-04-14 16:08:02 -07001912 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001913 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001914 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08001915 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001916 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001917 dismissPreview(mPreviousView);
1918 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001919 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001920 mWorkspace.exitWidgetResizeMode();
1921
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001922 // Back button is a no-op here, but give at least some feedback for the button press
1923 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001924 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001925 }
1926
Adam Cohen2801caf2011-05-13 20:57:39 -07001927 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 Folder folder = mWorkspace.getOpenFolder();
1929 if (folder != null) {
1930 closeFolder(folder);
Adam Cohen2801caf2011-05-13 20:57:39 -07001931 mDragLayer.setCurrentFolder(null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 }
1933 }
1934
1935 void closeFolder(Folder folder) {
1936 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001937
Michael Jurka8c920dd2011-01-20 14:16:56 -08001938 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001940 CellLayout cl = (CellLayout) parent;
Adam Cohen2801caf2011-05-13 20:57:39 -07001941 FolderIcon fi = (FolderIcon) cl.getChildAt(folder.mInfo.cellX, folder.mInfo.cellY);
1942 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001943 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001945
1946 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 folder.onClose();
1948 }
1949
1950 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001951 * Re-listen when widgets are reset.
1952 */
1953 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001954 if (mAppWidgetHost != null) {
1955 mAppWidgetHost.startListening();
1956 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001957 }
1958
1959 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001960 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1961 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001963 private void unbindDesktopItems() {
1964 for (ItemInfo item: mDesktopItems) {
1965 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001967 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001969
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 /**
1971 * Launches the intent referred by the clicked shortcut.
1972 *
1973 * @param v The view representing the clicked shortcut.
1974 */
1975 public void onClick(View v) {
1976 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001977 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001979 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001980 int[] pos = new int[2];
1981 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001982 intent.setSourceBounds(new Rect(pos[0], pos[1],
1983 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001984 boolean success = startActivitySafely(intent, tag);
1985
1986 if (success && v instanceof BubbleTextView) {
1987 mWaitingForResume = (BubbleTextView) v;
1988 mWaitingForResume.setStayPressed(true);
1989 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001991 if (v instanceof FolderIcon) {
1992 FolderIcon fi = (FolderIcon) v;
1993 handleFolderClick(fi);
1994 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001995 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001996 if (mState == State.ALL_APPS) {
1997 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001998 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001999 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002000 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 }
2002 }
2003
Michael Jurka0e260592010-06-30 17:07:39 -07002004 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002005 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002006 // clicking anywhere on the workspace causes the customization drawer to slide down
2007 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002008 return false;
2009 }
2010
Michael Jurkaaf442092010-06-10 17:01:57 -07002011 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002012 * Event handler for the search button
2013 *
2014 * @param v The view that was clicked.
2015 */
2016 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002017 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002018 // Use a custom animation for launching search
2019 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002020 }
2021
2022 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002023 * Event handler for the voice button
2024 *
2025 * @param v The view that was clicked.
2026 */
2027 public void onClickVoiceButton(View v) {
2028 startVoiceSearch();
2029 }
2030
2031 private void startVoiceSearch() {
2032 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2033 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2034 startActivity(intent);
2035 }
2036
2037 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002038 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002039 * enters home screen customization mode.
2040 *
2041 * @param v The view that was clicked.
2042 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002043 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002044 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002045 }
2046
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002047 /**
2048 * Event handler for the "grid" button that appears on the home screen, which
2049 * enters all apps mode.
2050 *
2051 * @param v The view that was clicked.
2052 */
2053 public void onClickAllAppsButton(View v) {
2054 showAllApps(true);
2055 }
2056
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002057 public void onClickAppMarketButton(View v) {
2058 if (mAppMarketIntent != null) {
2059 startActivitySafely(mAppMarketIntent, "app market");
2060 }
2061 }
2062
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002063 void startApplicationDetailsActivity(ComponentName componentName) {
2064 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002065 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2066 Uri.fromParts("package", packageName, null));
2067 startActivity(intent);
2068 }
2069
Patrick Dubroy5539af72010-09-07 15:22:01 -07002070 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2071 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2072 // System applications cannot be installed. For now, show a toast explaining that.
2073 // We may give them the option of disabling apps this way.
2074 int messageId = R.string.uninstall_system_app_text;
2075 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2076 } else {
2077 String packageName = appInfo.componentName.getPackageName();
2078 String className = appInfo.componentName.getClassName();
2079 Intent intent = new Intent(
2080 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2081 startActivity(intent);
2082 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002083 }
2084
Michael Jurkaddd62e92011-02-16 17:49:14 -08002085 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2087 try {
2088 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002089 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 } catch (ActivityNotFoundException e) {
2091 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002092 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 } catch (SecurityException e) {
2094 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002095 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002097 "or use the exported attribute for this activity. "
2098 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002100 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002102
Romain Guy8e633c52010-03-04 12:51:36 -08002103 void startActivityForResultSafely(Intent intent, int requestCode) {
2104 try {
2105 startActivityForResult(intent, requestCode);
2106 } catch (ActivityNotFoundException e) {
2107 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2108 } catch (SecurityException e) {
2109 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2110 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2111 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2112 "or use the exported attribute for this activity.", e);
2113 }
2114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115
Adam Cohena9cf38f2011-05-02 15:36:58 -07002116 private void handleFolderClick(FolderIcon folderIcon) {
2117 final FolderInfo info = folderIcon.mInfo;
2118 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 // Close any open folder
2120 closeFolder();
2121 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002122 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 } else {
2124 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07002125 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 int folderScreen;
2127 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002128 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 // .. and close it
2130 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002131 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 // Close any folder open on the current screen
2133 closeFolder();
2134 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002135 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 }
2137 }
2138 }
2139 }
2140
Adam Cohen2801caf2011-05-13 20:57:39 -07002141 private void growAndFadeOutFolderIcon(FolderIcon fi) {
2142 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2143 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2144 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2145
2146 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2147 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2148 oa.start();
2149 }
2150
2151 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
2152 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2153 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2154 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2155
2156 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2157 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2158 oa.start();
2159 }
2160
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002162 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163 * is animated relative to the specified View. If the View is null, no animation
2164 * is played.
2165 *
2166 * @param folderInfo The FolderInfo describing the folder to open.
2167 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002168 public void openFolder(FolderIcon folderIcon) {
2169 Folder folder = folderIcon.mFolder;
2170 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171
Adam Cohen2801caf2011-05-13 20:57:39 -07002172 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002173 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174
Adam Cohena9cf38f2011-05-02 15:36:58 -07002175 mWorkspace.addInFullScreen(folder, info.screen);
Adam Cohen2801caf2011-05-13 20:57:39 -07002176 mDragLayer.setCurrentFolder(folder);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002177 folder.animateOpen();
2178 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179 }
2180
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002182 if (mState != State.WORKSPACE) {
2183 return false;
2184 }
2185
Romain Guy1fbc1c82009-11-09 20:43:08 -08002186 switch (v.getId()) {
2187 case R.id.previous_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002188 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002189 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2190 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002191 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002192 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002193 return true;
2194 case R.id.next_screen:
Winson Chung785d2eb2011-04-14 16:08:02 -07002195 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002196 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2197 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002198 showPreviews(v);
2199 }
2200 return true;
2201 case R.id.all_apps_button:
Winson Chung785d2eb2011-04-14 16:08:02 -07002202 if (mState != State.ALL_APPS && mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002203 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2204 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2205 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002206 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002207 return true;
2208 }
2209
Joe Onorato9c1289c2009-08-17 11:03:03 -04002210 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211 return false;
2212 }
2213
2214 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002215 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 }
2217
Michael Jurka0280c3b2010-09-17 15:00:07 -07002218 resetAddInfo();
2219 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002221 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 return true;
2223 }
2224
Michael Jurka0280c3b2010-09-17 15:00:07 -07002225 final View itemUnderLongClick = longClickCellInfo.cell;
2226
Winson Chung304dcde2011-01-07 11:17:23 -08002227 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002228 if (itemUnderLongClick == null) {
2229 // User long pressed on empty space
2230 mWorkspace.setAllowLongPress(false);
2231 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2232 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07002233 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002235 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002237 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2238 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002239 mAddIntersectCellX = longClickCellInfo.cellX;
2240 mAddIntersectCellY = longClickCellInfo.cellY;
2241 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 }
2243 }
2244 }
2245 return true;
2246 }
2247
Romain Guye6b8e2f2009-11-10 11:56:55 -08002248 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002249 private void dismissPreview(final View v) {
2250 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002251 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002252 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2253 public void onDismiss() {
2254 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2255 int count = group.getChildCount();
2256 for (int i = 0; i < count; i++) {
2257 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2258 }
Michael Jurka25913ca2011-05-04 17:45:33 -07002259 ArrayList<Bitmap> bitmaps =
2260 (ArrayList<Bitmap>) v.getTag(R.id.all_apps_button_cluster);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002261 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2262
2263 v.setTag(R.id.workspace, null);
Michael Jurka25913ca2011-05-04 17:45:33 -07002264 v.setTag(R.id.all_apps_button_cluster, null);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002265 window.setOnDismissListener(null);
2266 }
2267 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002268 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002269 }
2270 v.setTag(null);
2271 }
2272
Romain Guyf8e6a802009-12-07 17:48:02 -08002273 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002274 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002275 }
2276
Romain Guya6abce82009-11-10 02:54:41 -08002277 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002278 final Resources resources = getResources();
2279 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002280
Romain Guya6abce82009-11-10 02:54:41 -08002281 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002282
Romain Guy9d31e9f2009-11-11 18:54:28 -08002283 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002284
Romain Guyf8e6a802009-12-07 17:48:02 -08002285 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002286 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002287 int extraW = (int) ((r.left + r.right) * max);
2288 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002289
2290 int aW = cell.getWidth() - extraW;
2291 float w = aW / max;
2292
2293 int width = cell.getWidth();
2294 int height = cell.getHeight();
2295 int x = cell.getLeftPadding();
2296 int y = cell.getTopPadding();
2297 width -= (x + cell.getRightPadding());
2298 height -= (y + cell.getBottomPadding());
2299
2300 float scale = w / width;
2301
2302 int count = end - start;
2303
2304 final float sWidth = width * scale;
2305 float sHeight = height * scale;
2306
Romain Guye6b8e2f2009-11-10 11:56:55 -08002307 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002308
Romain Guye6b8e2f2009-11-10 11:56:55 -08002309 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2310 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002311
2312 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002313 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002314 cell = (CellLayout) workspace.getChildAt(i);
2315
Romain Guyf8e6a802009-12-07 17:48:02 -08002316 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002317 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002318
2319 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002320 c.scale(scale, scale);
2321 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002322 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002323
Romain Guy9d31e9f2009-11-11 18:54:28 -08002324 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002325 image.setImageBitmap(bitmap);
2326 image.setTag(i);
2327 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002328 image.setOnFocusChangeListener(handler);
2329 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002330 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002331
2332 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002333 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2334
Winson Chungaafa03c2010-06-11 17:34:16 -07002335 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002336 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002337
2338 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002339 p.setContentView(preview);
2340 p.setWidth((int) (sWidth * count + extraW));
2341 p.setHeight((int) (sHeight + extraH));
2342 p.setAnimationStyle(R.style.AnimationPreview);
2343 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002344 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002345 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002346 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002347
Romain Guye6b8e2f2009-11-10 11:56:55 -08002348 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2349 public void onDismiss() {
2350 dismissPreview(anchor);
2351 }
2352 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002353
2354 anchor.setTag(p);
2355 anchor.setTag(R.id.workspace, preview);
Michael Jurka25913ca2011-05-04 17:45:33 -07002356 anchor.setTag(R.id.all_apps_button_cluster, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002357 }
2358
Romain Guy9d31e9f2009-11-11 18:54:28 -08002359 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002360 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002361
Romain Guye6b8e2f2009-11-10 11:56:55 -08002362 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002363 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002364 }
2365
2366 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002367 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002368 v.post(this);
2369 }
2370
2371 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002372 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002373 }
2374
2375 public void onFocusChange(View v, boolean hasFocus) {
2376 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002377 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002378 }
Romain Guya6abce82009-11-10 02:54:41 -08002379 }
2380 }
2381
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002382 Workspace getWorkspace() {
2383 return mWorkspace;
2384 }
2385
Michael Jurka3c38d562011-02-18 19:22:12 -08002386 TabHost getCustomizationDrawer() {
2387 return mHomeCustomizationDrawer;
2388 }
2389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390 @Override
2391 protected Dialog onCreateDialog(int id) {
2392 switch (id) {
2393 case DIALOG_CREATE_SHORTCUT:
2394 return new CreateShortcut().createDialog();
2395 case DIALOG_RENAME_FOLDER:
2396 return new RenameFolder().createDialog();
2397 }
2398
2399 return super.onCreateDialog(id);
2400 }
2401
2402 @Override
2403 protected void onPrepareDialog(int id, Dialog dialog) {
2404 switch (id) {
2405 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 break;
2407 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002408 if (mFolderInfo != null) {
2409 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2410 final CharSequence text = mFolderInfo.title;
2411 input.setText(text);
2412 input.setSelection(0, text.length());
2413 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 break;
2415 }
2416 }
2417
2418 void showRenameDialog(FolderInfo info) {
2419 mFolderInfo = info;
2420 mWaitingForResult = true;
2421 showDialog(DIALOG_RENAME_FOLDER);
2422 }
2423
Michael Jurka0280c3b2010-09-17 15:00:07 -07002424 private void showAddDialog(int intersectX, int intersectY) {
2425 resetAddInfo();
2426 mAddIntersectCellX = intersectX;
2427 mAddIntersectCellY = intersectY;
2428 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 mWaitingForResult = true;
2430 showDialog(DIALOG_CREATE_SHORTCUT);
2431 }
2432
Joe Onoratodeb98af2010-02-19 14:59:39 -08002433 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002434 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002435 Bundle bundle = new Bundle();
2436
2437 ArrayList<String> shortcutNames = new ArrayList<String>();
2438 shortcutNames.add(getString(R.string.group_applications));
2439 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2440
2441 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2442 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2443 R.drawable.ic_launcher_application));
2444 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2445
2446 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2447 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002448 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002449 pickIntent.putExtras(bundle);
2450
Joe Onoratodeb98af2010-02-19 14:59:39 -08002451 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002452 }
2453
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002454 private class RenameFolder {
2455 private EditText mInput;
2456
2457 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002458 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2459 mInput = (EditText) layout.findViewById(R.id.folder_name);
2460
2461 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2462 builder.setIcon(0);
2463 builder.setTitle(getString(R.string.rename_folder_title));
2464 builder.setCancelable(true);
2465 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2466 public void onCancel(DialogInterface dialog) {
2467 cleanup();
2468 }
2469 });
2470 builder.setNegativeButton(getString(R.string.cancel_action),
2471 new Dialog.OnClickListener() {
2472 public void onClick(DialogInterface dialog, int which) {
2473 cleanup();
2474 }
2475 }
2476 );
2477 builder.setPositiveButton(getString(R.string.rename_action),
2478 new Dialog.OnClickListener() {
2479 public void onClick(DialogInterface dialog, int which) {
2480 changeFolderName();
2481 }
2482 }
2483 );
2484 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002485
2486 final AlertDialog dialog = builder.create();
2487 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2488 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002489 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002490 mInput.requestFocus();
2491 InputMethodManager inputManager = (InputMethodManager)
2492 getSystemService(Context.INPUT_METHOD_SERVICE);
2493 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002494 }
2495 });
2496
2497 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 }
2499
2500 private void changeFolderName() {
2501 final String name = mInput.getText().toString();
2502 if (!TextUtils.isEmpty(name)) {
2503 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002504 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 mFolderInfo.title = name;
2506 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2507
Joe Onorato9c1289c2009-08-17 11:03:03 -04002508 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002509 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002510 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 } else {
2512 final FolderIcon folderIcon = (FolderIcon)
2513 mWorkspace.getViewForTag(mFolderInfo);
2514 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002515 // TODO: At some point we'll probably want some version of setting
2516 // the text for a folder icon.
2517 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002518 getWorkspace().requestLayout();
2519 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002520 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002521 mWorkspaceLoading = true;
2522 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 }
2524 }
2525 }
2526 cleanup();
2527 }
2528
2529 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002530 dismissDialog(DIALOG_RENAME_FOLDER);
2531 mWaitingForResult = false;
2532 mFolderInfo = null;
2533 }
2534 }
2535
Daniel Sandler843e8602010-06-07 14:59:01 -04002536 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2537 public boolean isAllAppsVisible() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002538 return (mState == State.ALL_APPS) || (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002539 }
2540
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002541 // AllAppsView.Watcher
2542 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002543 // In XLarge view, we zoom down the workspace below all apps so it's still visible
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002544 if (zoom == 1.0f && !LauncherApplication.isScreenLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002545 mWorkspace.setVisibility(View.GONE);
2546 }
2547 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002548
Michael Jurkaafca9532011-02-16 14:50:35 -08002549 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002550 button.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002551 }
2552
2553 private void hideToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002554 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2555 button.setVisibility(View.INVISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002556 button.setAlpha(0.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002557 }
2558
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002559 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002560 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002561 *
2562 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2563 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002564 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002565 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002566 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002567 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002568 final boolean showing = show;
2569 final boolean hiding = !show;
2570
2571 final int duration = show ?
2572 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2573 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2574
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002575 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002576 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002577 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002578 anim.addUpdateListener(new AnimatorUpdateListener() {
2579 public void onAnimationUpdate(ValueAnimator animation) {
2580 view.setAlpha((Float) animation.getAnimatedValue());
2581 }
2582 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002583 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002584 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002585 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002586 if (showing) showAndEnableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002587 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002588 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002589 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002590 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002591 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002592 });
2593 seq.play(anim);
2594 } else {
2595 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002596 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002597 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002598 } else {
2599 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002600 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002601 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002602 }
2603
2604 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002605 * Show/hide the appropriate toolbar buttons for newState.
2606 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002607 *
2608 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002609 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2610 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002611 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002612 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002613 switch (newState) {
2614 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002615 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002616 mDeleteZone.setDragAndDropEnabled(true);
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002617 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002618 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
2619 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002620 break;
2621 case ALL_APPS:
Winson Chung785d2eb2011-04-14 16:08:02 -07002622 case APPS_CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002623 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002624 mDeleteZone.setDragAndDropEnabled(false);
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002625 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002626 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
2627 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002628 break;
2629 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002630 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002631 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002632 break;
2633 }
2634 }
2635
2636 /**
2637 * Helper method for the cameraZoomIn/cameraZoomOut animations
2638 * @param view The view being animated
2639 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2640 * @param scaleFactor The scale factor used for the zoom
2641 */
2642 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2643 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002644
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002645 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002646 // Set pivotY so that at the starting zoom factor, the view is partially
2647 // visible. Modifying initialHeightFactor changes how much of the view is
2648 // initially showing, and hence the perceived angle from which the view enters.
Winson Chung785d2eb2011-04-14 16:08:02 -07002649 if (state == State.ALL_APPS || state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002650 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002651 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002652 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002653 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002654 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002655 }
2656 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002657
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002658 /**
2659 * Zoom the camera out from the workspace to reveal 'toView'.
2660 * Assumes that the view to show is anchored at either the very top or very bottom
2661 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002662 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002663 */
Winson Chung20f71112011-04-06 14:22:04 -07002664 private void cameraZoomOut(State toState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002665 final Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002666 final boolean toAllApps = (toState == State.ALL_APPS)
2667 || (toState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002668
Winson Chung785d2eb2011-04-14 16:08:02 -07002669 final int duration = (toAllApps ?
2670 res.getInteger(R.integer.config_appsCustomizeZoomInTime) :
2671 res.getInteger(R.integer.config_customizeZoomInTime));
2672 final int fadeDuration = (toAllApps ?
2673 res.getInteger(R.integer.config_appsCustomizeFadeInTime) :
2674 res.getInteger(R.integer.config_customizeFadeInTime));
Adam Cohenf16e5712011-01-13 13:31:45 -08002675
2676 final float scale = toAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002677 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002678 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2679
Winson Chung785d2eb2011-04-14 16:08:02 -07002680 View tmpView;
2681 if (toAllApps) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002682 tmpView = (LauncherApplication.isScreenLarge())
Winson Chung785d2eb2011-04-14 16:08:02 -07002683 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2684 } else {
2685 tmpView = mHomeCustomizationDrawer;
2686 }
2687 final View toView = tmpView;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002688
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002689 setPivotsForZoom(toView, toState, scale);
2690
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002691 if (toAllApps) {
Winson Chung20f71112011-04-06 14:22:04 -07002692 if (!springLoaded) {
2693 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2694
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002695 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002696 // Everytime we launch into AllApps, we reset the successful drop flag which
2697 // controls when it should hide/show the mini workspaces
2698 mAllAppsPagedView.resetSuccessfulDropFlag();
2699 }
Winson Chung20f71112011-04-06 14:22:04 -07002700 } else {
2701 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
2702 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002703 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002704 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002705 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002706
2707 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002708 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002709 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002710 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2711 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002712 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002713 toView.setFastScaleX(a * scale + b * 1f);
2714 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002715 }
2716 });
Adam Cohen61033d32010-11-15 18:29:44 -08002717
Adam Cohenf16e5712011-01-13 13:31:45 -08002718 if (toAllApps) {
Winson Chung4e6a9762011-05-09 11:56:34 -07002719 toView.setVisibility(View.VISIBLE);
Michael Jurka2763be32011-02-24 11:19:57 -08002720 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002721 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002722 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002723 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2724 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002725 // don't need to invalidate because we do so above
2726 toView.setFastAlpha(a * 0f + b * 1f);
2727 }
2728 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002729 alphaAnim.start();
2730 }
2731
Michael Jurkaabded662011-03-04 12:06:57 -08002732 if (toView instanceof LauncherTransitionable) {
2733 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002734 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002735 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002736 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002737 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002738 // Prepare the position
2739 toView.setTranslationX(0.0f);
2740 toView.setTranslationY(0.0f);
2741 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002742 toView.bringToFront();
Adam Cohenf16e5712011-01-13 13:31:45 -08002743 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002744 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002745 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002746 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002747 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002748 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002749 // If we don't set the final scale values here, if this animation is cancelled
2750 // it will have the wrong scale value and subsequent cameraPan animations will
2751 // not fix that
2752 toView.setScaleX(1.0f);
2753 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002754 if (toView instanceof LauncherTransitionable) {
2755 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002756 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002757 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002758 });
2759
Chet Haaseb1254a62010-09-07 13:35:00 -07002760 AnimatorSet toolbarHideAnim = new AnimatorSet();
2761 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002762 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2763
2764 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002765 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002766
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002767 if (mStateAnimation != null) mStateAnimation.cancel();
2768 mStateAnimation = new AnimatorSet();
2769 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2770 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002771
2772 // Show the new toolbar buttons just as the main animation is ending
2773 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002774 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2775 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002776 } else {
2777 toView.setTranslationX(0.0f);
2778 toView.setTranslationY(0.0f);
2779 toView.setScaleX(1.0f);
2780 toView.setScaleY(1.0f);
2781 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002782 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002783 if (toView instanceof LauncherTransitionable) {
2784 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2785 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2786 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002787 hideAndShowToolbarButtons(toState, null, null);
2788 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002789 }
2790
2791 /**
2792 * Zoom the camera back into the workspace, hiding 'fromView'.
2793 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002794 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2795 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002796 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08002797 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002798 Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -07002799 final boolean fromAllApps = (fromState == State.ALL_APPS)
2800 || (fromState == State.APPS_CUSTOMIZE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002801
2802 int duration = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002803 res.getInteger(R.integer.config_appsCustomizeZoomOutTime) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002804 res.getInteger(R.integer.config_customizeZoomOutTime);
2805
Michael Jurka742574b2011-02-02 23:51:01 -08002806 final float scaleFactor = fromAllApps ?
Winson Chung785d2eb2011-04-14 16:08:02 -07002807 (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) :
Adam Cohenf16e5712011-01-13 13:31:45 -08002808 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2809
Winson Chung785d2eb2011-04-14 16:08:02 -07002810 View tmpView;
2811 if (fromAllApps) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002812 tmpView = (LauncherApplication.isScreenLarge())
Winson Chung785d2eb2011-04-14 16:08:02 -07002813 ? (View) mAllAppsGrid : mAppsCustomizeTabHost;
2814 } else {
2815 tmpView = mHomeCustomizationDrawer;
2816 }
2817 final View fromView = tmpView;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002818
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002819 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002820 mCustomizePagedView.endChoiceMode();
2821 mAllAppsPagedView.endChoiceMode();
2822 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002823
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002824 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002825
Michael Jurkad3ef3062010-11-23 16:23:58 -08002826 if (!springLoaded) {
2827 mWorkspace.unshrink(animated);
2828 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002829 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002830 if (mStateAnimation != null) mStateAnimation.cancel();
2831 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002832
Michael Jurka742574b2011-02-02 23:51:01 -08002833 final float oldScaleX = fromView.getScaleX();
2834 final float oldScaleY = fromView.getScaleY();
2835
2836 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2837 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002838 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2839 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002840 ((View)fromView.getParent()).fastInvalidate();
2841 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2842 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2843 }
2844 });
Michael Jurkaabded662011-03-04 12:06:57 -08002845 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002846 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002847 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002848 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2849 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002850 // don't need to invalidate because we do so above
2851 fromView.setFastAlpha(a * 1f + b * 0f);
2852 }
2853 });
Michael Jurkaabded662011-03-04 12:06:57 -08002854 if (fromView instanceof LauncherTransitionable) {
2855 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002856 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002857 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002858 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002859 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002860 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002861 if (fromView instanceof LauncherTransitionable) {
2862 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002863 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002864 }
2865 });
2866
Chet Haaseb1254a62010-09-07 13:35:00 -07002867 AnimatorSet toolbarHideAnim = new AnimatorSet();
2868 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002869 if (!springLoaded) {
2870 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2871 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002872
Adam Cohen61033d32010-11-15 18:29:44 -08002873 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002874
2875 // Show the new toolbar buttons at the very end of the whole animation
2876 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2877 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002878 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2879 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002880 } else {
2881 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002882 if (fromView instanceof LauncherTransitionable) {
2883 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2884 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2885 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002886 if (!springLoaded) {
2887 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2888 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002889 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002890 }
2891
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002892 void showWorkspace(boolean animated) {
2893 showWorkspace(animated, null);
2894 }
2895
2896 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002897 if (layout != null) {
2898 // always animated, but that's ok since we never specify a layout and
2899 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002900 mWorkspace.unshrink(layout);
2901 } else {
2902 mWorkspace.unshrink(animated);
2903 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002904 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002905 closeAllApps(animated);
2906 } else if (mState == State.CUSTOMIZE) {
2907 hideCustomizationDrawer(animated);
2908 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002909
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002910 // Change the state *after* we've called all the transition code
2911 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002912
Winson Chung5fb63472011-02-02 17:03:37 -08002913 // Resume the auto-advance of widgets
2914 mUserPresent = true;
2915 updateRunning();
2916
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002917 // send an accessibility event to announce the context change
2918 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002919 }
2920
Michael Jurkad3ef3062010-11-23 16:23:58 -08002921 void enterSpringLoadedDragMode(CellLayout layout) {
2922 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -07002923 if (mState == State.ALL_APPS || mState == State.APPS_CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002924 mState = State.ALL_APPS_SPRING_LOADED;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002925 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002926 cameraZoomIn(State.ALL_APPS, true, true);
2927 } else {
2928 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
2929 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002930 } else if (mState == State.CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002931 mState = State.CUSTOMIZE_SPRING_LOADED;
Winson Chung20f71112011-04-06 14:22:04 -07002932 cameraZoomIn(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002933 }/* else {
2934 // we're already in spring loaded mode; don't do anything
2935 }*/
2936 }
2937
2938 void exitSpringLoadedDragMode() {
2939 if (mState == State.ALL_APPS_SPRING_LOADED) {
2940 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002941 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002942 cameraZoomOut(State.ALL_APPS, true, true);
2943 mState = State.ALL_APPS;
2944 } else {
2945 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2946 mState = State.APPS_CUSTOMIZE;
2947 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002948 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2949 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
Winson Chung20f71112011-04-06 14:22:04 -07002950 cameraZoomOut(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002951 mState = State.CUSTOMIZE;
2952 }/* else {
2953 // we're not in spring loaded mode; don't do anything
2954 }*/
2955 }
2956
Winson Chung785d2eb2011-04-14 16:08:02 -07002957 void showAllApps(boolean animated) {
2958 if (mState != State.WORKSPACE) return;
Michael Jurkaa2eb1702011-05-12 14:57:05 -07002959 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002960 cameraZoomOut(State.ALL_APPS, animated, false);
2961 ((View) mAllAppsGrid).requestFocus();
2962
2963 // TODO: fade these two too
2964 mDeleteZone.setVisibility(View.GONE);
2965
2966 // Change the state *after* we've called all the transition code
2967 mState = State.ALL_APPS;
2968 } else {
Winson Chung785d2eb2011-04-14 16:08:02 -07002969 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
Winson Chung55b65502011-05-26 12:03:43 -07002970 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002971
2972 // Change the state *after* we've called all the transition code
2973 mState = State.APPS_CUSTOMIZE;
2974 }
2975
2976 // Pause the auto-advance of widgets until we are out of AllApps
2977 mUserPresent = false;
2978 updateRunning();
2979
2980 // Send an accessibility event to announce the context change
2981 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2982 }
2983
Joe Onoratob2061212009-11-24 16:13:54 -05002984 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002985 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002986 * - Home from workspace
2987 * - from center screen
2988 * - from other screens
2989 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002990 * - from center screen
2991 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002992 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002993 * - from center screen
2994 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002995 * - Launch app from workspace and quit
2996 * - with back
2997 * - with home
2998 * - Launch app from all apps and quit
2999 * - with back
3000 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003001 * - Go to a screen that's not the default, then all
3002 * apps, and launch and app, and go back
3003 * - with back
3004 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003005 * - On workspace, long press power and go back
3006 * - with back
3007 * - with home
3008 * - On all apps, long press power and go back
3009 * - with back
3010 * - with home
3011 * - On workspace, power off
3012 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003013 * - Launch an app and turn off the screen while in that app
3014 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003015 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003016 * - From all apps
3017 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003018 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3019 * - From all apps
3020 * - From the center workspace
3021 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003022 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003023 void closeAllApps(boolean animated) {
Michael Jurkaa2eb1702011-05-12 14:57:05 -07003024 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003025 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
3026 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung20f71112011-04-06 14:22:04 -07003027 cameraZoomIn(State.ALL_APPS, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003028
3029 // Set focus to the AllApps button
3030 findViewById(R.id.all_apps_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003031 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003032 } else {
3033 if (mState == State.APPS_CUSTOMIZE || mState == State.ALL_APPS_SPRING_LOADED) {
3034 mWorkspace.setVisibility(View.VISIBLE);
3035 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
3036
3037 // Set focus to the AllApps button
3038 findViewById(R.id.all_apps_button).requestFocus();
3039 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003040 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003041 }
3042
Joe Onorato7c312c12009-08-13 21:36:53 -07003043 void lockAllApps() {
3044 // TODO
3045 }
3046
3047 void unlockAllApps() {
3048 // TODO
3049 }
3050
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003051 // Show the customization drawer (only exists in x-large configuration)
3052 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003053 if (mState != State.WORKSPACE) {
3054 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003055 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003056
Winson Chung20f71112011-04-06 14:22:04 -07003057 cameraZoomOut(State.CUSTOMIZE, animated, false);
Michael Jurkaafca9532011-02-16 14:50:35 -08003058
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003059 // Change the state *after* we've called all the transition code
3060 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003061
3062 // Pause the auto-advance of widgets until we are out of Customization drawer
3063 mUserPresent = false;
3064 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003065 }
3066
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003067 // Hide the customization drawer (only exists in x-large configuration)
3068 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003069 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Winson Chung20f71112011-04-06 14:22:04 -07003070 cameraZoomIn(State.CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003071
3072 // Set focus to the customize button
3073 findViewById(R.id.configure_button).requestFocus();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003074 }
3075 }
3076
Patrick Dubroy5f445422011-02-18 14:35:21 -08003077 /**
3078 * Add an item from all apps or customize onto the given workspace screen.
3079 * If layout is null, add to the current screen.
3080 */
3081 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003082 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3083 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003084 } else {
3085 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003086 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003087 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003088
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003089 void onWorkspaceClick(CellLayout layout) {
3090 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003091 }
3092
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003093 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003094 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003095 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003096 // Look for the toolbar icon specified in the activity meta-data
3097 Bundle metaData = packageManager.getActivityInfo(
3098 activityName, PackageManager.GET_META_DATA).metaData;
3099 if (metaData != null) {
3100 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3101 if (iconResId != 0) {
3102 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003103 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003104 }
3105 }
3106 } catch (NameNotFoundException e) {
3107 // Do nothing
3108 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003109 return null;
3110 }
3111
3112 // if successful in getting icon, return it; otherwise, set button to use default drawable
3113 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3114 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3115 TextView button = (TextView) findViewById(buttonId);
3116 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3117
3118 // If we were unable to find the icon via the meta-data, use a generic one
3119 if (toolbarIcon == null) {
3120 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3121 return null;
3122 } else {
3123 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3124 return toolbarIcon.getConstantState();
3125 }
3126 }
3127
3128 // if successful in getting icon, return it; otherwise, set button to use default drawable
3129 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3130 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3131 ImageView button = (ImageView) findViewById(buttonId);
3132 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3133
Michael Jurka0423dcf2010-10-05 14:56:18 -07003134 // If we were unable to find the icon via the meta-data, use a generic one
3135 if (toolbarIcon == null) {
3136 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003137 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003138 } else {
3139 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003140 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003141 }
3142 }
3143
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003144 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3145 TextView button = (TextView) findViewById(buttonId);
3146 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3147 }
3148
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003149 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003150 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003151 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003152 }
3153
Michael Jurka0423dcf2010-10-05 14:56:18 -07003154 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07003155 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
3156 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07003157
Winson Chung1cad91e2011-05-25 17:41:01 -07003158 final SearchManager searchManager =
3159 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3160 ComponentName activityName = searchManager.getGlobalSearchActivity();
3161 if (activityName != null) {
3162 // In landscape mode on the Phone UI, we only have enough space to show the magnifying
3163 // glass icon
3164 boolean iconLoaded = false;
3165 if (!LauncherApplication.isScreenLarge()) {
3166 // TODO-APPS_CUSTOMIZE: Remove when the QSB fixes itself?
3167 int orientation = getResources().getConfiguration().orientation;
3168 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
3169 searchButton.setImageResource(R.drawable.ic_generic_search);
3170 iconLoaded = true;
3171 sGlobalSearchIcon = null;
3172 }
3173 }
3174 if (!iconLoaded) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003175 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003176 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003177 }
Winson Chung1cad91e2011-05-25 17:41:01 -07003178 searchButton.setVisibility(View.VISIBLE);
3179 searchDivider.setVisibility(View.VISIBLE);
3180 } else {
3181 searchButton.setVisibility(View.GONE);
3182 searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003183 }
3184 }
3185
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003186 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003187 updateButtonWithDrawable(R.id.search_button, d);
3188 }
3189
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003190 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07003191 final View searchDivider = findViewById(R.id.search_divider);
3192 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003193
Winson Chung1cad91e2011-05-25 17:41:01 -07003194 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3195 ComponentName activityName = intent.resolveActivity(getPackageManager());
3196 if (activityName != null) {
3197 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
3198 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3199 searchDivider.setVisibility(View.VISIBLE);
3200 voiceButton.setVisibility(View.VISIBLE);
3201 } else {
3202 searchDivider.setVisibility(View.GONE);
3203 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003204 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003205 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003206
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003207 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003208 updateButtonWithDrawable(R.id.voice_button, d);
3209 }
3210
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003211 /**
3212 * Sets the app market icon (shown when all apps is visible on x-large screens)
3213 */
3214 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003215 final View marketButton = findViewById(R.id.market_button);
3216 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3217 // Find the app market activity by resolving an intent.
3218 // (If multiple app markets are installed, it will return the ResolverActivity.)
3219 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003220 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003221 mAppMarketIntent = intent;
3222 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
3223 R.id.market_button, activityName, R.drawable.app_market_generic);
3224 marketButton.setVisibility(View.VISIBLE);
3225 } else {
3226 // We should hide and disable the view so that we don't try and restore the visibility
3227 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3228 marketButton.setVisibility(View.GONE);
3229 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003230 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003231 }
3232
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003233 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003234 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003235 }
3236
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003237 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003238 * Displays the shortcut creation dialog and launches, if necessary, the
3239 * appropriate activity.
3240 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003241 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003242 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3243 DialogInterface.OnShowListener {
3244
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003245 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003246
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003247 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003248 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003249
Winson Chung55b65502011-05-26 12:03:43 -07003250 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
3251 AlertDialog.THEME_HOLO_DARK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003252 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003253 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003255 AlertDialog dialog = builder.create();
3256 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003257 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003258 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003260 return dialog;
3261 }
3262
3263 public void onCancel(DialogInterface dialog) {
3264 mWaitingForResult = false;
3265 cleanup();
3266 }
3267
Romain Guycbb89e42009-06-08 15:52:54 -07003268 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07003269 mWaitingForResult = false;
3270 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003271 }
3272
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003273 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003274 try {
3275 dismissDialog(DIALOG_CREATE_SHORTCUT);
3276 } catch (Exception e) {
3277 // An exception is thrown if the dialog is not visible, which is fine
3278 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003279 }
3280
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003281 /**
3282 * Handle the action clicked in the "Add to home" dialog.
3283 */
3284 public void onClick(DialogInterface dialog, int which) {
3285 Resources res = getResources();
3286 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003287
Winson Chung55b65502011-05-26 12:03:43 -07003288 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
3289 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003290 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003291 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003292 break;
3293 }
Winson Chung55b65502011-05-26 12:03:43 -07003294 case AddAdapter.ITEM_APPLICATION: {
3295 if (mAppsCustomizeTabHost != null) {
3296 mAppsCustomizeTabHost.selectAppsTab();
3297 }
3298 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003299 break;
3300 }
Winson Chung55b65502011-05-26 12:03:43 -07003301 case AddAdapter.ITEM_APPWIDGET: {
3302 if (mAppsCustomizeTabHost != null) {
3303 mAppsCustomizeTabHost.selectWidgetsTab();
3304 }
3305 showAllApps(true);
3306 break;
3307 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003308 case AddAdapter.ITEM_WALLPAPER: {
3309 startWallpaper();
3310 break;
3311 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003312 }
3313 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003314
3315 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003316 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003317 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003318 }
3319
3320 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003321 * Receives notifications when applications are added/removed.
3322 */
3323 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3324 @Override
3325 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003326 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003327 String reason = intent.getStringExtra("reason");
3328 if (!"homekey".equals(reason)) {
3329 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003330 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003331 animate = false;
3332 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003333 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003334 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003335 }
3336 }
3337
3338 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003339 * Receives notifications whenever the appwidgets are reset.
3340 */
3341 private class AppWidgetResetObserver extends ContentObserver {
3342 public AppWidgetResetObserver() {
3343 super(new Handler());
3344 }
3345
3346 @Override
3347 public void onChange(boolean selfChange) {
3348 onAppWidgetReset();
3349 }
3350 }
3351
3352 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003353 * If the activity is currently paused, signal that we need to re-run the loader
3354 * in onResume.
3355 *
3356 * This needs to be called from incoming places where resources might have been loaded
3357 * while we are paused. That is becaues the Configuration might be wrong
3358 * when we're not running, and if it comes back to what it was when we
3359 * were paused, we are not restarted.
3360 *
3361 * Implementation of the method from LauncherModel.Callbacks.
3362 *
3363 * @return true if we are currently paused. The caller might be able to
3364 * skip some work in that case since we will come back again.
3365 */
3366 public boolean setLoadOnResume() {
3367 if (mPaused) {
3368 Log.i(TAG, "setLoadOnResume");
3369 mOnResumeNeedsLoad = true;
3370 return true;
3371 } else {
3372 return false;
3373 }
3374 }
3375
3376 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003377 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003378 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003379 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003380 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003381 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003382 } else {
3383 return SCREEN_COUNT / 2;
3384 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003385 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003386
Winson Chunga12a2502010-12-20 14:41:35 -08003387 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003388 mAllAppsPagedView = view;
3389 }
3390
Joe Onorato9c1289c2009-08-17 11:03:03 -04003391 /**
3392 * Refreshes the shortcuts shown on the workspace.
3393 *
3394 * Implementation of the method from LauncherModel.Callbacks.
3395 */
3396 public void startBinding() {
3397 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003398
3399 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003400 int count = workspace.getChildCount();
3401 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003402 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003403 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3404 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003405 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003406
Joe Onorato9c1289c2009-08-17 11:03:03 -04003407 if (DEBUG_USER_INTERFACE) {
3408 android.widget.Button finishButton = new android.widget.Button(this);
3409 finishButton.setText("Finish");
3410 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3411
3412 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3413 public void onClick(View v) {
3414 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003415 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003416 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003417 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003418
3419 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3420 // mDesktopItems -> AppWidgetInfo -> hostView).
3421 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003422 }
3423
3424 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003425 * Bind the items start-end from the list.
3426 *
3427 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003428 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003429 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3430
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003431 setLoadOnResume();
3432
Joe Onorato9c1289c2009-08-17 11:03:03 -04003433 final Workspace workspace = mWorkspace;
3434
3435 for (int i=start; i<end; i++) {
3436 final ItemInfo item = shortcuts.get(i);
3437 mDesktopItems.add(item);
3438 switch (item.itemType) {
3439 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3440 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003441 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003442 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3443 false);
3444 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003445 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003446 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003447 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003448 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003449 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3450 false);
3451 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003452 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003453 }
3454
Joe Onorato9c1289c2009-08-17 11:03:03 -04003455 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003456 }
3457
Joe Onorato9c1289c2009-08-17 11:03:03 -04003458 /**
3459 * Implementation of the method from LauncherModel.Callbacks.
3460 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003461 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003462 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003463 sFolders.clear();
3464 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003465 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003466
3467 /**
3468 * Add the views for a widget to the workspace.
3469 *
3470 * Implementation of the method from LauncherModel.Callbacks.
3471 */
3472 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003473 setLoadOnResume();
3474
Daniel Sandler843e8602010-06-07 14:59:01 -04003475 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3476 if (DEBUG_WIDGETS) {
3477 Log.d(TAG, "bindAppWidget: " + item);
3478 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003479 final Workspace workspace = mWorkspace;
3480
3481 final int appWidgetId = item.appWidgetId;
3482 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003483 if (DEBUG_WIDGETS) {
3484 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3485 }
3486
Joe Onorato9c1289c2009-08-17 11:03:03 -04003487 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3488
Joe Onorato9c1289c2009-08-17 11:03:03 -04003489 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3490 item.hostView.setTag(item);
3491
3492 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3493 item.cellY, item.spanX, item.spanY, false);
3494
Adam Cohended9f8d2010-11-03 13:25:16 -07003495 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3496
Joe Onorato9c1289c2009-08-17 11:03:03 -04003497 workspace.requestLayout();
3498
3499 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003500
3501 if (DEBUG_WIDGETS) {
3502 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3503 + (SystemClock.uptimeMillis()-start) + "ms");
3504 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003505 }
3506
3507 /**
3508 * Callback saying that there aren't any more items to bind.
3509 *
3510 * Implementation of the method from LauncherModel.Callbacks.
3511 */
3512 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003513 setLoadOnResume();
3514
Joe Onorato9c1289c2009-08-17 11:03:03 -04003515 if (mSavedState != null) {
3516 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003517 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003518 }
3519
Adam Cohendf2cc412011-04-27 16:56:57 -07003520 final long[] folders = mSavedState.getLongArray(RUNTIME_STATE_FOLDERS);
3521 if (folders != null) {
3522 for (long folderId : folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003523 final FolderInfo info = sFolders.get(folderId);
Adam Cohena9cf38f2011-05-02 15:36:58 -07003524 final FolderIcon folderIcon = (FolderIcon)
3525 mWorkspace.getViewForTag(info);
3526 if (folderIcon != null) {
3527 openFolder(folderIcon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003528 }
3529 }
3530 final Folder openFolder = mWorkspace.getOpenFolder();
3531 if (openFolder != null) {
3532 openFolder.requestFocus();
3533 }
3534 }
3535
Joe Onorato9c1289c2009-08-17 11:03:03 -04003536 mSavedState = null;
3537 }
3538
3539 if (mSavedInstanceState != null) {
3540 super.onRestoreInstanceState(mSavedInstanceState);
3541 mSavedInstanceState = null;
3542 }
3543
Winson Chung32bb5e52011-01-31 14:51:56 -08003544 // Workaround a bug that occurs when rotating the device while the customization mode is
3545 // open, we trigger a new layout on all the CellLayout children.
Michael Jurkaa2eb1702011-05-12 14:57:05 -07003546 if (LauncherApplication.isScreenLarge() && (mState == State.CUSTOMIZE)) {
Winson Chung32bb5e52011-01-31 14:51:56 -08003547 final int childCount = mWorkspace.getChildCount();
3548 for (int i = 0; i < childCount; ++i) {
3549 mWorkspace.getChildAt(i).requestLayout();
3550 }
3551 }
3552
Joe Onorato9c1289c2009-08-17 11:03:03 -04003553 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003554
3555 // If we received the result of any pending adds while the loader was running (e.g. the
3556 // widget configuration forced an orientation change), process them now.
3557 for (int i = 0; i < sPendingAddList.size(); i++) {
3558 completeAdd(sPendingAddList.get(i));
3559 }
3560 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003561 }
3562
3563 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003564 * Updates the icons on the launcher that are affected by changes to the package list
3565 * on the device.
3566 */
3567 private void updateIconsAffectedByPackageManagerChanges() {
3568 updateAppMarketIcon();
3569 updateGlobalSearchIcon();
3570 updateVoiceSearchIcon();
3571 }
3572
3573 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003574 * Add the icons for all apps.
3575 *
3576 * Implementation of the method from LauncherModel.Callbacks.
3577 */
3578 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003579 if (mAllAppsGrid != null) {
3580 mAllAppsGrid.setApps(apps);
3581 }
3582 if (mAppsCustomizeContent != null) {
3583 mAppsCustomizeContent.setApps(apps);
3584 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003585 if (mCustomizePagedView != null) {
3586 mCustomizePagedView.setApps(apps);
3587 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003588 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589 }
3590
3591 /**
3592 * A package was installed.
3593 *
3594 * Implementation of the method from LauncherModel.Callbacks.
3595 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003596 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003597 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chung785d2eb2011-04-14 16:08:02 -07003599 if (mAllAppsGrid != null) {
3600 mAllAppsGrid.addApps(apps);
3601 }
3602 if (mAppsCustomizeContent != null) {
3603 mAppsCustomizeContent.addApps(apps);
3604 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003605 if (mCustomizePagedView != null) {
3606 mCustomizePagedView.addApps(apps);
3607 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003608 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003609 }
3610
3611 /**
3612 * A package was updated.
3613 *
3614 * Implementation of the method from LauncherModel.Callbacks.
3615 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003616 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003617 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003618 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003619 if (mWorkspace != null) {
3620 mWorkspace.updateShortcuts(apps);
3621 }
3622 if (mAllAppsGrid != null) {
3623 mAllAppsGrid.updateApps(apps);
3624 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003625 if (mAppsCustomizeContent != null) {
3626 mAppsCustomizeContent.updateApps(apps);
3627 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003628 if (mCustomizePagedView != null) {
3629 mCustomizePagedView.updateApps(apps);
3630 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003631 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003632 }
3633
3634 /**
3635 * A package was uninstalled.
3636 *
3637 * Implementation of the method from LauncherModel.Callbacks.
3638 */
Joe Onorato36115782010-06-17 13:28:48 -04003639 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003640 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003641 if (permanent) {
3642 mWorkspace.removeItems(apps);
3643 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003644 if (mAllAppsGrid != null) {
3645 mAllAppsGrid.removeApps(apps);
3646 }
3647 if (mAppsCustomizeContent != null) {
3648 mAppsCustomizeContent.removeApps(apps);
3649 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003650 if (mCustomizePagedView != null) {
3651 mCustomizePagedView.removeApps(apps);
3652 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003653 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003654 }
Joe Onoratobe386092009-11-17 17:32:16 -08003655
3656 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003657 * A number of packages were updated.
3658 */
3659 public void bindPackagesUpdated() {
3660 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003661 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003662 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003663 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003664 if (mAppsCustomizeContent != null) {
3665 mAppsCustomizeContent.onPackagesUpdated();
3666 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003667 }
3668
Winson Chung400438b2011-01-16 17:53:48 -08003669 private int mapConfigurationOriActivityInfoOri(int configOri) {
3670 final Display d = getWindowManager().getDefaultDisplay();
3671 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3672 switch (d.getRotation()) {
3673 case Surface.ROTATION_0:
3674 case Surface.ROTATION_180:
3675 // We are currently in the same basic orientation as the natural orientation
3676 naturalOri = configOri;
3677 break;
3678 case Surface.ROTATION_90:
3679 case Surface.ROTATION_270:
3680 // We are currently in the other basic orientation to the natural orientation
3681 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3682 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3683 break;
3684 }
3685
3686 int[] oriMap = {
3687 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3688 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3689 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3690 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3691 };
3692 // Since the map starts at portrait, we need to offset if this device's natural orientation
3693 // is landscape.
3694 int indexOffset = 0;
3695 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3696 indexOffset = 1;
3697 }
3698 return oriMap[(d.getRotation() + indexOffset) % 4];
3699 }
3700 public void lockScreenOrientation() {
3701 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3702 .getConfiguration().orientation));
3703 }
3704 public void unlockScreenOrientation() {
3705 mHandler.postDelayed(new Runnable() {
3706 public void run() {
3707 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3708 }
3709 }, mRestoreScreenOrientationDelay);
3710 }
3711
Winson Chung80baf5a2010-08-09 16:03:15 -07003712 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003713 * Prints out out state for debugging.
3714 */
3715 public void dumpState() {
3716 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003717 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003718 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3719 Log.d(TAG, "mRestoring=" + mRestoring);
3720 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3721 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3722 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003723 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003724 mModel.dumpState();
Winson Chung785d2eb2011-04-14 16:08:02 -07003725 if (mAllAppsGrid != null) {
3726 mAllAppsGrid.dumpState();
3727 }
3728 if (mAppsCustomizeContent != null) {
3729 mAppsCustomizeContent.dumpState();
3730 }
Joe Onoratobe386092009-11-17 17:32:16 -08003731 Log.d(TAG, "END launcher2 dump state");
3732 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003733}
Michael Jurka2763be32011-02-24 11:19:57 -08003734
Michael Jurkaabded662011-03-04 12:06:57 -08003735interface LauncherTransitionable {
3736 void onLauncherTransitionStart(Animator animation);
3737 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003738}