blob: 84fb9d06693b71a3b758bd98f1a315f05e2c0983 [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;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070041import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070042import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070045import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070047import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080048import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070049import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070050import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040051import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070052import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020054import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080055import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070056import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070057import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040058import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080059import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070060import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080061import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.text.Selection;
63import android.text.SpannableStringBuilder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070065import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080066import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080067import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.view.KeyEvent;
69import android.view.LayoutInflater;
70import android.view.Menu;
71import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070072import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080073import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080075import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070076import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080077import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080078import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070079import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080080import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070081import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070082import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080083import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070085import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070086import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070087import android.widget.TextView;
88import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070089
Adam Cohendf2cc412011-04-27 16:56:57 -070090import com.android.common.Search;
91import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070092import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080093
Adam Cohenc0dcf592011-06-01 15:30:43 -070094import java.io.DataInputStream;
95import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070096import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070097import java.io.FileNotFoundException;
98import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070099import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700101import java.util.Collection;
102import java.util.Collections;
103import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700104import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700105import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700106import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109/**
110 * Default launcher application.
111 */
Michael Jurka946ad472010-07-09 18:05:18 -0700112public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700113 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
114 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800115 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700116 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
Joe Onorato9c1289c2009-08-17 11:03:03 -0400118 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400119 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700120 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700121
Winson Chung70d72102011-08-12 11:24:35 -0700122 private static final int MENU_GROUP_WALLPAPER = 1;
123 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
124 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700125 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
126 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
128 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700129 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 private static final int REQUEST_PICK_APPLICATION = 6;
131 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700132 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700133 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Michael Jurka8b805b12012-04-18 14:23:14 -0700135 private static final int REQUEST_BIND_APPWIDGET = 11;
136
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
138
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800139 static final int SCREEN_COUNT = 5;
140 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Romain Guy98d01652009-06-30 16:21:04 -0700142 private static final String PREFERENCES = "launcher.preferences";
Adam Cohen23a4d302011-12-01 17:26:44 -0800143 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Winson Chung2672ff92012-05-04 16:22:30 -0700145 // The Intent extra that defines whether to ignore the launch animation
146 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
147 "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
148
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 // Type: int
150 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700151 // Type: int
152 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700154 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
155 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
157 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700158 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 // Type: boolean
162 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
163 // Type: long
164 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
169 // Type: parcelable
170 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700172 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700173 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
174 "com.android.launcher.toolbar_search_icon";
175 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
176 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700177
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700178 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700179 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700180 private State mState = State.WORKSPACE;
181 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800182 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700183
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700185 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
186 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700187 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700188 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800191 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Winson Chunga2413752012-04-03 14:22:34 -0700193 // How long to wait before the new-shortcut animation automatically pans the workspace
194 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
195
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800196 private final BroadcastReceiver mCloseSystemDialogsReceiver
197 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800198 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private LayoutInflater mInflater;
201
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800203 private View mQsbDivider;
204 private View mDockDivider;
205 private DragLayer mDragLayer;
206 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700207
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700208 private AppWidgetManager mAppWidgetManager;
209 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700210
Michael Jurkac9d95c52011-08-29 14:03:34 -0700211 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700212 private AppWidgetProviderInfo mPendingAddWidgetInfo;
213
Michael Jurka0280c3b2010-09-17 15:00:07 -0700214 private int[] mTmpAddItemCellCoordinates = new int[2];
215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private FolderInfo mFolderInfo;
217
Winson Chung3d503fb2011-07-13 17:25:49 -0700218 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800219 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700220
Winson Chungc51db6a2011-10-05 11:44:49 -0700221 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700222 private AppsCustomizeTabHost mAppsCustomizeTabHost;
223 private AppsCustomizePagedView mAppsCustomizeContent;
224 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 private Bundle mSavedState;
227
228 private SpannableStringBuilder mDefaultKeySsb = null;
229
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 private boolean mWorkspaceLoading = true;
231
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800232 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 private boolean mRestoring;
234 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700235 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236
237 private Bundle mSavedInstanceState;
238
Joe Onorato9c1289c2009-08-17 11:03:03 -0400239 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800240 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800241 private boolean mUserPresent = true;
242 private boolean mVisible = false;
243 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400244
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700245 private static LocaleConfiguration sLocaleConfiguration = null;
246
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700247 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700248
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700249 private Intent mAppMarketIntent = null;
250
Adam Cohended9f8d2010-11-03 13:25:16 -0700251 // Related to the auto-advancing of widgets
252 private final int ADVANCE_MSG = 1;
253 private final int mAdvanceInterval = 20000;
254 private final int mAdvanceStagger = 250;
255 private long mAutoAdvanceSentTime;
256 private long mAutoAdvanceTimeLeft = -1;
257 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
258 new HashMap<View, AppWidgetProviderInfo>();
259
Winson Chung400438b2011-01-16 17:53:48 -0800260 // Determines how long to wait after a rotation before restoring the screen orientation to
261 // match the sensor state.
262 private final int mRestoreScreenOrientationDelay = 500;
263
Michael Jurka4ef207b2010-11-29 17:05:45 -0800264 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700265 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
266 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
267 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800268
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700269 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
270
Winson Chung46353de2012-02-16 14:05:10 -0800271 // We only want to get the SharedPreferences once since it does an FS stat each time we get
272 // it from the context.
273 private SharedPreferences mSharedPrefs;
274
Winson Chungf0c6ae02012-03-21 16:10:31 -0700275 // Holds the page that we need to animate to, and the icon views that we need to animate up
276 // when we scroll to that page on resume.
277 private int mNewShortcutAnimatePage = -1;
278 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700279
Michael Jurkaddd62e92011-02-16 17:49:14 -0800280 private BubbleTextView mWaitingForResume;
281
Michael Jurkac1f5d262011-09-30 19:32:27 -0700282 private Runnable mBuildLayersRunnable = new Runnable() {
283 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700284 if (mWorkspace != null) {
285 mWorkspace.buildPageHardwareLayers();
286 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700287 }
288 };
289
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800290 private static ArrayList<PendingAddArguments> sPendingAddList
291 = new ArrayList<PendingAddArguments>();
292
293 private static class PendingAddArguments {
294 int requestCode;
295 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700296 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800297 int screen;
298 int cellX;
299 int cellY;
300 }
301
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302 @Override
303 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700304 if (DEBUG_STRICT_MODE) {
305 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
306 .detectDiskReads()
307 .detectDiskWrites()
308 .detectNetwork() // or .detectAll() for all detectable problems
309 .penaltyLog()
310 .build());
311 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
312 .detectLeakedSqlLiteObjects()
313 .detectLeakedClosableObjects()
314 .penaltyLog()
315 .penaltyDeath()
316 .build());
317 }
318
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800320 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700321 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
322 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800323 mModel = app.setLauncher(this);
324 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400325 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700327
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700328 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700329 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
330 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700331
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200333 android.os.Debug.startMethodTracing(
334 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 }
336
337 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 setContentView(R.layout.launcher);
339 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700340 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800342 registerContentObservers();
343
Joe Onorato7c312c12009-08-13 21:36:53 -0700344 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700345
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 mSavedState = savedInstanceState;
347 restoreState(mSavedState);
348
Winson Chunga12a2502010-12-20 14:41:35 -0800349 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700350 if (mAppsCustomizeContent != null) {
351 mAppsCustomizeContent.onPackagesUpdated();
352 }
Winson Chunga12a2502010-12-20 14:41:35 -0800353
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 if (PROFILE_STARTUP) {
355 android.os.Debug.stopMethodTracing();
356 }
357
358 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700359 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 }
361
Michael Jurkac57b7a82011-08-09 22:02:20 -0700362 if (!mModel.isAllAppsLoaded()) {
363 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
364 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
365 }
366
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 // For handling default keys
368 mDefaultKeySsb = new SpannableStringBuilder();
369 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800370
371 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
372 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800373
Winson Chungc51db6a2011-10-05 11:44:49 -0700374 boolean searchVisible = false;
375 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800376 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700377 int coi = getCurrentOrientationIndexForGlobalIcons();
378 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
379 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700380 updateAppMarketIcon();
381 searchVisible = updateGlobalSearchIcon();
382 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700383 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700384 if (sGlobalSearchIcon[coi] != null) {
385 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700386 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700387 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700388 if (sVoiceSearchIcon[coi] != null) {
389 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700390 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700391 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700392 if (sAppMarketIcon[coi] != null) {
393 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800394 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700395 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700396
397 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Winson Chung4b919f82012-05-01 10:44:08 -0700398 unlockScreenOrientation(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 }
Romain Guycbb89e42009-06-08 15:52:54 -0700400
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700402 if (sLocaleConfiguration == null) {
403 new AsyncTask<Void, Void, LocaleConfiguration>() {
404 @Override
405 protected LocaleConfiguration doInBackground(Void... unused) {
406 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
407 readConfiguration(Launcher.this, localeConfiguration);
408 return localeConfiguration;
409 }
410
411 @Override
412 protected void onPostExecute(LocaleConfiguration result) {
413 sLocaleConfiguration = result;
414 checkForLocaleChange(); // recursive, but now with a locale configuration
415 }
416 }.execute();
417 return;
418 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700419
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 final Configuration configuration = getResources().getConfiguration();
421
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700422 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 final String locale = configuration.locale.toString();
424
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700425 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 final int mcc = configuration.mcc;
427
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700428 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 final int mnc = configuration.mnc;
430
Romain Guy84f296c2009-11-04 15:00:44 -0800431 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432
Romain Guy84f296c2009-11-04 15:00:44 -0800433 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700434 sLocaleConfiguration.locale = locale;
435 sLocaleConfiguration.mcc = mcc;
436 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700437
Joe Onorato0589f0f2010-02-08 13:44:00 -0800438 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700439
440 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
441 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700442 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700443 public void run() {
444 writeConfiguration(Launcher.this, localeConfiguration);
445 }
446 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700447 }
448 }
449
450 private static class LocaleConfiguration {
451 public String locale;
452 public int mcc = -1;
453 public int mnc = -1;
454 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700455
Romain Guy98d01652009-06-30 16:21:04 -0700456 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
457 DataInputStream in = null;
458 try {
459 in = new DataInputStream(context.openFileInput(PREFERENCES));
460 configuration.locale = in.readUTF();
461 configuration.mcc = in.readInt();
462 configuration.mnc = in.readInt();
463 } catch (FileNotFoundException e) {
464 // Ignore
465 } catch (IOException e) {
466 // Ignore
467 } finally {
468 if (in != null) {
469 try {
470 in.close();
471 } catch (IOException e) {
472 // Ignore
473 }
474 }
475 }
476 }
477
478 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
479 DataOutputStream out = null;
480 try {
481 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
482 out.writeUTF(configuration.locale);
483 out.writeInt(configuration.mcc);
484 out.writeInt(configuration.mnc);
485 out.flush();
486 } catch (FileNotFoundException e) {
487 // Ignore
488 } catch (IOException e) {
489 //noinspection ResultOfMethodCallIgnored
490 context.getFileStreamPath(PREFERENCES).delete();
491 } finally {
492 if (out != null) {
493 try {
494 out.close();
495 } catch (IOException e) {
496 // Ignore
497 }
498 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 }
500 }
501
Adam Cohen716b51e2011-06-30 12:09:54 -0700502 public DragLayer getDragLayer() {
503 return mDragLayer;
504 }
505
Winson Chung36a62fe2012-05-06 18:04:42 -0700506 boolean isDraggingEnabled() {
507 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
508 // that is subsequently removed from the workspace in startBinding().
509 return !mModel.isLoadingWorkspace();
510 }
511
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512 static int getScreen() {
513 synchronized (sLock) {
514 return sScreen;
515 }
516 }
517
518 static void setScreen(int screen) {
519 synchronized (sLock) {
520 sScreen = screen;
521 }
522 }
523
Winson Chung557d6ed2011-07-08 15:34:52 -0700524 /**
525 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
526 * a configuration step, this allows the proper animations to run after other transitions.
527 */
528 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700529 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800530 switch (args.requestCode) {
531 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700532 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
533 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800534 break;
535 case REQUEST_PICK_SHORTCUT:
536 processShortcut(args.intent);
537 break;
538 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700539 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
540 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700541 result = true;
542 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800543 case REQUEST_CREATE_APPWIDGET:
544 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800545 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700546 result = true;
547 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800548 case REQUEST_PICK_WALLPAPER:
549 // We just wanted the activity result here so we can clear mWaitingForResult
550 break;
551 }
Michael Jurka27614d22012-04-02 06:40:22 -0700552 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
553 // if you turned the screen off and then back while in All Apps, Launcher would not
554 // return to the workspace. Clearing mAddInfo.container here fixes this issue
555 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700556 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800557 }
558
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700560 protected void onActivityResult(
561 final int requestCode, final int resultCode, final Intent data) {
562 if (requestCode == REQUEST_BIND_APPWIDGET) {
563 int appWidgetId = data != null ?
564 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
565 if (resultCode == RESULT_CANCELED) {
566 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
567 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700568 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700569 }
570 return;
571 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700572 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800573 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
574 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700575 mWaitingForResult = false;
576
Adam Cohened66b2b2012-01-23 17:28:51 -0800577 // We have special handling for widgets
578 if (isWidgetDrop) {
579 int appWidgetId = data != null ?
580 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700581 if (appWidgetId < 0) {
582 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
583 "widget configuration activity.");
584 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
585 } else {
586 completeTwoStageWidgetDrop(resultCode, appWidgetId);
587 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800588 return;
589 }
590
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800591 // The pattern used here is that a user PICKs a specific application,
592 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700593
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
595 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700596 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800597 final PendingAddArguments args = new PendingAddArguments();
598 args.requestCode = requestCode;
599 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700600 args.container = mPendingAddInfo.container;
601 args.screen = mPendingAddInfo.screen;
602 args.cellX = mPendingAddInfo.cellX;
603 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800604 if (isWorkspaceLocked()) {
605 sPendingAddList.add(args);
606 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700607 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800610 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700611 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800612 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
613 null);
614 }
615
616 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700617 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700618 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800619 Runnable onCompleteRunnable = null;
620 int animationType = 0;
621
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700622 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800623 if (resultCode == RESULT_OK) {
624 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
625 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700626 mPendingAddWidgetInfo);
627 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800628 onCompleteRunnable = new Runnable() {
629 @Override
630 public void run() {
631 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
632 mPendingAddInfo.screen, layout, null);
633 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
634 null);
635 }
636 };
637 } else if (resultCode == RESULT_CANCELED) {
638 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
639 onCompleteRunnable = new Runnable() {
640 @Override
641 public void run() {
642 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
643 null);
644 }
645 };
646 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700647 if (mDragLayer.getAnimatedView() != null) {
648 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
649 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
650 animationType, boundWidget, true);
651 } else {
652 // The animated view may be null in the case of a rotation during widget configuration
653 onCompleteRunnable.run();
654 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 }
656
657 @Override
658 protected void onResume() {
659 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700660
Winson Chungde0fb8f2012-05-08 14:37:08 -0700661 // Process any items that were added while Launcher was away
662 InstallShortcutReceiver.flushInstallQueue(this);
663
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800664 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700665 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400666 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700667 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400668 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700669 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
Winson Chunge4e50662012-01-23 14:45:13 -0800671
672 // Reset the pressed state of icons that were locked in the press state while activities
673 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800674 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800675 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800676 mWaitingForResume.setStayPressed(false);
677 }
Winson Chunge4e50662012-01-23 14:45:13 -0800678 if (mAppsCustomizeContent != null) {
679 // Resets the previous all apps icon press state
680 mAppsCustomizeContent.resetDrawableState();
681 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 }
683
684 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700685 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700686 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
687 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
688 // when Launcher resumes and we are still in AllApps.
689 updateWallpaperVisibility(true);
690
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700691 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700692 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800693 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700694 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700695 }
Romain Guycbb89e42009-06-08 15:52:54 -0700696
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700697 @Override
698 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400699 // Flag the loader to stop early before switching
700 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700701 if (mAppsCustomizeContent != null) {
702 mAppsCustomizeContent.surrender();
703 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800704 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700705 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800707 // We can't hide the IME if it was forced open. So don't bother
708 /*
709 @Override
710 public void onWindowFocusChanged(boolean hasFocus) {
711 super.onWindowFocusChanged(hasFocus);
712
713 if (hasFocus) {
714 final InputMethodManager inputManager = (InputMethodManager)
715 getSystemService(Context.INPUT_METHOD_SERVICE);
716 WindowManager.LayoutParams lp = getWindow().getAttributes();
717 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
718 android.os.Handler()) {
719 protected void onReceiveResult(int resultCode, Bundle resultData) {
720 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
721 }
722 });
723 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
724 }
725 }
726 */
727
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 private boolean acceptFilter() {
729 final InputMethodManager inputManager = (InputMethodManager)
730 getSystemService(Context.INPUT_METHOD_SERVICE);
731 return !inputManager.isFullscreenMode();
732 }
733
734 @Override
735 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700736 final int uniChar = event.getUnicodeChar();
737 final boolean handled = super.onKeyDown(keyCode, event);
738 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
739 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
741 keyCode, event);
742 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700743 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700744 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700745 // showSearchDialog()
746 // If there are multiple keystrokes before the search dialog takes focus,
747 // onSearchRequested() will be called for every keystroke,
748 // but it is idempotent, so it's fine.
749 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 }
751 }
752
Joe Onorato8a9625e2010-01-28 15:55:35 -0800753 // Eat the long press event so the keyboard doesn't come up.
754 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
755 return true;
756 }
757
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 return handled;
759 }
760
Karl Rosaen138a0412009-04-23 19:00:21 -0700761 private String getTypedText() {
762 return mDefaultKeySsb.toString();
763 }
764
765 private void clearTypedText() {
766 mDefaultKeySsb.clear();
767 mDefaultKeySsb.clearSpans();
768 Selection.setSelection(mDefaultKeySsb, 0);
769 }
770
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700772 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
773 * State
774 */
775 private static State intToState(int stateOrdinal) {
776 State state = State.WORKSPACE;
777 final State[] stateValues = State.values();
778 for (int i = 0; i < stateValues.length; i++) {
779 if (stateValues[i].ordinal() == stateOrdinal) {
780 state = stateValues[i];
781 break;
782 }
783 }
784 return state;
785 }
786
787 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 * Restores the previous state, if it exists.
789 *
790 * @param savedState The previous state.
791 */
792 private void restoreState(Bundle savedState) {
793 if (savedState == null) {
794 return;
795 }
796
Michael Jurka883f55b2010-10-21 15:47:14 -0700797 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700798 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800799 showAllApps(false);
800 }
801
Winson Chungf0c6ae02012-03-21 16:10:31 -0700802 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700804 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 }
806
Winson Chung3d503fb2011-07-13 17:25:49 -0700807 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
808 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700809
Winson Chung3d503fb2011-07-13 17:25:49 -0700810 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
811 mPendingAddInfo.container = pendingAddContainer;
812 mPendingAddInfo.screen = pendingAddScreen;
813 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
814 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700815 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
816 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
817 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 mRestoring = true;
819 }
820
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700821
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
823 if (renameFolder) {
824 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700825 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 mRestoring = true;
827 }
Winson Chunga12a2502010-12-20 14:41:35 -0800828
Winson Chung785d2eb2011-04-14 16:08:02 -0700829
830 // Restore the AppsCustomize tab
831 if (mAppsCustomizeTabHost != null) {
832 String curTab = savedState.getString("apps_customize_currentTab");
833 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700834 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700835 mAppsCustomizeContent.setContentType(
836 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
837 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700838 mAppsCustomizeContent.loadAssociatedPages(
839 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700840 }
841
Winson Chung5afbf7b2011-07-25 11:53:08 -0700842 int currentIndex = savedState.getInt("apps_customize_currentIndex");
843 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 }
846
847 /**
848 * Finds all the views we need and configure them properly.
849 */
850 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700851 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400852
Winson Chung4c98d922011-05-31 16:50:48 -0700853 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
854 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800855 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
856 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857
Winson Chung4c98d922011-05-31 16:50:48 -0700858 // Setup the drag layer
859 mDragLayer.setup(this, dragController);
860
Winson Chung3d503fb2011-07-13 17:25:49 -0700861 // Setup the hotseat
862 mHotseat = (Hotseat) findViewById(R.id.hotseat);
863 if (mHotseat != null) {
864 mHotseat.setup(this);
865 }
866
Winson Chung4c98d922011-05-31 16:50:48 -0700867 // Setup the workspace
868 mWorkspace.setHapticFeedbackEnabled(false);
869 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700870 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700871 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700872
Winson Chungf0ea4d32011-06-06 14:27:16 -0700873 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700874 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700875
Winson Chungf0ea4d32011-06-06 14:27:16 -0700876 // Setup AppsCustomize
877 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
878 findViewById(R.id.apps_customize_pane);
879 mAppsCustomizeContent = (AppsCustomizePagedView)
880 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700881 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700882 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400883
Michael Jurka5130e402011-10-13 04:55:35 -0700884 // Get the all apps button
885 mAllAppsButton = findViewById(R.id.all_apps_button);
886 if (mAllAppsButton != null) {
887 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
888 @Override
889 public boolean onTouch(View v, MotionEvent event) {
890 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
891 onTouchDownAllAppsButton(v);
892 }
893 return false;
894 }
895 });
896 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700897 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700898 dragController.setDragScoller(mWorkspace);
899 dragController.setScrollView(mDragLayer);
900 dragController.setMoveTarget(mWorkspace);
901 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700902 if (mSearchDropTargetBar != null) {
903 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 }
906
907 /**
908 * Creates a view representing a shortcut.
909 *
910 * @param info The data structure describing the shortcut.
911 *
912 * @return A View inflated from R.layout.application.
913 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800914 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700916 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 }
918
919 /**
920 * Creates a view representing a shortcut inflated from the specified resource.
921 *
922 * @param layoutResId The id of the XML layout used to create the shortcut.
923 * @param parent The group the shortcut belongs to.
924 * @param info The data structure describing the shortcut.
925 *
926 * @return A View inflated from layoutResId.
927 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800928 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800929 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
930 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 return favorite;
933 }
934
935 /**
936 * Add an application shortcut to the workspace.
937 *
938 * @param data The intent describing the application.
939 * @param cellInfo The position on screen where to create the shortcut.
940 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700941 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700942 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700943 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700944
Adam Cohenfbba09b2011-07-18 21:43:05 -0700945 // First we check if we already know the exact location where we want to add this item.
946 if (cellX >= 0 && cellY >= 0) {
947 cellXY[0] = cellX;
948 cellXY[1] = cellY;
949 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700950 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700951 return;
952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800954 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800955
Romain Guy73b979d2009-06-09 12:57:21 -0700956 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800957 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800959 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700960 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700961 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800962 } else {
963 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 }
965 }
Romain Guycbb89e42009-06-08 15:52:54 -0700966
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 /**
968 * Add a shortcut to the workspace.
969 *
970 * @param data The intent describing the shortcut.
971 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700973 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
974 int cellY) {
975 int[] cellXY = mTmpAddItemCellCoordinates;
976 int[] touchXY = mPendingAddInfo.dropPos;
977 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700978
Michael Jurkad3ef3062010-11-23 16:23:58 -0800979 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700980
Adam Cohen558baaf2011-08-15 15:22:57 -0700981 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800982 if (info == null) {
983 return;
984 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700985 final View view = createShortcut(info);
986
Adam Cohenfbba09b2011-07-18 21:43:05 -0700987 // First we check if we already know the exact location where we want to add this item.
988 if (cellX >= 0 && cellY >= 0) {
989 cellXY[0] = cellX;
990 cellXY[1] = cellY;
991 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700992
993 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800994 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700995 true, null,null)) {
996 return;
997 }
998 DragObject dragObject = new DragObject();
999 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001000 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1001 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001002 return;
1003 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001004 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001005 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001006 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001007 foundCellSpan = (result != null);
1008 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001009 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001010 }
1011
1012 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001013 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001014 return;
1015 }
1016
Adam Cohen558baaf2011-08-15 15:22:57 -07001017 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018
1019 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001020 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1021 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
1023 }
1024
Adam Cohen2f093b62012-04-30 18:59:53 -07001025 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1026 int minHeight) {
1027 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001028 // We want to account for the extra amount of padding that we are adding to the widget
1029 // to ensure that it gets the full amount of space that it has requested
1030 int requiredWidth = minWidth + padding.left + padding.right;
1031 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001032 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001033 }
1034
Adam Cohen2f093b62012-04-30 18:59:53 -07001035 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1036 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001037 }
1038
Adam Cohen2f093b62012-04-30 18:59:53 -07001039 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1040 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001041 }
1042
Adam Cohen2f093b62012-04-30 18:59:53 -07001043 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1044 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001045 }
1046
Adam Cohen2f093b62012-04-30 18:59:53 -07001047 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1048 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001049 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001050 }
1051
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001053 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001055 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001056 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001058 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1059 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1060 if (appWidgetInfo == null) {
1061 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1062 }
Romain Guycbb89e42009-06-08 15:52:54 -07001063
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001064 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001065 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001066
Adam Cohen2f093b62012-04-30 18:59:53 -07001067 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1068 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001071 // We have saved the position to which the widget was dragged-- this really only matters
1072 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001073 int[] cellXY = mTmpAddItemCellCoordinates;
1074 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001075 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001076 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001077 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1078 cellXY[0] = mPendingAddInfo.cellX;
1079 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001080 spanXY[0] = mPendingAddInfo.spanX;
1081 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001082 foundCellSpan = true;
1083 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001084 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001085 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001086 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1087 spanXY[1], cellXY, finalSpan);
1088 spanXY[0] = finalSpan[0];
1089 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001090 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001091 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001092 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001093 }
1094
Michael Jurka0280c3b2010-09-17 15:00:07 -07001095 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001096 if (appWidgetId != -1) {
1097 // Deleting an app widget ID is a void call but writes to disk before returning
1098 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001099 new Thread("deleteAppWidgetId") {
1100 public void run() {
1101 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1102 }
1103 }.start();
1104 }
Winson Chung93eef082012-03-23 15:59:27 -07001105 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001106 return;
1107 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001109 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001110 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1111 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001112 launcherInfo.spanX = spanXY[0];
1113 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001114 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1115 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001116
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001118 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119
1120 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001121 if (hostView == null) {
1122 // Perform actual inflation because we're live
1123 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1124 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1125 } else {
1126 // The AppWidgetHostView has already been inflated and instantiated
1127 launcherInfo.hostView = hostView;
1128 }
Romain Guycbb89e42009-06-08 15:52:54 -07001129
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001131 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohena897f392012-04-27 18:12:05 -07001132 AppWidgetResizeFrame.updateWidgetSizeRanges(launcherInfo.hostView,
1133 this, launcherInfo.spanX, launcherInfo.spanY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001134 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001135 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001136
1137 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001139 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
Romain Guycbb89e42009-06-08 15:52:54 -07001141
Adam Cohended9f8d2010-11-03 13:25:16 -07001142 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1143 @Override
1144 public void onReceive(Context context, Intent intent) {
1145 final String action = intent.getAction();
1146 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1147 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001148 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001149 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001150
Winson Chungc100e8e2011-08-09 16:02:43 -07001151 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001152 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001153 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1154 mAppsCustomizeTabHost.reset();
1155 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001156 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001157 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1158 mUserPresent = true;
1159 updateRunning();
1160 }
1161 }
1162 };
1163
1164 @Override
1165 public void onAttachedToWindow() {
1166 super.onAttachedToWindow();
1167
1168 // Listen for broadcasts related to user-presence
1169 final IntentFilter filter = new IntentFilter();
1170 filter.addAction(Intent.ACTION_SCREEN_OFF);
1171 filter.addAction(Intent.ACTION_USER_PRESENT);
1172 registerReceiver(mReceiver, filter);
1173
Adam Cohend113e0c2010-11-11 10:48:05 -08001174 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001175 mVisible = true;
1176 }
1177
1178 @Override
1179 public void onDetachedFromWindow() {
1180 super.onDetachedFromWindow();
1181 mVisible = false;
1182
Adam Cohend113e0c2010-11-11 10:48:05 -08001183 if (mAttached) {
1184 unregisterReceiver(mReceiver);
1185 mAttached = false;
1186 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001187 updateRunning();
1188 }
1189
1190 public void onWindowVisibilityChanged(int visibility) {
1191 mVisible = visibility == View.VISIBLE;
1192 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001193 // The following code used to be in onResume, but it turns out onResume is called when
1194 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1195 // is a more appropriate event to handle
1196 if (mVisible) {
1197 mAppsCustomizeTabHost.onWindowVisible();
1198 if (!mWorkspaceLoading) {
1199 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001200 // We want to let Launcher draw itself at least once before we force it to build
1201 // layers on all the workspace pages, so that transitioning to Launcher from other
1202 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1203 // a true draw so we wait until the second preDraw call to be safe
1204 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001205 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001206 // We delay the layer building a bit in order to give
1207 // other message processing a time to run. In particular
1208 // this avoids a delay in hiding the IME if it was
1209 // currently shown, because doing that may involve
1210 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001211 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001212
1213 // We had to enable the wallpaper visibility when launching apps from all
1214 // apps (so that the transitions would be the same as when launching from
1215 // workspace) so take this time to see if we need to re-disable the
1216 // wallpaper visibility to ensure performance.
1217 mWorkspace.post(new Runnable() {
1218 @Override
1219 public void run() {
Winson Chung18f41f82012-05-09 13:28:10 -07001220 disableWallpaperIfInAllApps();
Winson Chung31ce0732012-05-06 13:36:43 -07001221 }
1222 });
1223
Michael Jurka6ee21d22012-02-21 18:20:27 -08001224 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001225 return true;
1226 }
1227 });
1228 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001229 // When Launcher comes back to foreground, a different Activity might be responsible for
1230 // the app market intent, so refresh the icon
1231 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001232 clearTypedText();
1233 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001234 }
1235
1236 private void sendAdvanceMessage(long delay) {
1237 mHandler.removeMessages(ADVANCE_MSG);
1238 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1239 mHandler.sendMessageDelayed(msg, delay);
1240 mAutoAdvanceSentTime = System.currentTimeMillis();
1241 }
1242
1243 private void updateRunning() {
1244 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1245 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1246 mAutoAdvanceRunning = autoAdvanceRunning;
1247 if (autoAdvanceRunning) {
1248 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1249 sendAdvanceMessage(delay);
1250 } else {
1251 if (!mWidgetsToAdvance.isEmpty()) {
1252 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1253 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1254 }
1255 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001256 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001257 }
1258 }
1259 }
1260
1261 private final Handler mHandler = new Handler() {
1262 @Override
1263 public void handleMessage(Message msg) {
1264 if (msg.what == ADVANCE_MSG) {
1265 int i = 0;
1266 for (View key: mWidgetsToAdvance.keySet()) {
1267 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1268 final int delay = mAdvanceStagger * i;
1269 if (v instanceof Advanceable) {
1270 postDelayed(new Runnable() {
1271 public void run() {
1272 ((Advanceable) v).advance();
1273 }
1274 }, delay);
1275 }
1276 i++;
1277 }
1278 sendAdvanceMessage(mAdvanceInterval);
1279 }
1280 }
1281 };
1282
1283 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001284 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001285 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1286 if (v instanceof Advanceable) {
1287 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001288 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001289 updateRunning();
1290 }
1291 }
1292
1293 void removeWidgetToAutoAdvance(View hostView) {
1294 if (mWidgetsToAdvance.containsKey(hostView)) {
1295 mWidgetsToAdvance.remove(hostView);
1296 updateRunning();
1297 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001298 }
1299
Joe Onorato9c1289c2009-08-17 11:03:03 -04001300 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001301 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001302 launcherInfo.hostView = null;
1303 }
1304
Winson Chung93eef082012-03-23 15:59:27 -07001305 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1306 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1307 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001308 }
1309
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001310 public LauncherAppWidgetHost getAppWidgetHost() {
1311 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 }
Romain Guycbb89e42009-06-08 15:52:54 -07001313
Winson Chunga9abd0e2010-10-27 17:18:37 -07001314 public LauncherModel getModel() {
1315 return mModel;
1316 }
1317
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001318 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001319 getWindow().closeAllPanels();
1320
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001321 // Whatever we were doing is hereby canceled.
1322 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001323 }
1324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 @Override
1326 protected void onNewIntent(Intent intent) {
1327 super.onNewIntent(intent);
1328
1329 // Close the menu
1330 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001331 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001332 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001333
Joe Onorato14f122b2009-11-19 14:06:36 -08001334 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1335 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001336
Adam Cohenac56cff2011-09-28 20:45:37 -07001337 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001338 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001339 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001340 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1341 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001342 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001343 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001344
1345 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001346 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001347 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001348
Joe Onorato3a8820b2009-11-10 15:06:42 -08001349 final View v = getWindow().peekDecorView();
1350 if (v != null && v.getWindowToken() != null) {
1351 InputMethodManager imm = (InputMethodManager)getSystemService(
1352 INPUT_METHOD_SERVICE);
1353 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001355
Michael Jurka35aa14d2011-07-07 17:01:08 -07001356 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001357 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001358 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001359 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 }
1361 }
1362
1363 @Override
1364 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1365 // Do not call super here
1366 mSavedInstanceState = savedInstanceState;
1367 }
1368
1369 @Override
1370 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001371 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001372 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373
Michael Jurka883f55b2010-10-21 15:47:14 -07001374 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001375 // We close any open folder since it will not be re-opened, and we need to make sure
1376 // this state is reflected.
1377 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378
Winson Chung3d503fb2011-07-13 17:25:49 -07001379 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1380 mWaitingForResult) {
1381 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1382 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1383 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1384 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001385 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1386 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1387 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 }
1389
1390 if (mFolderInfo != null && mWaitingForResult) {
1391 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1392 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1393 }
Michael Jurka946ad472010-07-09 18:05:18 -07001394
Winson Chung785d2eb2011-04-14 16:08:02 -07001395 // Save the current AppsCustomize tab
1396 if (mAppsCustomizeTabHost != null) {
1397 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1398 if (currentTabTag != null) {
1399 outState.putString("apps_customize_currentTab", currentTabTag);
1400 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001401 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1402 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001403 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 }
1405
1406 @Override
1407 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001409
Winson Chunge7a03942011-08-05 15:05:12 -07001410 // Remove all pending runnables
1411 mHandler.removeMessages(ADVANCE_MSG);
1412 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001413 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001414
Winson Chungcd2b0142011-06-08 16:02:26 -07001415 // Stop callbacks from LauncherModel
1416 LauncherApplication app = ((LauncherApplication) getApplication());
1417 mModel.stopLoader();
1418 app.setLauncher(null);
1419
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001421 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001423 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001425 mAppWidgetHost = null;
1426
1427 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428
1429 TextKeyListener.getInstance().release();
1430
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431
Winson Chung3d503fb2011-07-13 17:25:49 -07001432 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001433
1434 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001435 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001436
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001437 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001438 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1439 mWorkspace.removeAllViews();
1440 mWorkspace = null;
1441 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001442
1443 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 }
1445
Adam Cohena9cf38f2011-05-02 15:36:58 -07001446 public DragController getDragController() {
1447 return mDragController;
1448 }
1449
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 @Override
1451 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001452 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 super.startActivityForResult(intent, requestCode);
1454 }
1455
Winson Chung70d72102011-08-12 11:24:35 -07001456 /**
1457 * Indicates that we want global search for this activity by setting the globalSearch
1458 * argument for {@link #startSearch} to true.
1459 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001461 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001463
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001464 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001465
Karl Rosaen138a0412009-04-23 19:00:21 -07001466 if (initialQuery == null) {
1467 // Use any text typed in the launcher as the initial query
1468 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001469 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 if (appSearchData == null) {
1471 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001472 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001474 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001475
Karl Rosaen138a0412009-04-23 19:00:21 -07001476 final SearchManager searchManager =
1477 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001478 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001479 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
1481
Winson Chung70d72102011-08-12 11:24:35 -07001482 @Override
1483 public boolean onCreateOptionsMenu(Menu menu) {
1484 if (isWorkspaceLocked()) {
1485 return false;
1486 }
1487
1488 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001489
1490 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1491 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1492 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001493 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1494 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1495 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001496 String helpUrl = getString(R.string.help_url);
1497 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001498 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1499 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1500
Winson Chung70d72102011-08-12 11:24:35 -07001501 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1502 .setIcon(android.R.drawable.ic_menu_gallery)
1503 .setAlphabeticShortcut('W');
1504 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1505 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001506 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001507 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001508 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1509 .setIcon(android.R.drawable.ic_menu_preferences)
1510 .setIntent(settings)
1511 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001512 if (!helpUrl.isEmpty()) {
1513 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1514 .setIcon(android.R.drawable.ic_menu_help)
1515 .setIntent(help)
1516 .setAlphabeticShortcut('H');
1517 }
Winson Chung70d72102011-08-12 11:24:35 -07001518 return true;
1519 }
1520
1521 @Override
1522 public boolean onPrepareOptionsMenu(Menu menu) {
1523 super.onPrepareOptionsMenu(menu);
1524
1525 if (mAppsCustomizeTabHost.isTransitioning()) {
1526 return false;
1527 }
1528 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1529 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1530
1531 return true;
1532 }
1533
1534 @Override
1535 public boolean onOptionsItemSelected(MenuItem item) {
1536 switch (item.getItemId()) {
1537 case MENU_WALLPAPER_SETTINGS:
1538 startWallpaper();
1539 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001540 }
1541
1542 return super.onOptionsItemSelected(item);
1543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001545 @Override
1546 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001547 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001548 // Use a custom animation for launching search
1549 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001550 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001551 }
1552
Joe Onorato9c1289c2009-08-17 11:03:03 -04001553 public boolean isWorkspaceLocked() {
1554 return mWorkspaceLoading || mWaitingForResult;
1555 }
1556
Michael Jurka0280c3b2010-09-17 15:00:07 -07001557 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001558 mPendingAddInfo.container = ItemInfo.NO_ID;
1559 mPendingAddInfo.screen = -1;
1560 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1561 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001562 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001563 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001564 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001565
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001566 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1567 AppWidgetProviderInfo appWidgetInfo) {
1568 if (appWidgetInfo.configure != null) {
1569 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001570
Bjorn Bringert7984c942009-12-09 15:38:25 +00001571 // Launch over to configure widget, if needed
1572 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001573 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001574 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001575 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001577 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001578 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1579 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001580 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001581 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 }
1583 }
Romain Guycbb89e42009-06-08 15:52:54 -07001584
Adam Cohenfbba09b2011-07-18 21:43:05 -07001585 /**
1586 * Process a shortcut drop.
1587 *
1588 * @param componentName The name of the component
1589 * @param screen The screen where it should be added
1590 * @param cell The cell it should be added to, optional
1591 * @param position The location on the screen where it was dropped, optional
1592 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001593 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1594 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001595 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001596 mPendingAddInfo.container = container;
1597 mPendingAddInfo.screen = screen;
1598 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001599
1600 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001601 mPendingAddInfo.cellX = cell[0];
1602 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001603 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001604
1605 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1606 createShortcutIntent.setComponent(componentName);
1607 processShortcut(createShortcutIntent);
1608 }
1609
Adam Cohenfbba09b2011-07-18 21:43:05 -07001610 /**
1611 * Process a widget drop.
1612 *
1613 * @param info The PendingAppWidgetInfo of the widget being added.
1614 * @param screen The screen where it should be added
1615 * @param cell The cell it should be added to, optional
1616 * @param position The location on the screen where it was dropped, optional
1617 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001618 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001619 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001620 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001621 mPendingAddInfo.container = info.container = container;
1622 mPendingAddInfo.screen = info.screen = screen;
1623 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001624 mPendingAddInfo.minSpanX = info.minSpanX;
1625 mPendingAddInfo.minSpanY = info.minSpanY;
1626
Adam Cohenfbba09b2011-07-18 21:43:05 -07001627 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001628 mPendingAddInfo.cellX = cell[0];
1629 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001630 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001631 if (span != null) {
1632 mPendingAddInfo.spanX = span[0];
1633 mPendingAddInfo.spanY = span[1];
1634 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001635
Adam Cohened66b2b2012-01-23 17:28:51 -08001636 AppWidgetHostView hostView = info.boundWidget;
1637 int appWidgetId;
1638 if (hostView != null) {
1639 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001640 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001641 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001642 // In this case, we either need to start an activity to get permission to bind
1643 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001644 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001645 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001646 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001647 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001648 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001649 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1650 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1651 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1652 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1653 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001654 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001655 }
1656
Joe Onoratodeb98af2010-02-19 14:59:39 -08001657 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001658 // Handle case where user selected "Applications"
1659 String applicationName = getResources().getString(R.string.group_applications);
1660 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001661
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001662 if (applicationName != null && applicationName.equals(shortcutName)) {
1663 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1664 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001665
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001666 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1667 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001668 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001669 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001670 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001671 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001672 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 }
1674
Winson Chung24ab2f12010-09-16 14:10:47 -07001675 void processWallpaper(Intent intent) {
1676 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1677 }
1678
Winson Chung3d503fb2011-07-13 17:25:49 -07001679 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1680 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001681 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 folderInfo.title = getText(R.string.folder_name);
1683
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001685 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1686 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001687 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688
1689 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001690 FolderIcon newFolder =
1691 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1692 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1693 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001694 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 }
Romain Guycbb89e42009-06-08 15:52:54 -07001696
Joe Onorato9c1289c2009-08-17 11:03:03 -04001697 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001698 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001699 }
1700
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001702 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001704 Intent chooser = Intent.createChooser(pickWallpaper,
1705 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001706 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1707 // Removed in Eclair MR1
1708// WallpaperManager wm = (WallpaperManager)
1709// getSystemService(Context.WALLPAPER_SERVICE);
1710// WallpaperInfo wi = wm.getWallpaperInfo();
1711// if (wi != null && wi.getSettingsActivity() != null) {
1712// LabeledIntent li = new LabeledIntent(getPackageName(),
1713// R.string.configure_wallpaper, 0);
1714// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1715// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1716// }
Mike Clerona0618e42009-10-22 13:55:21 -07001717 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 }
1719
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001720 /**
1721 * Registers various content observers. The current implementation registers
1722 * only a favorites observer to keep track of the favorites applications.
1723 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001724 private void registerContentObservers() {
1725 ContentResolver resolver = getContentResolver();
1726 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1727 true, mWidgetObserver);
1728 }
1729
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730 @Override
1731 public boolean dispatchKeyEvent(KeyEvent event) {
1732 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1733 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001735 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001736 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001737 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001738 dumpState();
1739 return true;
1740 }
1741 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001742 }
1743 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1744 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001745 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 return true;
1747 }
1748 }
1749
1750 return super.dispatchKeyEvent(event);
1751 }
1752
Joe Onorato88ec0992009-11-19 13:16:06 -08001753 @Override
1754 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001755 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001756 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001757 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001758 Folder openFolder = mWorkspace.getOpenFolder();
1759 if (openFolder.isEditingName()) {
1760 openFolder.dismissEditingName();
1761 } else {
1762 closeFolder();
1763 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001764 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001765 mWorkspace.exitWidgetResizeMode();
1766
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001767 // Back button is a no-op here, but give at least some feedback for the button press
1768 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001769 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001770 }
1771
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001773 * Re-listen when widgets are reset.
1774 */
1775 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001776 if (mAppWidgetHost != null) {
1777 mAppWidgetHost.startListening();
1778 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001779 }
1780
1781 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001782 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1783 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001785 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001786 if (mModel != null) {
1787 mModel.unbindWorkspaceItems();
1788 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001790
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 /**
1792 * Launches the intent referred by the clicked shortcut.
1793 *
1794 * @param v The view representing the clicked shortcut.
1795 */
1796 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001797 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1798 // view has detached (it's possible for this to happen if the view is removed mid touch).
1799 if (v.getWindowToken() == null) {
1800 return;
1801 }
1802
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001803 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001804 return;
1805 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001806
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001808 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001810 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001811 int[] pos = new int[2];
1812 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001813 intent.setSourceBounds(new Rect(pos[0], pos[1],
1814 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001815
1816 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001817
1818 if (success && v instanceof BubbleTextView) {
1819 mWaitingForResume = (BubbleTextView) v;
1820 mWaitingForResume.setStayPressed(true);
1821 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001823 if (v instanceof FolderIcon) {
1824 FolderIcon fi = (FolderIcon) v;
1825 handleFolderClick(fi);
1826 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001827 } else if (v == mAllAppsButton) {
1828 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001829 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001830 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001831 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 }
1834 }
1835
Michael Jurka0e260592010-06-30 17:07:39 -07001836 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001837 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001838 // clicking anywhere on the workspace causes the customization drawer to slide down
1839 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001840 return false;
1841 }
1842
Michael Jurkaaf442092010-06-10 17:01:57 -07001843 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001844 * Event handler for the search button
1845 *
1846 * @param v The view that was clicked.
1847 */
1848 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001849 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1850
Amith Yamasania135ba82011-08-09 17:42:01 -07001851 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001852 }
1853
1854 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001855 * Event handler for the voice button
1856 *
1857 * @param v The view that was clicked.
1858 */
1859 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001860 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001861
Michael Jurkaae65ee32012-04-30 11:45:54 -07001862 try {
1863 final SearchManager searchManager =
1864 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1865 ComponentName activityName = searchManager.getGlobalSearchActivity();
1866 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1867 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1868 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1869 if (activityName != null) {
1870 intent.setPackage(activityName.getPackageName());
1871 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001872 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07001873 } catch (ActivityNotFoundException e) {
1874 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1875 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1876 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1877 startActivitySafely(null, intent, "onClickVoiceButton");
1878 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001879 }
1880
1881 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001882 * Event handler for the "grid" button that appears on the home screen, which
1883 * enters all apps mode.
1884 *
1885 * @param v The view that was clicked.
1886 */
1887 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001888 showAllApps(true);
1889 }
1890
1891 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001892 // Provide the same haptic feedback that the system offers for virtual keys.
1893 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001894 }
1895
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001896 public void onClickAppMarketButton(View v) {
1897 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001898 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001899 } else {
1900 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001901 }
1902 }
1903
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001904 void startApplicationDetailsActivity(ComponentName componentName) {
1905 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001906 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1907 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001908 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001909 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001910 }
1911
Patrick Dubroy5539af72010-09-07 15:22:01 -07001912 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1913 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1914 // System applications cannot be installed. For now, show a toast explaining that.
1915 // We may give them the option of disabling apps this way.
1916 int messageId = R.string.uninstall_system_app_text;
1917 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1918 } else {
1919 String packageName = appInfo.componentName.getPackageName();
1920 String className = appInfo.componentName.getClassName();
1921 Intent intent = new Intent(
1922 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001923 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1924 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001925 startActivity(intent);
1926 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001927 }
1928
Michael Jurka86a720e2012-05-09 11:23:23 -07001929 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001931
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 try {
Winson Chung2672ff92012-05-04 16:22:30 -07001933 // Only launch using the new animation if the shortcut has not opted out (this is a
1934 // private contract between launcher and may be ignored in the future).
1935 boolean useLaunchAnimation = (v != null) &&
1936 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
1937 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07001938 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1939 v.getMeasuredWidth(), v.getMeasuredHeight());
1940
1941 startActivity(intent, opts.toBundle());
1942 } else {
1943 startActivity(intent);
1944 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001945 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 } catch (SecurityException e) {
1947 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001948 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001950 "or use the exported attribute for this activity. "
1951 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001953 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001955
Michael Jurka86a720e2012-05-09 11:23:23 -07001956 boolean startActivitySafely(View v, Intent intent, Object tag) {
1957 boolean success = false;
1958 try {
1959 success = startActivity(v, intent, tag);
1960 } catch (ActivityNotFoundException e) {
1961 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1962 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
1963 }
1964 return success;
1965 }
1966
Romain Guy8e633c52010-03-04 12:51:36 -08001967 void startActivityForResultSafely(Intent intent, int requestCode) {
1968 try {
1969 startActivityForResult(intent, requestCode);
1970 } catch (ActivityNotFoundException e) {
1971 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1972 } catch (SecurityException e) {
1973 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1974 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1975 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1976 "or use the exported attribute for this activity.", e);
1977 }
1978 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979
Adam Cohena9cf38f2011-05-02 15:36:58 -07001980 private void handleFolderClick(FolderIcon folderIcon) {
1981 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001982 Folder openFolder = mWorkspace.getFolderForTag(info);
1983
1984 // If the folder info reports that the associated folder is open, then verify that
1985 // it is actually opened. There have been a few instances where this gets out of sync.
1986 if (info.opened && openFolder == null) {
1987 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1988 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1989 info.opened = false;
1990 }
1991
Adam Cohena9cf38f2011-05-02 15:36:58 -07001992 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 // Close any open folder
1994 closeFolder();
1995 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001996 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 } else {
1998 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 int folderScreen;
2000 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002001 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 // .. and close it
2003 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002004 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 // Close any folder open on the current screen
2006 closeFolder();
2007 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002008 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
2010 }
2011 }
2012 }
2013
Adam Cohen2801caf2011-05-13 20:57:39 -07002014 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002015 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002016 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2017 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2018 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2019
Adam Cohenc51934b2011-07-26 21:07:43 -07002020 FolderInfo info = (FolderInfo) fi.getTag();
2021 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2022 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002023 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2024 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002025 }
2026
Adam Cohen2801caf2011-05-13 20:57:39 -07002027 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2028 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2029 oa.start();
2030 }
2031
2032 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002033 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002034 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2035 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2036 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2037
Adam Cohenc51934b2011-07-26 21:07:43 -07002038 FolderInfo info = (FolderInfo) fi.getTag();
2039 CellLayout cl = null;
2040 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2041 cl = (CellLayout) fi.getParent().getParent();
2042 }
2043
2044 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07002045 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2046 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002047 oa.addListener(new AnimatorListenerAdapter() {
2048 @Override
2049 public void onAnimationEnd(Animator animation) {
2050 if (layout != null) {
2051 layout.clearFolderLeaveBehind();
2052 }
2053 }
2054 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002055 oa.start();
2056 }
2057
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002059 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 * is animated relative to the specified View. If the View is null, no animation
2061 * is played.
2062 *
2063 * @param folderInfo The FolderInfo describing the folder to open.
2064 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002065 public void openFolder(FolderIcon folderIcon) {
2066 Folder folder = folderIcon.mFolder;
2067 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068
Adam Cohen2801caf2011-05-13 20:57:39 -07002069 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002070 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071
Adam Cohen4554ee12011-08-03 16:13:21 -07002072 // Just verify that the folder hasn't already been added to the DragLayer.
2073 // There was a one-off crash where the folder had a parent already.
2074 if (folder.getParent() == null) {
2075 mDragLayer.addView(folder);
2076 mDragController.addDropTarget((DropTarget) folder);
2077 } else {
2078 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2079 folder.getParent() + ").");
2080 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002081 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002082 }
2083
2084 public void closeFolder() {
2085 Folder folder = mWorkspace.getOpenFolder();
2086 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002087 if (folder.isEditingName()) {
2088 folder.dismissEditingName();
2089 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002090 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002091
2092 // Dismiss the folder cling
2093 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002094 }
2095 }
2096
2097 void closeFolder(Folder folder) {
2098 folder.getInfo().opened = false;
2099
2100 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2101 if (parent != null) {
2102 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2103 shrinkAndFadeInFolderIcon(fi);
2104 }
2105 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 }
2107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002109 if (!isDraggingEnabled()) return false;
2110 if (isWorkspaceLocked()) return false;
2111 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112
2113 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002114 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 }
2116
Michael Jurka0280c3b2010-09-17 15:00:07 -07002117 resetAddInfo();
2118 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002120 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 return true;
2122 }
2123
Winson Chung3d503fb2011-07-13 17:25:49 -07002124 // The hotseat touch handling does not go through Workspace, and we always allow long press
2125 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002126 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002127 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2128 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002129 if (itemUnderLongClick == null) {
2130 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002131 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2132 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002133 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002135 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002137 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 }
2139 }
2140 }
2141 return true;
2142 }
2143
Winson Chung3d503fb2011-07-13 17:25:49 -07002144 boolean isHotseatLayout(View layout) {
2145 return mHotseat != null && layout != null &&
2146 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2147 }
2148 Hotseat getHotseat() {
2149 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002150 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002151 SearchDropTargetBar getSearchBar() {
2152 return mSearchDropTargetBar;
2153 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002154
Winson Chung3d503fb2011-07-13 17:25:49 -07002155 /**
2156 * Returns the CellLayout of the specified container at the specified screen.
2157 */
2158 CellLayout getCellLayout(long container, int screen) {
2159 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2160 if (mHotseat != null) {
2161 return mHotseat.getLayout();
2162 } else {
2163 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002164 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002165 } else {
2166 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002167 }
2168 }
2169
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 Workspace getWorkspace() {
2171 return mWorkspace;
2172 }
2173
Daniel Sandler843e8602010-06-07 14:59:01 -04002174 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2175 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002176 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002177 }
2178
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002179 public boolean isAllAppsButtonRank(int rank) {
2180 return mHotseat.isAllAppsButtonRank(rank);
2181 }
2182
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002183 // AllAppsView.Watcher
2184 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002185 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002186 mWorkspace.setVisibility(View.GONE);
2187 }
2188 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002189
2190 /**
2191 * Helper method for the cameraZoomIn/cameraZoomOut animations
2192 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002193 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002194 * @param scaleFactor The scale factor used for the zoom
2195 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002196 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002197 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002198 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002199 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002200
Winson Chung18f41f82012-05-09 13:28:10 -07002201 void disableWallpaperIfInAllApps() {
2202 // Only disable it if we are in all apps
2203 if (mState == State.APPS_CUSTOMIZE) {
2204 if (mAppsCustomizeTabHost != null &&
2205 !mAppsCustomizeTabHost.isTransitioning()) {
2206 updateWallpaperVisibility(false);
2207 }
2208 }
2209 }
2210
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002211 void updateWallpaperVisibility(boolean visible) {
2212 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2213 int curflags = getWindow().getAttributes().flags
2214 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2215 if (wpflags != curflags) {
2216 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2217 }
2218 }
2219
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002220 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2221 if (v instanceof LauncherTransitionable) {
2222 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2223 }
2224 }
2225
Michael Jurkabed61d22012-02-14 22:51:29 -08002226 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2227 if (v instanceof LauncherTransitionable) {
2228 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2229 }
Winson Chung70442722012-02-10 15:43:22 -08002230
2231 // Update the workspace transition step as well
2232 dispatchOnLauncherTransitionStep(v, 0f);
2233 }
2234
2235 private void dispatchOnLauncherTransitionStep(View v, float t) {
2236 if (v instanceof LauncherTransitionable) {
2237 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2238 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002239 }
2240
2241 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2242 if (v instanceof LauncherTransitionable) {
2243 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2244 }
Winson Chung70442722012-02-10 15:43:22 -08002245
2246 // Update the workspace transition step as well
2247 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002248 }
2249
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002250 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002251 * Things to test when changing the following seven functions.
2252 * - Home from workspace
2253 * - from center screen
2254 * - from other screens
2255 * - Home from all apps
2256 * - from center screen
2257 * - from other screens
2258 * - Back from all apps
2259 * - from center screen
2260 * - from other screens
2261 * - Launch app from workspace and quit
2262 * - with back
2263 * - with home
2264 * - Launch app from all apps and quit
2265 * - with back
2266 * - with home
2267 * - Go to a screen that's not the default, then all
2268 * apps, and launch and app, and go back
2269 * - with back
2270 * -with home
2271 * - On workspace, long press power and go back
2272 * - with back
2273 * - with home
2274 * - On all apps, long press power and go back
2275 * - with back
2276 * - with home
2277 * - On workspace, power off
2278 * - On all apps, power off
2279 * - Launch an app and turn off the screen while in that app
2280 * - Go back with home key
2281 * - Go back with back key TODO: make this not go to workspace
2282 * - From all apps
2283 * - From workspace
2284 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2285 * - From all apps
2286 * - From the center workspace
2287 * - From another workspace
2288 */
2289
2290 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002291 * Zoom the camera out from the workspace to reveal 'toView'.
2292 * Assumes that the view to show is anchored at either the very top or very bottom
2293 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002294 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002295 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002296 if (mStateAnimation != null) {
2297 mStateAnimation.cancel();
2298 mStateAnimation = null;
2299 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002300 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002301
Winson Chungf0ea4d32011-06-06 14:27:16 -07002302 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2303 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2304 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002305 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002306 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002307 final int startDelay =
2308 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002309
Michael Jurkab3e22d92011-10-31 15:58:33 -07002310 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002311
Michael Jurkad74c9842011-07-10 12:44:21 -07002312 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002313 Animator workspaceAnim =
2314 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002315
2316 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002317 toView.setScaleX(scale);
2318 toView.setScaleY(scale);
2319 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2320 scaleAnim.
2321 scaleX(1f).scaleY(1f).
2322 setDuration(duration).
2323 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002324
Winson Chungf0ea4d32011-06-06 14:27:16 -07002325 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002326 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002327 final ObjectAnimator alphaAnim = ObjectAnimator
2328 .ofFloat(toView, "alpha", 0f, 1f)
2329 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002330 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002331 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2332 @Override
2333 public void onAnimationUpdate(ValueAnimator animation) {
2334 float t = (Float) animation.getAnimatedValue();
2335 dispatchOnLauncherTransitionStep(fromView, t);
2336 dispatchOnLauncherTransitionStep(toView, t);
2337 }
2338 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002339
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002340 // toView should appear right at the end of the workspace shrink
2341 // animation
2342 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002343 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002344 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002345
2346 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002347 boolean animationCancelled = false;
2348
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002349 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002350 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002351 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002352 // Prepare the position
2353 toView.setTranslationX(0.0f);
2354 toView.setTranslationY(0.0f);
2355 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002356 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002357 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002358 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002359 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002360 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2361 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002362
2363 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2364 // Hide the workspace scrollbar
2365 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002366 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002367 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002368 if (!animationCancelled) {
2369 updateWallpaperVisibility(false);
2370 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002371
2372 // Hide the search bar
2373 mSearchDropTargetBar.hideSearchBar(false);
Adam Cohenf4ddea32011-09-12 13:46:42 -07002374 }
2375
Adam Cohencff6af82011-09-13 14:51:53 -07002376 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002377 public void onAnimationCancel(Animator animation) {
2378 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002379 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002380 });
2381
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002382 if (workspaceAnim != null) {
2383 mStateAnimation.play(workspaceAnim);
2384 }
Michael Jurka1899a362011-11-03 13:50:45 -07002385
2386 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002387 final ViewTreeObserver observer;
2388
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002389 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2390 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002391
2392 // If any of the objects being animated haven't been measured/laid out
2393 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002394 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002395 (mWorkspace.getMeasuredWidth() == 0) ||
2396 (toView.getMeasuredWidth() == 0)) {
2397 observer = mWorkspace.getViewTreeObserver();
2398 delayAnim = true;
2399 } else {
2400 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002401 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002402
2403 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002404 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002405 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2406 public void onGlobalLayout() {
2407 mWorkspace.post(new Runnable() {
2408 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002409 // Check that mStateAnimation hasn't changed while
2410 // we waited for a layout pass
2411 if (mStateAnimation == stateAnimation) {
2412 // Need to update pivots for zoom if layout changed
2413 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002414 dispatchOnLauncherTransitionStart(fromView, animated, false);
2415 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka49779a12012-01-16 04:10:08 -08002416 mStateAnimation.start();
2417 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002418 }
2419 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002420 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002421 }
2422 };
2423 observer.addOnGlobalLayoutListener(delayedStart);
2424 } else {
2425 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002426 dispatchOnLauncherTransitionStart(fromView, animated, false);
2427 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka1899a362011-11-03 13:50:45 -07002428 mStateAnimation.start();
2429 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002430 } else {
2431 toView.setTranslationX(0.0f);
2432 toView.setTranslationY(0.0f);
2433 toView.setScaleX(1.0f);
2434 toView.setScaleY(1.0f);
2435 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002436 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002437
Michael Jurkabed61d22012-02-14 22:51:29 -08002438 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2439 // Hide the workspace scrollbar
2440 mWorkspace.hideScrollingIndicator(true);
2441 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002442
2443 // Hide the search bar
2444 mSearchDropTargetBar.hideSearchBar(false);
Michael Jurkaabded662011-03-04 12:06:57 -08002445 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002446 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002447 dispatchOnLauncherTransitionStart(fromView, animated, false);
2448 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002449 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002450 dispatchOnLauncherTransitionStart(toView, animated, false);
2451 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002452 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002453 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002454 }
2455
2456 /**
2457 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002458 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002459 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002460 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002461 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2462 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002463
Michael Jurkab3e22d92011-10-31 15:58:33 -07002464 if (mStateAnimation != null) {
2465 mStateAnimation.cancel();
2466 mStateAnimation = null;
2467 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002468 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002469
Winson Chungf0ea4d32011-06-06 14:27:16 -07002470 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002471 final int fadeOutDuration =
2472 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002473 final float scaleFactor = (float)
2474 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2475 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002476 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002477 Animator workspaceAnim = null;
2478
2479 if (toState == State.WORKSPACE) {
2480 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2481 workspaceAnim = mWorkspace.getChangeStateAnimation(
2482 Workspace.State.NORMAL, animated, stagger);
2483 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2484 workspaceAnim = mWorkspace.getChangeStateAnimation(
2485 Workspace.State.SPRING_LOADED, animated);
2486 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002487
Michael Jurkab3e22d92011-10-31 15:58:33 -07002488 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002489 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002490 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002491 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002492 final LauncherViewPropertyAnimator scaleAnim =
2493 new LauncherViewPropertyAnimator(fromView);
2494 scaleAnim.
2495 scaleX(scaleFactor).scaleY(scaleFactor).
2496 setDuration(duration).
2497 setInterpolator(new Workspace.ZoomInInterpolator());
2498
2499 final ObjectAnimator alphaAnim = ObjectAnimator
2500 .ofFloat(fromView, "alpha", 1f, 0f)
2501 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002502 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002503 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2504 @Override
2505 public void onAnimationUpdate(ValueAnimator animation) {
2506 float t = 1f - (Float) animation.getAnimatedValue();
2507 dispatchOnLauncherTransitionStep(fromView, t);
2508 dispatchOnLauncherTransitionStep(toView, t);
2509 }
2510 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002511
Michael Jurkabed61d22012-02-14 22:51:29 -08002512 mStateAnimation = new AnimatorSet();
2513
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002514 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2515 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002516
2517 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002518 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002519 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002520 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002521 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002522 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2523 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002524 if (mWorkspace != null) {
2525 mWorkspace.hideScrollingIndicator(false);
2526 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002527 if (onCompleteRunnable != null) {
2528 onCompleteRunnable.run();
2529 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002530 }
2531 });
2532
Winson Chungf0ea4d32011-06-06 14:27:16 -07002533 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002534 if (workspaceAnim != null) {
2535 mStateAnimation.play(workspaceAnim);
2536 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002537 dispatchOnLauncherTransitionStart(fromView, animated, true);
2538 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002539 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002540 } else {
2541 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002542 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002543 dispatchOnLauncherTransitionStart(fromView, animated, true);
2544 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002545 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002546 dispatchOnLauncherTransitionStart(toView, animated, true);
2547 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002548 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002549 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002550 }
2551
Michael Jurkae326f182011-11-21 14:05:46 -08002552 @Override
2553 public void onTrimMemory(int level) {
2554 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002555 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002556 mAppsCustomizeTabHost.onTrimMemory();
2557 }
2558 }
2559
Winson Chung18f41f82012-05-09 13:28:10 -07002560 @Override
2561 public void onWindowFocusChanged(boolean hasFocus) {
2562 if (!hasFocus) {
2563 // When another window occludes launcher (like the notification shade, or recents),
2564 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2565 updateWallpaperVisibility(true);
2566 } else {
2567 // When launcher has focus again, disable the wallpaper if we are in AllApps
2568 disableWallpaperIfInAllApps();
2569 }
2570 }
2571
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002572 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002573 showWorkspace(animated, null);
2574 }
2575
2576 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002577 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002578 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002579 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002580 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002581
Winson Chungc7d2b602012-05-16 17:02:20 -07002582 // Show the search bar (only animate if we were showing the drop target bar in spring
2583 // loaded mode)
2584 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2585
Michael Jurkab737ee62011-11-15 15:57:22 -08002586 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002587 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002588
2589 // Set focus to the AppsCustomize button
2590 if (mAllAppsButton != null) {
2591 mAllAppsButton.requestFocus();
2592 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002593 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002594
Michael Jurkab737ee62011-11-15 15:57:22 -08002595 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002596
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002597 // Change the state *after* we've called all the transition code
2598 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002599
Winson Chung5fb63472011-02-02 17:03:37 -08002600 // Resume the auto-advance of widgets
2601 mUserPresent = true;
2602 updateRunning();
2603
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002604 // send an accessibility event to announce the context change
2605 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002606 }
2607
Michael Jurkab3e22d92011-10-31 15:58:33 -07002608 void showAllApps(boolean animated) {
2609 if (mState != State.WORKSPACE) return;
2610
2611 showAppsCustomizeHelper(animated, false);
2612 mAppsCustomizeTabHost.requestFocus();
2613
Michael Jurkab3e22d92011-10-31 15:58:33 -07002614 // Change the state *after* we've called all the transition code
2615 mState = State.APPS_CUSTOMIZE;
2616
2617 // Pause the auto-advance of widgets until we are out of AllApps
2618 mUserPresent = false;
2619 updateRunning();
2620 closeFolder();
2621
2622 // Send an accessibility event to announce the context change
2623 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2624 }
2625
Adam Cohen7777d962011-08-18 18:58:38 -07002626 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002627 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002628 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002629 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002630 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002631 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002632 }
Adam Cohen7777d962011-08-18 18:58:38 -07002633
Adam Cohened66b2b2012-01-23 17:28:51 -08002634 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2635 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002636 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2637
Winson Chunge7a03942011-08-05 15:05:12 -07002638 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002639 @Override
2640 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002641 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002642 // Before we show workspace, hide all apps again because
2643 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2644 // clean up our state transition functions
2645 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002646 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002647 } else {
2648 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002649 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002650 }
2651 }, (extendedDelay ?
2652 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2653 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2654 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002655
Michael Jurkad3ef3062010-11-23 16:23:58 -08002656 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002657 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002658 final boolean animated = true;
2659 final boolean springLoaded = true;
2660 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002661 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002662 }
2663 // Otherwise, we are not in spring loaded mode, so don't do anything.
2664 }
2665
Michael Jurkab737ee62011-11-15 15:57:22 -08002666 void hideDockDivider() {
2667 if (mQsbDivider != null && mDockDivider != null) {
2668 mQsbDivider.setVisibility(View.INVISIBLE);
2669 mDockDivider.setVisibility(View.INVISIBLE);
2670 }
2671 }
2672
2673 void showDockDivider(boolean animated) {
2674 if (mQsbDivider != null && mDockDivider != null) {
2675 mQsbDivider.setVisibility(View.VISIBLE);
2676 mDockDivider.setVisibility(View.VISIBLE);
2677 if (mDividerAnimator != null) {
2678 mDividerAnimator.cancel();
2679 mQsbDivider.setAlpha(1f);
2680 mDockDivider.setAlpha(1f);
2681 mDividerAnimator = null;
2682 }
2683 if (animated) {
2684 mDividerAnimator = new AnimatorSet();
2685 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2686 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2687 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2688 mDividerAnimator.start();
2689 }
2690 }
2691 }
2692
Michael Jurkab3e22d92011-10-31 15:58:33 -07002693 void lockAllApps() {
2694 // TODO
2695 }
2696
2697 void unlockAllApps() {
2698 // TODO
2699 }
2700
Adam Cohenfc53cd22011-07-20 15:45:11 -07002701 public boolean isAllAppsCustomizeOpen() {
2702 return mState == State.APPS_CUSTOMIZE;
2703 }
2704
Winson Chungf0ea4d32011-06-06 14:27:16 -07002705 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002706 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002707 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002708 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002709 if (!LauncherApplication.isScreenLarge()) {
2710 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002711 if (mHotseat.getAlpha() != 1f) {
2712 int duration = mSearchDropTargetBar.getTransitionInDuration();
2713 mHotseat.animate().alpha(1f).setDuration(duration);
2714 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002715 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002716 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002717 }
2718 }
2719 }
2720
2721 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002722 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002723 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002724 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002725 if (!LauncherApplication.isScreenLarge()) {
2726 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002727 if (mHotseat.getAlpha() != 0f) {
2728 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2729 mHotseat.animate().alpha(0f).setDuration(duration);
2730 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002731 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002732 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002733 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002734 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002735 }
2736
Patrick Dubroy5f445422011-02-18 14:35:21 -08002737 /**
2738 * Add an item from all apps or customize onto the given workspace screen.
2739 * If layout is null, add to the current screen.
2740 */
2741 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002742 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002743 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002744 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002745 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002746
Winson Chungdff8ebb2011-09-08 17:25:31 -07002747 /** Maps the current orientation to an index for referencing orientation correct global icons */
2748 private int getCurrentOrientationIndexForGlobalIcons() {
2749 // default - 0, landscape - 1
2750 switch (getResources().getConfiguration().orientation) {
2751 case Configuration.ORIENTATION_LANDSCAPE:
2752 return 1;
2753 default:
2754 return 0;
2755 }
2756 }
2757
Michael Jurkaae65ee32012-04-30 11:45:54 -07002758 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002759 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002760 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002761 // Look for the toolbar icon specified in the activity meta-data
2762 Bundle metaData = packageManager.getActivityInfo(
2763 activityName, PackageManager.GET_META_DATA).metaData;
2764 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002765 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002766 if (iconResId != 0) {
2767 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002768 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002769 }
2770 }
2771 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002772 // This can happen if the activity defines an invalid drawable
2773 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2774 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002775 } catch (Resources.NotFoundException nfe) {
2776 // This can happen if the activity defines an invalid drawable
2777 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2778 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002779 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002780 return null;
2781 }
2782
2783 // if successful in getting icon, return it; otherwise, set button to use default drawable
2784 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002785 int buttonId, ComponentName activityName, int fallbackDrawableId,
2786 String toolbarResourceName) {
2787 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002788 Resources r = getResources();
2789 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2790 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002791
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002792 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002793 // If we were unable to find the icon via the meta-data, use a generic one
2794 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002795 toolbarIcon = r.getDrawable(fallbackDrawableId);
2796 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002797 if (button != null) {
2798 button.setCompoundDrawables(toolbarIcon, null, null, null);
2799 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002800 return null;
2801 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002802 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002803 if (button != null) {
2804 button.setCompoundDrawables(toolbarIcon, null, null, null);
2805 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002806 return toolbarIcon.getConstantState();
2807 }
2808 }
2809
2810 // if successful in getting icon, return it; otherwise, set button to use default drawable
2811 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002812 int buttonId, ComponentName activityName, int fallbackDrawableId,
2813 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002814 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002815 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002816
Michael Jurka19e0fc52011-07-22 18:00:21 -07002817 if (button != null) {
2818 // If we were unable to find the icon via the meta-data, use a
2819 // generic one
2820 if (toolbarIcon == null) {
2821 button.setImageResource(fallbackDrawableId);
2822 } else {
2823 button.setImageDrawable(toolbarIcon);
2824 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002825 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002826
2827 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2828
Michael Jurka0423dcf2010-10-05 14:56:18 -07002829 }
2830
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002831 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2832 TextView button = (TextView) findViewById(buttonId);
2833 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2834 }
2835
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002836 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002837 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002838 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002839 }
2840
Winson Chungbb185bd2011-11-21 12:31:42 -08002841 private void invalidatePressedFocusedStates(View container, View button) {
2842 if (container instanceof HolographicLinearLayout) {
2843 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2844 layout.invalidatePressedFocusedStates();
2845 } else if (button instanceof HolographicImageView) {
2846 HolographicImageView view = (HolographicImageView) button;
2847 view.invalidatePressedFocusedStates();
2848 }
2849 }
2850
Winson Chungc51db6a2011-10-05 11:44:49 -07002851 private boolean updateGlobalSearchIcon() {
2852 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002853 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2854 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002855 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002856 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002857 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002858
Winson Chung1cad91e2011-05-25 17:41:01 -07002859 final SearchManager searchManager =
2860 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2861 ComponentName activityName = searchManager.getGlobalSearchActivity();
2862 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002863 int coi = getCurrentOrientationIndexForGlobalIcons();
2864 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002865 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2866 TOOLBAR_SEARCH_ICON_METADATA_NAME);
2867 if (sGlobalSearchIcon[coi] == null) {
2868 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2869 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2870 TOOLBAR_ICON_METADATA_NAME);
2871 }
2872
Winson Chung649723c2011-07-06 20:41:23 -07002873 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002874 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2875 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002876 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002877 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002878 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002879 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002880 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002881 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2882 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2883 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002884 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07002885 if (voiceButtonProxy != null) {
2886 voiceButtonProxy.setVisibility(View.GONE);
2887 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002888 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002889 }
2890 }
2891
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002892 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002893 final View searchButtonContainer = findViewById(R.id.search_button_container);
2894 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002895 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002896 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002897 }
2898
Winson Chungc51db6a2011-10-05 11:44:49 -07002899 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002900 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002901 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002902 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002903 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002904
Winson Chungc51db6a2011-10-05 11:44:49 -07002905 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07002906 final SearchManager searchManager =
2907 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2908 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2909
2910 ComponentName activityName = null;
2911 if (globalSearchActivity != null) {
2912 // Check if the global search activity handles voice search
2913 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2914 intent.setPackage(globalSearchActivity.getPackageName());
2915 activityName = intent.resolveActivity(getPackageManager());
2916 }
2917
2918 if (activityName == null) {
2919 // Fallback: check if an activity other than the global search activity
2920 // resolves this
2921 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2922 activityName = intent.resolveActivity(getPackageManager());
2923 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002924 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002925 int coi = getCurrentOrientationIndexForGlobalIcons();
2926 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002927 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2928 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
2929 if (sVoiceSearchIcon[coi] == null) {
2930 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2931 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2932 TOOLBAR_ICON_METADATA_NAME);
2933 }
Winson Chung649723c2011-07-06 20:41:23 -07002934 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002935 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002936 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07002937 if (voiceButtonProxy != null) {
2938 voiceButtonProxy.setVisibility(View.VISIBLE);
2939 }
Winson Chungbb185bd2011-11-21 12:31:42 -08002940 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002941 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002942 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002943 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002944 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002945 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07002946 if (voiceButtonProxy != null) {
2947 voiceButtonProxy.setVisibility(View.GONE);
2948 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002949 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002950 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002951 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002952
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002953 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002954 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2955 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002956 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002957 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002958 }
2959
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002960 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002961 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002962 */
2963 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002964 final View marketButton = findViewById(R.id.market_button);
2965 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2966 // Find the app market activity by resolving an intent.
2967 // (If multiple app markets are installed, it will return the ResolverActivity.)
2968 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002969 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002970 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002971 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002972 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002973 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
2974 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07002975 marketButton.setVisibility(View.VISIBLE);
2976 } else {
2977 // We should hide and disable the view so that we don't try and restore the visibility
2978 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2979 marketButton.setVisibility(View.GONE);
2980 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002981 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002982 }
2983
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002984 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002985 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002986 }
2987
Michael Jurkad7c28052012-04-27 15:43:36 -07002988 @Override
2989 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2990 boolean result = super.dispatchPopulateAccessibilityEvent(event);
2991 final List<CharSequence> text = event.getText();
2992 text.clear();
2993 text.add(getString(R.string.home));
2994 return result;
2995 }
2996
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002997 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002998 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002999 */
3000 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3001 @Override
3002 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003003 closeSystemDialogs();
3004 }
3005 }
3006
3007 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003008 * Receives notifications whenever the appwidgets are reset.
3009 */
3010 private class AppWidgetResetObserver extends ContentObserver {
3011 public AppWidgetResetObserver() {
3012 super(new Handler());
3013 }
3014
3015 @Override
3016 public void onChange(boolean selfChange) {
3017 onAppWidgetReset();
3018 }
3019 }
3020
3021 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003022 * If the activity is currently paused, signal that we need to re-run the loader
3023 * in onResume.
3024 *
3025 * This needs to be called from incoming places where resources might have been loaded
3026 * while we are paused. That is becaues the Configuration might be wrong
3027 * when we're not running, and if it comes back to what it was when we
3028 * were paused, we are not restarted.
3029 *
3030 * Implementation of the method from LauncherModel.Callbacks.
3031 *
3032 * @return true if we are currently paused. The caller might be able to
3033 * skip some work in that case since we will come back again.
3034 */
3035 public boolean setLoadOnResume() {
3036 if (mPaused) {
3037 Log.i(TAG, "setLoadOnResume");
3038 mOnResumeNeedsLoad = true;
3039 return true;
3040 } else {
3041 return false;
3042 }
3043 }
3044
3045 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003046 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003047 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003048 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003049 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003050 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003051 } else {
3052 return SCREEN_COUNT / 2;
3053 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003054 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003055
Joe Onorato9c1289c2009-08-17 11:03:03 -04003056 /**
3057 * Refreshes the shortcuts shown on the workspace.
3058 *
3059 * Implementation of the method from LauncherModel.Callbacks.
3060 */
3061 public void startBinding() {
3062 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003063
Winson Chungf0c6ae02012-03-21 16:10:31 -07003064 mNewShortcutAnimatePage = -1;
3065 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003066 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003067 int count = workspace.getChildCount();
3068 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003069 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003070 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3071 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003072 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003073 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003074 if (mHotseat != null) {
3075 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003076 }
3077 }
3078
3079 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003080 * Bind the items start-end from the list.
3081 *
3082 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003083 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003084 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003085 setLoadOnResume();
3086
Winson Chungf0c6ae02012-03-21 16:10:31 -07003087 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3088 Set<String> newApps = new HashSet<String>();
3089 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3090
3091 Workspace workspace = mWorkspace;
3092 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003093 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003094
3095 // Short circuit if we are loading dock items for a configuration which has no dock
3096 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3097 mHotseat == null) {
3098 continue;
3099 }
3100
Joe Onorato9c1289c2009-08-17 11:03:03 -04003101 switch (item.itemType) {
3102 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3103 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003104 ShortcutInfo info = (ShortcutInfo) item;
3105 String uri = info.intent.toUri(0).toString();
3106 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003107 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3108 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003109 if (newApps.contains(uri)) {
3110 newApps.remove(uri);
3111
3112 // Prepare the view to be animated up
3113 shortcut.setAlpha(0f);
3114 shortcut.setScaleX(0f);
3115 shortcut.setScaleY(0f);
3116 mNewShortcutAnimatePage = item.screen;
3117 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3118 mNewShortcutAnimateViews.add(shortcut);
3119 }
3120 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003121 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003122 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003123 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003124 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003125 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003126 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3127 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003128 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003130 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003131
Joe Onorato9c1289c2009-08-17 11:03:03 -04003132 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003133 }
3134
Joe Onorato9c1289c2009-08-17 11:03:03 -04003135 /**
3136 * Implementation of the method from LauncherModel.Callbacks.
3137 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003138 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003139 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003140 sFolders.clear();
3141 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003142 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003143
3144 /**
3145 * Add the views for a widget to the workspace.
3146 *
3147 * Implementation of the method from LauncherModel.Callbacks.
3148 */
3149 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003150 setLoadOnResume();
3151
Daniel Sandler843e8602010-06-07 14:59:01 -04003152 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3153 if (DEBUG_WIDGETS) {
3154 Log.d(TAG, "bindAppWidget: " + item);
3155 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003156 final Workspace workspace = mWorkspace;
3157
3158 final int appWidgetId = item.appWidgetId;
3159 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003160 if (DEBUG_WIDGETS) {
3161 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3162 }
3163
Joe Onorato9c1289c2009-08-17 11:03:03 -04003164 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3165
Joe Onorato9c1289c2009-08-17 11:03:03 -04003166 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3167 item.hostView.setTag(item);
3168
Winson Chung3d503fb2011-07-13 17:25:49 -07003169 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003170 item.cellY, item.spanX, item.spanY, false);
3171
Adam Cohended9f8d2010-11-03 13:25:16 -07003172 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3173
Joe Onorato9c1289c2009-08-17 11:03:03 -04003174 workspace.requestLayout();
3175
Winson Chungf51025f2012-05-03 15:21:11 -07003176 AppWidgetResizeFrame.updateWidgetSizeRanges(item.hostView,
3177 this, item.spanX, item.spanY);
3178
Daniel Sandler843e8602010-06-07 14:59:01 -04003179 if (DEBUG_WIDGETS) {
3180 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3181 + (SystemClock.uptimeMillis()-start) + "ms");
3182 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003183 }
3184
3185 /**
3186 * Callback saying that there aren't any more items to bind.
3187 *
3188 * Implementation of the method from LauncherModel.Callbacks.
3189 */
3190 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003191 setLoadOnResume();
3192
Joe Onorato9c1289c2009-08-17 11:03:03 -04003193 if (mSavedState != null) {
3194 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003195 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003196 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003197 mSavedState = null;
3198 }
3199
3200 if (mSavedInstanceState != null) {
3201 super.onRestoreInstanceState(mSavedInstanceState);
3202 mSavedInstanceState = null;
3203 }
3204
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003205 // If we received the result of any pending adds while the loader was running (e.g. the
3206 // widget configuration forced an orientation change), process them now.
3207 for (int i = 0; i < sPendingAddList.size(); i++) {
3208 completeAdd(sPendingAddList.get(i));
3209 }
3210 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003211
Winson Chungc51db6a2011-10-05 11:44:49 -07003212 // Update the market app icon as necessary (the other icons will be managed in response to
3213 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003214 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003215
Winson Chungf0c6ae02012-03-21 16:10:31 -07003216 // Animate up any icons as necessary
3217 if (mVisible || mWorkspaceLoading) {
3218 Runnable newAppsRunnable = new Runnable() {
3219 @Override
3220 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003221 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003222 }
3223 };
Winson Chunga2413752012-04-03 14:22:34 -07003224
3225 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3226 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3227 if (canRunNewAppsAnimation()) {
3228 // If the user has not interacted recently, then either snap to the new page to show
3229 // the new-apps animation or just run them if they are to appear on the current page
3230 if (willSnapPage) {
3231 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3232 } else {
3233 runNewAppsAnimation(false);
3234 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003235 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003236 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003237 // are on another page (or just normally if they are added to the current page)
3238 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003239 }
3240 }
3241
3242 mWorkspaceLoading = false;
3243 }
3244
Winson Chunga2413752012-04-03 14:22:34 -07003245 private boolean canRunNewAppsAnimation() {
3246 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3247 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3248 }
3249
Winson Chungf0c6ae02012-03-21 16:10:31 -07003250 /**
3251 * Runs a new animation that scales up icons that were added while Launcher was in the
3252 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003253 *
3254 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003255 */
Winson Chunga2413752012-04-03 14:22:34 -07003256 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003257 AnimatorSet anim = new AnimatorSet();
3258 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003259
3260 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003261 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3262 @Override
3263 public int compare(View a, View b) {
3264 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3265 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3266 int cellCountX = LauncherModel.getCellCountX();
3267 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3268 }
3269 });
Winson Chunga2413752012-04-03 14:22:34 -07003270
3271 // Animate each of the views in place (or show them immediately if requested)
3272 if (immediate) {
3273 for (View v : mNewShortcutAnimateViews) {
3274 v.setAlpha(1f);
3275 v.setScaleX(1f);
3276 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003277 }
Winson Chunga2413752012-04-03 14:22:34 -07003278 } else {
3279 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3280 View v = mNewShortcutAnimateViews.get(i);
3281 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3282 PropertyValuesHolder.ofFloat("alpha", 1f),
3283 PropertyValuesHolder.ofFloat("scaleX", 1f),
3284 PropertyValuesHolder.ofFloat("scaleY", 1f));
3285 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3286 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3287 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3288 bounceAnims.add(bounceAnim);
3289 }
3290 anim.playTogether(bounceAnims);
3291 anim.addListener(new AnimatorListenerAdapter() {
3292 @Override
3293 public void onAnimationEnd(Animator animation) {
3294 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3295 }
3296 });
3297 anim.start();
3298 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003299
3300 // Clean up
3301 mNewShortcutAnimatePage = -1;
3302 mNewShortcutAnimateViews.clear();
3303 new Thread("clearNewAppsThread") {
3304 public void run() {
3305 mSharedPrefs.edit()
3306 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3307 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3308 .commit();
3309 }
3310 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003311 }
3312
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003313 @Override
3314 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003315 boolean searchVisible = updateGlobalSearchIcon();
3316 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3317 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003318 }
3319
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003320 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003321 * Add the icons for all apps.
3322 *
3323 * Implementation of the method from LauncherModel.Callbacks.
3324 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003325 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3326 // Remove the progress bar entirely; we could also make it GONE
3327 // but better to remove it since we know it's not going to be used
3328 View progressBar = mAppsCustomizeTabHost.
3329 findViewById(R.id.apps_customize_progress_bar);
3330 if (progressBar != null) {
3331 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003332 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003333 // We just post the call to setApps so the user sees the progress bar
3334 // disappear-- otherwise, it just looks like the progress bar froze
3335 // which doesn't look great
3336 mAppsCustomizeTabHost.post(new Runnable() {
3337 public void run() {
3338 if (mAppsCustomizeContent != null) {
3339 mAppsCustomizeContent.setApps(apps);
3340 }
3341 }
3342 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003343 }
3344
3345 /**
3346 * A package was installed.
3347 *
3348 * Implementation of the method from LauncherModel.Callbacks.
3349 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003350 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003351 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003352
Winson Chung785d2eb2011-04-14 16:08:02 -07003353 if (mAppsCustomizeContent != null) {
3354 mAppsCustomizeContent.addApps(apps);
3355 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003356 }
3357
3358 /**
3359 * A package was updated.
3360 *
3361 * Implementation of the method from LauncherModel.Callbacks.
3362 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003363 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003364 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003365 if (mWorkspace != null) {
3366 mWorkspace.updateShortcuts(apps);
3367 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003368
Winson Chung785d2eb2011-04-14 16:08:02 -07003369 if (mAppsCustomizeContent != null) {
3370 mAppsCustomizeContent.updateApps(apps);
3371 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003372 }
3373
3374 /**
3375 * A package was uninstalled.
3376 *
3377 * Implementation of the method from LauncherModel.Callbacks.
3378 */
Joe Onorato36115782010-06-17 13:28:48 -04003379 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003380 if (permanent) {
3381 mWorkspace.removeItems(apps);
3382 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003383
Winson Chung785d2eb2011-04-14 16:08:02 -07003384 if (mAppsCustomizeContent != null) {
3385 mAppsCustomizeContent.removeApps(apps);
3386 }
Winson Chunga1820962011-10-03 16:31:06 -07003387
3388 // Notify the drag controller
3389 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003390 }
Joe Onoratobe386092009-11-17 17:32:16 -08003391
3392 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003393 * A number of packages were updated.
3394 */
3395 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003396 if (mAppsCustomizeContent != null) {
3397 mAppsCustomizeContent.onPackagesUpdated();
3398 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003399 }
3400
Winson Chung400438b2011-01-16 17:53:48 -08003401 private int mapConfigurationOriActivityInfoOri(int configOri) {
3402 final Display d = getWindowManager().getDefaultDisplay();
3403 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3404 switch (d.getRotation()) {
3405 case Surface.ROTATION_0:
3406 case Surface.ROTATION_180:
3407 // We are currently in the same basic orientation as the natural orientation
3408 naturalOri = configOri;
3409 break;
3410 case Surface.ROTATION_90:
3411 case Surface.ROTATION_270:
3412 // We are currently in the other basic orientation to the natural orientation
3413 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3414 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3415 break;
3416 }
3417
3418 int[] oriMap = {
3419 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3420 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3421 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3422 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3423 };
3424 // Since the map starts at portrait, we need to offset if this device's natural orientation
3425 // is landscape.
3426 int indexOffset = 0;
3427 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3428 indexOffset = 1;
3429 }
3430 return oriMap[(d.getRotation() + indexOffset) % 4];
3431 }
Adam Cohen446e9402011-09-15 18:21:21 -07003432
Winson Chung4b919f82012-05-01 10:44:08 -07003433 public boolean isRotationEnabled() {
3434 boolean forceEnableRotation = "true".equalsIgnoreCase(SystemProperties.get(
3435 FORCE_ENABLE_ROTATION_PROPERTY, "false"));
3436 boolean enableRotation = forceEnableRotation ||
3437 getResources().getBoolean(R.bool.allow_rotation);
3438 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003439 }
Winson Chung4b919f82012-05-01 10:44:08 -07003440 public void lockScreenOrientation() {
3441 if (isRotationEnabled()) {
3442 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3443 .getConfiguration().orientation));
3444 }
3445 }
3446 public void unlockScreenOrientation(boolean immediate) {
3447 if (isRotationEnabled()) {
3448 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003449 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003450 } else {
3451 mHandler.postDelayed(new Runnable() {
3452 public void run() {
3453 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3454 }
3455 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003456 }
Winson Chung4b919f82012-05-01 10:44:08 -07003457 }
Winson Chung400438b2011-01-16 17:53:48 -08003458 }
3459
Winson Chung82f55532011-08-09 14:14:23 -07003460 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003461 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003462 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003463 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003464
Winson Chung7d7541e2011-09-16 20:14:36 -07003465 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003466 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003467 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3468 Cling cling = (Cling) findViewById(clingId);
3469 if (cling != null) {
3470 cling.init(this, positionData);
3471 cling.setVisibility(View.VISIBLE);
3472 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3473 if (animate) {
3474 cling.buildLayer();
3475 cling.setAlpha(0f);
3476 cling.animate()
3477 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003478 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003479 .setDuration(SHOW_CLING_DURATION)
3480 .setStartDelay(delay)
3481 .start();
3482 } else {
3483 cling.setAlpha(1f);
3484 }
3485 }
3486 return cling;
3487 }
3488 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003489 if (cling != null) {
3490 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003491 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003492 anim.addListener(new AnimatorListenerAdapter() {
3493 public void onAnimationEnd(Animator animation) {
3494 cling.setVisibility(View.GONE);
3495 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003496 // We should update the shared preferences on a background thread
3497 new Thread("dismissClingThread") {
3498 public void run() {
3499 SharedPreferences.Editor editor = mSharedPrefs.edit();
3500 editor.putBoolean(flag, true);
3501 editor.commit();
3502 }
3503 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003504 };
3505 });
3506 anim.start();
3507 }
3508 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003509 private void removeCling(int id) {
3510 final View cling = findViewById(id);
3511 if (cling != null) {
3512 final ViewGroup parent = (ViewGroup) cling.getParent();
3513 parent.post(new Runnable() {
3514 @Override
3515 public void run() {
3516 parent.removeView(cling);
3517 }
3518 });
3519 }
3520 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003521 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003522 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003523 if (isClingsEnabled() &&
3524 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003525 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003526 } else {
3527 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003528 }
3529 }
3530 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003531 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003532 if (isClingsEnabled() &&
3533 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003534 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003535 } else {
3536 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003537 }
3538 }
3539 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003540 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003541 if (isClingsEnabled() &&
3542 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3543 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003544 } else {
3545 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003546 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003547 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003548 }
3549 public boolean isFolderClingVisible() {
3550 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003551 if (cling != null) {
3552 return cling.getVisibility() == View.VISIBLE;
3553 }
3554 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003555 }
Winson Chung82f55532011-08-09 14:14:23 -07003556 public void dismissWorkspaceCling(View v) {
3557 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003558 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003559 }
3560 public void dismissAllAppsCling(View v) {
3561 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003562 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3563 }
3564 public void dismissFolderCling(View v) {
3565 Cling cling = (Cling) findViewById(R.id.folder_cling);
3566 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003567 }
3568
Winson Chung80baf5a2010-08-09 16:03:15 -07003569 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003570 * Prints out out state for debugging.
3571 */
3572 public void dumpState() {
3573 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003574 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003575 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3576 Log.d(TAG, "mRestoring=" + mRestoring);
3577 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3578 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003579 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003580 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003581
Winson Chung785d2eb2011-04-14 16:08:02 -07003582 if (mAppsCustomizeContent != null) {
3583 mAppsCustomizeContent.dumpState();
3584 }
Joe Onoratobe386092009-11-17 17:32:16 -08003585 Log.d(TAG, "END launcher2 dump state");
3586 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003587
3588 @Override
3589 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3590 super.dump(prefix, fd, writer, args);
3591 writer.println(" ");
3592 writer.println("Debug logs: ");
3593 for (int i = 0; i < sDumpLogs.size(); i++) {
3594 writer.println(" " + sDumpLogs.get(i));
3595 }
3596 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003597}
Michael Jurka2763be32011-02-24 11:19:57 -08003598
Michael Jurkaabded662011-03-04 12:06:57 -08003599interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003600 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003601 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003602 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003603 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003604 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003605}