blob: f6051601455356d745358dbd2b1fd22bd312e8b1 [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
Michael Jurka8b805b12012-04-18 14:23:14 -0700136 private static final int REQUEST_BIND_APPWIDGET = 11;
137
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
139
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800140 static final int SCREEN_COUNT = 5;
141 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Romain Guy98d01652009-06-30 16:21:04 -0700143 private static final String PREFERENCES = "launcher.preferences";
Adam Cohen23a4d302011-12-01 17:26:44 -0800144 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
146 // Type: int
147 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700148 // Type: int
149 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700151 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
152 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
154 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700155 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700157 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 // Type: boolean
159 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
160 // Type: long
161 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
162
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700163 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
164
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700165 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700166 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700167 private State mState = State.WORKSPACE;
168 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800169 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700170
Joe Onorato9c1289c2009-08-17 11:03:03 -0400171 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700172 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
173 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700174 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700175 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800178 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Winson Chunga2413752012-04-03 14:22:34 -0700180 // How long to wait before the new-shortcut animation automatically pans the workspace
181 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
182
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800183 private final BroadcastReceiver mCloseSystemDialogsReceiver
184 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800185 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private LayoutInflater mInflater;
188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800190 private View mQsbDivider;
191 private View mDockDivider;
192 private DragLayer mDragLayer;
193 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700194
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700195 private AppWidgetManager mAppWidgetManager;
196 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700197
Michael Jurkac9d95c52011-08-29 14:03:34 -0700198 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700199 private int[] mTmpAddItemCellCoordinates = new int[2];
200
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private FolderInfo mFolderInfo;
202
Winson Chung3d503fb2011-07-13 17:25:49 -0700203 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800204 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700205
Winson Chungc51db6a2011-10-05 11:44:49 -0700206 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700207 private AppsCustomizeTabHost mAppsCustomizeTabHost;
208 private AppsCustomizePagedView mAppsCustomizeContent;
209 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 private Bundle mSavedState;
212
213 private SpannableStringBuilder mDefaultKeySsb = null;
214
Joe Onorato9c1289c2009-08-17 11:03:03 -0400215 private boolean mWorkspaceLoading = true;
216
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800217 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 private boolean mRestoring;
219 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700220 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221
222 private Bundle mSavedInstanceState;
223
Joe Onorato9c1289c2009-08-17 11:03:03 -0400224 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800225 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800226 private boolean mUserPresent = true;
227 private boolean mVisible = false;
228 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700230 private static LocaleConfiguration sLocaleConfiguration = null;
231
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700232 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700233
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700234 private Intent mAppMarketIntent = null;
235
Adam Cohended9f8d2010-11-03 13:25:16 -0700236 // Related to the auto-advancing of widgets
237 private final int ADVANCE_MSG = 1;
238 private final int mAdvanceInterval = 20000;
239 private final int mAdvanceStagger = 250;
240 private long mAutoAdvanceSentTime;
241 private long mAutoAdvanceTimeLeft = -1;
242 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
243 new HashMap<View, AppWidgetProviderInfo>();
244
Winson Chung400438b2011-01-16 17:53:48 -0800245 // Determines how long to wait after a rotation before restoring the screen orientation to
246 // match the sensor state.
247 private final int mRestoreScreenOrientationDelay = 500;
248
Michael Jurka4ef207b2010-11-29 17:05:45 -0800249 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700250 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
251 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
252 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800253
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700254 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Michael Jurka8b805b12012-04-18 14:23:14 -0700255 PendingAddWidgetInfo mWidgetBeingBoundOrConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700256
Winson Chung46353de2012-02-16 14:05:10 -0800257 // We only want to get the SharedPreferences once since it does an FS stat each time we get
258 // it from the context.
259 private SharedPreferences mSharedPrefs;
260
Winson Chungf0c6ae02012-03-21 16:10:31 -0700261 // Holds the page that we need to animate to, and the icon views that we need to animate up
262 // when we scroll to that page on resume.
263 private int mNewShortcutAnimatePage = -1;
264 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700265
Michael Jurkaddd62e92011-02-16 17:49:14 -0800266 private BubbleTextView mWaitingForResume;
267
Michael Jurkac1f5d262011-09-30 19:32:27 -0700268 private Runnable mBuildLayersRunnable = new Runnable() {
269 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700270 if (mWorkspace != null) {
271 mWorkspace.buildPageHardwareLayers();
272 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700273 }
274 };
275
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800276 private static ArrayList<PendingAddArguments> sPendingAddList
277 = new ArrayList<PendingAddArguments>();
278
279 private static class PendingAddArguments {
280 int requestCode;
281 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700282 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800283 int screen;
284 int cellX;
285 int cellY;
286 }
287
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 @Override
289 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700290 if (DEBUG_STRICT_MODE) {
291 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
292 .detectDiskReads()
293 .detectDiskWrites()
294 .detectNetwork() // or .detectAll() for all detectable problems
295 .penaltyLog()
296 .build());
297 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
298 .detectLeakedSqlLiteObjects()
299 .detectLeakedClosableObjects()
300 .penaltyLog()
301 .penaltyDeath()
302 .build());
303 }
304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800306 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700307 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
308 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800309 mModel = app.setLauncher(this);
310 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400311 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700313
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700314 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700315 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
316 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700317
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200319 android.os.Debug.startMethodTracing(
320 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 }
322
323 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800324 setContentView(R.layout.launcher);
325 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700326 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800328 registerContentObservers();
329
Joe Onorato7c312c12009-08-13 21:36:53 -0700330 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700331
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 mSavedState = savedInstanceState;
333 restoreState(mSavedState);
334
Winson Chunga12a2502010-12-20 14:41:35 -0800335 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700336 if (mAppsCustomizeContent != null) {
337 mAppsCustomizeContent.onPackagesUpdated();
338 }
Winson Chunga12a2502010-12-20 14:41:35 -0800339
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 if (PROFILE_STARTUP) {
341 android.os.Debug.stopMethodTracing();
342 }
343
344 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700345 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 }
347
Michael Jurkac57b7a82011-08-09 22:02:20 -0700348 if (!mModel.isAllAppsLoaded()) {
349 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
350 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
351 }
352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 // For handling default keys
354 mDefaultKeySsb = new SpannableStringBuilder();
355 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800356
357 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
358 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800359
Winson Chungc51db6a2011-10-05 11:44:49 -0700360 boolean searchVisible = false;
361 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800362 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700363 int coi = getCurrentOrientationIndexForGlobalIcons();
364 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
365 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700366 updateAppMarketIcon();
367 searchVisible = updateGlobalSearchIcon();
368 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700369 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700370 if (sGlobalSearchIcon[coi] != null) {
371 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700372 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700373 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700374 if (sVoiceSearchIcon[coi] != null) {
375 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700376 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700377 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700378 if (sAppMarketIcon[coi] != null) {
379 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800380 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700381 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700382
Jim Miller14091b12012-04-24 19:27:54 -0700383 final String forceEnableRotation =
Adam Cohen23a4d302011-12-01 17:26:44 -0800384 SystemProperties.get(FORCE_ENABLE_ROTATION_PROPERTY, "false");
385
Adam Cohencb3f98e2012-04-24 14:24:35 -0700386 boolean enableRotation = getResources().getBoolean(R.bool.allow_rotation);
387
Adam Cohen446e9402011-09-15 18:21:21 -0700388 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohencb3f98e2012-04-24 14:24:35 -0700389 if (enableRotation || "true".equalsIgnoreCase(forceEnableRotation)) {
Adam Cohen446e9402011-09-15 18:21:21 -0700390 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
391 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 }
Romain Guycbb89e42009-06-08 15:52:54 -0700393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700395 if (sLocaleConfiguration == null) {
396 new AsyncTask<Void, Void, LocaleConfiguration>() {
397 @Override
398 protected LocaleConfiguration doInBackground(Void... unused) {
399 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
400 readConfiguration(Launcher.this, localeConfiguration);
401 return localeConfiguration;
402 }
403
404 @Override
405 protected void onPostExecute(LocaleConfiguration result) {
406 sLocaleConfiguration = result;
407 checkForLocaleChange(); // recursive, but now with a locale configuration
408 }
409 }.execute();
410 return;
411 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700412
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 final Configuration configuration = getResources().getConfiguration();
414
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700415 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 final String locale = configuration.locale.toString();
417
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700418 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 final int mcc = configuration.mcc;
420
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700421 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 final int mnc = configuration.mnc;
423
Romain Guy84f296c2009-11-04 15:00:44 -0800424 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425
Romain Guy84f296c2009-11-04 15:00:44 -0800426 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700427 sLocaleConfiguration.locale = locale;
428 sLocaleConfiguration.mcc = mcc;
429 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700430
Joe Onorato0589f0f2010-02-08 13:44:00 -0800431 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700432
433 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
434 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700435 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700436 public void run() {
437 writeConfiguration(Launcher.this, localeConfiguration);
438 }
439 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700440 }
441 }
442
443 private static class LocaleConfiguration {
444 public String locale;
445 public int mcc = -1;
446 public int mnc = -1;
447 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700448
Romain Guy98d01652009-06-30 16:21:04 -0700449 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
450 DataInputStream in = null;
451 try {
452 in = new DataInputStream(context.openFileInput(PREFERENCES));
453 configuration.locale = in.readUTF();
454 configuration.mcc = in.readInt();
455 configuration.mnc = in.readInt();
456 } catch (FileNotFoundException e) {
457 // Ignore
458 } catch (IOException e) {
459 // Ignore
460 } finally {
461 if (in != null) {
462 try {
463 in.close();
464 } catch (IOException e) {
465 // Ignore
466 }
467 }
468 }
469 }
470
471 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
472 DataOutputStream out = null;
473 try {
474 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
475 out.writeUTF(configuration.locale);
476 out.writeInt(configuration.mcc);
477 out.writeInt(configuration.mnc);
478 out.flush();
479 } catch (FileNotFoundException e) {
480 // Ignore
481 } catch (IOException e) {
482 //noinspection ResultOfMethodCallIgnored
483 context.getFileStreamPath(PREFERENCES).delete();
484 } finally {
485 if (out != null) {
486 try {
487 out.close();
488 } catch (IOException e) {
489 // Ignore
490 }
491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 }
493 }
494
Adam Cohen716b51e2011-06-30 12:09:54 -0700495 public DragLayer getDragLayer() {
496 return mDragLayer;
497 }
498
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 static int getScreen() {
500 synchronized (sLock) {
501 return sScreen;
502 }
503 }
504
505 static void setScreen(int screen) {
506 synchronized (sLock) {
507 sScreen = screen;
508 }
509 }
510
Winson Chung557d6ed2011-07-08 15:34:52 -0700511 /**
512 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
513 * a configuration step, this allows the proper animations to run after other transitions.
514 */
515 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700516 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800517 switch (args.requestCode) {
518 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700519 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
520 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800521 break;
522 case REQUEST_PICK_SHORTCUT:
523 processShortcut(args.intent);
524 break;
525 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700526 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
527 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700528 result = true;
529 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800530 case REQUEST_PICK_APPWIDGET:
531 addAppWidgetFromPick(args.intent);
532 break;
533 case REQUEST_CREATE_APPWIDGET:
534 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800535 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700536 result = true;
537 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800538 case REQUEST_PICK_WALLPAPER:
539 // We just wanted the activity result here so we can clear mWaitingForResult
540 break;
541 }
Michael Jurka27614d22012-04-02 06:40:22 -0700542 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
543 // if you turned the screen off and then back while in All Apps, Launcher would not
544 // return to the workspace. Clearing mAddInfo.container here fixes this issue
545 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700546 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800547 }
548
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700550 protected void onActivityResult(
551 final int requestCode, final int resultCode, final Intent data) {
552 if (requestCode == REQUEST_BIND_APPWIDGET) {
553 int appWidgetId = data != null ?
554 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
555 if (resultCode == RESULT_CANCELED) {
556 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
557 } else if (resultCode == RESULT_OK) {
558 addAppWidgetImpl(appWidgetId, mWidgetBeingBoundOrConfigured);
559 }
560 return;
561 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700562 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800563 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
564 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700565 mWaitingForResult = false;
566
Adam Cohened66b2b2012-01-23 17:28:51 -0800567 // We have special handling for widgets
568 if (isWidgetDrop) {
569 int appWidgetId = data != null ?
570 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
571 completeTwoStageWidgetDrop(resultCode, appWidgetId);
572 return;
573 }
574
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 // The pattern used here is that a user PICKs a specific application,
576 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700577
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
579 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700580 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800581 final PendingAddArguments args = new PendingAddArguments();
582 args.requestCode = requestCode;
583 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700584 args.container = mPendingAddInfo.container;
585 args.screen = mPendingAddInfo.screen;
586 args.cellX = mPendingAddInfo.cellX;
587 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800588 if (isWorkspaceLocked()) {
589 sPendingAddList.add(args);
590 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700591 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800594 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700595 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800596 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
597 null);
598 }
599
600 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700601 CellLayout cellLayout =
602 (CellLayout) mWorkspace.getChildAt(mWidgetBeingBoundOrConfigured.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800603 Runnable onCompleteRunnable = null;
604 int animationType = 0;
605
606 if (resultCode == RESULT_OK) {
607 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
608 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Michael Jurka8b805b12012-04-18 14:23:14 -0700609 mWidgetBeingBoundOrConfigured.info);
610 mWidgetBeingBoundOrConfigured.boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800611 onCompleteRunnable = new Runnable() {
612 @Override
613 public void run() {
614 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
615 mPendingAddInfo.screen, layout, null);
616 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
617 null);
618 }
619 };
620 } else if (resultCode == RESULT_CANCELED) {
621 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
622 onCompleteRunnable = new Runnable() {
623 @Override
624 public void run() {
625 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
626 null);
627 }
628 };
629 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700630 mWorkspace.animateWidgetDrop(mWidgetBeingBoundOrConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800631 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Michael Jurka8b805b12012-04-18 14:23:14 -0700632 animationType, mWidgetBeingBoundOrConfigured.boundWidget, true);
633 mWidgetBeingBoundOrConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 }
635
636 @Override
637 protected void onResume() {
638 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700639
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800640 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700641 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400642 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700643 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400644 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700645 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 }
Winson Chunge4e50662012-01-23 14:45:13 -0800647
648 // Reset the pressed state of icons that were locked in the press state while activities
649 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800650 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800651 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800652 mWaitingForResume.setStayPressed(false);
653 }
Winson Chunge4e50662012-01-23 14:45:13 -0800654 if (mAppsCustomizeContent != null) {
655 // Resets the previous all apps icon press state
656 mAppsCustomizeContent.resetDrawableState();
657 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 }
659
660 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700661 protected void onPause() {
662 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700663 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800664 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700665 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700666 }
Romain Guycbb89e42009-06-08 15:52:54 -0700667
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700668 @Override
669 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400670 // Flag the loader to stop early before switching
671 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700672 if (mAppsCustomizeContent != null) {
673 mAppsCustomizeContent.surrender();
674 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800675 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700676 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700677
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800678 // We can't hide the IME if it was forced open. So don't bother
679 /*
680 @Override
681 public void onWindowFocusChanged(boolean hasFocus) {
682 super.onWindowFocusChanged(hasFocus);
683
684 if (hasFocus) {
685 final InputMethodManager inputManager = (InputMethodManager)
686 getSystemService(Context.INPUT_METHOD_SERVICE);
687 WindowManager.LayoutParams lp = getWindow().getAttributes();
688 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
689 android.os.Handler()) {
690 protected void onReceiveResult(int resultCode, Bundle resultData) {
691 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
692 }
693 });
694 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
695 }
696 }
697 */
698
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 private boolean acceptFilter() {
700 final InputMethodManager inputManager = (InputMethodManager)
701 getSystemService(Context.INPUT_METHOD_SERVICE);
702 return !inputManager.isFullscreenMode();
703 }
704
705 @Override
706 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700707 final int uniChar = event.getUnicodeChar();
708 final boolean handled = super.onKeyDown(keyCode, event);
709 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
710 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
712 keyCode, event);
713 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700714 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700715 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700716 // showSearchDialog()
717 // If there are multiple keystrokes before the search dialog takes focus,
718 // onSearchRequested() will be called for every keystroke,
719 // but it is idempotent, so it's fine.
720 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 }
722 }
723
Joe Onorato8a9625e2010-01-28 15:55:35 -0800724 // Eat the long press event so the keyboard doesn't come up.
725 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
726 return true;
727 }
728
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 return handled;
730 }
731
Karl Rosaen138a0412009-04-23 19:00:21 -0700732 private String getTypedText() {
733 return mDefaultKeySsb.toString();
734 }
735
736 private void clearTypedText() {
737 mDefaultKeySsb.clear();
738 mDefaultKeySsb.clearSpans();
739 Selection.setSelection(mDefaultKeySsb, 0);
740 }
741
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700743 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
744 * State
745 */
746 private static State intToState(int stateOrdinal) {
747 State state = State.WORKSPACE;
748 final State[] stateValues = State.values();
749 for (int i = 0; i < stateValues.length; i++) {
750 if (stateValues[i].ordinal() == stateOrdinal) {
751 state = stateValues[i];
752 break;
753 }
754 }
755 return state;
756 }
757
758 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 * Restores the previous state, if it exists.
760 *
761 * @param savedState The previous state.
762 */
763 private void restoreState(Bundle savedState) {
764 if (savedState == null) {
765 return;
766 }
767
Michael Jurka883f55b2010-10-21 15:47:14 -0700768 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700769 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800770 showAllApps(false);
771 }
772
Winson Chungf0c6ae02012-03-21 16:10:31 -0700773 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700775 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 }
777
Winson Chung3d503fb2011-07-13 17:25:49 -0700778 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
779 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700780
Winson Chung3d503fb2011-07-13 17:25:49 -0700781 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
782 mPendingAddInfo.container = pendingAddContainer;
783 mPendingAddInfo.screen = pendingAddScreen;
784 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
785 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 mRestoring = true;
787 }
788
789 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
790 if (renameFolder) {
791 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700792 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 mRestoring = true;
794 }
Winson Chunga12a2502010-12-20 14:41:35 -0800795
Winson Chung785d2eb2011-04-14 16:08:02 -0700796
797 // Restore the AppsCustomize tab
798 if (mAppsCustomizeTabHost != null) {
799 String curTab = savedState.getString("apps_customize_currentTab");
800 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700801 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700802 mAppsCustomizeContent.setContentType(
803 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
804 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700805 mAppsCustomizeContent.loadAssociatedPages(
806 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700807 }
808
Winson Chung5afbf7b2011-07-25 11:53:08 -0700809 int currentIndex = savedState.getInt("apps_customize_currentIndex");
810 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
813
814 /**
815 * Finds all the views we need and configure them properly.
816 */
817 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700818 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400819
Winson Chung4c98d922011-05-31 16:50:48 -0700820 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
821 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800822 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
823 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824
Winson Chung4c98d922011-05-31 16:50:48 -0700825 // Setup the drag layer
826 mDragLayer.setup(this, dragController);
827
Winson Chung3d503fb2011-07-13 17:25:49 -0700828 // Setup the hotseat
829 mHotseat = (Hotseat) findViewById(R.id.hotseat);
830 if (mHotseat != null) {
831 mHotseat.setup(this);
832 }
833
Winson Chung4c98d922011-05-31 16:50:48 -0700834 // Setup the workspace
835 mWorkspace.setHapticFeedbackEnabled(false);
836 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700837 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700838 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700839
Winson Chungf0ea4d32011-06-06 14:27:16 -0700840 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700841 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700842
Winson Chungf0ea4d32011-06-06 14:27:16 -0700843 // Setup AppsCustomize
844 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
845 findViewById(R.id.apps_customize_pane);
846 mAppsCustomizeContent = (AppsCustomizePagedView)
847 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700848 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700849 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400850
Michael Jurka5130e402011-10-13 04:55:35 -0700851 // Get the all apps button
852 mAllAppsButton = findViewById(R.id.all_apps_button);
853 if (mAllAppsButton != null) {
854 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
855 @Override
856 public boolean onTouch(View v, MotionEvent event) {
857 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
858 onTouchDownAllAppsButton(v);
859 }
860 return false;
861 }
862 });
863 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700864 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700865 dragController.setDragScoller(mWorkspace);
866 dragController.setScrollView(mDragLayer);
867 dragController.setMoveTarget(mWorkspace);
868 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700869 if (mSearchDropTargetBar != null) {
870 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800871 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 }
873
874 /**
875 * Creates a view representing a shortcut.
876 *
877 * @param info The data structure describing the shortcut.
878 *
879 * @return A View inflated from R.layout.application.
880 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800881 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700883 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 }
885
886 /**
887 * Creates a view representing a shortcut inflated from the specified resource.
888 *
889 * @param layoutResId The id of the XML layout used to create the shortcut.
890 * @param parent The group the shortcut belongs to.
891 * @param info The data structure describing the shortcut.
892 *
893 * @return A View inflated from layoutResId.
894 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800895 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800896 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
897 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 return favorite;
900 }
901
902 /**
903 * Add an application shortcut to the workspace.
904 *
905 * @param data The intent describing the application.
906 * @param cellInfo The position on screen where to create the shortcut.
907 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700908 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700909 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700910 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700911
Adam Cohenfbba09b2011-07-18 21:43:05 -0700912 // First we check if we already know the exact location where we want to add this item.
913 if (cellX >= 0 && cellY >= 0) {
914 cellXY[0] = cellX;
915 cellXY[1] = cellY;
916 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700917 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700918 return;
919 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800921 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800922
Romain Guy73b979d2009-06-09 12:57:21 -0700923 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800924 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800926 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700927 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700928 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800929 } else {
930 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 }
932 }
Romain Guycbb89e42009-06-08 15:52:54 -0700933
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 /**
935 * Add a shortcut to the workspace.
936 *
937 * @param data The intent describing the shortcut.
938 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
941 int cellY) {
942 int[] cellXY = mTmpAddItemCellCoordinates;
943 int[] touchXY = mPendingAddInfo.dropPos;
944 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700945
Michael Jurkad3ef3062010-11-23 16:23:58 -0800946 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700947
Adam Cohen558baaf2011-08-15 15:22:57 -0700948 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800949 if (info == null) {
950 return;
951 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700952 final View view = createShortcut(info);
953
Adam Cohenfbba09b2011-07-18 21:43:05 -0700954 // First we check if we already know the exact location where we want to add this item.
955 if (cellX >= 0 && cellY >= 0) {
956 cellXY[0] = cellX;
957 cellXY[1] = cellY;
958 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700959
960 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800961 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700962 true, null,null)) {
963 return;
964 }
965 DragObject dragObject = new DragObject();
966 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -0800967 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
968 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -0700969 return;
970 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700971 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800972 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700973 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800974 foundCellSpan = (result != null);
975 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700976 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800977 }
978
979 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -0700980 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700981 return;
982 }
983
Adam Cohen558baaf2011-08-15 15:22:57 -0700984 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985
986 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700987 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
988 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 }
990 }
991
Winson Chunga5c96362012-04-12 14:04:41 -0700992 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800993 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700994 // We want to account for the extra amount of padding that we are adding to the widget
995 // to ensure that it gets the full amount of space that it has requested
996 int requiredWidth = minWidth + padding.left + padding.right;
997 int requiredHeight = minHeight + padding.top + padding.bottom;
998 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
999 }
1000
Winson Chunga5c96362012-04-12 14:04:41 -07001001 int[] getSpanForWidget(AppWidgetProviderInfo info) {
1002 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001003 }
1004
Winson Chunga5c96362012-04-12 14:04:41 -07001005 int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1006 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001007 }
1008
Winson Chunga5c96362012-04-12 14:04:41 -07001009 int[] getSpanForWidget(PendingAddWidgetInfo info) {
1010 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001011 }
1012
Winson Chunga5c96362012-04-12 14:04:41 -07001013 int[] getMinSpanForWidget(PendingAddWidgetInfo info) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001014 return getSpanForWidget(info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001015 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001016 }
1017
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001019 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001021 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001022 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001024 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1025 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1026 if (appWidgetInfo == null) {
1027 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1028 }
Romain Guycbb89e42009-06-08 15:52:54 -07001029
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001030 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001031 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001032
Winson Chunga5c96362012-04-12 14:04:41 -07001033 int[] minSpanXY = getMinSpanForWidget(appWidgetInfo);
1034 int[] spanXY = getSpanForWidget(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001035
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001037 // We have saved the position to which the widget was dragged-- this really only matters
1038 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001039 int[] cellXY = mTmpAddItemCellCoordinates;
1040 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001041 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001042 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001043 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1044 cellXY[0] = mPendingAddInfo.cellX;
1045 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001046 spanXY[0] = mPendingAddInfo.spanX;
1047 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001048 foundCellSpan = true;
1049 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001050 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001051 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001052 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1053 spanXY[1], cellXY, finalSpan);
1054 spanXY[0] = finalSpan[0];
1055 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001056 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001057 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001058 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001059 }
1060
Michael Jurka0280c3b2010-09-17 15:00:07 -07001061 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001062 if (appWidgetId != -1) {
1063 // Deleting an app widget ID is a void call but writes to disk before returning
1064 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001065 new Thread("deleteAppWidgetId") {
1066 public void run() {
1067 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1068 }
1069 }.start();
1070 }
Winson Chung93eef082012-03-23 15:59:27 -07001071 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001072 return;
1073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001075 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -07001076 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001077 launcherInfo.spanX = spanXY[0];
1078 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001079 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1080 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001083 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084
1085 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001086 if (hostView == null) {
1087 // Perform actual inflation because we're live
1088 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1089 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1090 } else {
1091 // The AppWidgetHostView has already been inflated and instantiated
1092 launcherInfo.hostView = hostView;
1093 }
Romain Guycbb89e42009-06-08 15:52:54 -07001094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001096 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung3d503fb2011-07-13 17:25:49 -07001097 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001098 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001099
1100 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001102 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 }
Romain Guycbb89e42009-06-08 15:52:54 -07001104
Adam Cohended9f8d2010-11-03 13:25:16 -07001105 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1106 @Override
1107 public void onReceive(Context context, Intent intent) {
1108 final String action = intent.getAction();
1109 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1110 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001111 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001112 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001113
Winson Chungc100e8e2011-08-09 16:02:43 -07001114 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001115 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001116 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1117 mAppsCustomizeTabHost.reset();
1118 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001119 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001120 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1121 mUserPresent = true;
1122 updateRunning();
1123 }
1124 }
1125 };
1126
1127 @Override
1128 public void onAttachedToWindow() {
1129 super.onAttachedToWindow();
1130
1131 // Listen for broadcasts related to user-presence
1132 final IntentFilter filter = new IntentFilter();
1133 filter.addAction(Intent.ACTION_SCREEN_OFF);
1134 filter.addAction(Intent.ACTION_USER_PRESENT);
1135 registerReceiver(mReceiver, filter);
1136
Adam Cohend113e0c2010-11-11 10:48:05 -08001137 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001138 mVisible = true;
1139 }
1140
1141 @Override
1142 public void onDetachedFromWindow() {
1143 super.onDetachedFromWindow();
1144 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001145 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001146
Adam Cohend113e0c2010-11-11 10:48:05 -08001147 if (mAttached) {
1148 unregisterReceiver(mReceiver);
1149 mAttached = false;
1150 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001151 updateRunning();
1152 }
1153
1154 public void onWindowVisibilityChanged(int visibility) {
1155 mVisible = visibility == View.VISIBLE;
1156 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001157 // The following code used to be in onResume, but it turns out onResume is called when
1158 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1159 // is a more appropriate event to handle
1160 if (mVisible) {
1161 mAppsCustomizeTabHost.onWindowVisible();
1162 if (!mWorkspaceLoading) {
1163 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001164 // We want to let Launcher draw itself at least once before we force it to build
1165 // layers on all the workspace pages, so that transitioning to Launcher from other
1166 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1167 // a true draw so we wait until the second preDraw call to be safe
1168 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001169 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001170 // We delay the layer building a bit in order to give
1171 // other message processing a time to run. In particular
1172 // this avoids a delay in hiding the IME if it was
1173 // currently shown, because doing that may involve
1174 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001175 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurka6ee21d22012-02-21 18:20:27 -08001176 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001177 return true;
1178 }
1179 });
1180 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001181 // When Launcher comes back to foreground, a different Activity might be responsible for
1182 // the app market intent, so refresh the icon
1183 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001184 clearTypedText();
1185 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001186 }
1187
1188 private void sendAdvanceMessage(long delay) {
1189 mHandler.removeMessages(ADVANCE_MSG);
1190 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1191 mHandler.sendMessageDelayed(msg, delay);
1192 mAutoAdvanceSentTime = System.currentTimeMillis();
1193 }
1194
1195 private void updateRunning() {
1196 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1197 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1198 mAutoAdvanceRunning = autoAdvanceRunning;
1199 if (autoAdvanceRunning) {
1200 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1201 sendAdvanceMessage(delay);
1202 } else {
1203 if (!mWidgetsToAdvance.isEmpty()) {
1204 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1205 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1206 }
1207 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001208 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001209 }
1210 }
1211 }
1212
1213 private final Handler mHandler = new Handler() {
1214 @Override
1215 public void handleMessage(Message msg) {
1216 if (msg.what == ADVANCE_MSG) {
1217 int i = 0;
1218 for (View key: mWidgetsToAdvance.keySet()) {
1219 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1220 final int delay = mAdvanceStagger * i;
1221 if (v instanceof Advanceable) {
1222 postDelayed(new Runnable() {
1223 public void run() {
1224 ((Advanceable) v).advance();
1225 }
1226 }, delay);
1227 }
1228 i++;
1229 }
1230 sendAdvanceMessage(mAdvanceInterval);
1231 }
1232 }
1233 };
1234
1235 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001236 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001237 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1238 if (v instanceof Advanceable) {
1239 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001240 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001241 updateRunning();
1242 }
1243 }
1244
1245 void removeWidgetToAutoAdvance(View hostView) {
1246 if (mWidgetsToAdvance.containsKey(hostView)) {
1247 mWidgetsToAdvance.remove(hostView);
1248 updateRunning();
1249 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001250 }
1251
Joe Onorato9c1289c2009-08-17 11:03:03 -04001252 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001253 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001254 launcherInfo.hostView = null;
1255 }
1256
Winson Chung93eef082012-03-23 15:59:27 -07001257 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1258 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1259 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001260 }
1261
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001262 public LauncherAppWidgetHost getAppWidgetHost() {
1263 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 }
Romain Guycbb89e42009-06-08 15:52:54 -07001265
Winson Chunga9abd0e2010-10-27 17:18:37 -07001266 public LauncherModel getModel() {
1267 return mModel;
1268 }
1269
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001270 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001271 getWindow().closeAllPanels();
1272
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001273 // Whatever we were doing is hereby canceled.
1274 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001275 }
1276
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 @Override
1278 protected void onNewIntent(Intent intent) {
1279 super.onNewIntent(intent);
1280
1281 // Close the menu
1282 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001283 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001284 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001285
Joe Onorato14f122b2009-11-19 14:06:36 -08001286 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1287 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001288
Adam Cohenac56cff2011-09-28 20:45:37 -07001289 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001290 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001291 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001292 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1293 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001294 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001295 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001296
1297 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001298 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001299 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001300
Joe Onorato3a8820b2009-11-10 15:06:42 -08001301 final View v = getWindow().peekDecorView();
1302 if (v != null && v.getWindowToken() != null) {
1303 InputMethodManager imm = (InputMethodManager)getSystemService(
1304 INPUT_METHOD_SERVICE);
1305 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001307
Michael Jurka35aa14d2011-07-07 17:01:08 -07001308 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001309 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001310 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001311 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 }
1313 }
1314
1315 @Override
1316 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1317 // Do not call super here
1318 mSavedInstanceState = savedInstanceState;
1319 }
1320
1321 @Override
1322 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001323 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001324 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325
Michael Jurka883f55b2010-10-21 15:47:14 -07001326 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001327 // We close any open folder since it will not be re-opened, and we need to make sure
1328 // this state is reflected.
1329 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330
Winson Chung3d503fb2011-07-13 17:25:49 -07001331 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1332 mWaitingForResult) {
1333 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1334 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1335 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1336 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
1338
1339 if (mFolderInfo != null && mWaitingForResult) {
1340 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1341 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1342 }
Michael Jurka946ad472010-07-09 18:05:18 -07001343
Winson Chung785d2eb2011-04-14 16:08:02 -07001344 // Save the current AppsCustomize tab
1345 if (mAppsCustomizeTabHost != null) {
1346 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1347 if (currentTabTag != null) {
1348 outState.putString("apps_customize_currentTab", currentTabTag);
1349 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001350 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1351 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001352 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 }
1354
1355 @Override
1356 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001358
Winson Chunge7a03942011-08-05 15:05:12 -07001359 // Remove all pending runnables
1360 mHandler.removeMessages(ADVANCE_MSG);
1361 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001362 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001363
Winson Chungcd2b0142011-06-08 16:02:26 -07001364 // Stop callbacks from LauncherModel
1365 LauncherApplication app = ((LauncherApplication) getApplication());
1366 mModel.stopLoader();
1367 app.setLauncher(null);
1368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001370 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001372 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001374 mAppWidgetHost = null;
1375
1376 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377
1378 TextKeyListener.getInstance().release();
1379
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380
Winson Chung3d503fb2011-07-13 17:25:49 -07001381 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001382
1383 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001384 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001385
1386 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1387 mWorkspace.removeAllViews();
1388 mWorkspace = null;
1389 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001390
1391 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 }
1393
Adam Cohena9cf38f2011-05-02 15:36:58 -07001394 public DragController getDragController() {
1395 return mDragController;
1396 }
1397
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 @Override
1399 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001400 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 super.startActivityForResult(intent, requestCode);
1402 }
1403
Winson Chung70d72102011-08-12 11:24:35 -07001404 /**
1405 * Indicates that we want global search for this activity by setting the globalSearch
1406 * argument for {@link #startSearch} to true.
1407 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001409 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001411
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001412 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001413
Karl Rosaen138a0412009-04-23 19:00:21 -07001414 if (initialQuery == null) {
1415 // Use any text typed in the launcher as the initial query
1416 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001417 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 if (appSearchData == null) {
1419 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001420 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001422 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001423
Karl Rosaen138a0412009-04-23 19:00:21 -07001424 final SearchManager searchManager =
1425 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001426 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001427 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 }
1429
Winson Chung70d72102011-08-12 11:24:35 -07001430 @Override
1431 public boolean onCreateOptionsMenu(Menu menu) {
1432 if (isWorkspaceLocked()) {
1433 return false;
1434 }
1435
1436 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001437
1438 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1439 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1440 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001441 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1442 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1443 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001444 String helpUrl = getString(R.string.help_url);
1445 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001446 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1447 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1448
Winson Chung70d72102011-08-12 11:24:35 -07001449 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1450 .setIcon(android.R.drawable.ic_menu_gallery)
1451 .setAlphabeticShortcut('W');
1452 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1453 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001454 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001455 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001456 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1457 .setIcon(android.R.drawable.ic_menu_preferences)
1458 .setIntent(settings)
1459 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001460 if (!helpUrl.isEmpty()) {
1461 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1462 .setIcon(android.R.drawable.ic_menu_help)
1463 .setIntent(help)
1464 .setAlphabeticShortcut('H');
1465 }
Winson Chung70d72102011-08-12 11:24:35 -07001466 return true;
1467 }
1468
1469 @Override
1470 public boolean onPrepareOptionsMenu(Menu menu) {
1471 super.onPrepareOptionsMenu(menu);
1472
1473 if (mAppsCustomizeTabHost.isTransitioning()) {
1474 return false;
1475 }
1476 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1477 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1478
1479 return true;
1480 }
1481
1482 @Override
1483 public boolean onOptionsItemSelected(MenuItem item) {
1484 switch (item.getItemId()) {
1485 case MENU_WALLPAPER_SETTINGS:
1486 startWallpaper();
1487 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001488 }
1489
1490 return super.onOptionsItemSelected(item);
1491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001493 @Override
1494 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001495 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001496 // Use a custom animation for launching search
1497 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001498 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001499 }
1500
Joe Onorato9c1289c2009-08-17 11:03:03 -04001501 public boolean isWorkspaceLocked() {
1502 return mWorkspaceLoading || mWaitingForResult;
1503 }
1504
Michael Jurka0280c3b2010-09-17 15:00:07 -07001505 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001506 mPendingAddInfo.container = ItemInfo.NO_ID;
1507 mPendingAddInfo.screen = -1;
1508 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1509 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001510 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001511 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001512 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001513
Michael Jurkaaf442092010-06-10 17:01:57 -07001514 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001515 // TODO: catch bad widget exception when sent
1516 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001517 // TODO: Is this log message meaningful?
1518 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001519 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001520 }
1521
Adam Cohened66b2b2012-01-23 17:28:51 -08001522 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1523 final AppWidgetProviderInfo appWidget = info.info;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001524 if (appWidget.configure != null) {
1525 // Launch over to configure widget, if needed
1526 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1527 intent.setComponent(appWidget.configure);
1528 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001529 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001530 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001531 intent.putExtra(InstallWidgetReceiver.
1532 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001533
1534 final String mimeType = info.mimeType;
1535 final ClipData clipData = (ClipData) info.configurationData;
1536 final ClipDescription clipDesc = clipData.getDescription();
1537 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1538 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001539 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001540 final CharSequence stringData = item.getText();
1541 final Uri uriData = item.getUri();
1542 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001543 final String key = InstallWidgetReceiver.
1544 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001545 if (uriData != null) {
1546 intent.putExtra(key, uriData);
1547 } else if (intentData != null) {
1548 intent.putExtra(key, intentData);
1549 } else if (stringData != null) {
1550 intent.putExtra(key, stringData);
1551 }
1552 break;
1553 }
1554 }
1555 }
Winson Chung55cef262010-10-28 14:14:18 -07001556 }
Romain Guy8e633c52010-03-04 12:51:36 -08001557 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001558 mWidgetBeingBoundOrConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001560 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001561 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001562 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001563 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 }
1565 }
Romain Guycbb89e42009-06-08 15:52:54 -07001566
Adam Cohenfbba09b2011-07-18 21:43:05 -07001567 /**
1568 * Process a shortcut drop.
1569 *
1570 * @param componentName The name of the component
1571 * @param screen The screen where it should be added
1572 * @param cell The cell it should be added to, optional
1573 * @param position The location on the screen where it was dropped, optional
1574 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001575 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1576 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001577 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001578 mPendingAddInfo.container = container;
1579 mPendingAddInfo.screen = screen;
1580 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001581
1582 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001583 mPendingAddInfo.cellX = cell[0];
1584 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001585 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001586
1587 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1588 createShortcutIntent.setComponent(componentName);
1589 processShortcut(createShortcutIntent);
1590 }
1591
Adam Cohenfbba09b2011-07-18 21:43:05 -07001592 /**
1593 * Process a widget drop.
1594 *
1595 * @param info The PendingAppWidgetInfo of the widget being added.
1596 * @param screen The screen where it should be added
1597 * @param cell The cell it should be added to, optional
1598 * @param position The location on the screen where it was dropped, optional
1599 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001600 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001601 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001602 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001603 mPendingAddInfo.container = info.container = container;
1604 mPendingAddInfo.screen = info.screen = screen;
1605 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001606 mPendingAddInfo.minSpanX = info.minSpanX;
1607 mPendingAddInfo.minSpanY = info.minSpanY;
1608
Adam Cohenfbba09b2011-07-18 21:43:05 -07001609 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001610 mPendingAddInfo.cellX = cell[0];
1611 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001612 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001613 if (span != null) {
1614 mPendingAddInfo.spanX = span[0];
1615 mPendingAddInfo.spanY = span[1];
1616 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001617
Adam Cohened66b2b2012-01-23 17:28:51 -08001618 AppWidgetHostView hostView = info.boundWidget;
1619 int appWidgetId;
1620 if (hostView != null) {
1621 appWidgetId = hostView.getAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001622 addAppWidgetImpl(appWidgetId, info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001623 } else {
1624 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001625 mWidgetBeingBoundOrConfigured = info;
1626 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
1627 addAppWidgetImpl(appWidgetId, info);
1628 } else {
1629 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1630 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1631 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1632 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1633 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001634 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001635 }
1636
Joe Onoratodeb98af2010-02-19 14:59:39 -08001637 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001638 // Handle case where user selected "Applications"
1639 String applicationName = getResources().getString(R.string.group_applications);
1640 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001641
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001642 if (applicationName != null && applicationName.equals(shortcutName)) {
1643 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1644 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001645
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001646 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1647 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001648 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001649 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001650 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001651 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001652 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 }
1654
Winson Chung24ab2f12010-09-16 14:10:47 -07001655 void processWallpaper(Intent intent) {
1656 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1657 }
1658
Winson Chung3d503fb2011-07-13 17:25:49 -07001659 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1660 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001661 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 folderInfo.title = getText(R.string.folder_name);
1663
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001665 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1666 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001667 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668
1669 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001670 FolderIcon newFolder =
1671 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1672 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1673 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001674 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 }
Romain Guycbb89e42009-06-08 15:52:54 -07001676
Joe Onorato9c1289c2009-08-17 11:03:03 -04001677 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001678 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001679 }
1680
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001682 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001684 Intent chooser = Intent.createChooser(pickWallpaper,
1685 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001686 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1687 // Removed in Eclair MR1
1688// WallpaperManager wm = (WallpaperManager)
1689// getSystemService(Context.WALLPAPER_SERVICE);
1690// WallpaperInfo wi = wm.getWallpaperInfo();
1691// if (wi != null && wi.getSettingsActivity() != null) {
1692// LabeledIntent li = new LabeledIntent(getPackageName(),
1693// R.string.configure_wallpaper, 0);
1694// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1695// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1696// }
Mike Clerona0618e42009-10-22 13:55:21 -07001697 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 }
1699
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001700 /**
1701 * Registers various content observers. The current implementation registers
1702 * only a favorites observer to keep track of the favorites applications.
1703 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001704 private void registerContentObservers() {
1705 ContentResolver resolver = getContentResolver();
1706 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1707 true, mWidgetObserver);
1708 }
1709
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710 @Override
1711 public boolean dispatchKeyEvent(KeyEvent event) {
1712 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1713 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001715 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001716 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001717 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001718 dumpState();
1719 return true;
1720 }
1721 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001722 }
1723 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1724 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001725 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 return true;
1727 }
1728 }
1729
1730 return super.dispatchKeyEvent(event);
1731 }
1732
Joe Onorato88ec0992009-11-19 13:16:06 -08001733 @Override
1734 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001735 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001736 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001737 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001738 Folder openFolder = mWorkspace.getOpenFolder();
1739 if (openFolder.isEditingName()) {
1740 openFolder.dismissEditingName();
1741 } else {
1742 closeFolder();
1743 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001744 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001745 mWorkspace.exitWidgetResizeMode();
1746
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001747 // Back button is a no-op here, but give at least some feedback for the button press
1748 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001749 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001750 }
1751
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001753 * Re-listen when widgets are reset.
1754 */
1755 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001756 if (mAppWidgetHost != null) {
1757 mAppWidgetHost.startListening();
1758 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001759 }
1760
1761 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001762 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1763 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001765 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001766 if (mModel != null) {
1767 mModel.unbindWorkspaceItems();
1768 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001770
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 /**
1772 * Launches the intent referred by the clicked shortcut.
1773 *
1774 * @param v The view representing the clicked shortcut.
1775 */
1776 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001777 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1778 // view has detached (it's possible for this to happen if the view is removed mid touch).
1779 if (v.getWindowToken() == null) {
1780 return;
1781 }
1782
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001783 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001784 return;
1785 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001786
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001788 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001790 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001791 int[] pos = new int[2];
1792 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001793 intent.setSourceBounds(new Rect(pos[0], pos[1],
1794 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001795
1796 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001797
1798 if (success && v instanceof BubbleTextView) {
1799 mWaitingForResume = (BubbleTextView) v;
1800 mWaitingForResume.setStayPressed(true);
1801 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001803 if (v instanceof FolderIcon) {
1804 FolderIcon fi = (FolderIcon) v;
1805 handleFolderClick(fi);
1806 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001807 } else if (v == mAllAppsButton) {
1808 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001809 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001810 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001811 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001812 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 }
1814 }
1815
Michael Jurka0e260592010-06-30 17:07:39 -07001816 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001817 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001818 // clicking anywhere on the workspace causes the customization drawer to slide down
1819 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001820 return false;
1821 }
1822
Michael Jurkaaf442092010-06-10 17:01:57 -07001823 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001824 * Event handler for the search button
1825 *
1826 * @param v The view that was clicked.
1827 */
1828 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001829 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1830
Amith Yamasania135ba82011-08-09 17:42:01 -07001831 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001832 }
1833
1834 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001835 * Event handler for the voice button
1836 *
1837 * @param v The view that was clicked.
1838 */
1839 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001840 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001841
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001842 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001843 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001844 startActivitySafely(null, intent, "onClickVoiceButton");
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001845 }
1846
1847 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001848 * Event handler for the "grid" button that appears on the home screen, which
1849 * enters all apps mode.
1850 *
1851 * @param v The view that was clicked.
1852 */
1853 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001854 showAllApps(true);
1855 }
1856
1857 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001858 // Provide the same haptic feedback that the system offers for virtual keys.
1859 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001860 }
1861
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001862 public void onClickAppMarketButton(View v) {
1863 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001864 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001865 } else {
1866 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001867 }
1868 }
1869
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001870 void startApplicationDetailsActivity(ComponentName componentName) {
1871 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001872 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1873 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001874 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001875 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001876 }
1877
Patrick Dubroy5539af72010-09-07 15:22:01 -07001878 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1879 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1880 // System applications cannot be installed. For now, show a toast explaining that.
1881 // We may give them the option of disabling apps this way.
1882 int messageId = R.string.uninstall_system_app_text;
1883 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1884 } else {
1885 String packageName = appInfo.componentName.getPackageName();
1886 String className = appInfo.componentName.getClassName();
1887 Intent intent = new Intent(
1888 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001889 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1890 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001891 startActivity(intent);
1892 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001893 }
1894
Winson Chungc7450e32012-04-17 17:34:08 -07001895 boolean startActivitySafely(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001897
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 try {
Winson Chungc7450e32012-04-17 17:34:08 -07001899 if (v != null) {
1900 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1901 v.getMeasuredWidth(), v.getMeasuredHeight());
1902
1903 startActivity(intent, opts.toBundle());
1904 } else {
1905 startActivity(intent);
1906 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001907 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 } catch (ActivityNotFoundException e) {
1909 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001910 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 } catch (SecurityException e) {
1912 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001913 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001915 "or use the exported attribute for this activity. "
1916 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001918 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001920
Romain Guy8e633c52010-03-04 12:51:36 -08001921 void startActivityForResultSafely(Intent intent, int requestCode) {
1922 try {
1923 startActivityForResult(intent, requestCode);
1924 } catch (ActivityNotFoundException e) {
1925 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1926 } catch (SecurityException e) {
1927 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1928 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1929 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1930 "or use the exported attribute for this activity.", e);
1931 }
1932 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933
Adam Cohena9cf38f2011-05-02 15:36:58 -07001934 private void handleFolderClick(FolderIcon folderIcon) {
1935 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001936 Folder openFolder = mWorkspace.getFolderForTag(info);
1937
1938 // If the folder info reports that the associated folder is open, then verify that
1939 // it is actually opened. There have been a few instances where this gets out of sync.
1940 if (info.opened && openFolder == null) {
1941 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1942 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1943 info.opened = false;
1944 }
1945
Adam Cohena9cf38f2011-05-02 15:36:58 -07001946 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 // Close any open folder
1948 closeFolder();
1949 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001950 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 } else {
1952 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 int folderScreen;
1954 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001955 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 // .. and close it
1957 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001958 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 // Close any folder open on the current screen
1960 closeFolder();
1961 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001962 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 }
1964 }
1965 }
1966 }
1967
Adam Cohen2801caf2011-05-13 20:57:39 -07001968 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001969 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001970 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1971 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1972 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1973
Adam Cohenc51934b2011-07-26 21:07:43 -07001974 FolderInfo info = (FolderInfo) fi.getTag();
1975 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1976 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001977 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1978 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001979 }
1980
Adam Cohen2801caf2011-05-13 20:57:39 -07001981 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1982 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1983 oa.start();
1984 }
1985
1986 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001987 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001988 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1989 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1990 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1991
Adam Cohenc51934b2011-07-26 21:07:43 -07001992 FolderInfo info = (FolderInfo) fi.getTag();
1993 CellLayout cl = null;
1994 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1995 cl = (CellLayout) fi.getParent().getParent();
1996 }
1997
1998 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001999 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2000 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002001 oa.addListener(new AnimatorListenerAdapter() {
2002 @Override
2003 public void onAnimationEnd(Animator animation) {
2004 if (layout != null) {
2005 layout.clearFolderLeaveBehind();
2006 }
2007 }
2008 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002009 oa.start();
2010 }
2011
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002013 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 * is animated relative to the specified View. If the View is null, no animation
2015 * is played.
2016 *
2017 * @param folderInfo The FolderInfo describing the folder to open.
2018 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002019 public void openFolder(FolderIcon folderIcon) {
2020 Folder folder = folderIcon.mFolder;
2021 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022
Adam Cohen2801caf2011-05-13 20:57:39 -07002023 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002024 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025
Adam Cohen4554ee12011-08-03 16:13:21 -07002026 // Just verify that the folder hasn't already been added to the DragLayer.
2027 // There was a one-off crash where the folder had a parent already.
2028 if (folder.getParent() == null) {
2029 mDragLayer.addView(folder);
2030 mDragController.addDropTarget((DropTarget) folder);
2031 } else {
2032 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2033 folder.getParent() + ").");
2034 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002035 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002036 }
2037
2038 public void closeFolder() {
2039 Folder folder = mWorkspace.getOpenFolder();
2040 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002041 if (folder.isEditingName()) {
2042 folder.dismissEditingName();
2043 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002044 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002045
2046 // Dismiss the folder cling
2047 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002048 }
2049 }
2050
2051 void closeFolder(Folder folder) {
2052 folder.getInfo().opened = false;
2053
2054 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2055 if (parent != null) {
2056 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2057 shrinkAndFadeInFolderIcon(fi);
2058 }
2059 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 }
2061
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002063
Patrick Dubroye708c522011-03-01 16:03:43 -08002064 if (mState != State.WORKSPACE) {
2065 return false;
2066 }
2067
Joe Onorato9c1289c2009-08-17 11:03:03 -04002068 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 return false;
2070 }
2071
2072 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002073 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 }
2075
Michael Jurka0280c3b2010-09-17 15:00:07 -07002076 resetAddInfo();
2077 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002079 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 return true;
2081 }
2082
Winson Chung3d503fb2011-07-13 17:25:49 -07002083 // The hotseat touch handling does not go through Workspace, and we always allow long press
2084 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002085 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002086 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2087 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002088 if (itemUnderLongClick == null) {
2089 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002090 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2091 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002092 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002094 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002096 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 }
2098 }
2099 }
2100 return true;
2101 }
2102
Winson Chung3d503fb2011-07-13 17:25:49 -07002103 boolean isHotseatLayout(View layout) {
2104 return mHotseat != null && layout != null &&
2105 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2106 }
2107 Hotseat getHotseat() {
2108 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002109 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002110 SearchDropTargetBar getSearchBar() {
2111 return mSearchDropTargetBar;
2112 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002113
Winson Chung3d503fb2011-07-13 17:25:49 -07002114 /**
2115 * Returns the CellLayout of the specified container at the specified screen.
2116 */
2117 CellLayout getCellLayout(long container, int screen) {
2118 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2119 if (mHotseat != null) {
2120 return mHotseat.getLayout();
2121 } else {
2122 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002123 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002124 } else {
2125 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002126 }
2127 }
2128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 Workspace getWorkspace() {
2130 return mWorkspace;
2131 }
2132
Daniel Sandler843e8602010-06-07 14:59:01 -04002133 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2134 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002135 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002136 }
2137
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002138 public boolean isAllAppsButtonRank(int rank) {
2139 return mHotseat.isAllAppsButtonRank(rank);
2140 }
2141
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002142 // AllAppsView.Watcher
2143 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002144 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002145 mWorkspace.setVisibility(View.GONE);
2146 }
2147 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002148
2149 /**
2150 * Helper method for the cameraZoomIn/cameraZoomOut animations
2151 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002152 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002153 * @param scaleFactor The scale factor used for the zoom
2154 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002155 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002156 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002157 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002158 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002159
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002160 void updateWallpaperVisibility(boolean visible) {
2161 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2162 int curflags = getWindow().getAttributes().flags
2163 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2164 if (wpflags != curflags) {
2165 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2166 }
2167 }
2168
Michael Jurkabed61d22012-02-14 22:51:29 -08002169 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2170 if (v instanceof LauncherTransitionable) {
2171 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2172 }
Winson Chung70442722012-02-10 15:43:22 -08002173
2174 // Update the workspace transition step as well
2175 dispatchOnLauncherTransitionStep(v, 0f);
2176 }
2177
2178 private void dispatchOnLauncherTransitionStep(View v, float t) {
2179 if (v instanceof LauncherTransitionable) {
2180 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2181 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002182 }
2183
2184 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2185 if (v instanceof LauncherTransitionable) {
2186 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2187 }
Winson Chung70442722012-02-10 15:43:22 -08002188
2189 // Update the workspace transition step as well
2190 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002191 }
2192
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002193 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002194 * Things to test when changing the following seven functions.
2195 * - Home from workspace
2196 * - from center screen
2197 * - from other screens
2198 * - Home from all apps
2199 * - from center screen
2200 * - from other screens
2201 * - Back from all apps
2202 * - from center screen
2203 * - from other screens
2204 * - Launch app from workspace and quit
2205 * - with back
2206 * - with home
2207 * - Launch app from all apps and quit
2208 * - with back
2209 * - with home
2210 * - Go to a screen that's not the default, then all
2211 * apps, and launch and app, and go back
2212 * - with back
2213 * -with home
2214 * - On workspace, long press power and go back
2215 * - with back
2216 * - with home
2217 * - On all apps, long press power and go back
2218 * - with back
2219 * - with home
2220 * - On workspace, power off
2221 * - On all apps, power off
2222 * - Launch an app and turn off the screen while in that app
2223 * - Go back with home key
2224 * - Go back with back key TODO: make this not go to workspace
2225 * - From all apps
2226 * - From workspace
2227 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2228 * - From all apps
2229 * - From the center workspace
2230 * - From another workspace
2231 */
2232
2233 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002234 * Zoom the camera out from the workspace to reveal 'toView'.
2235 * Assumes that the view to show is anchored at either the very top or very bottom
2236 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002237 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002238 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002239 if (mStateAnimation != null) {
2240 mStateAnimation.cancel();
2241 mStateAnimation = null;
2242 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002243 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002244
Winson Chungf0ea4d32011-06-06 14:27:16 -07002245 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2246 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2247 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002248 final View fromView = mWorkspace;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002249 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002250 final int startDelay =
2251 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002252
Michael Jurkab3e22d92011-10-31 15:58:33 -07002253 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002254
Michael Jurkad74c9842011-07-10 12:44:21 -07002255 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002256 Animator workspaceAnim =
2257 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002258
2259 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002260 toView.setScaleX(scale);
2261 toView.setScaleY(scale);
2262 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2263 scaleAnim.
2264 scaleX(1f).scaleY(1f).
2265 setDuration(duration).
2266 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002267
Winson Chungf0ea4d32011-06-06 14:27:16 -07002268 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002269 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002270 final ObjectAnimator alphaAnim = ObjectAnimator
2271 .ofFloat(toView, "alpha", 0f, 1f)
2272 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002273 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002274 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2275 @Override
2276 public void onAnimationUpdate(ValueAnimator animation) {
2277 float t = (Float) animation.getAnimatedValue();
2278 dispatchOnLauncherTransitionStep(fromView, t);
2279 dispatchOnLauncherTransitionStep(toView, t);
2280 }
2281 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002282
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002283 // toView should appear right at the end of the workspace shrink
2284 // animation
2285 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002286 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002287 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002288
2289 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002290 boolean animationCancelled = false;
2291
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002292 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002293 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002294 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002295 // Prepare the position
2296 toView.setTranslationX(0.0f);
2297 toView.setTranslationY(0.0f);
2298 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002299 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002300 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002301 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002302 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002303 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2304 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002305
2306 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2307 // Hide the workspace scrollbar
2308 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002309 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002310 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002311 if (!animationCancelled) {
2312 updateWallpaperVisibility(false);
2313 }
2314 }
2315
Adam Cohencff6af82011-09-13 14:51:53 -07002316 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002317 public void onAnimationCancel(Animator animation) {
2318 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002319 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002320 });
2321
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002322 if (workspaceAnim != null) {
2323 mStateAnimation.play(workspaceAnim);
2324 }
Michael Jurka1899a362011-11-03 13:50:45 -07002325
2326 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002327 final ViewTreeObserver observer;
2328
Michael Jurkabed61d22012-02-14 22:51:29 -08002329 dispatchOnLauncherTransitionStart(fromView, animated, false);
2330 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002331
2332 // If any of the objects being animated haven't been measured/laid out
2333 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002334 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002335 (mWorkspace.getMeasuredWidth() == 0) ||
2336 (toView.getMeasuredWidth() == 0)) {
2337 observer = mWorkspace.getViewTreeObserver();
2338 delayAnim = true;
2339 } else {
2340 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002341 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002342
2343 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002344 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002345 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2346 public void onGlobalLayout() {
2347 mWorkspace.post(new Runnable() {
2348 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002349 // Check that mStateAnimation hasn't changed while
2350 // we waited for a layout pass
2351 if (mStateAnimation == stateAnimation) {
2352 // Need to update pivots for zoom if layout changed
2353 setPivotsForZoom(toView, scale);
2354 mStateAnimation.start();
2355 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002356 }
2357 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002358 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002359 }
2360 };
2361 observer.addOnGlobalLayoutListener(delayedStart);
2362 } else {
2363 setPivotsForZoom(toView, scale);
Michael Jurka1899a362011-11-03 13:50:45 -07002364 mStateAnimation.start();
2365 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002366 } else {
2367 toView.setTranslationX(0.0f);
2368 toView.setTranslationY(0.0f);
2369 toView.setScaleX(1.0f);
2370 toView.setScaleY(1.0f);
2371 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002372 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002373
Michael Jurkabed61d22012-02-14 22:51:29 -08002374 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2375 // Hide the workspace scrollbar
2376 mWorkspace.hideScrollingIndicator(true);
2377 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002378 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002379 dispatchOnLauncherTransitionStart(fromView, animated, false);
2380 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2381 dispatchOnLauncherTransitionStart(toView, animated, false);
2382 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002383 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002384 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002385 }
2386
2387 /**
2388 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002389 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002390 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002391 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002392 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2393 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002394
Michael Jurkab3e22d92011-10-31 15:58:33 -07002395 if (mStateAnimation != null) {
2396 mStateAnimation.cancel();
2397 mStateAnimation = null;
2398 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002399 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002400
Winson Chungf0ea4d32011-06-06 14:27:16 -07002401 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002402 final int fadeOutDuration =
2403 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002404 final float scaleFactor = (float)
2405 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2406 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002407 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002408 Animator workspaceAnim = null;
2409
2410 if (toState == State.WORKSPACE) {
2411 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2412 workspaceAnim = mWorkspace.getChangeStateAnimation(
2413 Workspace.State.NORMAL, animated, stagger);
2414 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2415 workspaceAnim = mWorkspace.getChangeStateAnimation(
2416 Workspace.State.SPRING_LOADED, animated);
2417 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002418
Michael Jurkab3e22d92011-10-31 15:58:33 -07002419 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002420 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002421 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002422 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002423 final LauncherViewPropertyAnimator scaleAnim =
2424 new LauncherViewPropertyAnimator(fromView);
2425 scaleAnim.
2426 scaleX(scaleFactor).scaleY(scaleFactor).
2427 setDuration(duration).
2428 setInterpolator(new Workspace.ZoomInInterpolator());
2429
2430 final ObjectAnimator alphaAnim = ObjectAnimator
2431 .ofFloat(fromView, "alpha", 1f, 0f)
2432 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002433 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002434 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2435 @Override
2436 public void onAnimationUpdate(ValueAnimator animation) {
2437 float t = 1f - (Float) animation.getAnimatedValue();
2438 dispatchOnLauncherTransitionStep(fromView, t);
2439 dispatchOnLauncherTransitionStep(toView, t);
2440 }
2441 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002442
Michael Jurkabed61d22012-02-14 22:51:29 -08002443 mStateAnimation = new AnimatorSet();
2444
2445 dispatchOnLauncherTransitionStart(fromView, animated, true);
2446 dispatchOnLauncherTransitionStart(toView, animated, true);
2447
2448 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002449 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002450 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002451 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002452 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002453 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2454 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002455 if (mWorkspace != null) {
2456 mWorkspace.hideScrollingIndicator(false);
2457 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002458 if (onCompleteRunnable != null) {
2459 onCompleteRunnable.run();
2460 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002461 }
2462 });
2463
Winson Chungf0ea4d32011-06-06 14:27:16 -07002464 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002465 if (workspaceAnim != null) {
2466 mStateAnimation.play(workspaceAnim);
2467 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002468 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002469 } else {
2470 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002471 dispatchOnLauncherTransitionStart(fromView, animated, true);
2472 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2473 dispatchOnLauncherTransitionStart(toView, animated, true);
2474 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002475 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002476 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002477 }
2478
Michael Jurkae326f182011-11-21 14:05:46 -08002479 @Override
2480 public void onTrimMemory(int level) {
2481 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002482 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002483 mAppsCustomizeTabHost.onTrimMemory();
2484 }
2485 }
2486
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002487 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002488 showWorkspace(animated, null);
2489 }
2490
2491 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002492 if (mState != State.WORKSPACE) {
2493 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002494 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002495
2496 // Show the search bar and hotseat
2497 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002498 // We only need to animate in the dock divider if we're going from spring loaded mode
2499 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002500
2501 // Set focus to the AppsCustomize button
2502 if (mAllAppsButton != null) {
2503 mAllAppsButton.requestFocus();
2504 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002505 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002506
Michael Jurkab737ee62011-11-15 15:57:22 -08002507 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002508
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002509 // Change the state *after* we've called all the transition code
2510 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002511
Winson Chung5fb63472011-02-02 17:03:37 -08002512 // Resume the auto-advance of widgets
2513 mUserPresent = true;
2514 updateRunning();
2515
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002516 // send an accessibility event to announce the context change
2517 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002518 }
2519
Michael Jurkab3e22d92011-10-31 15:58:33 -07002520 void showAllApps(boolean animated) {
2521 if (mState != State.WORKSPACE) return;
2522
2523 showAppsCustomizeHelper(animated, false);
2524 mAppsCustomizeTabHost.requestFocus();
2525
2526 // Hide the search bar and hotseat
2527 mSearchDropTargetBar.hideSearchBar(animated);
2528
2529 // Change the state *after* we've called all the transition code
2530 mState = State.APPS_CUSTOMIZE;
2531
2532 // Pause the auto-advance of widgets until we are out of AllApps
2533 mUserPresent = false;
2534 updateRunning();
2535 closeFolder();
2536
2537 // Send an accessibility event to announce the context change
2538 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2539 }
2540
Adam Cohen7777d962011-08-18 18:58:38 -07002541 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002542 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002543 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002544 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002545 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002546 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002547 }
Adam Cohen7777d962011-08-18 18:58:38 -07002548
Adam Cohened66b2b2012-01-23 17:28:51 -08002549 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2550 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002551 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2552
Winson Chunge7a03942011-08-05 15:05:12 -07002553 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002554 @Override
2555 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002556 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002557 // Before we show workspace, hide all apps again because
2558 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2559 // clean up our state transition functions
2560 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002561 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002562 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002563 } else {
2564 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002565 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002566 }
2567 }, (extendedDelay ?
2568 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2569 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2570 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002571
Michael Jurkad3ef3062010-11-23 16:23:58 -08002572 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002573 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002574 final boolean animated = true;
2575 final boolean springLoaded = true;
2576 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002577 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002578 }
2579 // Otherwise, we are not in spring loaded mode, so don't do anything.
2580 }
2581
Michael Jurkab737ee62011-11-15 15:57:22 -08002582 void hideDockDivider() {
2583 if (mQsbDivider != null && mDockDivider != null) {
2584 mQsbDivider.setVisibility(View.INVISIBLE);
2585 mDockDivider.setVisibility(View.INVISIBLE);
2586 }
2587 }
2588
2589 void showDockDivider(boolean animated) {
2590 if (mQsbDivider != null && mDockDivider != null) {
2591 mQsbDivider.setVisibility(View.VISIBLE);
2592 mDockDivider.setVisibility(View.VISIBLE);
2593 if (mDividerAnimator != null) {
2594 mDividerAnimator.cancel();
2595 mQsbDivider.setAlpha(1f);
2596 mDockDivider.setAlpha(1f);
2597 mDividerAnimator = null;
2598 }
2599 if (animated) {
2600 mDividerAnimator = new AnimatorSet();
2601 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2602 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2603 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2604 mDividerAnimator.start();
2605 }
2606 }
2607 }
2608
Michael Jurkab3e22d92011-10-31 15:58:33 -07002609 void lockAllApps() {
2610 // TODO
2611 }
2612
2613 void unlockAllApps() {
2614 // TODO
2615 }
2616
Adam Cohenfc53cd22011-07-20 15:45:11 -07002617 public boolean isAllAppsCustomizeOpen() {
2618 return mState == State.APPS_CUSTOMIZE;
2619 }
2620
Winson Chungf0ea4d32011-06-06 14:27:16 -07002621 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002622 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002623 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002624 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002625 if (!LauncherApplication.isScreenLarge()) {
2626 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002627 if (mHotseat.getAlpha() != 1f) {
2628 int duration = mSearchDropTargetBar.getTransitionInDuration();
2629 mHotseat.animate().alpha(1f).setDuration(duration);
2630 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002631 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002632 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002633 }
2634 }
2635 }
2636
2637 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002638 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002639 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002640 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002641 if (!LauncherApplication.isScreenLarge()) {
2642 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002643 if (mHotseat.getAlpha() != 0f) {
2644 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2645 mHotseat.animate().alpha(0f).setDuration(duration);
2646 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002647 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002648 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002649 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002650 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002651 }
2652
Patrick Dubroy5f445422011-02-18 14:35:21 -08002653 /**
2654 * Add an item from all apps or customize onto the given workspace screen.
2655 * If layout is null, add to the current screen.
2656 */
2657 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002658 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002659 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002660 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002661 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002662
Winson Chungdff8ebb2011-09-08 17:25:31 -07002663 /** Maps the current orientation to an index for referencing orientation correct global icons */
2664 private int getCurrentOrientationIndexForGlobalIcons() {
2665 // default - 0, landscape - 1
2666 switch (getResources().getConfiguration().orientation) {
2667 case Configuration.ORIENTATION_LANDSCAPE:
2668 return 1;
2669 default:
2670 return 0;
2671 }
2672 }
2673
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002674 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002675 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002676 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002677 // Look for the toolbar icon specified in the activity meta-data
2678 Bundle metaData = packageManager.getActivityInfo(
2679 activityName, PackageManager.GET_META_DATA).metaData;
2680 if (metaData != null) {
2681 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2682 if (iconResId != 0) {
2683 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002684 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002685 }
2686 }
2687 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002688 // This can happen if the activity defines an invalid drawable
2689 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2690 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002691 } catch (Resources.NotFoundException nfe) {
2692 // This can happen if the activity defines an invalid drawable
2693 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2694 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002695 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002696 return null;
2697 }
2698
2699 // if successful in getting icon, return it; otherwise, set button to use default drawable
2700 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2701 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002702 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002703 Resources r = getResources();
2704 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2705 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002706
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002707 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002708 // If we were unable to find the icon via the meta-data, use a generic one
2709 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002710 toolbarIcon = r.getDrawable(fallbackDrawableId);
2711 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002712 if (button != null) {
2713 button.setCompoundDrawables(toolbarIcon, null, null, null);
2714 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002715 return null;
2716 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002717 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002718 if (button != null) {
2719 button.setCompoundDrawables(toolbarIcon, null, null, null);
2720 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002721 return toolbarIcon.getConstantState();
2722 }
2723 }
2724
2725 // if successful in getting icon, return it; otherwise, set button to use default drawable
2726 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2727 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2728 ImageView button = (ImageView) findViewById(buttonId);
2729 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2730
Michael Jurka19e0fc52011-07-22 18:00:21 -07002731 if (button != null) {
2732 // If we were unable to find the icon via the meta-data, use a
2733 // generic one
2734 if (toolbarIcon == null) {
2735 button.setImageResource(fallbackDrawableId);
2736 } else {
2737 button.setImageDrawable(toolbarIcon);
2738 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002739 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002740
2741 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2742
Michael Jurka0423dcf2010-10-05 14:56:18 -07002743 }
2744
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002745 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2746 TextView button = (TextView) findViewById(buttonId);
2747 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2748 }
2749
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002750 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002751 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002752 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002753 }
2754
Winson Chungbb185bd2011-11-21 12:31:42 -08002755 private void invalidatePressedFocusedStates(View container, View button) {
2756 if (container instanceof HolographicLinearLayout) {
2757 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2758 layout.invalidatePressedFocusedStates();
2759 } else if (button instanceof HolographicImageView) {
2760 HolographicImageView view = (HolographicImageView) button;
2761 view.invalidatePressedFocusedStates();
2762 }
2763 }
2764
Winson Chungc51db6a2011-10-05 11:44:49 -07002765 private boolean updateGlobalSearchIcon() {
2766 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002767 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2768 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002769 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002770 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002771 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002772
Winson Chung1cad91e2011-05-25 17:41:01 -07002773 final SearchManager searchManager =
2774 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2775 ComponentName activityName = searchManager.getGlobalSearchActivity();
2776 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002777 int coi = getCurrentOrientationIndexForGlobalIcons();
2778 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002779 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002780 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002781 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2782 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002783 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002784 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002785 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002786 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002787 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002788 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2789 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2790 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002791 voiceButton.setVisibility(View.GONE);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002792 voiceButtonProxy.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002793 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002794 }
2795 }
2796
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002797 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002798 final View searchButtonContainer = findViewById(R.id.search_button_container);
2799 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002800 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002801 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002802 }
2803
Winson Chungc51db6a2011-10-05 11:44:49 -07002804 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002805 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002806 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002807 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002808 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002809
Winson Chungc51db6a2011-10-05 11:44:49 -07002810 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002811 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2812 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002813 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002814 int coi = getCurrentOrientationIndexForGlobalIcons();
2815 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002816 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002817 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002818 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002819 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07002820 if (voiceButtonProxy != null) {
2821 voiceButtonProxy.setVisibility(View.VISIBLE);
2822 }
Winson Chungbb185bd2011-11-21 12:31:42 -08002823 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002824 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002825 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002826 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002827 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002828 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07002829 if (voiceButtonProxy != null) {
2830 voiceButtonProxy.setVisibility(View.GONE);
2831 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002832 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002833 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002834 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002835
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002836 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002837 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2838 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002839 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002840 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002841 }
2842
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002843 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002844 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002845 */
2846 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002847 final View marketButton = findViewById(R.id.market_button);
2848 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2849 // Find the app market activity by resolving an intent.
2850 // (If multiple app markets are installed, it will return the ResolverActivity.)
2851 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002852 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002853 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002854 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002855 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002856 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002857 marketButton.setVisibility(View.VISIBLE);
2858 } else {
2859 // We should hide and disable the view so that we don't try and restore the visibility
2860 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2861 marketButton.setVisibility(View.GONE);
2862 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002863 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002864 }
2865
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002866 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002867 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002868 }
2869
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002870 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002871 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002872 */
2873 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2874 @Override
2875 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002876 closeSystemDialogs();
2877 }
2878 }
2879
2880 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002881 * Receives notifications whenever the appwidgets are reset.
2882 */
2883 private class AppWidgetResetObserver extends ContentObserver {
2884 public AppWidgetResetObserver() {
2885 super(new Handler());
2886 }
2887
2888 @Override
2889 public void onChange(boolean selfChange) {
2890 onAppWidgetReset();
2891 }
2892 }
2893
2894 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002895 * If the activity is currently paused, signal that we need to re-run the loader
2896 * in onResume.
2897 *
2898 * This needs to be called from incoming places where resources might have been loaded
2899 * while we are paused. That is becaues the Configuration might be wrong
2900 * when we're not running, and if it comes back to what it was when we
2901 * were paused, we are not restarted.
2902 *
2903 * Implementation of the method from LauncherModel.Callbacks.
2904 *
2905 * @return true if we are currently paused. The caller might be able to
2906 * skip some work in that case since we will come back again.
2907 */
2908 public boolean setLoadOnResume() {
2909 if (mPaused) {
2910 Log.i(TAG, "setLoadOnResume");
2911 mOnResumeNeedsLoad = true;
2912 return true;
2913 } else {
2914 return false;
2915 }
2916 }
2917
2918 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002919 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002920 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002921 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002922 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002923 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002924 } else {
2925 return SCREEN_COUNT / 2;
2926 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002927 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002928
Joe Onorato9c1289c2009-08-17 11:03:03 -04002929 /**
2930 * Refreshes the shortcuts shown on the workspace.
2931 *
2932 * Implementation of the method from LauncherModel.Callbacks.
2933 */
2934 public void startBinding() {
2935 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002936
Winson Chungf0c6ae02012-03-21 16:10:31 -07002937 mNewShortcutAnimatePage = -1;
2938 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07002939 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002940 int count = workspace.getChildCount();
2941 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002942 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002943 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2944 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002945 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08002946 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07002947 if (mHotseat != null) {
2948 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002949 }
2950 }
2951
2952 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002953 * Bind the items start-end from the list.
2954 *
2955 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002957 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002958 setLoadOnResume();
2959
Winson Chungf0c6ae02012-03-21 16:10:31 -07002960 // Get the list of added shortcuts and intersect them with the set of shortcuts here
2961 Set<String> newApps = new HashSet<String>();
2962 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
2963
2964 Workspace workspace = mWorkspace;
2965 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002966 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002967
2968 // Short circuit if we are loading dock items for a configuration which has no dock
2969 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2970 mHotseat == null) {
2971 continue;
2972 }
2973
Joe Onorato9c1289c2009-08-17 11:03:03 -04002974 switch (item.itemType) {
2975 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2976 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07002977 ShortcutInfo info = (ShortcutInfo) item;
2978 String uri = info.intent.toUri(0).toString();
2979 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07002980 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2981 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07002982 if (newApps.contains(uri)) {
2983 newApps.remove(uri);
2984
2985 // Prepare the view to be animated up
2986 shortcut.setAlpha(0f);
2987 shortcut.setScaleX(0f);
2988 shortcut.setScaleY(0f);
2989 mNewShortcutAnimatePage = item.screen;
2990 if (!mNewShortcutAnimateViews.contains(shortcut)) {
2991 mNewShortcutAnimateViews.add(shortcut);
2992 }
2993 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002994 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002995 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002996 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002997 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002998 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002999 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3000 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003001 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003002 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003003 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003004
Joe Onorato9c1289c2009-08-17 11:03:03 -04003005 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003006 }
3007
Joe Onorato9c1289c2009-08-17 11:03:03 -04003008 /**
3009 * Implementation of the method from LauncherModel.Callbacks.
3010 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003011 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003012 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003013 sFolders.clear();
3014 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003015 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003016
3017 /**
3018 * Add the views for a widget to the workspace.
3019 *
3020 * Implementation of the method from LauncherModel.Callbacks.
3021 */
3022 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003023 setLoadOnResume();
3024
Daniel Sandler843e8602010-06-07 14:59:01 -04003025 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3026 if (DEBUG_WIDGETS) {
3027 Log.d(TAG, "bindAppWidget: " + item);
3028 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003029 final Workspace workspace = mWorkspace;
3030
3031 final int appWidgetId = item.appWidgetId;
3032 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003033 if (DEBUG_WIDGETS) {
3034 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3035 }
3036
Joe Onorato9c1289c2009-08-17 11:03:03 -04003037 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3038
Joe Onorato9c1289c2009-08-17 11:03:03 -04003039 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3040 item.hostView.setTag(item);
3041
Winson Chung3d503fb2011-07-13 17:25:49 -07003042 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003043 item.cellY, item.spanX, item.spanY, false);
3044
Adam Cohended9f8d2010-11-03 13:25:16 -07003045 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3046
Joe Onorato9c1289c2009-08-17 11:03:03 -04003047 workspace.requestLayout();
3048
Daniel Sandler843e8602010-06-07 14:59:01 -04003049 if (DEBUG_WIDGETS) {
3050 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3051 + (SystemClock.uptimeMillis()-start) + "ms");
3052 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003053 }
3054
3055 /**
3056 * Callback saying that there aren't any more items to bind.
3057 *
3058 * Implementation of the method from LauncherModel.Callbacks.
3059 */
3060 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003061 setLoadOnResume();
3062
Joe Onorato9c1289c2009-08-17 11:03:03 -04003063 if (mSavedState != null) {
3064 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003065 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003066 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003067 mSavedState = null;
3068 }
3069
3070 if (mSavedInstanceState != null) {
3071 super.onRestoreInstanceState(mSavedInstanceState);
3072 mSavedInstanceState = null;
3073 }
3074
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003075 // If we received the result of any pending adds while the loader was running (e.g. the
3076 // widget configuration forced an orientation change), process them now.
3077 for (int i = 0; i < sPendingAddList.size(); i++) {
3078 completeAdd(sPendingAddList.get(i));
3079 }
3080 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003081
Winson Chungc51db6a2011-10-05 11:44:49 -07003082 // Update the market app icon as necessary (the other icons will be managed in response to
3083 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003084 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003085
Winson Chungf0c6ae02012-03-21 16:10:31 -07003086 // Animate up any icons as necessary
3087 if (mVisible || mWorkspaceLoading) {
3088 Runnable newAppsRunnable = new Runnable() {
3089 @Override
3090 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003091 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003092 }
3093 };
Winson Chunga2413752012-04-03 14:22:34 -07003094
3095 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3096 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3097 if (canRunNewAppsAnimation()) {
3098 // If the user has not interacted recently, then either snap to the new page to show
3099 // the new-apps animation or just run them if they are to appear on the current page
3100 if (willSnapPage) {
3101 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3102 } else {
3103 runNewAppsAnimation(false);
3104 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003105 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003106 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003107 // are on another page (or just normally if they are added to the current page)
3108 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003109 }
3110 }
3111
3112 mWorkspaceLoading = false;
3113 }
3114
Winson Chunga2413752012-04-03 14:22:34 -07003115 private boolean canRunNewAppsAnimation() {
3116 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3117 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3118 }
3119
Winson Chungf0c6ae02012-03-21 16:10:31 -07003120 /**
3121 * Runs a new animation that scales up icons that were added while Launcher was in the
3122 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003123 *
3124 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003125 */
Winson Chunga2413752012-04-03 14:22:34 -07003126 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003127 AnimatorSet anim = new AnimatorSet();
3128 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003129
3130 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003131 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3132 @Override
3133 public int compare(View a, View b) {
3134 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3135 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3136 int cellCountX = LauncherModel.getCellCountX();
3137 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3138 }
3139 });
Winson Chunga2413752012-04-03 14:22:34 -07003140
3141 // Animate each of the views in place (or show them immediately if requested)
3142 if (immediate) {
3143 for (View v : mNewShortcutAnimateViews) {
3144 v.setAlpha(1f);
3145 v.setScaleX(1f);
3146 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003147 }
Winson Chunga2413752012-04-03 14:22:34 -07003148 } else {
3149 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3150 View v = mNewShortcutAnimateViews.get(i);
3151 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3152 PropertyValuesHolder.ofFloat("alpha", 1f),
3153 PropertyValuesHolder.ofFloat("scaleX", 1f),
3154 PropertyValuesHolder.ofFloat("scaleY", 1f));
3155 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3156 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3157 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3158 bounceAnims.add(bounceAnim);
3159 }
3160 anim.playTogether(bounceAnims);
3161 anim.addListener(new AnimatorListenerAdapter() {
3162 @Override
3163 public void onAnimationEnd(Animator animation) {
3164 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3165 }
3166 });
3167 anim.start();
3168 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003169
3170 // Clean up
3171 mNewShortcutAnimatePage = -1;
3172 mNewShortcutAnimateViews.clear();
3173 new Thread("clearNewAppsThread") {
3174 public void run() {
3175 mSharedPrefs.edit()
3176 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3177 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3178 .commit();
3179 }
3180 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003181 }
3182
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003183 @Override
3184 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003185 boolean searchVisible = updateGlobalSearchIcon();
3186 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3187 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003188 }
3189
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003190 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003191 * Add the icons for all apps.
3192 *
3193 * Implementation of the method from LauncherModel.Callbacks.
3194 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003195 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3196 // Remove the progress bar entirely; we could also make it GONE
3197 // but better to remove it since we know it's not going to be used
3198 View progressBar = mAppsCustomizeTabHost.
3199 findViewById(R.id.apps_customize_progress_bar);
3200 if (progressBar != null) {
3201 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003202 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003203 // We just post the call to setApps so the user sees the progress bar
3204 // disappear-- otherwise, it just looks like the progress bar froze
3205 // which doesn't look great
3206 mAppsCustomizeTabHost.post(new Runnable() {
3207 public void run() {
3208 if (mAppsCustomizeContent != null) {
3209 mAppsCustomizeContent.setApps(apps);
3210 }
3211 }
3212 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003213 }
3214
3215 /**
3216 * A package was installed.
3217 *
3218 * Implementation of the method from LauncherModel.Callbacks.
3219 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003220 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003221 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003222
Winson Chung785d2eb2011-04-14 16:08:02 -07003223 if (mAppsCustomizeContent != null) {
3224 mAppsCustomizeContent.addApps(apps);
3225 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003226 }
3227
3228 /**
3229 * A package was updated.
3230 *
3231 * Implementation of the method from LauncherModel.Callbacks.
3232 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003233 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003234 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003235 if (mWorkspace != null) {
3236 mWorkspace.updateShortcuts(apps);
3237 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003238
Winson Chung785d2eb2011-04-14 16:08:02 -07003239 if (mAppsCustomizeContent != null) {
3240 mAppsCustomizeContent.updateApps(apps);
3241 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003242 }
3243
3244 /**
3245 * A package was uninstalled.
3246 *
3247 * Implementation of the method from LauncherModel.Callbacks.
3248 */
Joe Onorato36115782010-06-17 13:28:48 -04003249 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003250 if (permanent) {
3251 mWorkspace.removeItems(apps);
3252 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003253
Winson Chung785d2eb2011-04-14 16:08:02 -07003254 if (mAppsCustomizeContent != null) {
3255 mAppsCustomizeContent.removeApps(apps);
3256 }
Winson Chunga1820962011-10-03 16:31:06 -07003257
3258 // Notify the drag controller
3259 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003260 }
Joe Onoratobe386092009-11-17 17:32:16 -08003261
3262 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003263 * A number of packages were updated.
3264 */
3265 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003266 if (mAppsCustomizeContent != null) {
3267 mAppsCustomizeContent.onPackagesUpdated();
3268 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003269 }
3270
Winson Chung400438b2011-01-16 17:53:48 -08003271 private int mapConfigurationOriActivityInfoOri(int configOri) {
3272 final Display d = getWindowManager().getDefaultDisplay();
3273 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3274 switch (d.getRotation()) {
3275 case Surface.ROTATION_0:
3276 case Surface.ROTATION_180:
3277 // We are currently in the same basic orientation as the natural orientation
3278 naturalOri = configOri;
3279 break;
3280 case Surface.ROTATION_90:
3281 case Surface.ROTATION_270:
3282 // We are currently in the other basic orientation to the natural orientation
3283 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3284 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3285 break;
3286 }
3287
3288 int[] oriMap = {
3289 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3290 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3291 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3292 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3293 };
3294 // Since the map starts at portrait, we need to offset if this device's natural orientation
3295 // is landscape.
3296 int indexOffset = 0;
3297 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3298 indexOffset = 1;
3299 }
3300 return oriMap[(d.getRotation() + indexOffset) % 4];
3301 }
Adam Cohen446e9402011-09-15 18:21:21 -07003302
Winson Chung641d71d2012-04-26 15:58:01 -07003303 public void lockScreenOrientation() {
3304 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3305 .getConfiguration().orientation));
Winson Chung400438b2011-01-16 17:53:48 -08003306 }
Winson Chung641d71d2012-04-26 15:58:01 -07003307 public void unlockScreenOrientation() {
3308 mHandler.postDelayed(new Runnable() {
3309 public void run() {
3310 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3311 }
3312 }, mRestoreScreenOrientationDelay);
Winson Chung400438b2011-01-16 17:53:48 -08003313 }
3314
Winson Chung82f55532011-08-09 14:14:23 -07003315 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003316 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003317 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003318 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003319
Winson Chung7d7541e2011-09-16 20:14:36 -07003320 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003321 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003322 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3323 Cling cling = (Cling) findViewById(clingId);
3324 if (cling != null) {
3325 cling.init(this, positionData);
3326 cling.setVisibility(View.VISIBLE);
3327 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3328 if (animate) {
3329 cling.buildLayer();
3330 cling.setAlpha(0f);
3331 cling.animate()
3332 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003333 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003334 .setDuration(SHOW_CLING_DURATION)
3335 .setStartDelay(delay)
3336 .start();
3337 } else {
3338 cling.setAlpha(1f);
3339 }
3340 }
3341 return cling;
3342 }
3343 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003344 if (cling != null) {
3345 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003346 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003347 anim.addListener(new AnimatorListenerAdapter() {
3348 public void onAnimationEnd(Animator animation) {
3349 cling.setVisibility(View.GONE);
3350 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003351 // We should update the shared preferences on a background thread
3352 new Thread("dismissClingThread") {
3353 public void run() {
3354 SharedPreferences.Editor editor = mSharedPrefs.edit();
3355 editor.putBoolean(flag, true);
3356 editor.commit();
3357 }
3358 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003359 };
3360 });
3361 anim.start();
3362 }
3363 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003364 private void removeCling(int id) {
3365 final View cling = findViewById(id);
3366 if (cling != null) {
3367 final ViewGroup parent = (ViewGroup) cling.getParent();
3368 parent.post(new Runnable() {
3369 @Override
3370 public void run() {
3371 parent.removeView(cling);
3372 }
3373 });
3374 }
3375 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003376 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003377 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003378 if (isClingsEnabled() &&
3379 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003380 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003381 } else {
3382 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003383 }
3384 }
3385 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003386 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003387 if (isClingsEnabled() &&
3388 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003389 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003390 } else {
3391 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003392 }
3393 }
3394 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003395 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003396 if (isClingsEnabled() &&
3397 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3398 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003399 } else {
3400 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003401 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003402 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003403 }
3404 public boolean isFolderClingVisible() {
3405 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003406 if (cling != null) {
3407 return cling.getVisibility() == View.VISIBLE;
3408 }
3409 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003410 }
Winson Chung82f55532011-08-09 14:14:23 -07003411 public void dismissWorkspaceCling(View v) {
3412 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003413 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003414 }
3415 public void dismissAllAppsCling(View v) {
3416 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003417 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3418 }
3419 public void dismissFolderCling(View v) {
3420 Cling cling = (Cling) findViewById(R.id.folder_cling);
3421 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003422 }
3423
Winson Chung80baf5a2010-08-09 16:03:15 -07003424 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003425 * Prints out out state for debugging.
3426 */
3427 public void dumpState() {
3428 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003429 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003430 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3431 Log.d(TAG, "mRestoring=" + mRestoring);
3432 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3433 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003434 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003435 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003436
Winson Chung785d2eb2011-04-14 16:08:02 -07003437 if (mAppsCustomizeContent != null) {
3438 mAppsCustomizeContent.dumpState();
3439 }
Joe Onoratobe386092009-11-17 17:32:16 -08003440 Log.d(TAG, "END launcher2 dump state");
3441 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003442
3443 @Override
3444 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3445 super.dump(prefix, fd, writer, args);
3446 writer.println(" ");
3447 writer.println("Debug logs: ");
3448 for (int i = 0; i < sDumpLogs.size(); i++) {
3449 writer.println(" " + sDumpLogs.get(i));
3450 }
3451 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003452}
Michael Jurka2763be32011-02-24 11:19:57 -08003453
Michael Jurkaabded662011-03-04 12:06:57 -08003454interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003455 View getContent();
Michael Jurkabed61d22012-02-14 22:51:29 -08003456 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003457 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003458 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003459}