blob: e78ee4a36805b13b035f820b7001f0fea4a1c4d5 [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;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070036import android.content.ClipData;
37import android.content.ClipDescription;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040053import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070054import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020056import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080057import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070058import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070059import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040060import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080061import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070062import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080063import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.Selection;
65import android.text.SpannableStringBuilder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070067import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080068import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080069import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.KeyEvent;
71import android.view.LayoutInflater;
72import android.view.Menu;
73import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070074import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080075import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080077import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070078import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080079import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080080import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070081import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080082import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070083import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070084import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080085import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070087import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070088import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070089import android.widget.TextView;
90import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070091
Adam Cohendf2cc412011-04-27 16:56:57 -070092import com.android.common.Search;
93import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070094import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080095
Adam Cohenc0dcf592011-06-01 15:30:43 -070096import java.io.DataInputStream;
97import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070098import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.FileNotFoundException;
100import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700101import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700103import java.util.Collection;
104import java.util.Collections;
105import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.HashSet;
108import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110/**
111 * Default launcher application.
112 */
Michael Jurka946ad472010-07-09 18:05:18 -0700113public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700114 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
115 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800116 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700117 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400120 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700121 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700122
Winson Chung70d72102011-08-12 11:24:35 -0700123 private static final int MENU_GROUP_WALLPAPER = 1;
124 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
125 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700126 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
127 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
129 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700130 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 private static final int REQUEST_PICK_APPLICATION = 6;
132 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700133 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700134 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
136 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
137
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800138 static final int SCREEN_COUNT = 5;
139 static final int DEFAULT_SCREEN = 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
Winson Chunga2413752012-04-03 14:22:34 -0700178 // How long to wait before the new-shortcut animation automatically pans the workspace
179 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
180
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800181 private final BroadcastReceiver mCloseSystemDialogsReceiver
182 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800183 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private LayoutInflater mInflater;
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800188 private View mQsbDivider;
189 private View mDockDivider;
190 private DragLayer mDragLayer;
191 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700192
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700193 private AppWidgetManager mAppWidgetManager;
194 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700195
Michael Jurkac9d95c52011-08-29 14:03:34 -0700196 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700197 private int[] mTmpAddItemCellCoordinates = new int[2];
198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 private FolderInfo mFolderInfo;
200
Winson Chung3d503fb2011-07-13 17:25:49 -0700201 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800202 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700203
Winson Chungc51db6a2011-10-05 11:44:49 -0700204 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700205 private AppsCustomizeTabHost mAppsCustomizeTabHost;
206 private AppsCustomizePagedView mAppsCustomizeContent;
207 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 private Bundle mSavedState;
210
211 private SpannableStringBuilder mDefaultKeySsb = null;
212
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 private boolean mWorkspaceLoading = true;
214
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800215 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private boolean mRestoring;
217 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700218 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219
220 private Bundle mSavedInstanceState;
221
Joe Onorato9c1289c2009-08-17 11:03:03 -0400222 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800223 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800224 private boolean mUserPresent = true;
225 private boolean mVisible = false;
226 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400227
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700228 private static LocaleConfiguration sLocaleConfiguration = null;
229
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700230 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700231
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700232 private Intent mAppMarketIntent = null;
233
Adam Cohended9f8d2010-11-03 13:25:16 -0700234 // Related to the auto-advancing of widgets
235 private final int ADVANCE_MSG = 1;
236 private final int mAdvanceInterval = 20000;
237 private final int mAdvanceStagger = 250;
238 private long mAutoAdvanceSentTime;
239 private long mAutoAdvanceTimeLeft = -1;
240 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
241 new HashMap<View, AppWidgetProviderInfo>();
242
Winson Chung400438b2011-01-16 17:53:48 -0800243 // Determines how long to wait after a rotation before restoring the screen orientation to
244 // match the sensor state.
245 private final int mRestoreScreenOrientationDelay = 500;
246
Michael Jurka4ef207b2010-11-29 17:05:45 -0800247 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700248 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
249 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
250 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800251
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700252 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohened66b2b2012-01-23 17:28:51 -0800253 PendingAddWidgetInfo mWidgetBeingConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700254
Winson Chung46353de2012-02-16 14:05:10 -0800255 // We only want to get the SharedPreferences once since it does an FS stat each time we get
256 // it from the context.
257 private SharedPreferences mSharedPrefs;
258
Winson Chungf0c6ae02012-03-21 16:10:31 -0700259 // Holds the page that we need to animate to, and the icon views that we need to animate up
260 // when we scroll to that page on resume.
261 private int mNewShortcutAnimatePage = -1;
262 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700263
Michael Jurkaddd62e92011-02-16 17:49:14 -0800264 private BubbleTextView mWaitingForResume;
265
Michael Jurkac1f5d262011-09-30 19:32:27 -0700266 private Runnable mBuildLayersRunnable = new Runnable() {
267 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700268 if (mWorkspace != null) {
269 mWorkspace.buildPageHardwareLayers();
270 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700271 }
272 };
273
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800274 private static ArrayList<PendingAddArguments> sPendingAddList
275 = new ArrayList<PendingAddArguments>();
276
277 private static class PendingAddArguments {
278 int requestCode;
279 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700280 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800281 int screen;
282 int cellX;
283 int cellY;
284 }
285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 @Override
287 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700288 if (DEBUG_STRICT_MODE) {
289 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
290 .detectDiskReads()
291 .detectDiskWrites()
292 .detectNetwork() // or .detectAll() for all detectable problems
293 .penaltyLog()
294 .build());
295 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
296 .detectLeakedSqlLiteObjects()
297 .detectLeakedClosableObjects()
298 .penaltyLog()
299 .penaltyDeath()
300 .build());
301 }
302
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800304 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700305 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
306 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800307 mModel = app.setLauncher(this);
308 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400309 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700311
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700312 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700313 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
314 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700315
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200317 android.os.Debug.startMethodTracing(
318 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 }
320
321 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 setContentView(R.layout.launcher);
323 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700324 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800326 registerContentObservers();
327
Joe Onorato7c312c12009-08-13 21:36:53 -0700328 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700329
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 mSavedState = savedInstanceState;
331 restoreState(mSavedState);
332
Winson Chunga12a2502010-12-20 14:41:35 -0800333 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700334 if (mAppsCustomizeContent != null) {
335 mAppsCustomizeContent.onPackagesUpdated();
336 }
Winson Chunga12a2502010-12-20 14:41:35 -0800337
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 if (PROFILE_STARTUP) {
339 android.os.Debug.stopMethodTracing();
340 }
341
342 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700343 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 }
345
Michael Jurkac57b7a82011-08-09 22:02:20 -0700346 if (!mModel.isAllAppsLoaded()) {
347 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
348 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
349 }
350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 // For handling default keys
352 mDefaultKeySsb = new SpannableStringBuilder();
353 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800354
355 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
356 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800357
Winson Chungc51db6a2011-10-05 11:44:49 -0700358 boolean searchVisible = false;
359 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800360 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700361 int coi = getCurrentOrientationIndexForGlobalIcons();
362 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
363 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700364 updateAppMarketIcon();
365 searchVisible = updateGlobalSearchIcon();
366 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700367 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700368 if (sGlobalSearchIcon[coi] != null) {
369 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700370 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700371 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700372 if (sVoiceSearchIcon[coi] != null) {
373 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700374 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700375 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700376 if (sAppMarketIcon[coi] != null) {
377 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800378 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700379 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700380
Adam Cohen23a4d302011-12-01 17:26:44 -0800381 final String forceEnableRotation =
382 SystemProperties.get(FORCE_ENABLE_ROTATION_PROPERTY, "false");
383
Adam Cohencb3f98e2012-04-24 14:24:35 -0700384 boolean enableRotation = getResources().getBoolean(R.bool.allow_rotation);
385
Adam Cohen446e9402011-09-15 18:21:21 -0700386 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohencb3f98e2012-04-24 14:24:35 -0700387 if (enableRotation || "true".equalsIgnoreCase(forceEnableRotation)) {
Adam Cohen446e9402011-09-15 18:21:21 -0700388 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
389 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 }
Romain Guycbb89e42009-06-08 15:52:54 -0700391
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700393 if (sLocaleConfiguration == null) {
394 new AsyncTask<Void, Void, LocaleConfiguration>() {
395 @Override
396 protected LocaleConfiguration doInBackground(Void... unused) {
397 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
398 readConfiguration(Launcher.this, localeConfiguration);
399 return localeConfiguration;
400 }
401
402 @Override
403 protected void onPostExecute(LocaleConfiguration result) {
404 sLocaleConfiguration = result;
405 checkForLocaleChange(); // recursive, but now with a locale configuration
406 }
407 }.execute();
408 return;
409 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700410
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 final Configuration configuration = getResources().getConfiguration();
412
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700413 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 final String locale = configuration.locale.toString();
415
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700416 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 final int mcc = configuration.mcc;
418
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700419 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 final int mnc = configuration.mnc;
421
Romain Guy84f296c2009-11-04 15:00:44 -0800422 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423
Romain Guy84f296c2009-11-04 15:00:44 -0800424 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700425 sLocaleConfiguration.locale = locale;
426 sLocaleConfiguration.mcc = mcc;
427 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700428
Joe Onorato0589f0f2010-02-08 13:44:00 -0800429 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700430
431 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
432 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700433 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700434 public void run() {
435 writeConfiguration(Launcher.this, localeConfiguration);
436 }
437 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700438 }
439 }
440
441 private static class LocaleConfiguration {
442 public String locale;
443 public int mcc = -1;
444 public int mnc = -1;
445 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700446
Romain Guy98d01652009-06-30 16:21:04 -0700447 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
448 DataInputStream in = null;
449 try {
450 in = new DataInputStream(context.openFileInput(PREFERENCES));
451 configuration.locale = in.readUTF();
452 configuration.mcc = in.readInt();
453 configuration.mnc = in.readInt();
454 } catch (FileNotFoundException e) {
455 // Ignore
456 } catch (IOException e) {
457 // Ignore
458 } finally {
459 if (in != null) {
460 try {
461 in.close();
462 } catch (IOException e) {
463 // Ignore
464 }
465 }
466 }
467 }
468
469 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
470 DataOutputStream out = null;
471 try {
472 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
473 out.writeUTF(configuration.locale);
474 out.writeInt(configuration.mcc);
475 out.writeInt(configuration.mnc);
476 out.flush();
477 } catch (FileNotFoundException e) {
478 // Ignore
479 } catch (IOException e) {
480 //noinspection ResultOfMethodCallIgnored
481 context.getFileStreamPath(PREFERENCES).delete();
482 } finally {
483 if (out != null) {
484 try {
485 out.close();
486 } catch (IOException e) {
487 // Ignore
488 }
489 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 }
491 }
492
Adam Cohen716b51e2011-06-30 12:09:54 -0700493 public DragLayer getDragLayer() {
494 return mDragLayer;
495 }
496
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800497 static int getScreen() {
498 synchronized (sLock) {
499 return sScreen;
500 }
501 }
502
503 static void setScreen(int screen) {
504 synchronized (sLock) {
505 sScreen = screen;
506 }
507 }
508
Winson Chung557d6ed2011-07-08 15:34:52 -0700509 /**
510 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
511 * a configuration step, this allows the proper animations to run after other transitions.
512 */
513 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700514 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800515 switch (args.requestCode) {
516 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700517 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
518 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800519 break;
520 case REQUEST_PICK_SHORTCUT:
521 processShortcut(args.intent);
522 break;
523 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700524 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
525 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700526 result = true;
527 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800528 case REQUEST_PICK_APPWIDGET:
529 addAppWidgetFromPick(args.intent);
530 break;
531 case REQUEST_CREATE_APPWIDGET:
532 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800533 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700534 result = true;
535 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800536 case REQUEST_PICK_WALLPAPER:
537 // We just wanted the activity result here so we can clear mWaitingForResult
538 break;
539 }
Michael Jurka27614d22012-04-02 06:40:22 -0700540 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
541 // if you turned the screen off and then back while in All Apps, Launcher would not
542 // return to the workspace. Clearing mAddInfo.container here fixes this issue
543 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700544 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800545 }
546
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800548 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700549 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800550 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
551 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700552 mWaitingForResult = false;
553
Adam Cohened66b2b2012-01-23 17:28:51 -0800554 // We have special handling for widgets
555 if (isWidgetDrop) {
556 int appWidgetId = data != null ?
557 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
558 completeTwoStageWidgetDrop(resultCode, appWidgetId);
559 return;
560 }
561
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 // The pattern used here is that a user PICKs a specific application,
563 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700564
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
566 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700567 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800568 final PendingAddArguments args = new PendingAddArguments();
569 args.requestCode = requestCode;
570 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700571 args.container = mPendingAddInfo.container;
572 args.screen = mPendingAddInfo.screen;
573 args.cellX = mPendingAddInfo.cellX;
574 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800575 if (isWorkspaceLocked()) {
576 sPendingAddList.add(args);
577 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700578 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800581 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700582 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800583 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
584 null);
585 }
586
587 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
588 CellLayout cellLayout = (CellLayout) mWorkspace.getChildAt(mWidgetBeingConfigured.screen);
589 Runnable onCompleteRunnable = null;
590 int animationType = 0;
591
592 if (resultCode == RESULT_OK) {
593 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
594 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
595 mWidgetBeingConfigured.info);
596 mWidgetBeingConfigured.boundWidget = layout;
597 onCompleteRunnable = new Runnable() {
598 @Override
599 public void run() {
600 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
601 mPendingAddInfo.screen, layout, null);
602 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
603 null);
604 }
605 };
606 } else if (resultCode == RESULT_CANCELED) {
607 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
608 onCompleteRunnable = new Runnable() {
609 @Override
610 public void run() {
611 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
612 null);
613 }
614 };
615 }
Adam Cohend41fbf52012-02-16 23:53:59 -0800616 mWorkspace.animateWidgetDrop(mWidgetBeingConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800617 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Adam Cohend41fbf52012-02-16 23:53:59 -0800618 animationType, mWidgetBeingConfigured.boundWidget, true);
Adam Cohen1b36dc32012-02-13 19:27:37 -0800619 mWidgetBeingConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 }
621
622 @Override
623 protected void onResume() {
624 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700625
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800626 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700627 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400628 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700629 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400630 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700631 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 }
Winson Chunge4e50662012-01-23 14:45:13 -0800633
634 // Reset the pressed state of icons that were locked in the press state while activities
635 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800636 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800637 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800638 mWaitingForResume.setStayPressed(false);
639 }
Winson Chunge4e50662012-01-23 14:45:13 -0800640 if (mAppsCustomizeContent != null) {
641 // Resets the previous all apps icon press state
642 mAppsCustomizeContent.resetDrawableState();
643 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 }
645
646 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700647 protected void onPause() {
648 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700649 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800650 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700651 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700652 }
Romain Guycbb89e42009-06-08 15:52:54 -0700653
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700654 @Override
655 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400656 // Flag the loader to stop early before switching
657 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700658 if (mAppsCustomizeContent != null) {
659 mAppsCustomizeContent.surrender();
660 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800661 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700662 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700663
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800664 // We can't hide the IME if it was forced open. So don't bother
665 /*
666 @Override
667 public void onWindowFocusChanged(boolean hasFocus) {
668 super.onWindowFocusChanged(hasFocus);
669
670 if (hasFocus) {
671 final InputMethodManager inputManager = (InputMethodManager)
672 getSystemService(Context.INPUT_METHOD_SERVICE);
673 WindowManager.LayoutParams lp = getWindow().getAttributes();
674 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
675 android.os.Handler()) {
676 protected void onReceiveResult(int resultCode, Bundle resultData) {
677 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
678 }
679 });
680 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
681 }
682 }
683 */
684
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 private boolean acceptFilter() {
686 final InputMethodManager inputManager = (InputMethodManager)
687 getSystemService(Context.INPUT_METHOD_SERVICE);
688 return !inputManager.isFullscreenMode();
689 }
690
691 @Override
692 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700693 final int uniChar = event.getUnicodeChar();
694 final boolean handled = super.onKeyDown(keyCode, event);
695 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
696 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
698 keyCode, event);
699 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700700 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700701 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700702 // showSearchDialog()
703 // If there are multiple keystrokes before the search dialog takes focus,
704 // onSearchRequested() will be called for every keystroke,
705 // but it is idempotent, so it's fine.
706 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 }
708 }
709
Joe Onorato8a9625e2010-01-28 15:55:35 -0800710 // Eat the long press event so the keyboard doesn't come up.
711 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
712 return true;
713 }
714
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 return handled;
716 }
717
Karl Rosaen138a0412009-04-23 19:00:21 -0700718 private String getTypedText() {
719 return mDefaultKeySsb.toString();
720 }
721
722 private void clearTypedText() {
723 mDefaultKeySsb.clear();
724 mDefaultKeySsb.clearSpans();
725 Selection.setSelection(mDefaultKeySsb, 0);
726 }
727
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700729 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
730 * State
731 */
732 private static State intToState(int stateOrdinal) {
733 State state = State.WORKSPACE;
734 final State[] stateValues = State.values();
735 for (int i = 0; i < stateValues.length; i++) {
736 if (stateValues[i].ordinal() == stateOrdinal) {
737 state = stateValues[i];
738 break;
739 }
740 }
741 return state;
742 }
743
744 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 * Restores the previous state, if it exists.
746 *
747 * @param savedState The previous state.
748 */
749 private void restoreState(Bundle savedState) {
750 if (savedState == null) {
751 return;
752 }
753
Michael Jurka883f55b2010-10-21 15:47:14 -0700754 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700755 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800756 showAllApps(false);
757 }
758
Winson Chungf0c6ae02012-03-21 16:10:31 -0700759 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700761 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 }
763
Winson Chung3d503fb2011-07-13 17:25:49 -0700764 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
765 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700766
Winson Chung3d503fb2011-07-13 17:25:49 -0700767 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
768 mPendingAddInfo.container = pendingAddContainer;
769 mPendingAddInfo.screen = pendingAddScreen;
770 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
771 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 mRestoring = true;
773 }
774
775 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
776 if (renameFolder) {
777 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700778 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 mRestoring = true;
780 }
Winson Chunga12a2502010-12-20 14:41:35 -0800781
Winson Chung785d2eb2011-04-14 16:08:02 -0700782
783 // Restore the AppsCustomize tab
784 if (mAppsCustomizeTabHost != null) {
785 String curTab = savedState.getString("apps_customize_currentTab");
786 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700787 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700788 mAppsCustomizeContent.setContentType(
789 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
790 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700791 mAppsCustomizeContent.loadAssociatedPages(
792 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700793 }
794
Winson Chung5afbf7b2011-07-25 11:53:08 -0700795 int currentIndex = savedState.getInt("apps_customize_currentIndex");
796 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700797 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 }
799
800 /**
801 * Finds all the views we need and configure them properly.
802 */
803 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700804 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400805
Winson Chung4c98d922011-05-31 16:50:48 -0700806 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
807 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800808 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
809 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810
Winson Chung4c98d922011-05-31 16:50:48 -0700811 // Setup the drag layer
812 mDragLayer.setup(this, dragController);
813
Winson Chung3d503fb2011-07-13 17:25:49 -0700814 // Setup the hotseat
815 mHotseat = (Hotseat) findViewById(R.id.hotseat);
816 if (mHotseat != null) {
817 mHotseat.setup(this);
818 }
819
Winson Chung4c98d922011-05-31 16:50:48 -0700820 // Setup the workspace
821 mWorkspace.setHapticFeedbackEnabled(false);
822 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700823 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700824 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700825
Winson Chungf0ea4d32011-06-06 14:27:16 -0700826 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700827 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700828
Winson Chungf0ea4d32011-06-06 14:27:16 -0700829 // Setup AppsCustomize
830 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
831 findViewById(R.id.apps_customize_pane);
832 mAppsCustomizeContent = (AppsCustomizePagedView)
833 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700834 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700835 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400836
Michael Jurka5130e402011-10-13 04:55:35 -0700837 // Get the all apps button
838 mAllAppsButton = findViewById(R.id.all_apps_button);
839 if (mAllAppsButton != null) {
840 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
841 @Override
842 public boolean onTouch(View v, MotionEvent event) {
843 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
844 onTouchDownAllAppsButton(v);
845 }
846 return false;
847 }
848 });
849 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700850 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700851 dragController.setDragScoller(mWorkspace);
852 dragController.setScrollView(mDragLayer);
853 dragController.setMoveTarget(mWorkspace);
854 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700855 if (mSearchDropTargetBar != null) {
856 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800857 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 }
859
860 /**
861 * Creates a view representing a shortcut.
862 *
863 * @param info The data structure describing the shortcut.
864 *
865 * @return A View inflated from R.layout.application.
866 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800867 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700869 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 }
871
872 /**
873 * Creates a view representing a shortcut inflated from the specified resource.
874 *
875 * @param layoutResId The id of the XML layout used to create the shortcut.
876 * @param parent The group the shortcut belongs to.
877 * @param info The data structure describing the shortcut.
878 *
879 * @return A View inflated from layoutResId.
880 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800881 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800882 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
883 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 return favorite;
886 }
887
888 /**
889 * Add an application shortcut to the workspace.
890 *
891 * @param data The intent describing the application.
892 * @param cellInfo The position on screen where to create the shortcut.
893 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700894 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700895 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700896 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700897
Adam Cohenfbba09b2011-07-18 21:43:05 -0700898 // First we check if we already know the exact location where we want to add this item.
899 if (cellX >= 0 && cellY >= 0) {
900 cellXY[0] = cellX;
901 cellXY[1] = cellY;
902 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700903 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700904 return;
905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800907 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800908
Romain Guy73b979d2009-06-09 12:57:21 -0700909 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800910 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800912 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700913 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700914 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800915 } else {
916 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 }
918 }
Romain Guycbb89e42009-06-08 15:52:54 -0700919
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 /**
921 * Add a shortcut to the workspace.
922 *
923 * @param data The intent describing the shortcut.
924 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700926 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
927 int cellY) {
928 int[] cellXY = mTmpAddItemCellCoordinates;
929 int[] touchXY = mPendingAddInfo.dropPos;
930 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700931
Michael Jurkad3ef3062010-11-23 16:23:58 -0800932 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700933
Adam Cohen558baaf2011-08-15 15:22:57 -0700934 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800935 if (info == null) {
936 return;
937 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700938 final View view = createShortcut(info);
939
Adam Cohenfbba09b2011-07-18 21:43:05 -0700940 // First we check if we already know the exact location where we want to add this item.
941 if (cellX >= 0 && cellY >= 0) {
942 cellXY[0] = cellX;
943 cellXY[1] = cellY;
944 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700945
946 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800947 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700948 true, null,null)) {
949 return;
950 }
951 DragObject dragObject = new DragObject();
952 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -0800953 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
954 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -0700955 return;
956 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700957 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800958 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700959 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800960 foundCellSpan = (result != null);
961 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700962 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800963 }
964
965 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -0700966 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700967 return;
968 }
969
Adam Cohen558baaf2011-08-15 15:22:57 -0700970 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971
972 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700973 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
974 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 }
976 }
977
Winson Chunga5c96362012-04-12 14:04:41 -0700978 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800979 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700980 // We want to account for the extra amount of padding that we are adding to the widget
981 // to ensure that it gets the full amount of space that it has requested
982 int requiredWidth = minWidth + padding.left + padding.right;
983 int requiredHeight = minHeight + padding.top + padding.bottom;
984 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
985 }
986
Winson Chunga5c96362012-04-12 14:04:41 -0700987 int[] getSpanForWidget(AppWidgetProviderInfo info) {
988 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -0700989 }
990
Winson Chunga5c96362012-04-12 14:04:41 -0700991 int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
992 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -0700993 }
994
Winson Chunga5c96362012-04-12 14:04:41 -0700995 int[] getSpanForWidget(PendingAddWidgetInfo info) {
996 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -0700997 }
998
Winson Chunga5c96362012-04-12 14:04:41 -0700999 int[] getMinSpanForWidget(PendingAddWidgetInfo info) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001000 return getSpanForWidget(info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001001 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001002 }
1003
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001005 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001007 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001008 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001010 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1011 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1012 if (appWidgetInfo == null) {
1013 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1014 }
Romain Guycbb89e42009-06-08 15:52:54 -07001015
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001016 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001017 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001018
Winson Chunga5c96362012-04-12 14:04:41 -07001019 int[] minSpanXY = getMinSpanForWidget(appWidgetInfo);
1020 int[] spanXY = getSpanForWidget(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001021
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001023 // We have saved the position to which the widget was dragged-- this really only matters
1024 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001025 int[] cellXY = mTmpAddItemCellCoordinates;
1026 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001027 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001028 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001029 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1030 cellXY[0] = mPendingAddInfo.cellX;
1031 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001032 spanXY[0] = mPendingAddInfo.spanX;
1033 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001034 foundCellSpan = true;
1035 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001036 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001037 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001038 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1039 spanXY[1], cellXY, finalSpan);
1040 spanXY[0] = finalSpan[0];
1041 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001042 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001043 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001044 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001045 }
1046
Michael Jurka0280c3b2010-09-17 15:00:07 -07001047 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001048 if (appWidgetId != -1) {
1049 // Deleting an app widget ID is a void call but writes to disk before returning
1050 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001051 new Thread("deleteAppWidgetId") {
1052 public void run() {
1053 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1054 }
1055 }.start();
1056 }
Winson Chung93eef082012-03-23 15:59:27 -07001057 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001058 return;
1059 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001061 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -07001062 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001063 launcherInfo.spanX = spanXY[0];
1064 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001065 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1066 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001069 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070
1071 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001072 if (hostView == null) {
1073 // Perform actual inflation because we're live
1074 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1075 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1076 } else {
1077 // The AppWidgetHostView has already been inflated and instantiated
1078 launcherInfo.hostView = hostView;
1079 }
Romain Guycbb89e42009-06-08 15:52:54 -07001080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001082 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung3d503fb2011-07-13 17:25:49 -07001083 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001084 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001085
1086 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001088 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 }
Romain Guycbb89e42009-06-08 15:52:54 -07001090
Adam Cohended9f8d2010-11-03 13:25:16 -07001091 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1092 @Override
1093 public void onReceive(Context context, Intent intent) {
1094 final String action = intent.getAction();
1095 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1096 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001097 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001098 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001099
Winson Chungc100e8e2011-08-09 16:02:43 -07001100 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001101 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001102 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1103 mAppsCustomizeTabHost.reset();
1104 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001105 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001106 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1107 mUserPresent = true;
1108 updateRunning();
1109 }
1110 }
1111 };
1112
1113 @Override
1114 public void onAttachedToWindow() {
1115 super.onAttachedToWindow();
1116
1117 // Listen for broadcasts related to user-presence
1118 final IntentFilter filter = new IntentFilter();
1119 filter.addAction(Intent.ACTION_SCREEN_OFF);
1120 filter.addAction(Intent.ACTION_USER_PRESENT);
1121 registerReceiver(mReceiver, filter);
1122
Adam Cohend113e0c2010-11-11 10:48:05 -08001123 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001124 mVisible = true;
1125 }
1126
1127 @Override
1128 public void onDetachedFromWindow() {
1129 super.onDetachedFromWindow();
1130 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001131 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001132
Adam Cohend113e0c2010-11-11 10:48:05 -08001133 if (mAttached) {
1134 unregisterReceiver(mReceiver);
1135 mAttached = false;
1136 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001137 updateRunning();
1138 }
1139
1140 public void onWindowVisibilityChanged(int visibility) {
1141 mVisible = visibility == View.VISIBLE;
1142 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001143 // The following code used to be in onResume, but it turns out onResume is called when
1144 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1145 // is a more appropriate event to handle
1146 if (mVisible) {
1147 mAppsCustomizeTabHost.onWindowVisible();
1148 if (!mWorkspaceLoading) {
1149 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001150 // We want to let Launcher draw itself at least once before we force it to build
1151 // layers on all the workspace pages, so that transitioning to Launcher from other
1152 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1153 // a true draw so we wait until the second preDraw call to be safe
1154 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001155 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001156 // We delay the layer building a bit in order to give
1157 // other message processing a time to run. In particular
1158 // this avoids a delay in hiding the IME if it was
1159 // currently shown, because doing that may involve
1160 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001161 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurka6ee21d22012-02-21 18:20:27 -08001162 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001163 return true;
1164 }
1165 });
1166 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001167 // When Launcher comes back to foreground, a different Activity might be responsible for
1168 // the app market intent, so refresh the icon
1169 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001170 clearTypedText();
1171 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001172 }
1173
1174 private void sendAdvanceMessage(long delay) {
1175 mHandler.removeMessages(ADVANCE_MSG);
1176 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1177 mHandler.sendMessageDelayed(msg, delay);
1178 mAutoAdvanceSentTime = System.currentTimeMillis();
1179 }
1180
1181 private void updateRunning() {
1182 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1183 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1184 mAutoAdvanceRunning = autoAdvanceRunning;
1185 if (autoAdvanceRunning) {
1186 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1187 sendAdvanceMessage(delay);
1188 } else {
1189 if (!mWidgetsToAdvance.isEmpty()) {
1190 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1191 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1192 }
1193 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001194 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001195 }
1196 }
1197 }
1198
1199 private final Handler mHandler = new Handler() {
1200 @Override
1201 public void handleMessage(Message msg) {
1202 if (msg.what == ADVANCE_MSG) {
1203 int i = 0;
1204 for (View key: mWidgetsToAdvance.keySet()) {
1205 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1206 final int delay = mAdvanceStagger * i;
1207 if (v instanceof Advanceable) {
1208 postDelayed(new Runnable() {
1209 public void run() {
1210 ((Advanceable) v).advance();
1211 }
1212 }, delay);
1213 }
1214 i++;
1215 }
1216 sendAdvanceMessage(mAdvanceInterval);
1217 }
1218 }
1219 };
1220
1221 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001222 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001223 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1224 if (v instanceof Advanceable) {
1225 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001226 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001227 updateRunning();
1228 }
1229 }
1230
1231 void removeWidgetToAutoAdvance(View hostView) {
1232 if (mWidgetsToAdvance.containsKey(hostView)) {
1233 mWidgetsToAdvance.remove(hostView);
1234 updateRunning();
1235 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001236 }
1237
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001239 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001240 launcherInfo.hostView = null;
1241 }
1242
Winson Chung93eef082012-03-23 15:59:27 -07001243 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1244 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1245 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001246 }
1247
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001248 public LauncherAppWidgetHost getAppWidgetHost() {
1249 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 }
Romain Guycbb89e42009-06-08 15:52:54 -07001251
Winson Chunga9abd0e2010-10-27 17:18:37 -07001252 public LauncherModel getModel() {
1253 return mModel;
1254 }
1255
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001256 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001257 getWindow().closeAllPanels();
1258
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001259 // Whatever we were doing is hereby canceled.
1260 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001261 }
1262
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 @Override
1264 protected void onNewIntent(Intent intent) {
1265 super.onNewIntent(intent);
1266
1267 // Close the menu
1268 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001269 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001270 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001271
Joe Onorato14f122b2009-11-19 14:06:36 -08001272 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1273 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001274
Adam Cohenac56cff2011-09-28 20:45:37 -07001275 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001276 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001277 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001278 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1279 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001280 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001281 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001282
1283 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001284 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001285 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001286
Joe Onorato3a8820b2009-11-10 15:06:42 -08001287 final View v = getWindow().peekDecorView();
1288 if (v != null && v.getWindowToken() != null) {
1289 InputMethodManager imm = (InputMethodManager)getSystemService(
1290 INPUT_METHOD_SERVICE);
1291 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001293
Michael Jurka35aa14d2011-07-07 17:01:08 -07001294 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001295 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001296 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001297 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 }
1299 }
1300
1301 @Override
1302 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1303 // Do not call super here
1304 mSavedInstanceState = savedInstanceState;
1305 }
1306
1307 @Override
1308 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001309 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001310 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311
Michael Jurka883f55b2010-10-21 15:47:14 -07001312 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001313 // We close any open folder since it will not be re-opened, and we need to make sure
1314 // this state is reflected.
1315 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316
Winson Chung3d503fb2011-07-13 17:25:49 -07001317 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1318 mWaitingForResult) {
1319 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1320 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1321 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1322 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 }
1324
1325 if (mFolderInfo != null && mWaitingForResult) {
1326 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1327 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1328 }
Michael Jurka946ad472010-07-09 18:05:18 -07001329
Winson Chung785d2eb2011-04-14 16:08:02 -07001330 // Save the current AppsCustomize tab
1331 if (mAppsCustomizeTabHost != null) {
1332 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1333 if (currentTabTag != null) {
1334 outState.putString("apps_customize_currentTab", currentTabTag);
1335 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001336 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1337 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001338 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 }
1340
1341 @Override
1342 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001344
Winson Chunge7a03942011-08-05 15:05:12 -07001345 // Remove all pending runnables
1346 mHandler.removeMessages(ADVANCE_MSG);
1347 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001348 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001349
Winson Chungcd2b0142011-06-08 16:02:26 -07001350 // Stop callbacks from LauncherModel
1351 LauncherApplication app = ((LauncherApplication) getApplication());
1352 mModel.stopLoader();
1353 app.setLauncher(null);
1354
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001356 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001358 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001360 mAppWidgetHost = null;
1361
1362 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363
1364 TextKeyListener.getInstance().release();
1365
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366
Winson Chung3d503fb2011-07-13 17:25:49 -07001367 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001368
1369 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001370 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001371
1372 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1373 mWorkspace.removeAllViews();
1374 mWorkspace = null;
1375 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001376
1377 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 }
1379
Adam Cohena9cf38f2011-05-02 15:36:58 -07001380 public DragController getDragController() {
1381 return mDragController;
1382 }
1383
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 @Override
1385 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001386 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 super.startActivityForResult(intent, requestCode);
1388 }
1389
Winson Chung70d72102011-08-12 11:24:35 -07001390 /**
1391 * Indicates that we want global search for this activity by setting the globalSearch
1392 * argument for {@link #startSearch} to true.
1393 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001395 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001397
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001398 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001399
Karl Rosaen138a0412009-04-23 19:00:21 -07001400 if (initialQuery == null) {
1401 // Use any text typed in the launcher as the initial query
1402 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001403 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 if (appSearchData == null) {
1405 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001406 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001408 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001409
Karl Rosaen138a0412009-04-23 19:00:21 -07001410 final SearchManager searchManager =
1411 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001412 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001413 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 }
1415
Winson Chung70d72102011-08-12 11:24:35 -07001416 @Override
1417 public boolean onCreateOptionsMenu(Menu menu) {
1418 if (isWorkspaceLocked()) {
1419 return false;
1420 }
1421
1422 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001423
1424 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1425 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1426 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001427 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1428 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1429 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001430 String helpUrl = getString(R.string.help_url);
1431 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001432 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1433 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1434
Winson Chung70d72102011-08-12 11:24:35 -07001435 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1436 .setIcon(android.R.drawable.ic_menu_gallery)
1437 .setAlphabeticShortcut('W');
1438 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1439 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001440 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001441 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001442 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1443 .setIcon(android.R.drawable.ic_menu_preferences)
1444 .setIntent(settings)
1445 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001446 if (!helpUrl.isEmpty()) {
1447 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1448 .setIcon(android.R.drawable.ic_menu_help)
1449 .setIntent(help)
1450 .setAlphabeticShortcut('H');
1451 }
Winson Chung70d72102011-08-12 11:24:35 -07001452 return true;
1453 }
1454
1455 @Override
1456 public boolean onPrepareOptionsMenu(Menu menu) {
1457 super.onPrepareOptionsMenu(menu);
1458
1459 if (mAppsCustomizeTabHost.isTransitioning()) {
1460 return false;
1461 }
1462 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1463 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1464
1465 return true;
1466 }
1467
1468 @Override
1469 public boolean onOptionsItemSelected(MenuItem item) {
1470 switch (item.getItemId()) {
1471 case MENU_WALLPAPER_SETTINGS:
1472 startWallpaper();
1473 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001474 }
1475
1476 return super.onOptionsItemSelected(item);
1477 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001479 @Override
1480 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001481 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001482 // Use a custom animation for launching search
1483 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001484 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001485 }
1486
Joe Onorato9c1289c2009-08-17 11:03:03 -04001487 public boolean isWorkspaceLocked() {
1488 return mWorkspaceLoading || mWaitingForResult;
1489 }
1490
Michael Jurka0280c3b2010-09-17 15:00:07 -07001491 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001492 mPendingAddInfo.container = ItemInfo.NO_ID;
1493 mPendingAddInfo.screen = -1;
1494 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1495 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001496 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001497 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001498 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001499
Michael Jurkaaf442092010-06-10 17:01:57 -07001500 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001501 // TODO: catch bad widget exception when sent
1502 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001503 // TODO: Is this log message meaningful?
1504 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001505 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001506 }
1507
Adam Cohened66b2b2012-01-23 17:28:51 -08001508 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1509 final AppWidgetProviderInfo appWidget = info.info;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001510 if (appWidget.configure != null) {
1511 // Launch over to configure widget, if needed
1512 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1513 intent.setComponent(appWidget.configure);
1514 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001515 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001516 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001517 intent.putExtra(InstallWidgetReceiver.
1518 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001519
1520 final String mimeType = info.mimeType;
1521 final ClipData clipData = (ClipData) info.configurationData;
1522 final ClipDescription clipDesc = clipData.getDescription();
1523 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1524 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001525 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001526 final CharSequence stringData = item.getText();
1527 final Uri uriData = item.getUri();
1528 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001529 final String key = InstallWidgetReceiver.
1530 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001531 if (uriData != null) {
1532 intent.putExtra(key, uriData);
1533 } else if (intentData != null) {
1534 intent.putExtra(key, intentData);
1535 } else if (stringData != null) {
1536 intent.putExtra(key, stringData);
1537 }
1538 break;
1539 }
1540 }
1541 }
Winson Chung55cef262010-10-28 14:14:18 -07001542 }
Romain Guy8e633c52010-03-04 12:51:36 -08001543 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Adam Cohened66b2b2012-01-23 17:28:51 -08001544 mWidgetBeingConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001546 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001547 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001548 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001549 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 }
1551 }
Romain Guycbb89e42009-06-08 15:52:54 -07001552
Adam Cohenfbba09b2011-07-18 21:43:05 -07001553 /**
1554 * Process a shortcut drop.
1555 *
1556 * @param componentName The name of the component
1557 * @param screen The screen where it should be added
1558 * @param cell The cell it should be added to, optional
1559 * @param position The location on the screen where it was dropped, optional
1560 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001561 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1562 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001563 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001564 mPendingAddInfo.container = container;
1565 mPendingAddInfo.screen = screen;
1566 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001567
1568 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001569 mPendingAddInfo.cellX = cell[0];
1570 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001571 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001572
1573 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1574 createShortcutIntent.setComponent(componentName);
1575 processShortcut(createShortcutIntent);
1576 }
1577
Adam Cohenfbba09b2011-07-18 21:43:05 -07001578 /**
1579 * Process a widget drop.
1580 *
1581 * @param info The PendingAppWidgetInfo of the widget being added.
1582 * @param screen The screen where it should be added
1583 * @param cell The cell it should be added to, optional
1584 * @param position The location on the screen where it was dropped, optional
1585 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001586 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001587 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001588 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001589 mPendingAddInfo.container = info.container = container;
1590 mPendingAddInfo.screen = info.screen = screen;
1591 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001592 mPendingAddInfo.minSpanX = info.minSpanX;
1593 mPendingAddInfo.minSpanY = info.minSpanY;
1594
Adam Cohenfbba09b2011-07-18 21:43:05 -07001595 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001596 mPendingAddInfo.cellX = cell[0];
1597 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001598 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001599 if (span != null) {
1600 mPendingAddInfo.spanX = span[0];
1601 mPendingAddInfo.spanY = span[1];
1602 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001603
Adam Cohened66b2b2012-01-23 17:28:51 -08001604 AppWidgetHostView hostView = info.boundWidget;
1605 int appWidgetId;
1606 if (hostView != null) {
1607 appWidgetId = hostView.getAppWidgetId();
1608 } else {
1609 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1610 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1611 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001612 addAppWidgetImpl(appWidgetId, info);
1613 }
1614
Joe Onoratodeb98af2010-02-19 14:59:39 -08001615 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001616 // Handle case where user selected "Applications"
1617 String applicationName = getResources().getString(R.string.group_applications);
1618 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001619
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001620 if (applicationName != null && applicationName.equals(shortcutName)) {
1621 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1622 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001623
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001624 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1625 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001626 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001627 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001628 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001629 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001630 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 }
1632
Winson Chung24ab2f12010-09-16 14:10:47 -07001633 void processWallpaper(Intent intent) {
1634 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1635 }
1636
Winson Chung3d503fb2011-07-13 17:25:49 -07001637 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1638 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001639 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 folderInfo.title = getText(R.string.folder_name);
1641
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001643 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1644 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001645 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646
1647 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001648 FolderIcon newFolder =
1649 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1650 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1651 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001652 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 }
Romain Guycbb89e42009-06-08 15:52:54 -07001654
Joe Onorato9c1289c2009-08-17 11:03:03 -04001655 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001656 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001657 }
1658
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001660 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001662 Intent chooser = Intent.createChooser(pickWallpaper,
1663 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001664 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1665 // Removed in Eclair MR1
1666// WallpaperManager wm = (WallpaperManager)
1667// getSystemService(Context.WALLPAPER_SERVICE);
1668// WallpaperInfo wi = wm.getWallpaperInfo();
1669// if (wi != null && wi.getSettingsActivity() != null) {
1670// LabeledIntent li = new LabeledIntent(getPackageName(),
1671// R.string.configure_wallpaper, 0);
1672// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1673// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1674// }
Mike Clerona0618e42009-10-22 13:55:21 -07001675 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 }
1677
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001678 /**
1679 * Registers various content observers. The current implementation registers
1680 * only a favorites observer to keep track of the favorites applications.
1681 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001682 private void registerContentObservers() {
1683 ContentResolver resolver = getContentResolver();
1684 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1685 true, mWidgetObserver);
1686 }
1687
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 @Override
1689 public boolean dispatchKeyEvent(KeyEvent event) {
1690 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1691 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001693 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001694 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001695 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001696 dumpState();
1697 return true;
1698 }
1699 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001700 }
1701 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1702 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001703 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 return true;
1705 }
1706 }
1707
1708 return super.dispatchKeyEvent(event);
1709 }
1710
Joe Onorato88ec0992009-11-19 13:16:06 -08001711 @Override
1712 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001713 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001714 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001715 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001716 Folder openFolder = mWorkspace.getOpenFolder();
1717 if (openFolder.isEditingName()) {
1718 openFolder.dismissEditingName();
1719 } else {
1720 closeFolder();
1721 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001722 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001723 mWorkspace.exitWidgetResizeMode();
1724
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001725 // Back button is a no-op here, but give at least some feedback for the button press
1726 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001727 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001728 }
1729
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001731 * Re-listen when widgets are reset.
1732 */
1733 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001734 if (mAppWidgetHost != null) {
1735 mAppWidgetHost.startListening();
1736 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001737 }
1738
1739 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001740 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1741 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001743 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001744 if (mModel != null) {
1745 mModel.unbindWorkspaceItems();
1746 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001748
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 /**
1750 * Launches the intent referred by the clicked shortcut.
1751 *
1752 * @param v The view representing the clicked shortcut.
1753 */
1754 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001755 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1756 // view has detached (it's possible for this to happen if the view is removed mid touch).
1757 if (v.getWindowToken() == null) {
1758 return;
1759 }
1760
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001761 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001762 return;
1763 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001764
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001766 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001768 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001769 int[] pos = new int[2];
1770 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001771 intent.setSourceBounds(new Rect(pos[0], pos[1],
1772 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001773
1774 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001775
1776 if (success && v instanceof BubbleTextView) {
1777 mWaitingForResume = (BubbleTextView) v;
1778 mWaitingForResume.setStayPressed(true);
1779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001781 if (v instanceof FolderIcon) {
1782 FolderIcon fi = (FolderIcon) v;
1783 handleFolderClick(fi);
1784 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001785 } else if (v == mAllAppsButton) {
1786 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001787 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001788 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001789 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001790 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 }
1792 }
1793
Michael Jurka0e260592010-06-30 17:07:39 -07001794 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001795 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001796 // clicking anywhere on the workspace causes the customization drawer to slide down
1797 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001798 return false;
1799 }
1800
Michael Jurkaaf442092010-06-10 17:01:57 -07001801 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001802 * Event handler for the search button
1803 *
1804 * @param v The view that was clicked.
1805 */
1806 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001807 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1808
Amith Yamasania135ba82011-08-09 17:42:01 -07001809 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001810 }
1811
1812 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001813 * Event handler for the voice button
1814 *
1815 * @param v The view that was clicked.
1816 */
1817 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001818 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001819
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001820 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001821 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001822 startActivitySafely(null, intent, "onClickVoiceButton");
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001823 }
1824
1825 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001826 * Event handler for the "grid" button that appears on the home screen, which
1827 * enters all apps mode.
1828 *
1829 * @param v The view that was clicked.
1830 */
1831 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001832 showAllApps(true);
1833 }
1834
1835 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001836 // Provide the same haptic feedback that the system offers for virtual keys.
1837 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001838 }
1839
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001840 public void onClickAppMarketButton(View v) {
1841 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001842 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001843 } else {
1844 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001845 }
1846 }
1847
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001848 void startApplicationDetailsActivity(ComponentName componentName) {
1849 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001850 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1851 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001852 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001853 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001854 }
1855
Patrick Dubroy5539af72010-09-07 15:22:01 -07001856 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1857 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1858 // System applications cannot be installed. For now, show a toast explaining that.
1859 // We may give them the option of disabling apps this way.
1860 int messageId = R.string.uninstall_system_app_text;
1861 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1862 } else {
1863 String packageName = appInfo.componentName.getPackageName();
1864 String className = appInfo.componentName.getClassName();
1865 Intent intent = new Intent(
1866 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001867 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1868 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001869 startActivity(intent);
1870 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001871 }
1872
Winson Chungc7450e32012-04-17 17:34:08 -07001873 boolean startActivitySafely(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001875
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 try {
Winson Chungc7450e32012-04-17 17:34:08 -07001877 if (v != null) {
1878 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1879 v.getMeasuredWidth(), v.getMeasuredHeight());
1880
1881 startActivity(intent, opts.toBundle());
1882 } else {
1883 startActivity(intent);
1884 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001885 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 } catch (ActivityNotFoundException e) {
1887 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001888 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 } catch (SecurityException e) {
1890 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001891 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001893 "or use the exported attribute for this activity. "
1894 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001896 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001898
Romain Guy8e633c52010-03-04 12:51:36 -08001899 void startActivityForResultSafely(Intent intent, int requestCode) {
1900 try {
1901 startActivityForResult(intent, requestCode);
1902 } catch (ActivityNotFoundException e) {
1903 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1904 } catch (SecurityException e) {
1905 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1906 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1907 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1908 "or use the exported attribute for this activity.", e);
1909 }
1910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911
Adam Cohena9cf38f2011-05-02 15:36:58 -07001912 private void handleFolderClick(FolderIcon folderIcon) {
1913 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001914 Folder openFolder = mWorkspace.getFolderForTag(info);
1915
1916 // If the folder info reports that the associated folder is open, then verify that
1917 // it is actually opened. There have been a few instances where this gets out of sync.
1918 if (info.opened && openFolder == null) {
1919 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1920 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1921 info.opened = false;
1922 }
1923
Adam Cohena9cf38f2011-05-02 15:36:58 -07001924 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 // Close any open folder
1926 closeFolder();
1927 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001928 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 } else {
1930 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 int folderScreen;
1932 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001933 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 // .. and close it
1935 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001936 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 // Close any folder open on the current screen
1938 closeFolder();
1939 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001940 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 }
1942 }
1943 }
1944 }
1945
Adam Cohen2801caf2011-05-13 20:57:39 -07001946 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001947 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001948 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1949 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1950 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1951
Adam Cohenc51934b2011-07-26 21:07:43 -07001952 FolderInfo info = (FolderInfo) fi.getTag();
1953 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1954 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001955 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1956 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001957 }
1958
Adam Cohen2801caf2011-05-13 20:57:39 -07001959 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1960 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1961 oa.start();
1962 }
1963
1964 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001965 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001966 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1967 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1968 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1969
Adam Cohenc51934b2011-07-26 21:07:43 -07001970 FolderInfo info = (FolderInfo) fi.getTag();
1971 CellLayout cl = null;
1972 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1973 cl = (CellLayout) fi.getParent().getParent();
1974 }
1975
1976 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001977 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1978 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001979 oa.addListener(new AnimatorListenerAdapter() {
1980 @Override
1981 public void onAnimationEnd(Animator animation) {
1982 if (layout != null) {
1983 layout.clearFolderLeaveBehind();
1984 }
1985 }
1986 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001987 oa.start();
1988 }
1989
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001991 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 * is animated relative to the specified View. If the View is null, no animation
1993 * is played.
1994 *
1995 * @param folderInfo The FolderInfo describing the folder to open.
1996 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001997 public void openFolder(FolderIcon folderIcon) {
1998 Folder folder = folderIcon.mFolder;
1999 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000
Adam Cohen2801caf2011-05-13 20:57:39 -07002001 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002002 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003
Adam Cohen4554ee12011-08-03 16:13:21 -07002004 // Just verify that the folder hasn't already been added to the DragLayer.
2005 // There was a one-off crash where the folder had a parent already.
2006 if (folder.getParent() == null) {
2007 mDragLayer.addView(folder);
2008 mDragController.addDropTarget((DropTarget) folder);
2009 } else {
2010 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2011 folder.getParent() + ").");
2012 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002013 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002014 }
2015
2016 public void closeFolder() {
2017 Folder folder = mWorkspace.getOpenFolder();
2018 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002019 if (folder.isEditingName()) {
2020 folder.dismissEditingName();
2021 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002022 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002023
2024 // Dismiss the folder cling
2025 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002026 }
2027 }
2028
2029 void closeFolder(Folder folder) {
2030 folder.getInfo().opened = false;
2031
2032 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2033 if (parent != null) {
2034 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2035 shrinkAndFadeInFolderIcon(fi);
2036 }
2037 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 }
2039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002041
Patrick Dubroye708c522011-03-01 16:03:43 -08002042 if (mState != State.WORKSPACE) {
2043 return false;
2044 }
2045
Joe Onorato9c1289c2009-08-17 11:03:03 -04002046 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 return false;
2048 }
2049
2050 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002051 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 }
2053
Michael Jurka0280c3b2010-09-17 15:00:07 -07002054 resetAddInfo();
2055 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002057 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 return true;
2059 }
2060
Winson Chung3d503fb2011-07-13 17:25:49 -07002061 // The hotseat touch handling does not go through Workspace, and we always allow long press
2062 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002063 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002064 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2065 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002066 if (itemUnderLongClick == null) {
2067 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002068 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2069 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002070 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002072 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002074 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 }
2076 }
2077 }
2078 return true;
2079 }
2080
Winson Chung3d503fb2011-07-13 17:25:49 -07002081 boolean isHotseatLayout(View layout) {
2082 return mHotseat != null && layout != null &&
2083 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2084 }
2085 Hotseat getHotseat() {
2086 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002087 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002088 SearchDropTargetBar getSearchBar() {
2089 return mSearchDropTargetBar;
2090 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002091
Winson Chung3d503fb2011-07-13 17:25:49 -07002092 /**
2093 * Returns the CellLayout of the specified container at the specified screen.
2094 */
2095 CellLayout getCellLayout(long container, int screen) {
2096 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2097 if (mHotseat != null) {
2098 return mHotseat.getLayout();
2099 } else {
2100 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002101 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002102 } else {
2103 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002104 }
2105 }
2106
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 Workspace getWorkspace() {
2108 return mWorkspace;
2109 }
2110
Daniel Sandler843e8602010-06-07 14:59:01 -04002111 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2112 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002113 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002114 }
2115
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002116 public boolean isAllAppsButtonRank(int rank) {
2117 return mHotseat.isAllAppsButtonRank(rank);
2118 }
2119
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002120 // AllAppsView.Watcher
2121 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002122 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002123 mWorkspace.setVisibility(View.GONE);
2124 }
2125 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002126
2127 /**
2128 * Helper method for the cameraZoomIn/cameraZoomOut animations
2129 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002130 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002131 * @param scaleFactor The scale factor used for the zoom
2132 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002133 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002134 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002135 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002136 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002137
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002138 void updateWallpaperVisibility(boolean visible) {
2139 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2140 int curflags = getWindow().getAttributes().flags
2141 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2142 if (wpflags != curflags) {
2143 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2144 }
2145 }
2146
Michael Jurkabed61d22012-02-14 22:51:29 -08002147 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2148 if (v instanceof LauncherTransitionable) {
2149 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2150 }
Winson Chung70442722012-02-10 15:43:22 -08002151
2152 // Update the workspace transition step as well
2153 dispatchOnLauncherTransitionStep(v, 0f);
2154 }
2155
2156 private void dispatchOnLauncherTransitionStep(View v, float t) {
2157 if (v instanceof LauncherTransitionable) {
2158 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2159 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002160 }
2161
2162 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2163 if (v instanceof LauncherTransitionable) {
2164 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2165 }
Winson Chung70442722012-02-10 15:43:22 -08002166
2167 // Update the workspace transition step as well
2168 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002169 }
2170
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002171 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002172 * Things to test when changing the following seven functions.
2173 * - Home from workspace
2174 * - from center screen
2175 * - from other screens
2176 * - Home from all apps
2177 * - from center screen
2178 * - from other screens
2179 * - Back from all apps
2180 * - from center screen
2181 * - from other screens
2182 * - Launch app from workspace and quit
2183 * - with back
2184 * - with home
2185 * - Launch app from all apps and quit
2186 * - with back
2187 * - with home
2188 * - Go to a screen that's not the default, then all
2189 * apps, and launch and app, and go back
2190 * - with back
2191 * -with home
2192 * - On workspace, long press power and go back
2193 * - with back
2194 * - with home
2195 * - On all apps, long press power and go back
2196 * - with back
2197 * - with home
2198 * - On workspace, power off
2199 * - On all apps, power off
2200 * - Launch an app and turn off the screen while in that app
2201 * - Go back with home key
2202 * - Go back with back key TODO: make this not go to workspace
2203 * - From all apps
2204 * - From workspace
2205 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2206 * - From all apps
2207 * - From the center workspace
2208 * - From another workspace
2209 */
2210
2211 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002212 * Zoom the camera out from the workspace to reveal 'toView'.
2213 * Assumes that the view to show is anchored at either the very top or very bottom
2214 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002215 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002216 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002217 if (mStateAnimation != null) {
2218 mStateAnimation.cancel();
2219 mStateAnimation = null;
2220 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002221 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002222
Winson Chungf0ea4d32011-06-06 14:27:16 -07002223 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2224 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2225 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002226 final View fromView = mWorkspace;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002227 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002228 final int startDelay =
2229 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002230
Michael Jurkab3e22d92011-10-31 15:58:33 -07002231 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002232
Michael Jurkad74c9842011-07-10 12:44:21 -07002233 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002234 Animator workspaceAnim =
2235 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002236
2237 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002238 toView.setScaleX(scale);
2239 toView.setScaleY(scale);
2240 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2241 scaleAnim.
2242 scaleX(1f).scaleY(1f).
2243 setDuration(duration).
2244 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002245
Winson Chungf0ea4d32011-06-06 14:27:16 -07002246 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002247 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002248 final ObjectAnimator alphaAnim = ObjectAnimator
2249 .ofFloat(toView, "alpha", 0f, 1f)
2250 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002251 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002252 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2253 @Override
2254 public void onAnimationUpdate(ValueAnimator animation) {
2255 float t = (Float) animation.getAnimatedValue();
2256 dispatchOnLauncherTransitionStep(fromView, t);
2257 dispatchOnLauncherTransitionStep(toView, t);
2258 }
2259 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002260
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002261 // toView should appear right at the end of the workspace shrink
2262 // animation
2263 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002264 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002265 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002266
2267 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002268 boolean animationCancelled = false;
2269
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002270 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002271 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002272 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002273 // Prepare the position
2274 toView.setTranslationX(0.0f);
2275 toView.setTranslationY(0.0f);
2276 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002277 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002278 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002279 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002280 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002281 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2282 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002283
2284 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2285 // Hide the workspace scrollbar
2286 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002287 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002288 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002289 if (!animationCancelled) {
2290 updateWallpaperVisibility(false);
2291 }
2292 }
2293
Adam Cohencff6af82011-09-13 14:51:53 -07002294 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002295 public void onAnimationCancel(Animator animation) {
2296 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002297 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002298 });
2299
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002300 if (workspaceAnim != null) {
2301 mStateAnimation.play(workspaceAnim);
2302 }
Michael Jurka1899a362011-11-03 13:50:45 -07002303
2304 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002305 final ViewTreeObserver observer;
2306
Michael Jurkabed61d22012-02-14 22:51:29 -08002307 dispatchOnLauncherTransitionStart(fromView, animated, false);
2308 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002309
2310 // If any of the objects being animated haven't been measured/laid out
2311 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002312 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002313 (mWorkspace.getMeasuredWidth() == 0) ||
2314 (toView.getMeasuredWidth() == 0)) {
2315 observer = mWorkspace.getViewTreeObserver();
2316 delayAnim = true;
2317 } else {
2318 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002319 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002320
2321 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002322 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002323 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2324 public void onGlobalLayout() {
2325 mWorkspace.post(new Runnable() {
2326 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002327 // Check that mStateAnimation hasn't changed while
2328 // we waited for a layout pass
2329 if (mStateAnimation == stateAnimation) {
2330 // Need to update pivots for zoom if layout changed
2331 setPivotsForZoom(toView, scale);
2332 mStateAnimation.start();
2333 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002334 }
2335 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002336 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002337 }
2338 };
2339 observer.addOnGlobalLayoutListener(delayedStart);
2340 } else {
2341 setPivotsForZoom(toView, scale);
Michael Jurka1899a362011-11-03 13:50:45 -07002342 mStateAnimation.start();
2343 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002344 } else {
2345 toView.setTranslationX(0.0f);
2346 toView.setTranslationY(0.0f);
2347 toView.setScaleX(1.0f);
2348 toView.setScaleY(1.0f);
2349 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002350 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002351
Michael Jurkabed61d22012-02-14 22:51:29 -08002352 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2353 // Hide the workspace scrollbar
2354 mWorkspace.hideScrollingIndicator(true);
2355 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002356 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002357 dispatchOnLauncherTransitionStart(fromView, animated, false);
2358 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2359 dispatchOnLauncherTransitionStart(toView, animated, false);
2360 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002361 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002362 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002363 }
2364
2365 /**
2366 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002367 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002368 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002369 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002370 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2371 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002372
Michael Jurkab3e22d92011-10-31 15:58:33 -07002373 if (mStateAnimation != null) {
2374 mStateAnimation.cancel();
2375 mStateAnimation = null;
2376 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002377 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002378
Winson Chungf0ea4d32011-06-06 14:27:16 -07002379 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002380 final int fadeOutDuration =
2381 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002382 final float scaleFactor = (float)
2383 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2384 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002385 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002386 Animator workspaceAnim = null;
2387
2388 if (toState == State.WORKSPACE) {
2389 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2390 workspaceAnim = mWorkspace.getChangeStateAnimation(
2391 Workspace.State.NORMAL, animated, stagger);
2392 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2393 workspaceAnim = mWorkspace.getChangeStateAnimation(
2394 Workspace.State.SPRING_LOADED, animated);
2395 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002396
Michael Jurkab3e22d92011-10-31 15:58:33 -07002397 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002398 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002399 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002400 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002401 final LauncherViewPropertyAnimator scaleAnim =
2402 new LauncherViewPropertyAnimator(fromView);
2403 scaleAnim.
2404 scaleX(scaleFactor).scaleY(scaleFactor).
2405 setDuration(duration).
2406 setInterpolator(new Workspace.ZoomInInterpolator());
2407
2408 final ObjectAnimator alphaAnim = ObjectAnimator
2409 .ofFloat(fromView, "alpha", 1f, 0f)
2410 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002411 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002412 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2413 @Override
2414 public void onAnimationUpdate(ValueAnimator animation) {
2415 float t = 1f - (Float) animation.getAnimatedValue();
2416 dispatchOnLauncherTransitionStep(fromView, t);
2417 dispatchOnLauncherTransitionStep(toView, t);
2418 }
2419 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002420
Michael Jurkabed61d22012-02-14 22:51:29 -08002421 mStateAnimation = new AnimatorSet();
2422
2423 dispatchOnLauncherTransitionStart(fromView, animated, true);
2424 dispatchOnLauncherTransitionStart(toView, animated, true);
2425
2426 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002427 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002428 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002429 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002430 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002431 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2432 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurka430e8a52011-08-08 15:52:14 -07002433 mWorkspace.hideScrollingIndicator(false);
Adam Cohened66b2b2012-01-23 17:28:51 -08002434 if (onCompleteRunnable != null) {
2435 onCompleteRunnable.run();
2436 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002437 }
2438 });
2439
Winson Chungf0ea4d32011-06-06 14:27:16 -07002440 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002441 if (workspaceAnim != null) {
2442 mStateAnimation.play(workspaceAnim);
2443 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002444 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002445 } else {
2446 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002447 dispatchOnLauncherTransitionStart(fromView, animated, true);
2448 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2449 dispatchOnLauncherTransitionStart(toView, animated, true);
2450 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002451 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002452 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002453 }
2454
Michael Jurkae326f182011-11-21 14:05:46 -08002455 @Override
2456 public void onTrimMemory(int level) {
2457 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002458 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002459 mAppsCustomizeTabHost.onTrimMemory();
2460 }
2461 }
2462
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002463 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002464 showWorkspace(animated, null);
2465 }
2466
2467 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002468 if (mState != State.WORKSPACE) {
2469 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002470 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002471
2472 // Show the search bar and hotseat
2473 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002474 // We only need to animate in the dock divider if we're going from spring loaded mode
2475 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002476
2477 // Set focus to the AppsCustomize button
2478 if (mAllAppsButton != null) {
2479 mAllAppsButton.requestFocus();
2480 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002481 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002482
Michael Jurkab737ee62011-11-15 15:57:22 -08002483 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002484
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002485 // Change the state *after* we've called all the transition code
2486 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002487
Winson Chung5fb63472011-02-02 17:03:37 -08002488 // Resume the auto-advance of widgets
2489 mUserPresent = true;
2490 updateRunning();
2491
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002492 // send an accessibility event to announce the context change
2493 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002494 }
2495
Michael Jurkab3e22d92011-10-31 15:58:33 -07002496 void showAllApps(boolean animated) {
2497 if (mState != State.WORKSPACE) return;
2498
2499 showAppsCustomizeHelper(animated, false);
2500 mAppsCustomizeTabHost.requestFocus();
2501
2502 // Hide the search bar and hotseat
2503 mSearchDropTargetBar.hideSearchBar(animated);
2504
2505 // Change the state *after* we've called all the transition code
2506 mState = State.APPS_CUSTOMIZE;
2507
2508 // Pause the auto-advance of widgets until we are out of AllApps
2509 mUserPresent = false;
2510 updateRunning();
2511 closeFolder();
2512
2513 // Send an accessibility event to announce the context change
2514 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2515 }
2516
Adam Cohen7777d962011-08-18 18:58:38 -07002517 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002518 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002519 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002520 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002521 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002522 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002523 }
Adam Cohen7777d962011-08-18 18:58:38 -07002524
Adam Cohened66b2b2012-01-23 17:28:51 -08002525 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2526 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002527 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2528
Winson Chunge7a03942011-08-05 15:05:12 -07002529 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002530 @Override
2531 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002532 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002533 // Before we show workspace, hide all apps again because
2534 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2535 // clean up our state transition functions
2536 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002537 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002538 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002539 } else {
2540 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002541 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002542 }
2543 }, (extendedDelay ?
2544 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2545 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2546 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002547
Michael Jurkad3ef3062010-11-23 16:23:58 -08002548 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002549 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002550 final boolean animated = true;
2551 final boolean springLoaded = true;
2552 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002553 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002554 }
2555 // Otherwise, we are not in spring loaded mode, so don't do anything.
2556 }
2557
Michael Jurkab737ee62011-11-15 15:57:22 -08002558 void hideDockDivider() {
2559 if (mQsbDivider != null && mDockDivider != null) {
2560 mQsbDivider.setVisibility(View.INVISIBLE);
2561 mDockDivider.setVisibility(View.INVISIBLE);
2562 }
2563 }
2564
2565 void showDockDivider(boolean animated) {
2566 if (mQsbDivider != null && mDockDivider != null) {
2567 mQsbDivider.setVisibility(View.VISIBLE);
2568 mDockDivider.setVisibility(View.VISIBLE);
2569 if (mDividerAnimator != null) {
2570 mDividerAnimator.cancel();
2571 mQsbDivider.setAlpha(1f);
2572 mDockDivider.setAlpha(1f);
2573 mDividerAnimator = null;
2574 }
2575 if (animated) {
2576 mDividerAnimator = new AnimatorSet();
2577 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2578 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2579 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2580 mDividerAnimator.start();
2581 }
2582 }
2583 }
2584
Michael Jurkab3e22d92011-10-31 15:58:33 -07002585 void lockAllApps() {
2586 // TODO
2587 }
2588
2589 void unlockAllApps() {
2590 // TODO
2591 }
2592
Adam Cohenfc53cd22011-07-20 15:45:11 -07002593 public boolean isAllAppsCustomizeOpen() {
2594 return mState == State.APPS_CUSTOMIZE;
2595 }
2596
Winson Chungf0ea4d32011-06-06 14:27:16 -07002597 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002598 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002599 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002600 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002601 if (!LauncherApplication.isScreenLarge()) {
2602 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002603 if (mHotseat.getAlpha() != 1f) {
2604 int duration = mSearchDropTargetBar.getTransitionInDuration();
2605 mHotseat.animate().alpha(1f).setDuration(duration);
2606 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002607 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002608 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002609 }
2610 }
2611 }
2612
2613 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002614 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002615 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002616 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002617 if (!LauncherApplication.isScreenLarge()) {
2618 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002619 if (mHotseat.getAlpha() != 0f) {
2620 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2621 mHotseat.animate().alpha(0f).setDuration(duration);
2622 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002623 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002624 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002625 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002626 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002627 }
2628
Patrick Dubroy5f445422011-02-18 14:35:21 -08002629 /**
2630 * Add an item from all apps or customize onto the given workspace screen.
2631 * If layout is null, add to the current screen.
2632 */
2633 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002634 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002635 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002636 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002637 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002638
Winson Chungdff8ebb2011-09-08 17:25:31 -07002639 /** Maps the current orientation to an index for referencing orientation correct global icons */
2640 private int getCurrentOrientationIndexForGlobalIcons() {
2641 // default - 0, landscape - 1
2642 switch (getResources().getConfiguration().orientation) {
2643 case Configuration.ORIENTATION_LANDSCAPE:
2644 return 1;
2645 default:
2646 return 0;
2647 }
2648 }
2649
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002650 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002651 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002652 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002653 // Look for the toolbar icon specified in the activity meta-data
2654 Bundle metaData = packageManager.getActivityInfo(
2655 activityName, PackageManager.GET_META_DATA).metaData;
2656 if (metaData != null) {
2657 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2658 if (iconResId != 0) {
2659 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002660 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002661 }
2662 }
2663 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002664 // This can happen if the activity defines an invalid drawable
2665 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2666 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002667 } catch (Resources.NotFoundException nfe) {
2668 // This can happen if the activity defines an invalid drawable
2669 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2670 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002671 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002672 return null;
2673 }
2674
2675 // if successful in getting icon, return it; otherwise, set button to use default drawable
2676 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2677 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002678 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002679 Resources r = getResources();
2680 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2681 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002682
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002683 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002684 // If we were unable to find the icon via the meta-data, use a generic one
2685 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002686 toolbarIcon = r.getDrawable(fallbackDrawableId);
2687 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002688 if (button != null) {
2689 button.setCompoundDrawables(toolbarIcon, null, null, null);
2690 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002691 return null;
2692 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002693 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002694 if (button != null) {
2695 button.setCompoundDrawables(toolbarIcon, null, null, null);
2696 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002697 return toolbarIcon.getConstantState();
2698 }
2699 }
2700
2701 // if successful in getting icon, return it; otherwise, set button to use default drawable
2702 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2703 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2704 ImageView button = (ImageView) findViewById(buttonId);
2705 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2706
Michael Jurka19e0fc52011-07-22 18:00:21 -07002707 if (button != null) {
2708 // If we were unable to find the icon via the meta-data, use a
2709 // generic one
2710 if (toolbarIcon == null) {
2711 button.setImageResource(fallbackDrawableId);
2712 } else {
2713 button.setImageDrawable(toolbarIcon);
2714 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002715 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002716
2717 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2718
Michael Jurka0423dcf2010-10-05 14:56:18 -07002719 }
2720
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002721 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2722 TextView button = (TextView) findViewById(buttonId);
2723 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2724 }
2725
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002726 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002727 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002728 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002729 }
2730
Winson Chungbb185bd2011-11-21 12:31:42 -08002731 private void invalidatePressedFocusedStates(View container, View button) {
2732 if (container instanceof HolographicLinearLayout) {
2733 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2734 layout.invalidatePressedFocusedStates();
2735 } else if (button instanceof HolographicImageView) {
2736 HolographicImageView view = (HolographicImageView) button;
2737 view.invalidatePressedFocusedStates();
2738 }
2739 }
2740
Winson Chungc51db6a2011-10-05 11:44:49 -07002741 private boolean updateGlobalSearchIcon() {
2742 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002743 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2744 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002745 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002746 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002747 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002748
Winson Chung1cad91e2011-05-25 17:41:01 -07002749 final SearchManager searchManager =
2750 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2751 ComponentName activityName = searchManager.getGlobalSearchActivity();
2752 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002753 int coi = getCurrentOrientationIndexForGlobalIcons();
2754 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002755 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002756 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002757 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2758 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002759 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002760 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002761 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002762 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002763 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002764 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2765 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2766 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002767 voiceButton.setVisibility(View.GONE);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002768 voiceButtonProxy.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002769 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002770 }
2771 }
2772
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002773 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002774 final View searchButtonContainer = findViewById(R.id.search_button_container);
2775 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002776 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002777 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002778 }
2779
Winson Chungc51db6a2011-10-05 11:44:49 -07002780 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002781 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002782 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002783 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002784 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002785
Winson Chungc51db6a2011-10-05 11:44:49 -07002786 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002787 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2788 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002789 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002790 int coi = getCurrentOrientationIndexForGlobalIcons();
2791 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002792 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002793 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002794 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002795 voiceButton.setVisibility(View.VISIBLE);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002796 voiceButtonProxy.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002797 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002798 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002799 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002800 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002801 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002802 voiceButton.setVisibility(View.GONE);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002803 voiceButtonProxy.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002804 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002805 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002806 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002807
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002808 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002809 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2810 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002811 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002812 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002813 }
2814
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002815 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002816 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002817 */
2818 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002819 final View marketButton = findViewById(R.id.market_button);
2820 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2821 // Find the app market activity by resolving an intent.
2822 // (If multiple app markets are installed, it will return the ResolverActivity.)
2823 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002824 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002825 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002826 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002827 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002828 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002829 marketButton.setVisibility(View.VISIBLE);
2830 } else {
2831 // We should hide and disable the view so that we don't try and restore the visibility
2832 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2833 marketButton.setVisibility(View.GONE);
2834 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002835 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002836 }
2837
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002838 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002839 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002840 }
2841
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002842 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002843 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002844 */
2845 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2846 @Override
2847 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002848 closeSystemDialogs();
2849 }
2850 }
2851
2852 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002853 * Receives notifications whenever the appwidgets are reset.
2854 */
2855 private class AppWidgetResetObserver extends ContentObserver {
2856 public AppWidgetResetObserver() {
2857 super(new Handler());
2858 }
2859
2860 @Override
2861 public void onChange(boolean selfChange) {
2862 onAppWidgetReset();
2863 }
2864 }
2865
2866 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002867 * If the activity is currently paused, signal that we need to re-run the loader
2868 * in onResume.
2869 *
2870 * This needs to be called from incoming places where resources might have been loaded
2871 * while we are paused. That is becaues the Configuration might be wrong
2872 * when we're not running, and if it comes back to what it was when we
2873 * were paused, we are not restarted.
2874 *
2875 * Implementation of the method from LauncherModel.Callbacks.
2876 *
2877 * @return true if we are currently paused. The caller might be able to
2878 * skip some work in that case since we will come back again.
2879 */
2880 public boolean setLoadOnResume() {
2881 if (mPaused) {
2882 Log.i(TAG, "setLoadOnResume");
2883 mOnResumeNeedsLoad = true;
2884 return true;
2885 } else {
2886 return false;
2887 }
2888 }
2889
2890 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002891 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002892 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002893 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002894 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002895 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002896 } else {
2897 return SCREEN_COUNT / 2;
2898 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002899 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002900
Joe Onorato9c1289c2009-08-17 11:03:03 -04002901 /**
2902 * Refreshes the shortcuts shown on the workspace.
2903 *
2904 * Implementation of the method from LauncherModel.Callbacks.
2905 */
2906 public void startBinding() {
2907 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002908
Winson Chungf0c6ae02012-03-21 16:10:31 -07002909 mNewShortcutAnimatePage = -1;
2910 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07002911 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002912 int count = workspace.getChildCount();
2913 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002914 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002915 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2916 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002917 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08002918 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07002919 if (mHotseat != null) {
2920 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002921 }
2922 }
2923
2924 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002925 * Bind the items start-end from the list.
2926 *
2927 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002928 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002929 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002930 setLoadOnResume();
2931
Winson Chungf0c6ae02012-03-21 16:10:31 -07002932 // Get the list of added shortcuts and intersect them with the set of shortcuts here
2933 Set<String> newApps = new HashSet<String>();
2934 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
2935
2936 Workspace workspace = mWorkspace;
2937 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002938 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002939
2940 // Short circuit if we are loading dock items for a configuration which has no dock
2941 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2942 mHotseat == null) {
2943 continue;
2944 }
2945
Joe Onorato9c1289c2009-08-17 11:03:03 -04002946 switch (item.itemType) {
2947 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2948 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07002949 ShortcutInfo info = (ShortcutInfo) item;
2950 String uri = info.intent.toUri(0).toString();
2951 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07002952 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2953 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07002954 if (newApps.contains(uri)) {
2955 newApps.remove(uri);
2956
2957 // Prepare the view to be animated up
2958 shortcut.setAlpha(0f);
2959 shortcut.setScaleX(0f);
2960 shortcut.setScaleY(0f);
2961 mNewShortcutAnimatePage = item.screen;
2962 if (!mNewShortcutAnimateViews.contains(shortcut)) {
2963 mNewShortcutAnimateViews.add(shortcut);
2964 }
2965 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002966 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002967 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002968 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002969 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002970 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002971 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2972 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002973 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002974 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002975 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002976
Joe Onorato9c1289c2009-08-17 11:03:03 -04002977 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002978 }
2979
Joe Onorato9c1289c2009-08-17 11:03:03 -04002980 /**
2981 * Implementation of the method from LauncherModel.Callbacks.
2982 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002983 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002984 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002985 sFolders.clear();
2986 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002987 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002988
2989 /**
2990 * Add the views for a widget to the workspace.
2991 *
2992 * Implementation of the method from LauncherModel.Callbacks.
2993 */
2994 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002995 setLoadOnResume();
2996
Daniel Sandler843e8602010-06-07 14:59:01 -04002997 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2998 if (DEBUG_WIDGETS) {
2999 Log.d(TAG, "bindAppWidget: " + item);
3000 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003001 final Workspace workspace = mWorkspace;
3002
3003 final int appWidgetId = item.appWidgetId;
3004 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003005 if (DEBUG_WIDGETS) {
3006 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3007 }
3008
Joe Onorato9c1289c2009-08-17 11:03:03 -04003009 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3010
Joe Onorato9c1289c2009-08-17 11:03:03 -04003011 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3012 item.hostView.setTag(item);
3013
Winson Chung3d503fb2011-07-13 17:25:49 -07003014 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003015 item.cellY, item.spanX, item.spanY, false);
3016
Adam Cohended9f8d2010-11-03 13:25:16 -07003017 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3018
Joe Onorato9c1289c2009-08-17 11:03:03 -04003019 workspace.requestLayout();
3020
Daniel Sandler843e8602010-06-07 14:59:01 -04003021 if (DEBUG_WIDGETS) {
3022 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3023 + (SystemClock.uptimeMillis()-start) + "ms");
3024 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003025 }
3026
3027 /**
3028 * Callback saying that there aren't any more items to bind.
3029 *
3030 * Implementation of the method from LauncherModel.Callbacks.
3031 */
3032 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003033 setLoadOnResume();
3034
Joe Onorato9c1289c2009-08-17 11:03:03 -04003035 if (mSavedState != null) {
3036 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003037 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003038 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003039 mSavedState = null;
3040 }
3041
3042 if (mSavedInstanceState != null) {
3043 super.onRestoreInstanceState(mSavedInstanceState);
3044 mSavedInstanceState = null;
3045 }
3046
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003047 // If we received the result of any pending adds while the loader was running (e.g. the
3048 // widget configuration forced an orientation change), process them now.
3049 for (int i = 0; i < sPendingAddList.size(); i++) {
3050 completeAdd(sPendingAddList.get(i));
3051 }
3052 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003053
Winson Chungc51db6a2011-10-05 11:44:49 -07003054 // Update the market app icon as necessary (the other icons will be managed in response to
3055 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003056 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003057
Winson Chungf0c6ae02012-03-21 16:10:31 -07003058 // Animate up any icons as necessary
3059 if (mVisible || mWorkspaceLoading) {
3060 Runnable newAppsRunnable = new Runnable() {
3061 @Override
3062 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003063 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003064 }
3065 };
Winson Chunga2413752012-04-03 14:22:34 -07003066
3067 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3068 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3069 if (canRunNewAppsAnimation()) {
3070 // If the user has not interacted recently, then either snap to the new page to show
3071 // the new-apps animation or just run them if they are to appear on the current page
3072 if (willSnapPage) {
3073 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3074 } else {
3075 runNewAppsAnimation(false);
3076 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003077 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003078 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003079 // are on another page (or just normally if they are added to the current page)
3080 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003081 }
3082 }
3083
3084 mWorkspaceLoading = false;
3085 }
3086
Winson Chunga2413752012-04-03 14:22:34 -07003087 private boolean canRunNewAppsAnimation() {
3088 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3089 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3090 }
3091
Winson Chungf0c6ae02012-03-21 16:10:31 -07003092 /**
3093 * Runs a new animation that scales up icons that were added while Launcher was in the
3094 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003095 *
3096 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003097 */
Winson Chunga2413752012-04-03 14:22:34 -07003098 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003099 AnimatorSet anim = new AnimatorSet();
3100 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003101
3102 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003103 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3104 @Override
3105 public int compare(View a, View b) {
3106 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3107 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3108 int cellCountX = LauncherModel.getCellCountX();
3109 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3110 }
3111 });
Winson Chunga2413752012-04-03 14:22:34 -07003112
3113 // Animate each of the views in place (or show them immediately if requested)
3114 if (immediate) {
3115 for (View v : mNewShortcutAnimateViews) {
3116 v.setAlpha(1f);
3117 v.setScaleX(1f);
3118 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003119 }
Winson Chunga2413752012-04-03 14:22:34 -07003120 } else {
3121 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3122 View v = mNewShortcutAnimateViews.get(i);
3123 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3124 PropertyValuesHolder.ofFloat("alpha", 1f),
3125 PropertyValuesHolder.ofFloat("scaleX", 1f),
3126 PropertyValuesHolder.ofFloat("scaleY", 1f));
3127 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3128 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3129 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3130 bounceAnims.add(bounceAnim);
3131 }
3132 anim.playTogether(bounceAnims);
3133 anim.addListener(new AnimatorListenerAdapter() {
3134 @Override
3135 public void onAnimationEnd(Animator animation) {
3136 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3137 }
3138 });
3139 anim.start();
3140 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003141
3142 // Clean up
3143 mNewShortcutAnimatePage = -1;
3144 mNewShortcutAnimateViews.clear();
3145 new Thread("clearNewAppsThread") {
3146 public void run() {
3147 mSharedPrefs.edit()
3148 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3149 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3150 .commit();
3151 }
3152 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003153 }
3154
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003155 @Override
3156 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003157 boolean searchVisible = updateGlobalSearchIcon();
3158 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3159 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003160 }
3161
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003162 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003163 * Add the icons for all apps.
3164 *
3165 * Implementation of the method from LauncherModel.Callbacks.
3166 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003167 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3168 // Remove the progress bar entirely; we could also make it GONE
3169 // but better to remove it since we know it's not going to be used
3170 View progressBar = mAppsCustomizeTabHost.
3171 findViewById(R.id.apps_customize_progress_bar);
3172 if (progressBar != null) {
3173 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003174 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003175 // We just post the call to setApps so the user sees the progress bar
3176 // disappear-- otherwise, it just looks like the progress bar froze
3177 // which doesn't look great
3178 mAppsCustomizeTabHost.post(new Runnable() {
3179 public void run() {
3180 if (mAppsCustomizeContent != null) {
3181 mAppsCustomizeContent.setApps(apps);
3182 }
3183 }
3184 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003185 }
3186
3187 /**
3188 * A package was installed.
3189 *
3190 * Implementation of the method from LauncherModel.Callbacks.
3191 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003192 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003193 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003194
Winson Chung785d2eb2011-04-14 16:08:02 -07003195 if (mAppsCustomizeContent != null) {
3196 mAppsCustomizeContent.addApps(apps);
3197 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003198 }
3199
3200 /**
3201 * A package was updated.
3202 *
3203 * Implementation of the method from LauncherModel.Callbacks.
3204 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003205 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003206 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003207 if (mWorkspace != null) {
3208 mWorkspace.updateShortcuts(apps);
3209 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003210
Winson Chung785d2eb2011-04-14 16:08:02 -07003211 if (mAppsCustomizeContent != null) {
3212 mAppsCustomizeContent.updateApps(apps);
3213 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003214 }
3215
3216 /**
3217 * A package was uninstalled.
3218 *
3219 * Implementation of the method from LauncherModel.Callbacks.
3220 */
Joe Onorato36115782010-06-17 13:28:48 -04003221 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003222 if (permanent) {
3223 mWorkspace.removeItems(apps);
3224 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003225
Winson Chung785d2eb2011-04-14 16:08:02 -07003226 if (mAppsCustomizeContent != null) {
3227 mAppsCustomizeContent.removeApps(apps);
3228 }
Winson Chunga1820962011-10-03 16:31:06 -07003229
3230 // Notify the drag controller
3231 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003232 }
Joe Onoratobe386092009-11-17 17:32:16 -08003233
3234 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003235 * A number of packages were updated.
3236 */
3237 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003238 if (mAppsCustomizeContent != null) {
3239 mAppsCustomizeContent.onPackagesUpdated();
3240 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003241 }
3242
Winson Chung400438b2011-01-16 17:53:48 -08003243 private int mapConfigurationOriActivityInfoOri(int configOri) {
3244 final Display d = getWindowManager().getDefaultDisplay();
3245 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3246 switch (d.getRotation()) {
3247 case Surface.ROTATION_0:
3248 case Surface.ROTATION_180:
3249 // We are currently in the same basic orientation as the natural orientation
3250 naturalOri = configOri;
3251 break;
3252 case Surface.ROTATION_90:
3253 case Surface.ROTATION_270:
3254 // We are currently in the other basic orientation to the natural orientation
3255 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3256 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3257 break;
3258 }
3259
3260 int[] oriMap = {
3261 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3262 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3263 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3264 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3265 };
3266 // Since the map starts at portrait, we need to offset if this device's natural orientation
3267 // is landscape.
3268 int indexOffset = 0;
3269 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3270 indexOffset = 1;
3271 }
3272 return oriMap[(d.getRotation() + indexOffset) % 4];
3273 }
Adam Cohen446e9402011-09-15 18:21:21 -07003274
3275 public void lockScreenOrientationOnLargeUI() {
3276 if (LauncherApplication.isScreenLarge()) {
3277 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3278 .getConfiguration().orientation));
3279 }
Winson Chung400438b2011-01-16 17:53:48 -08003280 }
Adam Cohen446e9402011-09-15 18:21:21 -07003281 public void unlockScreenOrientationOnLargeUI() {
3282 if (LauncherApplication.isScreenLarge()) {
3283 mHandler.postDelayed(new Runnable() {
3284 public void run() {
3285 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3286 }
3287 }, mRestoreScreenOrientationDelay);
3288 }
Winson Chung400438b2011-01-16 17:53:48 -08003289 }
3290
Winson Chung82f55532011-08-09 14:14:23 -07003291 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003292 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003293 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003294 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003295
Winson Chung7d7541e2011-09-16 20:14:36 -07003296 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003297 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003298 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3299 Cling cling = (Cling) findViewById(clingId);
3300 if (cling != null) {
3301 cling.init(this, positionData);
3302 cling.setVisibility(View.VISIBLE);
3303 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3304 if (animate) {
3305 cling.buildLayer();
3306 cling.setAlpha(0f);
3307 cling.animate()
3308 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003309 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003310 .setDuration(SHOW_CLING_DURATION)
3311 .setStartDelay(delay)
3312 .start();
3313 } else {
3314 cling.setAlpha(1f);
3315 }
3316 }
3317 return cling;
3318 }
3319 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003320 if (cling != null) {
3321 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003322 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003323 anim.addListener(new AnimatorListenerAdapter() {
3324 public void onAnimationEnd(Animator animation) {
3325 cling.setVisibility(View.GONE);
3326 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003327 // We should update the shared preferences on a background thread
3328 new Thread("dismissClingThread") {
3329 public void run() {
3330 SharedPreferences.Editor editor = mSharedPrefs.edit();
3331 editor.putBoolean(flag, true);
3332 editor.commit();
3333 }
3334 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003335 };
3336 });
3337 anim.start();
3338 }
3339 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003340 private void removeCling(int id) {
3341 final View cling = findViewById(id);
3342 if (cling != null) {
3343 final ViewGroup parent = (ViewGroup) cling.getParent();
3344 parent.post(new Runnable() {
3345 @Override
3346 public void run() {
3347 parent.removeView(cling);
3348 }
3349 });
3350 }
3351 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003352 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003353 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003354 if (isClingsEnabled() &&
3355 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003356 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003357 } else {
3358 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003359 }
3360 }
3361 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003362 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003363 if (isClingsEnabled() &&
3364 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003365 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003366 } else {
3367 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003368 }
3369 }
3370 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003371 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003372 if (isClingsEnabled() &&
3373 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3374 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003375 } else {
3376 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003377 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003378 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003379 }
3380 public boolean isFolderClingVisible() {
3381 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003382 if (cling != null) {
3383 return cling.getVisibility() == View.VISIBLE;
3384 }
3385 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003386 }
Winson Chung82f55532011-08-09 14:14:23 -07003387 public void dismissWorkspaceCling(View v) {
3388 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003389 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003390 }
3391 public void dismissAllAppsCling(View v) {
3392 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003393 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3394 }
3395 public void dismissFolderCling(View v) {
3396 Cling cling = (Cling) findViewById(R.id.folder_cling);
3397 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003398 }
3399
Winson Chung80baf5a2010-08-09 16:03:15 -07003400 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003401 * Prints out out state for debugging.
3402 */
3403 public void dumpState() {
3404 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003405 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003406 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3407 Log.d(TAG, "mRestoring=" + mRestoring);
3408 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3409 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003410 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003411 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003412
Winson Chung785d2eb2011-04-14 16:08:02 -07003413 if (mAppsCustomizeContent != null) {
3414 mAppsCustomizeContent.dumpState();
3415 }
Joe Onoratobe386092009-11-17 17:32:16 -08003416 Log.d(TAG, "END launcher2 dump state");
3417 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003418
3419 @Override
3420 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3421 super.dump(prefix, fd, writer, args);
3422 writer.println(" ");
3423 writer.println("Debug logs: ");
3424 for (int i = 0; i < sDumpLogs.size(); i++) {
3425 writer.println(" " + sDumpLogs.get(i));
3426 }
3427 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003428}
Michael Jurka2763be32011-02-24 11:19:57 -08003429
Michael Jurkaabded662011-03-04 12:06:57 -08003430interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003431 View getContent();
Michael Jurkabed61d22012-02-14 22:51:29 -08003432 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003433 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003434 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003435}