blob: b7d4c8bc97da254aeae9164dd19bf3df5ec33f58 [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
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.Dialog;
31import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070037import android.content.ClipData;
38import android.content.ClipDescription;
Michael Jurkae326f182011-11-21 14:05:46 -080039import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040040import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080041import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Context;
43import android.content.DialogInterface;
44import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070045import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070046import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070053import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040055import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070056import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020058import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080059import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070060import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040061import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080062import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070063import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080064import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.text.Selection;
66import android.text.SpannableStringBuilder;
67import android.text.TextUtils;
68import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070069import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080070import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080071import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.view.KeyEvent;
73import android.view.LayoutInflater;
74import android.view.Menu;
75import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070076import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080077import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080079import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070080import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080081import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080082import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070083import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070085import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070086import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080087import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070089import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070091import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070092import android.widget.TextView;
93import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070094
Adam Cohendf2cc412011-04-27 16:56:57 -070095import com.android.common.Search;
96import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070097import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080098
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.DataInputStream;
100import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700101import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.FileNotFoundException;
103import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700104import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.util.ArrayList;
106import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108/**
109 * Default launcher application.
110 */
Michael Jurka946ad472010-07-09 18:05:18 -0700111public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700112 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
113 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800114 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700115 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116
Joe Onorato9c1289c2009-08-17 11:03:03 -0400117 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400118 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700119
Winson Chung70d72102011-08-12 11:24:35 -0700120 private static final int MENU_GROUP_WALLPAPER = 1;
121 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
122 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700123 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
124 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700127 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 private static final int REQUEST_PICK_APPLICATION = 6;
129 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700130 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700131 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
134
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800135 static final int SCREEN_COUNT = 5;
136 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Joe Onorato7c312c12009-08-13 21:36:53 -0700138 static final int DIALOG_CREATE_SHORTCUT = 1;
139 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Romain Guy98d01652009-06-30 16:21:04 -0700141 private static final String PREFERENCES = "launcher.preferences";
Adam Cohen23a4d302011-12-01 17:26:44 -0800142 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
144 // Type: int
145 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700146 // Type: int
147 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700149 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
150 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
152 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700153 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700155 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: boolean
157 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
158 // Type: long
159 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
160
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700161 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
162
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700163 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700164 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700165 private State mState = State.WORKSPACE;
166 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800167 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700168
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700170 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
171 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700172 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700173 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800176 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800178 private final BroadcastReceiver mCloseSystemDialogsReceiver
179 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800180 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private LayoutInflater mInflater;
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800185 private View mQsbDivider;
186 private View mDockDivider;
187 private DragLayer mDragLayer;
188 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700189
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700190 private AppWidgetManager mAppWidgetManager;
191 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700192
Michael Jurkac9d95c52011-08-29 14:03:34 -0700193 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700194 private int[] mTmpAddItemCellCoordinates = new int[2];
195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 private FolderInfo mFolderInfo;
197
Winson Chung3d503fb2011-07-13 17:25:49 -0700198 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800199 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700200
Winson Chungc51db6a2011-10-05 11:44:49 -0700201 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700202 private AppsCustomizeTabHost mAppsCustomizeTabHost;
203 private AppsCustomizePagedView mAppsCustomizeContent;
204 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private Bundle mSavedState;
207
208 private SpannableStringBuilder mDefaultKeySsb = null;
209
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 private boolean mWorkspaceLoading = true;
211
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800212 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 private boolean mRestoring;
214 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700215 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
217 private Bundle mSavedInstanceState;
218
Joe Onorato9c1289c2009-08-17 11:03:03 -0400219 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800220 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800221 private boolean mUserPresent = true;
222 private boolean mVisible = false;
223 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400224
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700225 private static LocaleConfiguration sLocaleConfiguration = null;
226
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700227 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700228
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700229 private Intent mAppMarketIntent = null;
230
Adam Cohended9f8d2010-11-03 13:25:16 -0700231 // Related to the auto-advancing of widgets
232 private final int ADVANCE_MSG = 1;
233 private final int mAdvanceInterval = 20000;
234 private final int mAdvanceStagger = 250;
235 private long mAutoAdvanceSentTime;
236 private long mAutoAdvanceTimeLeft = -1;
237 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
238 new HashMap<View, AppWidgetProviderInfo>();
239
Winson Chung400438b2011-01-16 17:53:48 -0800240 // Determines how long to wait after a rotation before restoring the screen orientation to
241 // match the sensor state.
242 private final int mRestoreScreenOrientationDelay = 500;
243
Michael Jurka4ef207b2010-11-29 17:05:45 -0800244 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700245 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
246 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
247 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800248
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700249 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohened66b2b2012-01-23 17:28:51 -0800250 PendingAddWidgetInfo mWidgetBeingConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700251
Winson Chung46353de2012-02-16 14:05:10 -0800252 // We only want to get the SharedPreferences once since it does an FS stat each time we get
253 // it from the context.
254 private SharedPreferences mSharedPrefs;
255
Adam Cohen2801caf2011-05-13 20:57:39 -0700256
Michael Jurkaddd62e92011-02-16 17:49:14 -0800257 private BubbleTextView mWaitingForResume;
258
Michael Jurkac1f5d262011-09-30 19:32:27 -0700259 private Runnable mBuildLayersRunnable = new Runnable() {
260 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700261 if (mWorkspace != null) {
262 mWorkspace.buildPageHardwareLayers();
263 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700264 }
265 };
266
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800267 private static ArrayList<PendingAddArguments> sPendingAddList
268 = new ArrayList<PendingAddArguments>();
269
270 private static class PendingAddArguments {
271 int requestCode;
272 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700273 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800274 int screen;
275 int cellX;
276 int cellY;
277 }
278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 @Override
280 protected void onCreate(Bundle savedInstanceState) {
281 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800282 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chung46353de2012-02-16 14:05:10 -0800283 mSharedPrefs = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800284 mModel = app.setLauncher(this);
285 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400286 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700288
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700289 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700290 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
291 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200294 android.os.Debug.startMethodTracing(
295 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 }
297
298 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 setContentView(R.layout.launcher);
300 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700301 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800303 registerContentObservers();
304
Joe Onorato7c312c12009-08-13 21:36:53 -0700305 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700306
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 mSavedState = savedInstanceState;
308 restoreState(mSavedState);
309
Winson Chunga12a2502010-12-20 14:41:35 -0800310 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700311 if (mAppsCustomizeContent != null) {
312 mAppsCustomizeContent.onPackagesUpdated();
313 }
Winson Chunga12a2502010-12-20 14:41:35 -0800314
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 if (PROFILE_STARTUP) {
316 android.os.Debug.stopMethodTracing();
317 }
318
319 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400320 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 }
322
Michael Jurkac57b7a82011-08-09 22:02:20 -0700323 if (!mModel.isAllAppsLoaded()) {
324 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
325 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
326 }
327
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 // For handling default keys
329 mDefaultKeySsb = new SpannableStringBuilder();
330 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800331
332 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
333 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800334
Winson Chungc51db6a2011-10-05 11:44:49 -0700335 boolean searchVisible = false;
336 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800337 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700338 int coi = getCurrentOrientationIndexForGlobalIcons();
339 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
340 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700341 updateAppMarketIcon();
342 searchVisible = updateGlobalSearchIcon();
343 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700344 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700345 if (sGlobalSearchIcon[coi] != null) {
346 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700347 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700348 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700349 if (sVoiceSearchIcon[coi] != null) {
350 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700351 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700352 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700353 if (sAppMarketIcon[coi] != null) {
354 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800355 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700356 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700357
Adam Cohen23a4d302011-12-01 17:26:44 -0800358 final String forceEnableRotation =
359 SystemProperties.get(FORCE_ENABLE_ROTATION_PROPERTY, "false");
360
Adam Cohen446e9402011-09-15 18:21:21 -0700361 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohen23a4d302011-12-01 17:26:44 -0800362 if (LauncherApplication.isScreenLarge() || "true".equalsIgnoreCase(forceEnableRotation)) {
Adam Cohen446e9402011-09-15 18:21:21 -0700363 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
364 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 }
Romain Guycbb89e42009-06-08 15:52:54 -0700366
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700368 if (sLocaleConfiguration == null) {
369 new AsyncTask<Void, Void, LocaleConfiguration>() {
370 @Override
371 protected LocaleConfiguration doInBackground(Void... unused) {
372 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
373 readConfiguration(Launcher.this, localeConfiguration);
374 return localeConfiguration;
375 }
376
377 @Override
378 protected void onPostExecute(LocaleConfiguration result) {
379 sLocaleConfiguration = result;
380 checkForLocaleChange(); // recursive, but now with a locale configuration
381 }
382 }.execute();
383 return;
384 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700385
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 final Configuration configuration = getResources().getConfiguration();
387
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700388 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 final String locale = configuration.locale.toString();
390
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700391 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 final int mcc = configuration.mcc;
393
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700394 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 final int mnc = configuration.mnc;
396
Romain Guy84f296c2009-11-04 15:00:44 -0800397 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398
Romain Guy84f296c2009-11-04 15:00:44 -0800399 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700400 sLocaleConfiguration.locale = locale;
401 sLocaleConfiguration.mcc = mcc;
402 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700403
Joe Onorato0589f0f2010-02-08 13:44:00 -0800404 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700405
406 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
407 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700408 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700409 public void run() {
410 writeConfiguration(Launcher.this, localeConfiguration);
411 }
412 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700413 }
414 }
415
416 private static class LocaleConfiguration {
417 public String locale;
418 public int mcc = -1;
419 public int mnc = -1;
420 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700421
Romain Guy98d01652009-06-30 16:21:04 -0700422 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
423 DataInputStream in = null;
424 try {
425 in = new DataInputStream(context.openFileInput(PREFERENCES));
426 configuration.locale = in.readUTF();
427 configuration.mcc = in.readInt();
428 configuration.mnc = in.readInt();
429 } catch (FileNotFoundException e) {
430 // Ignore
431 } catch (IOException e) {
432 // Ignore
433 } finally {
434 if (in != null) {
435 try {
436 in.close();
437 } catch (IOException e) {
438 // Ignore
439 }
440 }
441 }
442 }
443
444 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
445 DataOutputStream out = null;
446 try {
447 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
448 out.writeUTF(configuration.locale);
449 out.writeInt(configuration.mcc);
450 out.writeInt(configuration.mnc);
451 out.flush();
452 } catch (FileNotFoundException e) {
453 // Ignore
454 } catch (IOException e) {
455 //noinspection ResultOfMethodCallIgnored
456 context.getFileStreamPath(PREFERENCES).delete();
457 } finally {
458 if (out != null) {
459 try {
460 out.close();
461 } catch (IOException e) {
462 // Ignore
463 }
464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465 }
466 }
467
Adam Cohen716b51e2011-06-30 12:09:54 -0700468 public DragLayer getDragLayer() {
469 return mDragLayer;
470 }
471
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 static int getScreen() {
473 synchronized (sLock) {
474 return sScreen;
475 }
476 }
477
478 static void setScreen(int screen) {
479 synchronized (sLock) {
480 sScreen = screen;
481 }
482 }
483
Winson Chung557d6ed2011-07-08 15:34:52 -0700484 /**
485 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
486 * a configuration step, this allows the proper animations to run after other transitions.
487 */
488 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700489 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800490 switch (args.requestCode) {
491 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700492 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
493 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800494 break;
495 case REQUEST_PICK_SHORTCUT:
496 processShortcut(args.intent);
497 break;
498 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700499 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
500 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700501 result = true;
502 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800503 case REQUEST_PICK_APPWIDGET:
504 addAppWidgetFromPick(args.intent);
505 break;
506 case REQUEST_CREATE_APPWIDGET:
507 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800508 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700509 result = true;
510 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800511 case REQUEST_PICK_WALLPAPER:
512 // We just wanted the activity result here so we can clear mWaitingForResult
513 break;
514 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700515 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800516 }
517
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800519 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700520 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800521 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
522 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700523 mWaitingForResult = false;
524
Adam Cohened66b2b2012-01-23 17:28:51 -0800525 // We have special handling for widgets
526 if (isWidgetDrop) {
527 int appWidgetId = data != null ?
528 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
529 completeTwoStageWidgetDrop(resultCode, appWidgetId);
530 return;
531 }
532
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533 // The pattern used here is that a user PICKs a specific application,
534 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700535
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
537 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700538 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800539 final PendingAddArguments args = new PendingAddArguments();
540 args.requestCode = requestCode;
541 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700542 args.container = mPendingAddInfo.container;
543 args.screen = mPendingAddInfo.screen;
544 args.cellX = mPendingAddInfo.cellX;
545 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800546 if (isWorkspaceLocked()) {
547 sPendingAddList.add(args);
548 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700549 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800552 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700553 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800554 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
555 null);
556 }
557
558 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
559 CellLayout cellLayout = (CellLayout) mWorkspace.getChildAt(mWidgetBeingConfigured.screen);
560 Runnable onCompleteRunnable = null;
561 int animationType = 0;
562
563 if (resultCode == RESULT_OK) {
564 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
565 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
566 mWidgetBeingConfigured.info);
567 mWidgetBeingConfigured.boundWidget = layout;
568 onCompleteRunnable = new Runnable() {
569 @Override
570 public void run() {
571 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
572 mPendingAddInfo.screen, layout, null);
573 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
574 null);
575 }
576 };
577 } else if (resultCode == RESULT_CANCELED) {
578 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
579 onCompleteRunnable = new Runnable() {
580 @Override
581 public void run() {
582 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
583 null);
584 }
585 };
586 }
Adam Cohend41fbf52012-02-16 23:53:59 -0800587 mWorkspace.animateWidgetDrop(mWidgetBeingConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800588 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Adam Cohend41fbf52012-02-16 23:53:59 -0800589 animationType, mWidgetBeingConfigured.boundWidget, true);
Adam Cohen1b36dc32012-02-13 19:27:37 -0800590 mWidgetBeingConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800591 }
592
593 @Override
594 protected void onResume() {
595 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800596 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700597 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400598 mWorkspaceLoading = true;
599 mModel.startLoader(this, true);
600 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700601 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 }
Winson Chunge4e50662012-01-23 14:45:13 -0800603
604 // Reset the pressed state of icons that were locked in the press state while activities
605 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800606 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800607 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800608 mWaitingForResume.setStayPressed(false);
609 }
Winson Chunge4e50662012-01-23 14:45:13 -0800610 if (mAppsCustomizeContent != null) {
611 // Resets the previous all apps icon press state
612 mAppsCustomizeContent.resetDrawableState();
613 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 }
615
616 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700617 protected void onPause() {
618 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700619 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800620 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700621 }
Romain Guycbb89e42009-06-08 15:52:54 -0700622
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700623 @Override
624 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400625 // Flag the loader to stop early before switching
626 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700627 if (mAppsCustomizeContent != null) {
628 mAppsCustomizeContent.surrender();
629 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800630 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700631 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700632
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800633 // We can't hide the IME if it was forced open. So don't bother
634 /*
635 @Override
636 public void onWindowFocusChanged(boolean hasFocus) {
637 super.onWindowFocusChanged(hasFocus);
638
639 if (hasFocus) {
640 final InputMethodManager inputManager = (InputMethodManager)
641 getSystemService(Context.INPUT_METHOD_SERVICE);
642 WindowManager.LayoutParams lp = getWindow().getAttributes();
643 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
644 android.os.Handler()) {
645 protected void onReceiveResult(int resultCode, Bundle resultData) {
646 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
647 }
648 });
649 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
650 }
651 }
652 */
653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 private boolean acceptFilter() {
655 final InputMethodManager inputManager = (InputMethodManager)
656 getSystemService(Context.INPUT_METHOD_SERVICE);
657 return !inputManager.isFullscreenMode();
658 }
659
660 @Override
661 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700662 final int uniChar = event.getUnicodeChar();
663 final boolean handled = super.onKeyDown(keyCode, event);
664 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
665 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
667 keyCode, event);
668 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700669 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700670 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700671 // showSearchDialog()
672 // If there are multiple keystrokes before the search dialog takes focus,
673 // onSearchRequested() will be called for every keystroke,
674 // but it is idempotent, so it's fine.
675 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
677 }
678
Joe Onorato8a9625e2010-01-28 15:55:35 -0800679 // Eat the long press event so the keyboard doesn't come up.
680 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
681 return true;
682 }
683
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 return handled;
685 }
686
Karl Rosaen138a0412009-04-23 19:00:21 -0700687 private String getTypedText() {
688 return mDefaultKeySsb.toString();
689 }
690
691 private void clearTypedText() {
692 mDefaultKeySsb.clear();
693 mDefaultKeySsb.clearSpans();
694 Selection.setSelection(mDefaultKeySsb, 0);
695 }
696
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700698 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
699 * State
700 */
701 private static State intToState(int stateOrdinal) {
702 State state = State.WORKSPACE;
703 final State[] stateValues = State.values();
704 for (int i = 0; i < stateValues.length; i++) {
705 if (stateValues[i].ordinal() == stateOrdinal) {
706 state = stateValues[i];
707 break;
708 }
709 }
710 return state;
711 }
712
713 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 * Restores the previous state, if it exists.
715 *
716 * @param savedState The previous state.
717 */
718 private void restoreState(Bundle savedState) {
719 if (savedState == null) {
720 return;
721 }
722
Michael Jurka883f55b2010-10-21 15:47:14 -0700723 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700724 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800725 showAllApps(false);
726 }
727
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
729 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700730 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 }
732
Winson Chung3d503fb2011-07-13 17:25:49 -0700733 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
734 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700735
Winson Chung3d503fb2011-07-13 17:25:49 -0700736 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
737 mPendingAddInfo.container = pendingAddContainer;
738 mPendingAddInfo.screen = pendingAddScreen;
739 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
740 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 mRestoring = true;
742 }
743
744 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
745 if (renameFolder) {
746 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700747 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 mRestoring = true;
749 }
Winson Chunga12a2502010-12-20 14:41:35 -0800750
Winson Chung785d2eb2011-04-14 16:08:02 -0700751
752 // Restore the AppsCustomize tab
753 if (mAppsCustomizeTabHost != null) {
754 String curTab = savedState.getString("apps_customize_currentTab");
755 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700756 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700757 mAppsCustomizeContent.setContentType(
758 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
759 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700760 mAppsCustomizeContent.loadAssociatedPages(
761 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700762 }
763
Winson Chung5afbf7b2011-07-25 11:53:08 -0700764 int currentIndex = savedState.getInt("apps_customize_currentIndex");
765 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700766 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 }
768
769 /**
770 * Finds all the views we need and configure them properly.
771 */
772 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700773 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400774
Winson Chung4c98d922011-05-31 16:50:48 -0700775 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
776 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800777 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
778 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779
Winson Chung4c98d922011-05-31 16:50:48 -0700780 // Setup the drag layer
781 mDragLayer.setup(this, dragController);
782
Winson Chung3d503fb2011-07-13 17:25:49 -0700783 // Setup the hotseat
784 mHotseat = (Hotseat) findViewById(R.id.hotseat);
785 if (mHotseat != null) {
786 mHotseat.setup(this);
787 }
788
Winson Chung4c98d922011-05-31 16:50:48 -0700789 // Setup the workspace
790 mWorkspace.setHapticFeedbackEnabled(false);
791 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700792 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700793 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700794
Winson Chungf0ea4d32011-06-06 14:27:16 -0700795 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700796 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700797
Winson Chungf0ea4d32011-06-06 14:27:16 -0700798 // Setup AppsCustomize
799 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
800 findViewById(R.id.apps_customize_pane);
801 mAppsCustomizeContent = (AppsCustomizePagedView)
802 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
803 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400804
Michael Jurka5130e402011-10-13 04:55:35 -0700805 // Get the all apps button
806 mAllAppsButton = findViewById(R.id.all_apps_button);
807 if (mAllAppsButton != null) {
808 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
809 @Override
810 public boolean onTouch(View v, MotionEvent event) {
811 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
812 onTouchDownAllAppsButton(v);
813 }
814 return false;
815 }
816 });
817 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700818 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700819 dragController.setDragScoller(mWorkspace);
820 dragController.setScrollView(mDragLayer);
821 dragController.setMoveTarget(mWorkspace);
822 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700823 if (mSearchDropTargetBar != null) {
824 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800825 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 }
827
828 /**
829 * Creates a view representing a shortcut.
830 *
831 * @param info The data structure describing the shortcut.
832 *
833 * @return A View inflated from R.layout.application.
834 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800835 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700837 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 }
839
840 /**
841 * Creates a view representing a shortcut inflated from the specified resource.
842 *
843 * @param layoutResId The id of the XML layout used to create the shortcut.
844 * @param parent The group the shortcut belongs to.
845 * @param info The data structure describing the shortcut.
846 *
847 * @return A View inflated from layoutResId.
848 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800849 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800850 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
851 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 return favorite;
854 }
855
856 /**
857 * Add an application shortcut to the workspace.
858 *
859 * @param data The intent describing the application.
860 * @param cellInfo The position on screen where to create the shortcut.
861 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700862 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700863 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700864 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700865
Adam Cohenfbba09b2011-07-18 21:43:05 -0700866 // First we check if we already know the exact location where we want to add this item.
867 if (cellX >= 0 && cellY >= 0) {
868 cellXY[0] = cellX;
869 cellXY[1] = cellY;
870 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700871 showOutOfSpaceMessage();
872 return;
873 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800875 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800876
Romain Guy73b979d2009-06-09 12:57:21 -0700877 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800878 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800880 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700881 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700882 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800883 } else {
884 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886 }
Romain Guycbb89e42009-06-08 15:52:54 -0700887
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 /**
889 * Add a shortcut to the workspace.
890 *
891 * @param data The intent describing the shortcut.
892 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700894 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
895 int cellY) {
896 int[] cellXY = mTmpAddItemCellCoordinates;
897 int[] touchXY = mPendingAddInfo.dropPos;
898 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700899
Michael Jurkad3ef3062010-11-23 16:23:58 -0800900 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700901
Adam Cohen558baaf2011-08-15 15:22:57 -0700902 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800903 if (info == null) {
904 return;
905 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700906 final View view = createShortcut(info);
907
Adam Cohenfbba09b2011-07-18 21:43:05 -0700908 // First we check if we already know the exact location where we want to add this item.
909 if (cellX >= 0 && cellY >= 0) {
910 cellXY[0] = cellX;
911 cellXY[1] = cellY;
912 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700913
914 // If appropriate, either create a folder or add to an existing folder
915 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
916 true, null,null)) {
917 return;
918 }
919 DragObject dragObject = new DragObject();
920 dragObject.dragInfo = info;
921 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
922 return;
923 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700924 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800925 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700926 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800927 foundCellSpan = (result != null);
928 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700929 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800930 }
931
932 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700933 showOutOfSpaceMessage();
934 return;
935 }
936
Adam Cohen558baaf2011-08-15 15:22:57 -0700937 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938
939 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
941 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943 }
944
Adam Cohenf814aa02011-09-01 13:48:05 -0700945 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
946 if (spanXY == null) {
947 spanXY = new int[2];
948 }
949
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800950 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700951 // We want to account for the extra amount of padding that we are adding to the widget
952 // to ensure that it gets the full amount of space that it has requested
953 int requiredWidth = minWidth + padding.left + padding.right;
954 int requiredHeight = minHeight + padding.top + padding.bottom;
955 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
956 }
957
958 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
959 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
960 }
961
Adam Cohend41fbf52012-02-16 23:53:59 -0800962 int[] getMinSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
Adam Cohencbf47e32011-09-16 17:32:37 -0700963 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
964 }
965
Adam Cohenf814aa02011-09-01 13:48:05 -0700966 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
967 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
968 }
969
Adam Cohend41fbf52012-02-16 23:53:59 -0800970 int[] getMinSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
971 return getSpanForWidget(info.componentName, info.minResizeWidth,
972 info.minResizeHeight, spanXY);
973 }
974
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700976 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700978 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700979 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 */
Adam Cohened66b2b2012-01-23 17:28:51 -0800981 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
982 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
983 if (appWidgetInfo == null) {
984 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
985 }
Romain Guycbb89e42009-06-08 15:52:54 -0700986
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700987 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700988 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700989
Adam Cohend41fbf52012-02-16 23:53:59 -0800990 int[] minSpanXY = getMinSpanForWidget(appWidgetInfo, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700991 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700992
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700994 // We have saved the position to which the widget was dragged-- this really only matters
995 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700996 int[] cellXY = mTmpAddItemCellCoordinates;
997 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -0800998 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700999 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001000 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1001 cellXY[0] = mPendingAddInfo.cellX;
1002 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001003 spanXY[0] = mPendingAddInfo.spanX;
1004 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001005 foundCellSpan = true;
1006 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001007 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001008 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001009 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1010 spanXY[1], cellXY, finalSpan);
1011 spanXY[0] = finalSpan[0];
1012 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001013 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001014 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001015 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001016 }
1017
Michael Jurka0280c3b2010-09-17 15:00:07 -07001018 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001019 if (appWidgetId != -1) {
1020 // Deleting an app widget ID is a void call but writes to disk before returning
1021 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001022 new Thread("deleteAppWidgetId") {
1023 public void run() {
1024 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1025 }
1026 }.start();
1027 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001028 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001029 return;
1030 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001032 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -07001033 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001034 launcherInfo.spanX = spanXY[0];
1035 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001036 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1037 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001040 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041
1042 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001043 if (hostView == null) {
1044 // Perform actual inflation because we're live
1045 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1046 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1047 } else {
1048 // The AppWidgetHostView has already been inflated and instantiated
1049 launcherInfo.hostView = hostView;
1050 }
Romain Guycbb89e42009-06-08 15:52:54 -07001051
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001053 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung3d503fb2011-07-13 17:25:49 -07001054 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001055 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001056
1057 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001059 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 }
Romain Guycbb89e42009-06-08 15:52:54 -07001061
Adam Cohended9f8d2010-11-03 13:25:16 -07001062 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1063 @Override
1064 public void onReceive(Context context, Intent intent) {
1065 final String action = intent.getAction();
1066 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1067 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001068 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001069 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001070
Winson Chungc100e8e2011-08-09 16:02:43 -07001071 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001072 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001073 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1074 mAppsCustomizeTabHost.reset();
1075 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001076 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001077 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1078 mUserPresent = true;
1079 updateRunning();
1080 }
1081 }
1082 };
1083
1084 @Override
1085 public void onAttachedToWindow() {
1086 super.onAttachedToWindow();
1087
1088 // Listen for broadcasts related to user-presence
1089 final IntentFilter filter = new IntentFilter();
1090 filter.addAction(Intent.ACTION_SCREEN_OFF);
1091 filter.addAction(Intent.ACTION_USER_PRESENT);
1092 registerReceiver(mReceiver, filter);
1093
Adam Cohend113e0c2010-11-11 10:48:05 -08001094 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001095 mVisible = true;
1096 }
1097
1098 @Override
1099 public void onDetachedFromWindow() {
1100 super.onDetachedFromWindow();
1101 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001102 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001103
Adam Cohend113e0c2010-11-11 10:48:05 -08001104 if (mAttached) {
1105 unregisterReceiver(mReceiver);
1106 mAttached = false;
1107 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001108 updateRunning();
1109 }
1110
1111 public void onWindowVisibilityChanged(int visibility) {
1112 mVisible = visibility == View.VISIBLE;
1113 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001114 // The following code used to be in onResume, but it turns out onResume is called when
1115 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1116 // is a more appropriate event to handle
1117 if (mVisible) {
1118 mAppsCustomizeTabHost.onWindowVisible();
1119 if (!mWorkspaceLoading) {
1120 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001121 // We want to let Launcher draw itself at least once before we force it to build
1122 // layers on all the workspace pages, so that transitioning to Launcher from other
1123 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1124 // a true draw so we wait until the second preDraw call to be safe
1125 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001126 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001127 // We delay the layer building a bit in order to give
1128 // other message processing a time to run. In particular
1129 // this avoids a delay in hiding the IME if it was
1130 // currently shown, because doing that may involve
1131 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001132 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurka6ee21d22012-02-21 18:20:27 -08001133 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001134 return true;
1135 }
1136 });
1137 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001138 // When Launcher comes back to foreground, a different Activity might be responsible for
1139 // the app market intent, so refresh the icon
1140 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001141 clearTypedText();
1142 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001143 }
1144
1145 private void sendAdvanceMessage(long delay) {
1146 mHandler.removeMessages(ADVANCE_MSG);
1147 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1148 mHandler.sendMessageDelayed(msg, delay);
1149 mAutoAdvanceSentTime = System.currentTimeMillis();
1150 }
1151
1152 private void updateRunning() {
1153 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1154 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1155 mAutoAdvanceRunning = autoAdvanceRunning;
1156 if (autoAdvanceRunning) {
1157 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1158 sendAdvanceMessage(delay);
1159 } else {
1160 if (!mWidgetsToAdvance.isEmpty()) {
1161 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1162 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1163 }
1164 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001165 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001166 }
1167 }
1168 }
1169
1170 private final Handler mHandler = new Handler() {
1171 @Override
1172 public void handleMessage(Message msg) {
1173 if (msg.what == ADVANCE_MSG) {
1174 int i = 0;
1175 for (View key: mWidgetsToAdvance.keySet()) {
1176 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1177 final int delay = mAdvanceStagger * i;
1178 if (v instanceof Advanceable) {
1179 postDelayed(new Runnable() {
1180 public void run() {
1181 ((Advanceable) v).advance();
1182 }
1183 }, delay);
1184 }
1185 i++;
1186 }
1187 sendAdvanceMessage(mAdvanceInterval);
1188 }
1189 }
1190 };
1191
1192 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001193 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001194 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1195 if (v instanceof Advanceable) {
1196 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001197 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001198 updateRunning();
1199 }
1200 }
1201
1202 void removeWidgetToAutoAdvance(View hostView) {
1203 if (mWidgetsToAdvance.containsKey(hostView)) {
1204 mWidgetsToAdvance.remove(hostView);
1205 updateRunning();
1206 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001207 }
1208
Joe Onorato9c1289c2009-08-17 11:03:03 -04001209 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001210 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001211 launcherInfo.hostView = null;
1212 }
1213
Adam Cohended9f8d2010-11-03 13:25:16 -07001214 void showOutOfSpaceMessage() {
1215 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1216 }
1217
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001218 public LauncherAppWidgetHost getAppWidgetHost() {
1219 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 }
Romain Guycbb89e42009-06-08 15:52:54 -07001221
Winson Chunga9abd0e2010-10-27 17:18:37 -07001222 public LauncherModel getModel() {
1223 return mModel;
1224 }
1225
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001226 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001227 getWindow().closeAllPanels();
1228
Winson Chung87acb482011-08-23 17:28:05 -07001229 /**
1230 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001231 try {
1232 dismissDialog(DIALOG_CREATE_SHORTCUT);
1233 // Unlock the workspace if the dialog was showing
1234 } catch (Exception e) {
1235 // An exception is thrown if the dialog is not visible, which is fine
1236 }
1237
1238 try {
1239 dismissDialog(DIALOG_RENAME_FOLDER);
1240 // Unlock the workspace if the dialog was showing
1241 } catch (Exception e) {
1242 // An exception is thrown if the dialog is not visible, which is fine
1243 }
Winson Chung87acb482011-08-23 17:28:05 -07001244 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001245
1246 // Whatever we were doing is hereby canceled.
1247 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001248 }
1249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 @Override
1251 protected void onNewIntent(Intent intent) {
1252 super.onNewIntent(intent);
1253
1254 // Close the menu
1255 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001256 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001257 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001258
Joe Onorato14f122b2009-11-19 14:06:36 -08001259 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1260 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001261
Adam Cohenac56cff2011-09-28 20:45:37 -07001262 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001263 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001264 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001265 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1266 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001267 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001268 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001269
1270 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001271 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001272 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001273
Joe Onorato3a8820b2009-11-10 15:06:42 -08001274 final View v = getWindow().peekDecorView();
1275 if (v != null && v.getWindowToken() != null) {
1276 InputMethodManager imm = (InputMethodManager)getSystemService(
1277 INPUT_METHOD_SERVICE);
1278 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001280
Michael Jurka35aa14d2011-07-07 17:01:08 -07001281 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001282 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001283 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001284 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
1286 }
1287
1288 @Override
1289 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1290 // Do not call super here
1291 mSavedInstanceState = savedInstanceState;
1292 }
1293
1294 @Override
1295 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001296 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001297 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298
Michael Jurka883f55b2010-10-21 15:47:14 -07001299 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001300 // We close any open folder since it will not be re-opened, and we need to make sure
1301 // this state is reflected.
1302 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303
Winson Chung3d503fb2011-07-13 17:25:49 -07001304 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1305 mWaitingForResult) {
1306 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1307 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1308 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1309 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 }
1311
1312 if (mFolderInfo != null && mWaitingForResult) {
1313 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1314 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1315 }
Michael Jurka946ad472010-07-09 18:05:18 -07001316
Winson Chung785d2eb2011-04-14 16:08:02 -07001317 // Save the current AppsCustomize tab
1318 if (mAppsCustomizeTabHost != null) {
1319 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1320 if (currentTabTag != null) {
1321 outState.putString("apps_customize_currentTab", currentTabTag);
1322 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001323 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1324 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001325 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 }
1327
1328 @Override
1329 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001331
Winson Chunge7a03942011-08-05 15:05:12 -07001332 // Remove all pending runnables
1333 mHandler.removeMessages(ADVANCE_MSG);
1334 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001335 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001336
Winson Chungcd2b0142011-06-08 16:02:26 -07001337 // Stop callbacks from LauncherModel
1338 LauncherApplication app = ((LauncherApplication) getApplication());
1339 mModel.stopLoader();
1340 app.setLauncher(null);
1341
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001343 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001345 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001347 mAppWidgetHost = null;
1348
1349 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350
1351 TextKeyListener.getInstance().release();
1352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353
Winson Chung3d503fb2011-07-13 17:25:49 -07001354 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001355
1356 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001357 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001358
1359 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1360 mWorkspace.removeAllViews();
1361 mWorkspace = null;
1362 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001363
1364 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 }
1366
Adam Cohena9cf38f2011-05-02 15:36:58 -07001367 public DragController getDragController() {
1368 return mDragController;
1369 }
1370
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 @Override
1372 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001373 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 super.startActivityForResult(intent, requestCode);
1375 }
1376
Winson Chung70d72102011-08-12 11:24:35 -07001377 /**
1378 * Indicates that we want global search for this activity by setting the globalSearch
1379 * argument for {@link #startSearch} to true.
1380 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001382 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001384
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001385 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001386
Karl Rosaen138a0412009-04-23 19:00:21 -07001387 if (initialQuery == null) {
1388 // Use any text typed in the launcher as the initial query
1389 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001390 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 if (appSearchData == null) {
1392 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001393 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001395 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001396
Karl Rosaen138a0412009-04-23 19:00:21 -07001397 final SearchManager searchManager =
1398 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001399 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001400 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 }
1402
Winson Chung70d72102011-08-12 11:24:35 -07001403 @Override
1404 public boolean onCreateOptionsMenu(Menu menu) {
1405 if (isWorkspaceLocked()) {
1406 return false;
1407 }
1408
1409 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001410
1411 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1412 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1413 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001414 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1415 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1416 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001417 String helpUrl = getString(R.string.help_url);
1418 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001419 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1420 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1421
Winson Chung70d72102011-08-12 11:24:35 -07001422 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1423 .setIcon(android.R.drawable.ic_menu_gallery)
1424 .setAlphabeticShortcut('W');
1425 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1426 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001427 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001428 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001429 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1430 .setIcon(android.R.drawable.ic_menu_preferences)
1431 .setIntent(settings)
1432 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001433 if (!helpUrl.isEmpty()) {
1434 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1435 .setIcon(android.R.drawable.ic_menu_help)
1436 .setIntent(help)
1437 .setAlphabeticShortcut('H');
1438 }
Winson Chung70d72102011-08-12 11:24:35 -07001439 return true;
1440 }
1441
1442 @Override
1443 public boolean onPrepareOptionsMenu(Menu menu) {
1444 super.onPrepareOptionsMenu(menu);
1445
1446 if (mAppsCustomizeTabHost.isTransitioning()) {
1447 return false;
1448 }
1449 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1450 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1451
1452 return true;
1453 }
1454
1455 @Override
1456 public boolean onOptionsItemSelected(MenuItem item) {
1457 switch (item.getItemId()) {
1458 case MENU_WALLPAPER_SETTINGS:
1459 startWallpaper();
1460 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001461 }
1462
1463 return super.onOptionsItemSelected(item);
1464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001466 @Override
1467 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001468 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001469 // Use a custom animation for launching search
1470 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001471 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001472 }
1473
Joe Onorato9c1289c2009-08-17 11:03:03 -04001474 public boolean isWorkspaceLocked() {
1475 return mWorkspaceLoading || mWaitingForResult;
1476 }
1477
Michael Jurka0280c3b2010-09-17 15:00:07 -07001478 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001479 mPendingAddInfo.container = ItemInfo.NO_ID;
1480 mPendingAddInfo.screen = -1;
1481 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1482 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001483 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001484 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001485 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001486
Michael Jurkaaf442092010-06-10 17:01:57 -07001487 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001488 // TODO: catch bad widget exception when sent
1489 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001490 // TODO: Is this log message meaningful?
1491 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001492 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001493 }
1494
Adam Cohened66b2b2012-01-23 17:28:51 -08001495 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1496 final AppWidgetProviderInfo appWidget = info.info;
1497 Runnable configurationActivity = null;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001498 if (appWidget.configure != null) {
1499 // Launch over to configure widget, if needed
1500 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1501 intent.setComponent(appWidget.configure);
1502 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001503 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001504 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001505 intent.putExtra(InstallWidgetReceiver.
1506 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001507
1508 final String mimeType = info.mimeType;
1509 final ClipData clipData = (ClipData) info.configurationData;
1510 final ClipDescription clipDesc = clipData.getDescription();
1511 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1512 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001513 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001514 final CharSequence stringData = item.getText();
1515 final Uri uriData = item.getUri();
1516 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001517 final String key = InstallWidgetReceiver.
1518 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001519 if (uriData != null) {
1520 intent.putExtra(key, uriData);
1521 } else if (intentData != null) {
1522 intent.putExtra(key, intentData);
1523 } else if (stringData != null) {
1524 intent.putExtra(key, stringData);
1525 }
1526 break;
1527 }
1528 }
1529 }
Winson Chung55cef262010-10-28 14:14:18 -07001530 }
Romain Guy8e633c52010-03-04 12:51:36 -08001531 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Adam Cohened66b2b2012-01-23 17:28:51 -08001532 mWidgetBeingConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001534 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001535 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001536 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001537 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 }
1539 }
Romain Guycbb89e42009-06-08 15:52:54 -07001540
Adam Cohenfbba09b2011-07-18 21:43:05 -07001541 /**
1542 * Process a shortcut drop.
1543 *
1544 * @param componentName The name of the component
1545 * @param screen The screen where it should be added
1546 * @param cell The cell it should be added to, optional
1547 * @param position The location on the screen where it was dropped, optional
1548 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001549 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1550 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001551 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001552 mPendingAddInfo.container = container;
1553 mPendingAddInfo.screen = screen;
1554 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001555
1556 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001557 mPendingAddInfo.cellX = cell[0];
1558 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001559 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001560
1561 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1562 createShortcutIntent.setComponent(componentName);
1563 processShortcut(createShortcutIntent);
1564 }
1565
Adam Cohenfbba09b2011-07-18 21:43:05 -07001566 /**
1567 * Process a widget drop.
1568 *
1569 * @param info The PendingAppWidgetInfo of the widget being added.
1570 * @param screen The screen where it should be added
1571 * @param cell The cell it should be added to, optional
1572 * @param position The location on the screen where it was dropped, optional
1573 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001574 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001575 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001576 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001577 mPendingAddInfo.container = info.container = container;
1578 mPendingAddInfo.screen = info.screen = screen;
1579 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001580 mPendingAddInfo.minSpanX = info.minSpanX;
1581 mPendingAddInfo.minSpanY = info.minSpanY;
1582
Adam Cohenfbba09b2011-07-18 21:43:05 -07001583 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001584 mPendingAddInfo.cellX = cell[0];
1585 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001586 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001587 if (span != null) {
1588 mPendingAddInfo.spanX = span[0];
1589 mPendingAddInfo.spanY = span[1];
1590 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001591
Adam Cohened66b2b2012-01-23 17:28:51 -08001592 AppWidgetHostView hostView = info.boundWidget;
1593 int appWidgetId;
1594 if (hostView != null) {
1595 appWidgetId = hostView.getAppWidgetId();
1596 } else {
1597 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1598 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1599 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001600 addAppWidgetImpl(appWidgetId, info);
1601 }
1602
Joe Onoratodeb98af2010-02-19 14:59:39 -08001603 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001604 // Handle case where user selected "Applications"
1605 String applicationName = getResources().getString(R.string.group_applications);
1606 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001607
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001608 if (applicationName != null && applicationName.equals(shortcutName)) {
1609 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1610 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001611
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001612 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1613 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001614 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001615 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001616 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001617 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001618 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 }
1620
Winson Chung24ab2f12010-09-16 14:10:47 -07001621 void processWallpaper(Intent intent) {
1622 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1623 }
1624
Winson Chung3d503fb2011-07-13 17:25:49 -07001625 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1626 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001627 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 folderInfo.title = getText(R.string.folder_name);
1629
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001631 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1632 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001633 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634
1635 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001636 FolderIcon newFolder =
1637 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1638 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1639 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001640 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 }
Romain Guycbb89e42009-06-08 15:52:54 -07001642
Joe Onorato9c1289c2009-08-17 11:03:03 -04001643 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001644 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001645 }
1646
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001648 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001650 Intent chooser = Intent.createChooser(pickWallpaper,
1651 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001652 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1653 // Removed in Eclair MR1
1654// WallpaperManager wm = (WallpaperManager)
1655// getSystemService(Context.WALLPAPER_SERVICE);
1656// WallpaperInfo wi = wm.getWallpaperInfo();
1657// if (wi != null && wi.getSettingsActivity() != null) {
1658// LabeledIntent li = new LabeledIntent(getPackageName(),
1659// R.string.configure_wallpaper, 0);
1660// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1661// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1662// }
Mike Clerona0618e42009-10-22 13:55:21 -07001663 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 }
1665
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001666 /**
1667 * Registers various content observers. The current implementation registers
1668 * only a favorites observer to keep track of the favorites applications.
1669 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001670 private void registerContentObservers() {
1671 ContentResolver resolver = getContentResolver();
1672 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1673 true, mWidgetObserver);
1674 }
1675
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 @Override
1677 public boolean dispatchKeyEvent(KeyEvent event) {
1678 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1679 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001681 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001682 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001683 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001684 dumpState();
1685 return true;
1686 }
1687 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001688 }
1689 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1690 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001691 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 return true;
1693 }
1694 }
1695
1696 return super.dispatchKeyEvent(event);
1697 }
1698
Joe Onorato88ec0992009-11-19 13:16:06 -08001699 @Override
1700 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001701 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001702 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001703 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001704 Folder openFolder = mWorkspace.getOpenFolder();
1705 if (openFolder.isEditingName()) {
1706 openFolder.dismissEditingName();
1707 } else {
1708 closeFolder();
1709 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001710 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001711 mWorkspace.exitWidgetResizeMode();
1712
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001713 // Back button is a no-op here, but give at least some feedback for the button press
1714 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001715 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001716 }
1717
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001719 * Re-listen when widgets are reset.
1720 */
1721 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001722 if (mAppWidgetHost != null) {
1723 mAppWidgetHost.startListening();
1724 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001725 }
1726
1727 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001728 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1729 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001731 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001732 if (mModel != null) {
1733 mModel.unbindWorkspaceItems();
1734 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001736
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 /**
1738 * Launches the intent referred by the clicked shortcut.
1739 *
1740 * @param v The view representing the clicked shortcut.
1741 */
1742 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001743 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1744 // view has detached (it's possible for this to happen if the view is removed mid touch).
1745 if (v.getWindowToken() == null) {
1746 return;
1747 }
1748
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001749 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001750 return;
1751 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001752
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001754 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001756 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001757 int[] pos = new int[2];
1758 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001759 intent.setSourceBounds(new Rect(pos[0], pos[1],
1760 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001761 boolean success = startActivitySafely(intent, tag);
1762
1763 if (success && v instanceof BubbleTextView) {
1764 mWaitingForResume = (BubbleTextView) v;
1765 mWaitingForResume.setStayPressed(true);
1766 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001768 if (v instanceof FolderIcon) {
1769 FolderIcon fi = (FolderIcon) v;
1770 handleFolderClick(fi);
1771 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001772 } else if (v == mAllAppsButton) {
1773 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001774 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001775 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001776 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001777 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 }
1779 }
1780
Michael Jurka0e260592010-06-30 17:07:39 -07001781 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001782 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001783 // clicking anywhere on the workspace causes the customization drawer to slide down
1784 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001785 return false;
1786 }
1787
Michael Jurkaaf442092010-06-10 17:01:57 -07001788 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001789 * Event handler for the search button
1790 *
1791 * @param v The view that was clicked.
1792 */
1793 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001794 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1795
Amith Yamasania135ba82011-08-09 17:42:01 -07001796 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001797 }
1798
1799 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001800 * Event handler for the voice button
1801 *
1802 * @param v The view that was clicked.
1803 */
1804 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001805 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001806
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001807 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001808 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001809 startActivity(intent);
1810 }
1811
1812 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001813 * Event handler for the "grid" button that appears on the home screen, which
1814 * enters all apps mode.
1815 *
1816 * @param v The view that was clicked.
1817 */
1818 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001819 showAllApps(true);
1820 }
1821
1822 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001823 // Provide the same haptic feedback that the system offers for virtual keys.
1824 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001825 }
1826
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001827 public void onClickAppMarketButton(View v) {
1828 if (mAppMarketIntent != null) {
1829 startActivitySafely(mAppMarketIntent, "app market");
1830 }
1831 }
1832
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001833 void startApplicationDetailsActivity(ComponentName componentName) {
1834 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001835 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1836 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001837 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001838 startActivity(intent);
1839 }
1840
Patrick Dubroy5539af72010-09-07 15:22:01 -07001841 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1842 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1843 // System applications cannot be installed. For now, show a toast explaining that.
1844 // We may give them the option of disabling apps this way.
1845 int messageId = R.string.uninstall_system_app_text;
1846 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1847 } else {
1848 String packageName = appInfo.componentName.getPackageName();
1849 String className = appInfo.componentName.getClassName();
1850 Intent intent = new Intent(
1851 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001852 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1853 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001854 startActivity(intent);
1855 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001856 }
1857
Michael Jurkaddd62e92011-02-16 17:49:14 -08001858 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1860 try {
1861 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001862 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 } catch (ActivityNotFoundException e) {
1864 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001865 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 } catch (SecurityException e) {
1867 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001868 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001870 "or use the exported attribute for this activity. "
1871 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001873 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001875
Romain Guy8e633c52010-03-04 12:51:36 -08001876 void startActivityForResultSafely(Intent intent, int requestCode) {
1877 try {
1878 startActivityForResult(intent, requestCode);
1879 } catch (ActivityNotFoundException e) {
1880 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1881 } catch (SecurityException e) {
1882 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1883 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1884 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1885 "or use the exported attribute for this activity.", e);
1886 }
1887 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888
Adam Cohena9cf38f2011-05-02 15:36:58 -07001889 private void handleFolderClick(FolderIcon folderIcon) {
1890 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001891 Folder openFolder = mWorkspace.getFolderForTag(info);
1892
1893 // If the folder info reports that the associated folder is open, then verify that
1894 // it is actually opened. There have been a few instances where this gets out of sync.
1895 if (info.opened && openFolder == null) {
1896 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1897 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1898 info.opened = false;
1899 }
1900
Adam Cohena9cf38f2011-05-02 15:36:58 -07001901 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 // Close any open folder
1903 closeFolder();
1904 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001905 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 } else {
1907 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 int folderScreen;
1909 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001910 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 // .. and close it
1912 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001913 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 // Close any folder open on the current screen
1915 closeFolder();
1916 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001917 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 }
1919 }
1920 }
1921 }
1922
Adam Cohen2801caf2011-05-13 20:57:39 -07001923 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001924 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001925 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1926 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1927 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1928
Adam Cohenc51934b2011-07-26 21:07:43 -07001929 FolderInfo info = (FolderInfo) fi.getTag();
1930 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1931 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001932 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1933 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001934 }
1935
Adam Cohen2801caf2011-05-13 20:57:39 -07001936 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1937 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1938 oa.start();
1939 }
1940
1941 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001942 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001943 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1944 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1945 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1946
Adam Cohenc51934b2011-07-26 21:07:43 -07001947 FolderInfo info = (FolderInfo) fi.getTag();
1948 CellLayout cl = null;
1949 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1950 cl = (CellLayout) fi.getParent().getParent();
1951 }
1952
1953 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001954 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1955 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001956 oa.addListener(new AnimatorListenerAdapter() {
1957 @Override
1958 public void onAnimationEnd(Animator animation) {
1959 if (layout != null) {
1960 layout.clearFolderLeaveBehind();
1961 }
1962 }
1963 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001964 oa.start();
1965 }
1966
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001968 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 * is animated relative to the specified View. If the View is null, no animation
1970 * is played.
1971 *
1972 * @param folderInfo The FolderInfo describing the folder to open.
1973 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001974 public void openFolder(FolderIcon folderIcon) {
1975 Folder folder = folderIcon.mFolder;
1976 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977
Adam Cohen2801caf2011-05-13 20:57:39 -07001978 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001979 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980
Adam Cohen4554ee12011-08-03 16:13:21 -07001981 // Just verify that the folder hasn't already been added to the DragLayer.
1982 // There was a one-off crash where the folder had a parent already.
1983 if (folder.getParent() == null) {
1984 mDragLayer.addView(folder);
1985 mDragController.addDropTarget((DropTarget) folder);
1986 } else {
1987 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1988 folder.getParent() + ").");
1989 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001990 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001991 }
1992
1993 public void closeFolder() {
1994 Folder folder = mWorkspace.getOpenFolder();
1995 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07001996 if (folder.isEditingName()) {
1997 folder.dismissEditingName();
1998 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001999 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002000
2001 // Dismiss the folder cling
2002 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002003 }
2004 }
2005
2006 void closeFolder(Folder folder) {
2007 folder.getInfo().opened = false;
2008
2009 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2010 if (parent != null) {
2011 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2012 shrinkAndFadeInFolderIcon(fi);
2013 }
2014 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 }
2016
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002018
Patrick Dubroye708c522011-03-01 16:03:43 -08002019 if (mState != State.WORKSPACE) {
2020 return false;
2021 }
2022
Joe Onorato9c1289c2009-08-17 11:03:03 -04002023 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 return false;
2025 }
2026
2027 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002028 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 }
2030
Michael Jurka0280c3b2010-09-17 15:00:07 -07002031 resetAddInfo();
2032 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002034 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 return true;
2036 }
2037
Winson Chung3d503fb2011-07-13 17:25:49 -07002038 // The hotseat touch handling does not go through Workspace, and we always allow long press
2039 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002040 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002041 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2042 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002043 if (itemUnderLongClick == null) {
2044 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002045 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2046 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002047 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002049 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002051 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 }
2053 }
2054 }
2055 return true;
2056 }
2057
Winson Chung3d503fb2011-07-13 17:25:49 -07002058 boolean isHotseatLayout(View layout) {
2059 return mHotseat != null && layout != null &&
2060 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2061 }
2062 Hotseat getHotseat() {
2063 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002064 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002065 SearchDropTargetBar getSearchBar() {
2066 return mSearchDropTargetBar;
2067 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002068
Winson Chung3d503fb2011-07-13 17:25:49 -07002069 /**
2070 * Returns the CellLayout of the specified container at the specified screen.
2071 */
2072 CellLayout getCellLayout(long container, int screen) {
2073 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2074 if (mHotseat != null) {
2075 return mHotseat.getLayout();
2076 } else {
2077 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002078 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002079 } else {
2080 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002081 }
2082 }
2083
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 Workspace getWorkspace() {
2085 return mWorkspace;
2086 }
2087
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 @Override
2089 protected Dialog onCreateDialog(int id) {
2090 switch (id) {
2091 case DIALOG_CREATE_SHORTCUT:
2092 return new CreateShortcut().createDialog();
2093 case DIALOG_RENAME_FOLDER:
2094 return new RenameFolder().createDialog();
2095 }
2096
2097 return super.onCreateDialog(id);
2098 }
2099
2100 @Override
2101 protected void onPrepareDialog(int id, Dialog dialog) {
2102 switch (id) {
2103 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 break;
2105 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002106 if (mFolderInfo != null) {
2107 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2108 final CharSequence text = mFolderInfo.title;
2109 input.setText(text);
2110 input.setSelection(0, text.length());
2111 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 break;
2113 }
2114 }
2115
2116 void showRenameDialog(FolderInfo info) {
2117 mFolderInfo = info;
2118 mWaitingForResult = true;
2119 showDialog(DIALOG_RENAME_FOLDER);
2120 }
2121
Adam Cohenfbba09b2011-07-18 21:43:05 -07002122 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002123 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002124 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2125 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 mWaitingForResult = true;
2127 showDialog(DIALOG_CREATE_SHORTCUT);
2128 }
2129
2130 private class RenameFolder {
2131 private EditText mInput;
2132
2133 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2135 mInput = (EditText) layout.findViewById(R.id.folder_name);
2136
2137 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2138 builder.setIcon(0);
2139 builder.setTitle(getString(R.string.rename_folder_title));
2140 builder.setCancelable(true);
2141 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2142 public void onCancel(DialogInterface dialog) {
2143 cleanup();
2144 }
2145 });
2146 builder.setNegativeButton(getString(R.string.cancel_action),
2147 new Dialog.OnClickListener() {
2148 public void onClick(DialogInterface dialog, int which) {
2149 cleanup();
2150 }
2151 }
2152 );
2153 builder.setPositiveButton(getString(R.string.rename_action),
2154 new Dialog.OnClickListener() {
2155 public void onClick(DialogInterface dialog, int which) {
2156 changeFolderName();
2157 }
2158 }
2159 );
2160 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002161
2162 final AlertDialog dialog = builder.create();
2163 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2164 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002165 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002166 mInput.requestFocus();
2167 InputMethodManager inputManager = (InputMethodManager)
2168 getSystemService(Context.INPUT_METHOD_SERVICE);
2169 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002170 }
2171 });
2172
2173 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 }
2175
2176 private void changeFolderName() {
2177 final String name = mInput.getText().toString();
2178 if (!TextUtils.isEmpty(name)) {
2179 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002180 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 mFolderInfo.title = name;
2182 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2183
Joe Onorato9c1289c2009-08-17 11:03:03 -04002184 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002185 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002186 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 } else {
2188 final FolderIcon folderIcon = (FolderIcon)
2189 mWorkspace.getViewForTag(mFolderInfo);
2190 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002191 // TODO: At some point we'll probably want some version of setting
2192 // the text for a folder icon.
2193 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 getWorkspace().requestLayout();
2195 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002196 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002197 mWorkspaceLoading = true;
2198 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 }
2200 }
2201 }
2202 cleanup();
2203 }
2204
2205 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 dismissDialog(DIALOG_RENAME_FOLDER);
2207 mWaitingForResult = false;
2208 mFolderInfo = null;
2209 }
2210 }
2211
Daniel Sandler843e8602010-06-07 14:59:01 -04002212 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2213 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002214 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002215 }
2216
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002217 public boolean isAllAppsButtonRank(int rank) {
2218 return mHotseat.isAllAppsButtonRank(rank);
2219 }
2220
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002221 // AllAppsView.Watcher
2222 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002223 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002224 mWorkspace.setVisibility(View.GONE);
2225 }
2226 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002227
2228 /**
2229 * Helper method for the cameraZoomIn/cameraZoomOut animations
2230 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002231 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002232 * @param scaleFactor The scale factor used for the zoom
2233 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002234 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002235 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002236 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002237 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002238
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002239 void updateWallpaperVisibility(boolean visible) {
2240 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2241 int curflags = getWindow().getAttributes().flags
2242 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2243 if (wpflags != curflags) {
2244 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2245 }
2246 }
2247
Michael Jurkabed61d22012-02-14 22:51:29 -08002248 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2249 if (v instanceof LauncherTransitionable) {
2250 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2251 }
Winson Chung70442722012-02-10 15:43:22 -08002252
2253 // Update the workspace transition step as well
2254 dispatchOnLauncherTransitionStep(v, 0f);
2255 }
2256
2257 private void dispatchOnLauncherTransitionStep(View v, float t) {
2258 if (v instanceof LauncherTransitionable) {
2259 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2260 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002261 }
2262
2263 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2264 if (v instanceof LauncherTransitionable) {
2265 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2266 }
Winson Chung70442722012-02-10 15:43:22 -08002267
2268 // Update the workspace transition step as well
2269 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002270 }
2271
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002272 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002273 * Things to test when changing the following seven functions.
2274 * - Home from workspace
2275 * - from center screen
2276 * - from other screens
2277 * - Home from all apps
2278 * - from center screen
2279 * - from other screens
2280 * - Back from all apps
2281 * - from center screen
2282 * - from other screens
2283 * - Launch app from workspace and quit
2284 * - with back
2285 * - with home
2286 * - Launch app from all apps and quit
2287 * - with back
2288 * - with home
2289 * - Go to a screen that's not the default, then all
2290 * apps, and launch and app, and go back
2291 * - with back
2292 * -with home
2293 * - On workspace, long press power and go back
2294 * - with back
2295 * - with home
2296 * - On all apps, long press power and go back
2297 * - with back
2298 * - with home
2299 * - On workspace, power off
2300 * - On all apps, power off
2301 * - Launch an app and turn off the screen while in that app
2302 * - Go back with home key
2303 * - Go back with back key TODO: make this not go to workspace
2304 * - From all apps
2305 * - From workspace
2306 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2307 * - From all apps
2308 * - From the center workspace
2309 * - From another workspace
2310 */
2311
2312 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002313 * Zoom the camera out from the workspace to reveal 'toView'.
2314 * Assumes that the view to show is anchored at either the very top or very bottom
2315 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002316 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002317 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002318 if (mStateAnimation != null) {
2319 mStateAnimation.cancel();
2320 mStateAnimation = null;
2321 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002322 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002323 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002324
Winson Chungf0ea4d32011-06-06 14:27:16 -07002325 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2326 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2327 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002328 final View fromView = mWorkspace;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002329 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002330 final int startDelay =
2331 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002332
Michael Jurkab3e22d92011-10-31 15:58:33 -07002333 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002334
Michael Jurkad74c9842011-07-10 12:44:21 -07002335 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002336 Animator workspaceAnim =
2337 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002338
2339 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002340 toView.setScaleX(scale);
2341 toView.setScaleY(scale);
2342 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2343 scaleAnim.
2344 scaleX(1f).scaleY(1f).
2345 setDuration(duration).
2346 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002347
Winson Chungf0ea4d32011-06-06 14:27:16 -07002348 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002349 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002350 final ObjectAnimator alphaAnim = ObjectAnimator
2351 .ofFloat(toView, "alpha", 0f, 1f)
2352 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002353 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002354 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2355 @Override
2356 public void onAnimationUpdate(ValueAnimator animation) {
2357 float t = (Float) animation.getAnimatedValue();
2358 dispatchOnLauncherTransitionStep(fromView, t);
2359 dispatchOnLauncherTransitionStep(toView, t);
2360 }
2361 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002362
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002363 // toView should appear right at the end of the workspace shrink
2364 // animation
2365 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002366 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002367 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002368
2369 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002370 boolean animationCancelled = false;
2371
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002372 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002373 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002374 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002375 // Prepare the position
2376 toView.setTranslationX(0.0f);
2377 toView.setTranslationY(0.0f);
2378 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002379 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002380 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002381 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002382 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002383 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2384 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002385
2386 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2387 // Hide the workspace scrollbar
2388 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002389 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002390 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002391 if (!animationCancelled) {
2392 updateWallpaperVisibility(false);
2393 }
2394 }
2395
Adam Cohencff6af82011-09-13 14:51:53 -07002396 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002397 public void onAnimationCancel(Animator animation) {
2398 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002399 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002400 });
2401
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002402 if (workspaceAnim != null) {
2403 mStateAnimation.play(workspaceAnim);
2404 }
Michael Jurka1899a362011-11-03 13:50:45 -07002405
2406 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002407 final ViewTreeObserver observer;
2408
Michael Jurkabed61d22012-02-14 22:51:29 -08002409 dispatchOnLauncherTransitionStart(fromView, animated, false);
2410 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002411
2412 // If any of the objects being animated haven't been measured/laid out
2413 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002414 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002415 (mWorkspace.getMeasuredWidth() == 0) ||
2416 (toView.getMeasuredWidth() == 0)) {
2417 observer = mWorkspace.getViewTreeObserver();
2418 delayAnim = true;
2419 } else {
2420 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002421 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002422
2423 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002424 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002425 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2426 public void onGlobalLayout() {
2427 mWorkspace.post(new Runnable() {
2428 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002429 // Check that mStateAnimation hasn't changed while
2430 // we waited for a layout pass
2431 if (mStateAnimation == stateAnimation) {
2432 // Need to update pivots for zoom if layout changed
2433 setPivotsForZoom(toView, scale);
2434 mStateAnimation.start();
2435 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002436 }
2437 });
2438 observer.removeGlobalOnLayoutListener(this);
2439 }
2440 };
2441 observer.addOnGlobalLayoutListener(delayedStart);
2442 } else {
2443 setPivotsForZoom(toView, scale);
Michael Jurka1899a362011-11-03 13:50:45 -07002444 mStateAnimation.start();
2445 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002446 } else {
2447 toView.setTranslationX(0.0f);
2448 toView.setTranslationY(0.0f);
2449 toView.setScaleX(1.0f);
2450 toView.setScaleY(1.0f);
2451 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002452 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002453
Michael Jurkabed61d22012-02-14 22:51:29 -08002454 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2455 // Hide the workspace scrollbar
2456 mWorkspace.hideScrollingIndicator(true);
2457 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002458 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002459 dispatchOnLauncherTransitionStart(fromView, animated, false);
2460 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2461 dispatchOnLauncherTransitionStart(toView, animated, false);
2462 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002463 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002464 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002465 }
2466
2467 /**
2468 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002469 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002470 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002471 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002472 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2473 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002474
Michael Jurkab3e22d92011-10-31 15:58:33 -07002475 if (mStateAnimation != null) {
2476 mStateAnimation.cancel();
2477 mStateAnimation = null;
2478 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002479 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002480
Winson Chungf0ea4d32011-06-06 14:27:16 -07002481 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002482 final int fadeOutDuration =
2483 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002484 final float scaleFactor = (float)
2485 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2486 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002487 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002488 Animator workspaceAnim = null;
2489
2490 if (toState == State.WORKSPACE) {
2491 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2492 workspaceAnim = mWorkspace.getChangeStateAnimation(
2493 Workspace.State.NORMAL, animated, stagger);
2494 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2495 workspaceAnim = mWorkspace.getChangeStateAnimation(
2496 Workspace.State.SPRING_LOADED, animated);
2497 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002498
Michael Jurkab3e22d92011-10-31 15:58:33 -07002499 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002500 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002501 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002502 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002503 final float oldScaleX = fromView.getScaleX();
2504 final float oldScaleY = fromView.getScaleY();
2505
Michael Jurka159b4cc2012-01-17 03:00:35 -08002506 final LauncherViewPropertyAnimator scaleAnim =
2507 new LauncherViewPropertyAnimator(fromView);
2508 scaleAnim.
2509 scaleX(scaleFactor).scaleY(scaleFactor).
2510 setDuration(duration).
2511 setInterpolator(new Workspace.ZoomInInterpolator());
2512
2513 final ObjectAnimator alphaAnim = ObjectAnimator
2514 .ofFloat(fromView, "alpha", 1f, 0f)
2515 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002516 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002517 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2518 @Override
2519 public void onAnimationUpdate(ValueAnimator animation) {
2520 float t = 1f - (Float) animation.getAnimatedValue();
2521 dispatchOnLauncherTransitionStep(fromView, t);
2522 dispatchOnLauncherTransitionStep(toView, t);
2523 }
2524 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002525
Michael Jurkabed61d22012-02-14 22:51:29 -08002526 mStateAnimation = new AnimatorSet();
2527
2528 dispatchOnLauncherTransitionStart(fromView, animated, true);
2529 dispatchOnLauncherTransitionStart(toView, animated, true);
2530
2531 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002532 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002533 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002534 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002535 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002536 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2537 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurka430e8a52011-08-08 15:52:14 -07002538 mWorkspace.hideScrollingIndicator(false);
Adam Cohened66b2b2012-01-23 17:28:51 -08002539 if (onCompleteRunnable != null) {
2540 onCompleteRunnable.run();
2541 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002542 }
2543 });
2544
Winson Chungf0ea4d32011-06-06 14:27:16 -07002545 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002546 if (workspaceAnim != null) {
2547 mStateAnimation.play(workspaceAnim);
2548 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002549 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002550 } else {
2551 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002552 dispatchOnLauncherTransitionStart(fromView, animated, true);
2553 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2554 dispatchOnLauncherTransitionStart(toView, animated, true);
2555 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002556 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002557 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002558 }
2559
Michael Jurkae326f182011-11-21 14:05:46 -08002560 @Override
2561 public void onTrimMemory(int level) {
2562 super.onTrimMemory(level);
Dianne Hackborn4f560ce2012-03-06 19:07:47 -08002563 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
Michael Jurkae326f182011-11-21 14:05:46 -08002564 mAppsCustomizeTabHost.onTrimMemory();
2565 }
2566 }
2567
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002568 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002569 showWorkspace(animated, null);
2570 }
2571
2572 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002573 if (mState != State.WORKSPACE) {
2574 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002575 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002576
2577 // Show the search bar and hotseat
2578 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002579 // We only need to animate in the dock divider if we're going from spring loaded mode
2580 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002581
2582 // Set focus to the AppsCustomize button
2583 if (mAllAppsButton != null) {
2584 mAllAppsButton.requestFocus();
2585 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002586 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002587
Michael Jurkab737ee62011-11-15 15:57:22 -08002588 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002589
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002590 // Change the state *after* we've called all the transition code
2591 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002592
Winson Chung5fb63472011-02-02 17:03:37 -08002593 // Resume the auto-advance of widgets
2594 mUserPresent = true;
2595 updateRunning();
2596
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002597 // send an accessibility event to announce the context change
2598 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002599 }
2600
Michael Jurkab3e22d92011-10-31 15:58:33 -07002601 void showAllApps(boolean animated) {
2602 if (mState != State.WORKSPACE) return;
2603
2604 showAppsCustomizeHelper(animated, false);
2605 mAppsCustomizeTabHost.requestFocus();
2606
2607 // Hide the search bar and hotseat
2608 mSearchDropTargetBar.hideSearchBar(animated);
2609
2610 // Change the state *after* we've called all the transition code
2611 mState = State.APPS_CUSTOMIZE;
2612
2613 // Pause the auto-advance of widgets until we are out of AllApps
2614 mUserPresent = false;
2615 updateRunning();
2616 closeFolder();
2617
2618 // Send an accessibility event to announce the context change
2619 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2620 }
2621
Adam Cohen7777d962011-08-18 18:58:38 -07002622 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002623 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002624 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002625 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002626 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002627 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002628 }
Adam Cohen7777d962011-08-18 18:58:38 -07002629
Adam Cohened66b2b2012-01-23 17:28:51 -08002630 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2631 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002632 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2633
Winson Chunge7a03942011-08-05 15:05:12 -07002634 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002635 @Override
2636 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002637 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002638 // Before we show workspace, hide all apps again because
2639 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2640 // clean up our state transition functions
2641 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002642 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002643 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002644 } else {
2645 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002646 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002647 }
2648 }, (extendedDelay ?
2649 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2650 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2651 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002652
Michael Jurkad3ef3062010-11-23 16:23:58 -08002653 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002654 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002655 final boolean animated = true;
2656 final boolean springLoaded = true;
2657 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002658 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002659 }
2660 // Otherwise, we are not in spring loaded mode, so don't do anything.
2661 }
2662
Michael Jurkab737ee62011-11-15 15:57:22 -08002663 void hideDockDivider() {
2664 if (mQsbDivider != null && mDockDivider != null) {
2665 mQsbDivider.setVisibility(View.INVISIBLE);
2666 mDockDivider.setVisibility(View.INVISIBLE);
2667 }
2668 }
2669
2670 void showDockDivider(boolean animated) {
2671 if (mQsbDivider != null && mDockDivider != null) {
2672 mQsbDivider.setVisibility(View.VISIBLE);
2673 mDockDivider.setVisibility(View.VISIBLE);
2674 if (mDividerAnimator != null) {
2675 mDividerAnimator.cancel();
2676 mQsbDivider.setAlpha(1f);
2677 mDockDivider.setAlpha(1f);
2678 mDividerAnimator = null;
2679 }
2680 if (animated) {
2681 mDividerAnimator = new AnimatorSet();
2682 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2683 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2684 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2685 mDividerAnimator.start();
2686 }
2687 }
2688 }
2689
Michael Jurkab3e22d92011-10-31 15:58:33 -07002690 void lockAllApps() {
2691 // TODO
2692 }
2693
2694 void unlockAllApps() {
2695 // TODO
2696 }
2697
Adam Cohenfc53cd22011-07-20 15:45:11 -07002698 public boolean isAllAppsCustomizeOpen() {
2699 return mState == State.APPS_CUSTOMIZE;
2700 }
2701
Winson Chungf0ea4d32011-06-06 14:27:16 -07002702 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002703 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002704 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002705 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002706 if (!LauncherApplication.isScreenLarge()) {
2707 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002708 if (mHotseat.getAlpha() != 1f) {
2709 int duration = mSearchDropTargetBar.getTransitionInDuration();
2710 mHotseat.animate().alpha(1f).setDuration(duration);
2711 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002712 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002713 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002714 }
2715 }
2716 }
2717
2718 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002719 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002720 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002721 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002722 if (!LauncherApplication.isScreenLarge()) {
2723 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002724 if (mHotseat.getAlpha() != 0f) {
2725 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2726 mHotseat.animate().alpha(0f).setDuration(duration);
2727 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002728 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002729 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002730 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002731 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002732 }
2733
Patrick Dubroy5f445422011-02-18 14:35:21 -08002734 /**
2735 * Add an item from all apps or customize onto the given workspace screen.
2736 * If layout is null, add to the current screen.
2737 */
2738 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002739 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2740 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002741 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002742 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002743
Winson Chungdff8ebb2011-09-08 17:25:31 -07002744 /** Maps the current orientation to an index for referencing orientation correct global icons */
2745 private int getCurrentOrientationIndexForGlobalIcons() {
2746 // default - 0, landscape - 1
2747 switch (getResources().getConfiguration().orientation) {
2748 case Configuration.ORIENTATION_LANDSCAPE:
2749 return 1;
2750 default:
2751 return 0;
2752 }
2753 }
2754
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002755 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002756 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002757 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002758 // Look for the toolbar icon specified in the activity meta-data
2759 Bundle metaData = packageManager.getActivityInfo(
2760 activityName, PackageManager.GET_META_DATA).metaData;
2761 if (metaData != null) {
2762 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2763 if (iconResId != 0) {
2764 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002765 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002766 }
2767 }
2768 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002769 // This can happen if the activity defines an invalid drawable
2770 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2771 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002772 } catch (Resources.NotFoundException nfe) {
2773 // This can happen if the activity defines an invalid drawable
2774 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2775 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002776 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002777 return null;
2778 }
2779
2780 // if successful in getting icon, return it; otherwise, set button to use default drawable
2781 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2782 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002783 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002784 Resources r = getResources();
2785 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2786 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002787
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002788 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002789 // If we were unable to find the icon via the meta-data, use a generic one
2790 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002791 toolbarIcon = r.getDrawable(fallbackDrawableId);
2792 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002793 if (button != null) {
2794 button.setCompoundDrawables(toolbarIcon, null, null, null);
2795 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002796 return null;
2797 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002798 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002799 if (button != null) {
2800 button.setCompoundDrawables(toolbarIcon, null, null, null);
2801 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002802 return toolbarIcon.getConstantState();
2803 }
2804 }
2805
2806 // if successful in getting icon, return it; otherwise, set button to use default drawable
2807 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2808 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2809 ImageView button = (ImageView) findViewById(buttonId);
2810 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2811
Michael Jurka19e0fc52011-07-22 18:00:21 -07002812 if (button != null) {
2813 // If we were unable to find the icon via the meta-data, use a
2814 // generic one
2815 if (toolbarIcon == null) {
2816 button.setImageResource(fallbackDrawableId);
2817 } else {
2818 button.setImageDrawable(toolbarIcon);
2819 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002820 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002821
2822 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2823
Michael Jurka0423dcf2010-10-05 14:56:18 -07002824 }
2825
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002826 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2827 TextView button = (TextView) findViewById(buttonId);
2828 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2829 }
2830
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002831 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002832 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002833 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002834 }
2835
Winson Chungbb185bd2011-11-21 12:31:42 -08002836 private void invalidatePressedFocusedStates(View container, View button) {
2837 if (container instanceof HolographicLinearLayout) {
2838 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2839 layout.invalidatePressedFocusedStates();
2840 } else if (button instanceof HolographicImageView) {
2841 HolographicImageView view = (HolographicImageView) button;
2842 view.invalidatePressedFocusedStates();
2843 }
2844 }
2845
Winson Chungc51db6a2011-10-05 11:44:49 -07002846 private boolean updateGlobalSearchIcon() {
2847 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002848 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2849 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002850 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002851 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002852
Winson Chung1cad91e2011-05-25 17:41:01 -07002853 final SearchManager searchManager =
2854 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2855 ComponentName activityName = searchManager.getGlobalSearchActivity();
2856 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002857 int coi = getCurrentOrientationIndexForGlobalIcons();
2858 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002859 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002860 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002861 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2862 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002863 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002864 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002865 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002866 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002867 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002868 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2869 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2870 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002871 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002872 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002873 }
2874 }
2875
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002876 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002877 final View searchButtonContainer = findViewById(R.id.search_button_container);
2878 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002879 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002880 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002881 }
2882
Winson Chungc51db6a2011-10-05 11:44:49 -07002883 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002884 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002885 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002886 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002887
Winson Chungc51db6a2011-10-05 11:44:49 -07002888 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002889 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2890 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002891 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002892 int coi = getCurrentOrientationIndexForGlobalIcons();
2893 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002894 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002895 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002896 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002897 voiceButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002898 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002899 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002900 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002901 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002902 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002903 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002904 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002905 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002906 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002907
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002908 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002909 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2910 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002911 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002912 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002913 }
2914
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002915 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002916 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002917 */
2918 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002919 final View marketButton = findViewById(R.id.market_button);
2920 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2921 // Find the app market activity by resolving an intent.
2922 // (If multiple app markets are installed, it will return the ResolverActivity.)
2923 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002924 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002925 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002926 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002927 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002928 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002929 marketButton.setVisibility(View.VISIBLE);
2930 } else {
2931 // We should hide and disable the view so that we don't try and restore the visibility
2932 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2933 marketButton.setVisibility(View.GONE);
2934 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002935 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002936 }
2937
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002938 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002939 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002940 }
2941
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002942 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002943 * Displays the shortcut creation dialog and launches, if necessary, the
2944 * appropriate activity.
2945 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002946 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002947 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2948 DialogInterface.OnShowListener {
2949
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002951
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002952 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002953 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002954
Winson Chung55b65502011-05-26 12:03:43 -07002955 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2956 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002957 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002958
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959 AlertDialog dialog = builder.create();
2960 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002961 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002962 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002964 return dialog;
2965 }
2966
2967 public void onCancel(DialogInterface dialog) {
2968 mWaitingForResult = false;
2969 cleanup();
2970 }
2971
Romain Guycbb89e42009-06-08 15:52:54 -07002972 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002973 mWaitingForResult = false;
2974 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002975 }
2976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002978 try {
2979 dismissDialog(DIALOG_CREATE_SHORTCUT);
2980 } catch (Exception e) {
2981 // An exception is thrown if the dialog is not visible, which is fine
2982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002983 }
2984
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002985 /**
2986 * Handle the action clicked in the "Add to home" dialog.
2987 */
2988 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002989 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002990
Winson Chung55b65502011-05-26 12:03:43 -07002991 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2992 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002993 case AddAdapter.ITEM_APPLICATION: {
2994 if (mAppsCustomizeTabHost != null) {
2995 mAppsCustomizeTabHost.selectAppsTab();
2996 }
2997 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002998 break;
2999 }
Winson Chung55b65502011-05-26 12:03:43 -07003000 case AddAdapter.ITEM_APPWIDGET: {
3001 if (mAppsCustomizeTabHost != null) {
3002 mAppsCustomizeTabHost.selectWidgetsTab();
3003 }
3004 showAllApps(true);
3005 break;
3006 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003007 case AddAdapter.ITEM_WALLPAPER: {
3008 startWallpaper();
3009 break;
3010 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003011 }
3012 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003013
3014 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003015 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003017 }
3018
3019 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003020 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003021 */
3022 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3023 @Override
3024 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003025 closeSystemDialogs();
3026 }
3027 }
3028
3029 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003030 * Receives notifications whenever the appwidgets are reset.
3031 */
3032 private class AppWidgetResetObserver extends ContentObserver {
3033 public AppWidgetResetObserver() {
3034 super(new Handler());
3035 }
3036
3037 @Override
3038 public void onChange(boolean selfChange) {
3039 onAppWidgetReset();
3040 }
3041 }
3042
3043 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003044 * If the activity is currently paused, signal that we need to re-run the loader
3045 * in onResume.
3046 *
3047 * This needs to be called from incoming places where resources might have been loaded
3048 * while we are paused. That is becaues the Configuration might be wrong
3049 * when we're not running, and if it comes back to what it was when we
3050 * were paused, we are not restarted.
3051 *
3052 * Implementation of the method from LauncherModel.Callbacks.
3053 *
3054 * @return true if we are currently paused. The caller might be able to
3055 * skip some work in that case since we will come back again.
3056 */
3057 public boolean setLoadOnResume() {
3058 if (mPaused) {
3059 Log.i(TAG, "setLoadOnResume");
3060 mOnResumeNeedsLoad = true;
3061 return true;
3062 } else {
3063 return false;
3064 }
3065 }
3066
3067 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003068 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003069 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003070 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003071 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003072 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003073 } else {
3074 return SCREEN_COUNT / 2;
3075 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003076 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003077
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003078
Joe Onorato9c1289c2009-08-17 11:03:03 -04003079 /**
3080 * Refreshes the shortcuts shown on the workspace.
3081 *
3082 * Implementation of the method from LauncherModel.Callbacks.
3083 */
3084 public void startBinding() {
3085 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003086
3087 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003088 int count = workspace.getChildCount();
3089 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003090 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003091 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3092 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003093 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003094 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003095 if (mHotseat != null) {
3096 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003097 }
3098 }
3099
3100 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003101 * Bind the items start-end from the list.
3102 *
3103 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003104 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003105 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003106 setLoadOnResume();
3107
Joe Onorato9c1289c2009-08-17 11:03:03 -04003108 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003109 for (int i=start; i<end; i++) {
3110 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003111
3112 // Short circuit if we are loading dock items for a configuration which has no dock
3113 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3114 mHotseat == null) {
3115 continue;
3116 }
3117
Joe Onorato9c1289c2009-08-17 11:03:03 -04003118 switch (item.itemType) {
3119 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3120 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07003121 View shortcut = createShortcut((ShortcutInfo)item);
3122 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3123 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003124 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003125 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003126 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003127 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003128 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003129 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3130 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003131 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003133 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003134 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003135 }
3136
Joe Onorato9c1289c2009-08-17 11:03:03 -04003137 /**
3138 * Implementation of the method from LauncherModel.Callbacks.
3139 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003140 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003141 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003142 sFolders.clear();
3143 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003144 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003145
3146 /**
3147 * Add the views for a widget to the workspace.
3148 *
3149 * Implementation of the method from LauncherModel.Callbacks.
3150 */
3151 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003152 setLoadOnResume();
3153
Daniel Sandler843e8602010-06-07 14:59:01 -04003154 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3155 if (DEBUG_WIDGETS) {
3156 Log.d(TAG, "bindAppWidget: " + item);
3157 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003158 final Workspace workspace = mWorkspace;
3159
3160 final int appWidgetId = item.appWidgetId;
3161 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003162 if (DEBUG_WIDGETS) {
3163 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3164 }
3165
Joe Onorato9c1289c2009-08-17 11:03:03 -04003166 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3167
Joe Onorato9c1289c2009-08-17 11:03:03 -04003168 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3169 item.hostView.setTag(item);
3170
Adam Cohenca83eeb2012-02-29 15:17:57 -08003171 // We need to load the minimum span and embed it into the item info
3172 int[] minSpan = getMinSpanForWidget(appWidgetInfo, null);
3173 item.minSpanX = minSpan[0];
3174 item.minSpanY = minSpan[1];
3175
Winson Chung3d503fb2011-07-13 17:25:49 -07003176 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003177 item.cellY, item.spanX, item.spanY, false);
3178
Adam Cohended9f8d2010-11-03 13:25:16 -07003179 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3180
Joe Onorato9c1289c2009-08-17 11:03:03 -04003181 workspace.requestLayout();
3182
Daniel Sandler843e8602010-06-07 14:59:01 -04003183 if (DEBUG_WIDGETS) {
3184 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3185 + (SystemClock.uptimeMillis()-start) + "ms");
3186 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003187 }
3188
3189 /**
3190 * Callback saying that there aren't any more items to bind.
3191 *
3192 * Implementation of the method from LauncherModel.Callbacks.
3193 */
3194 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003195 setLoadOnResume();
3196
Joe Onorato9c1289c2009-08-17 11:03:03 -04003197 if (mSavedState != null) {
3198 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003199 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003200 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003201 mSavedState = null;
3202 }
3203
3204 if (mSavedInstanceState != null) {
3205 super.onRestoreInstanceState(mSavedInstanceState);
3206 mSavedInstanceState = null;
3207 }
3208
Joe Onorato9c1289c2009-08-17 11:03:03 -04003209 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003210
3211 // If we received the result of any pending adds while the loader was running (e.g. the
3212 // widget configuration forced an orientation change), process them now.
3213 for (int i = 0; i < sPendingAddList.size(); i++) {
3214 completeAdd(sPendingAddList.get(i));
3215 }
3216 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003217
Winson Chungc51db6a2011-10-05 11:44:49 -07003218 // Update the market app icon as necessary (the other icons will be managed in response to
3219 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003220 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003221
Michael Jurka6ee21d22012-02-21 18:20:27 -08003222 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003223 }
3224
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003225 @Override
3226 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003227 boolean searchVisible = updateGlobalSearchIcon();
3228 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3229 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003230 }
3231
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003232 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003233 * Add the icons for all apps.
3234 *
3235 * Implementation of the method from LauncherModel.Callbacks.
3236 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003237 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3238 // Remove the progress bar entirely; we could also make it GONE
3239 // but better to remove it since we know it's not going to be used
3240 View progressBar = mAppsCustomizeTabHost.
3241 findViewById(R.id.apps_customize_progress_bar);
3242 if (progressBar != null) {
3243 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003244 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003245 // We just post the call to setApps so the user sees the progress bar
3246 // disappear-- otherwise, it just looks like the progress bar froze
3247 // which doesn't look great
3248 mAppsCustomizeTabHost.post(new Runnable() {
3249 public void run() {
3250 if (mAppsCustomizeContent != null) {
3251 mAppsCustomizeContent.setApps(apps);
3252 }
3253 }
3254 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003255 }
3256
3257 /**
3258 * A package was installed.
3259 *
3260 * Implementation of the method from LauncherModel.Callbacks.
3261 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003262 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003263 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003264 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003265
Winson Chung785d2eb2011-04-14 16:08:02 -07003266 if (mAppsCustomizeContent != null) {
3267 mAppsCustomizeContent.addApps(apps);
3268 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003269 }
3270
3271 /**
3272 * A package was updated.
3273 *
3274 * Implementation of the method from LauncherModel.Callbacks.
3275 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003276 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003277 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003278 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003279 if (mWorkspace != null) {
3280 mWorkspace.updateShortcuts(apps);
3281 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003282
Winson Chung785d2eb2011-04-14 16:08:02 -07003283 if (mAppsCustomizeContent != null) {
3284 mAppsCustomizeContent.updateApps(apps);
3285 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003286 }
3287
3288 /**
3289 * A package was uninstalled.
3290 *
3291 * Implementation of the method from LauncherModel.Callbacks.
3292 */
Joe Onorato36115782010-06-17 13:28:48 -04003293 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003294 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003295 if (permanent) {
3296 mWorkspace.removeItems(apps);
3297 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003298
Winson Chung785d2eb2011-04-14 16:08:02 -07003299 if (mAppsCustomizeContent != null) {
3300 mAppsCustomizeContent.removeApps(apps);
3301 }
Winson Chunga1820962011-10-03 16:31:06 -07003302
3303 // Notify the drag controller
3304 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003305 }
Joe Onoratobe386092009-11-17 17:32:16 -08003306
3307 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003308 * A number of packages were updated.
3309 */
3310 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003311 if (mAppsCustomizeContent != null) {
3312 mAppsCustomizeContent.onPackagesUpdated();
3313 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003314 }
3315
Winson Chung400438b2011-01-16 17:53:48 -08003316 private int mapConfigurationOriActivityInfoOri(int configOri) {
3317 final Display d = getWindowManager().getDefaultDisplay();
3318 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3319 switch (d.getRotation()) {
3320 case Surface.ROTATION_0:
3321 case Surface.ROTATION_180:
3322 // We are currently in the same basic orientation as the natural orientation
3323 naturalOri = configOri;
3324 break;
3325 case Surface.ROTATION_90:
3326 case Surface.ROTATION_270:
3327 // We are currently in the other basic orientation to the natural orientation
3328 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3329 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3330 break;
3331 }
3332
3333 int[] oriMap = {
3334 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3335 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3336 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3337 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3338 };
3339 // Since the map starts at portrait, we need to offset if this device's natural orientation
3340 // is landscape.
3341 int indexOffset = 0;
3342 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3343 indexOffset = 1;
3344 }
3345 return oriMap[(d.getRotation() + indexOffset) % 4];
3346 }
Adam Cohen446e9402011-09-15 18:21:21 -07003347
3348 public void lockScreenOrientationOnLargeUI() {
3349 if (LauncherApplication.isScreenLarge()) {
3350 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3351 .getConfiguration().orientation));
3352 }
Winson Chung400438b2011-01-16 17:53:48 -08003353 }
Adam Cohen446e9402011-09-15 18:21:21 -07003354 public void unlockScreenOrientationOnLargeUI() {
3355 if (LauncherApplication.isScreenLarge()) {
3356 mHandler.postDelayed(new Runnable() {
3357 public void run() {
3358 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3359 }
3360 }, mRestoreScreenOrientationDelay);
3361 }
Winson Chung400438b2011-01-16 17:53:48 -08003362 }
3363
Winson Chung82f55532011-08-09 14:14:23 -07003364 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003365 private static final String PREFS_KEY = "com.android.launcher2.prefs";
3366 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003367 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003368 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003369
Winson Chung7d7541e2011-09-16 20:14:36 -07003370 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003371 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003372 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3373 Cling cling = (Cling) findViewById(clingId);
3374 if (cling != null) {
3375 cling.init(this, positionData);
3376 cling.setVisibility(View.VISIBLE);
3377 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3378 if (animate) {
3379 cling.buildLayer();
3380 cling.setAlpha(0f);
3381 cling.animate()
3382 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003383 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003384 .setDuration(SHOW_CLING_DURATION)
3385 .setStartDelay(delay)
3386 .start();
3387 } else {
3388 cling.setAlpha(1f);
3389 }
3390 }
3391 return cling;
3392 }
3393 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003394 if (cling != null) {
3395 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003396 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003397 anim.addListener(new AnimatorListenerAdapter() {
3398 public void onAnimationEnd(Animator animation) {
3399 cling.setVisibility(View.GONE);
3400 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003401 // We should update the shared preferences on a background thread
3402 new Thread("dismissClingThread") {
3403 public void run() {
3404 SharedPreferences.Editor editor = mSharedPrefs.edit();
3405 editor.putBoolean(flag, true);
3406 editor.commit();
3407 }
3408 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003409 };
3410 });
3411 anim.start();
3412 }
3413 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003414 private void removeCling(int id) {
3415 final View cling = findViewById(id);
3416 if (cling != null) {
3417 final ViewGroup parent = (ViewGroup) cling.getParent();
3418 parent.post(new Runnable() {
3419 @Override
3420 public void run() {
3421 parent.removeView(cling);
3422 }
3423 });
3424 }
3425 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003426 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003427 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003428 if (isClingsEnabled() &&
3429 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003430 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003431 } else {
3432 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003433 }
3434 }
3435 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003436 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003437 if (isClingsEnabled() &&
3438 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003439 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003440 } else {
3441 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003442 }
3443 }
3444 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003445 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003446 if (isClingsEnabled() &&
3447 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3448 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003449 } else {
3450 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003451 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003452 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003453 }
3454 public boolean isFolderClingVisible() {
3455 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003456 if (cling != null) {
3457 return cling.getVisibility() == View.VISIBLE;
3458 }
3459 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003460 }
Winson Chung82f55532011-08-09 14:14:23 -07003461 public void dismissWorkspaceCling(View v) {
3462 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003463 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003464 }
3465 public void dismissAllAppsCling(View v) {
3466 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003467 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3468 }
3469 public void dismissFolderCling(View v) {
3470 Cling cling = (Cling) findViewById(R.id.folder_cling);
3471 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003472 }
3473
Winson Chung80baf5a2010-08-09 16:03:15 -07003474 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003475 * Prints out out state for debugging.
3476 */
3477 public void dumpState() {
3478 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003479 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003480 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3481 Log.d(TAG, "mRestoring=" + mRestoring);
3482 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3483 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003484 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003485 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003486
Winson Chung785d2eb2011-04-14 16:08:02 -07003487 if (mAppsCustomizeContent != null) {
3488 mAppsCustomizeContent.dumpState();
3489 }
Joe Onoratobe386092009-11-17 17:32:16 -08003490 Log.d(TAG, "END launcher2 dump state");
3491 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003492
3493 @Override
3494 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3495 super.dump(prefix, fd, writer, args);
3496 writer.println(" ");
3497 writer.println("Debug logs: ");
3498 for (int i = 0; i < sDumpLogs.size(); i++) {
3499 writer.println(" " + sDumpLogs.get(i));
3500 }
3501 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003502}
Michael Jurka2763be32011-02-24 11:19:57 -08003503
Michael Jurkaabded662011-03-04 12:06:57 -08003504interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003505 View getContent();
Michael Jurkabed61d22012-02-14 22:51:29 -08003506 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003507 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003508 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003509}