blob: 2e7caff3ddcb1c07d14b3ce6964109daa4e7de34 [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 }
2371 }
2372
Adam Cohencff6af82011-09-13 14:51:53 -07002373 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002374 public void onAnimationCancel(Animator animation) {
2375 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002376 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002377 });
2378
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002379 if (workspaceAnim != null) {
2380 mStateAnimation.play(workspaceAnim);
2381 }
Michael Jurka1899a362011-11-03 13:50:45 -07002382
2383 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002384 final ViewTreeObserver observer;
2385
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002386 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2387 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002388
2389 // If any of the objects being animated haven't been measured/laid out
2390 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002391 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002392 (mWorkspace.getMeasuredWidth() == 0) ||
2393 (toView.getMeasuredWidth() == 0)) {
2394 observer = mWorkspace.getViewTreeObserver();
2395 delayAnim = true;
2396 } else {
2397 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002398 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002399
2400 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002401 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002402 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2403 public void onGlobalLayout() {
2404 mWorkspace.post(new Runnable() {
2405 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002406 // Check that mStateAnimation hasn't changed while
2407 // we waited for a layout pass
2408 if (mStateAnimation == stateAnimation) {
2409 // Need to update pivots for zoom if layout changed
2410 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002411 dispatchOnLauncherTransitionStart(fromView, animated, false);
2412 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka49779a12012-01-16 04:10:08 -08002413 mStateAnimation.start();
2414 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002415 }
2416 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002417 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002418 }
2419 };
2420 observer.addOnGlobalLayoutListener(delayedStart);
2421 } else {
2422 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002423 dispatchOnLauncherTransitionStart(fromView, animated, false);
2424 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka1899a362011-11-03 13:50:45 -07002425 mStateAnimation.start();
2426 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002427 } else {
2428 toView.setTranslationX(0.0f);
2429 toView.setTranslationY(0.0f);
2430 toView.setScaleX(1.0f);
2431 toView.setScaleY(1.0f);
2432 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002433 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002434
Michael Jurkabed61d22012-02-14 22:51:29 -08002435 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2436 // Hide the workspace scrollbar
2437 mWorkspace.hideScrollingIndicator(true);
2438 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002439 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002440 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002441 dispatchOnLauncherTransitionStart(fromView, animated, false);
2442 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002443 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002444 dispatchOnLauncherTransitionStart(toView, animated, false);
2445 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002446 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002447 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002448 }
2449
2450 /**
2451 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002452 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002453 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002454 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002455 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2456 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002457
Michael Jurkab3e22d92011-10-31 15:58:33 -07002458 if (mStateAnimation != null) {
2459 mStateAnimation.cancel();
2460 mStateAnimation = null;
2461 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002462 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002463
Winson Chungf0ea4d32011-06-06 14:27:16 -07002464 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002465 final int fadeOutDuration =
2466 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002467 final float scaleFactor = (float)
2468 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2469 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002470 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002471 Animator workspaceAnim = null;
2472
2473 if (toState == State.WORKSPACE) {
2474 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2475 workspaceAnim = mWorkspace.getChangeStateAnimation(
2476 Workspace.State.NORMAL, animated, stagger);
2477 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2478 workspaceAnim = mWorkspace.getChangeStateAnimation(
2479 Workspace.State.SPRING_LOADED, animated);
2480 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002481
Michael Jurkab3e22d92011-10-31 15:58:33 -07002482 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002483 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002484 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002485 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002486 final LauncherViewPropertyAnimator scaleAnim =
2487 new LauncherViewPropertyAnimator(fromView);
2488 scaleAnim.
2489 scaleX(scaleFactor).scaleY(scaleFactor).
2490 setDuration(duration).
2491 setInterpolator(new Workspace.ZoomInInterpolator());
2492
2493 final ObjectAnimator alphaAnim = ObjectAnimator
2494 .ofFloat(fromView, "alpha", 1f, 0f)
2495 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002496 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002497 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2498 @Override
2499 public void onAnimationUpdate(ValueAnimator animation) {
2500 float t = 1f - (Float) animation.getAnimatedValue();
2501 dispatchOnLauncherTransitionStep(fromView, t);
2502 dispatchOnLauncherTransitionStep(toView, t);
2503 }
2504 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002505
Michael Jurkabed61d22012-02-14 22:51:29 -08002506 mStateAnimation = new AnimatorSet();
2507
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002508 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2509 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002510
2511 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002512 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002513 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002514 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002515 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002516 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2517 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002518 if (mWorkspace != null) {
2519 mWorkspace.hideScrollingIndicator(false);
2520 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002521 if (onCompleteRunnable != null) {
2522 onCompleteRunnable.run();
2523 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002524 }
2525 });
2526
Winson Chungf0ea4d32011-06-06 14:27:16 -07002527 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002528 if (workspaceAnim != null) {
2529 mStateAnimation.play(workspaceAnim);
2530 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002531 dispatchOnLauncherTransitionStart(fromView, animated, true);
2532 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002533 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002534 } else {
2535 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002536 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002537 dispatchOnLauncherTransitionStart(fromView, animated, true);
2538 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002539 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002540 dispatchOnLauncherTransitionStart(toView, animated, true);
2541 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002542 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002543 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002544 }
2545
Michael Jurkae326f182011-11-21 14:05:46 -08002546 @Override
2547 public void onTrimMemory(int level) {
2548 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002549 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002550 mAppsCustomizeTabHost.onTrimMemory();
2551 }
2552 }
2553
Winson Chung18f41f82012-05-09 13:28:10 -07002554 @Override
2555 public void onWindowFocusChanged(boolean hasFocus) {
2556 if (!hasFocus) {
2557 // When another window occludes launcher (like the notification shade, or recents),
2558 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2559 updateWallpaperVisibility(true);
2560 } else {
2561 // When launcher has focus again, disable the wallpaper if we are in AllApps
2562 disableWallpaperIfInAllApps();
2563 }
2564 }
2565
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002566 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002567 showWorkspace(animated, null);
2568 }
2569
2570 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002571 if (mState != State.WORKSPACE) {
2572 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002573 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002574
2575 // Show the search bar and hotseat
2576 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002577 // We only need to animate in the dock divider if we're going from spring loaded mode
2578 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002579
2580 // Set focus to the AppsCustomize button
2581 if (mAllAppsButton != null) {
2582 mAllAppsButton.requestFocus();
2583 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002584 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002585
Michael Jurkab737ee62011-11-15 15:57:22 -08002586 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002587
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002588 // Change the state *after* we've called all the transition code
2589 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002590
Winson Chung5fb63472011-02-02 17:03:37 -08002591 // Resume the auto-advance of widgets
2592 mUserPresent = true;
2593 updateRunning();
2594
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002595 // send an accessibility event to announce the context change
2596 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002597 }
2598
Michael Jurkab3e22d92011-10-31 15:58:33 -07002599 void showAllApps(boolean animated) {
2600 if (mState != State.WORKSPACE) return;
2601
2602 showAppsCustomizeHelper(animated, false);
2603 mAppsCustomizeTabHost.requestFocus();
2604
2605 // Hide the search bar and hotseat
2606 mSearchDropTargetBar.hideSearchBar(animated);
2607
2608 // Change the state *after* we've called all the transition code
2609 mState = State.APPS_CUSTOMIZE;
2610
2611 // Pause the auto-advance of widgets until we are out of AllApps
2612 mUserPresent = false;
2613 updateRunning();
2614 closeFolder();
2615
2616 // Send an accessibility event to announce the context change
2617 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2618 }
2619
Adam Cohen7777d962011-08-18 18:58:38 -07002620 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002621 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002622 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002623 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002624 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002625 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002626 }
Adam Cohen7777d962011-08-18 18:58:38 -07002627
Adam Cohened66b2b2012-01-23 17:28:51 -08002628 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2629 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002630 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2631
Winson Chunge7a03942011-08-05 15:05:12 -07002632 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002633 @Override
2634 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002635 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002636 // Before we show workspace, hide all apps again because
2637 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2638 // clean up our state transition functions
2639 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002640 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002641 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002642 } else {
2643 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002644 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002645 }
2646 }, (extendedDelay ?
2647 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2648 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2649 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002650
Michael Jurkad3ef3062010-11-23 16:23:58 -08002651 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002652 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002653 final boolean animated = true;
2654 final boolean springLoaded = true;
2655 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002656 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002657 }
2658 // Otherwise, we are not in spring loaded mode, so don't do anything.
2659 }
2660
Michael Jurkab737ee62011-11-15 15:57:22 -08002661 void hideDockDivider() {
2662 if (mQsbDivider != null && mDockDivider != null) {
2663 mQsbDivider.setVisibility(View.INVISIBLE);
2664 mDockDivider.setVisibility(View.INVISIBLE);
2665 }
2666 }
2667
2668 void showDockDivider(boolean animated) {
2669 if (mQsbDivider != null && mDockDivider != null) {
2670 mQsbDivider.setVisibility(View.VISIBLE);
2671 mDockDivider.setVisibility(View.VISIBLE);
2672 if (mDividerAnimator != null) {
2673 mDividerAnimator.cancel();
2674 mQsbDivider.setAlpha(1f);
2675 mDockDivider.setAlpha(1f);
2676 mDividerAnimator = null;
2677 }
2678 if (animated) {
2679 mDividerAnimator = new AnimatorSet();
2680 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2681 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2682 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2683 mDividerAnimator.start();
2684 }
2685 }
2686 }
2687
Michael Jurkab3e22d92011-10-31 15:58:33 -07002688 void lockAllApps() {
2689 // TODO
2690 }
2691
2692 void unlockAllApps() {
2693 // TODO
2694 }
2695
Adam Cohenfc53cd22011-07-20 15:45:11 -07002696 public boolean isAllAppsCustomizeOpen() {
2697 return mState == State.APPS_CUSTOMIZE;
2698 }
2699
Winson Chungf0ea4d32011-06-06 14:27:16 -07002700 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002701 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002702 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002703 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002704 if (!LauncherApplication.isScreenLarge()) {
2705 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002706 if (mHotseat.getAlpha() != 1f) {
2707 int duration = mSearchDropTargetBar.getTransitionInDuration();
2708 mHotseat.animate().alpha(1f).setDuration(duration);
2709 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002710 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002711 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002712 }
2713 }
2714 }
2715
2716 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002717 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002718 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002719 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002720 if (!LauncherApplication.isScreenLarge()) {
2721 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002722 if (mHotseat.getAlpha() != 0f) {
2723 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2724 mHotseat.animate().alpha(0f).setDuration(duration);
2725 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002726 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002727 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002728 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002729 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002730 }
2731
Patrick Dubroy5f445422011-02-18 14:35:21 -08002732 /**
2733 * Add an item from all apps or customize onto the given workspace screen.
2734 * If layout is null, add to the current screen.
2735 */
2736 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002737 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002738 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002739 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002740 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002741
Winson Chungdff8ebb2011-09-08 17:25:31 -07002742 /** Maps the current orientation to an index for referencing orientation correct global icons */
2743 private int getCurrentOrientationIndexForGlobalIcons() {
2744 // default - 0, landscape - 1
2745 switch (getResources().getConfiguration().orientation) {
2746 case Configuration.ORIENTATION_LANDSCAPE:
2747 return 1;
2748 default:
2749 return 0;
2750 }
2751 }
2752
Michael Jurkaae65ee32012-04-30 11:45:54 -07002753 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002754 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002755 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002756 // Look for the toolbar icon specified in the activity meta-data
2757 Bundle metaData = packageManager.getActivityInfo(
2758 activityName, PackageManager.GET_META_DATA).metaData;
2759 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002760 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002761 if (iconResId != 0) {
2762 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002763 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002764 }
2765 }
2766 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002767 // This can happen if the activity defines an invalid drawable
2768 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2769 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002770 } catch (Resources.NotFoundException nfe) {
2771 // This can happen if the activity defines an invalid drawable
2772 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2773 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002774 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002775 return null;
2776 }
2777
2778 // if successful in getting icon, return it; otherwise, set button to use default drawable
2779 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002780 int buttonId, ComponentName activityName, int fallbackDrawableId,
2781 String toolbarResourceName) {
2782 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002783 Resources r = getResources();
2784 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2785 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002786
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002787 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002788 // If we were unable to find the icon via the meta-data, use a generic one
2789 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002790 toolbarIcon = r.getDrawable(fallbackDrawableId);
2791 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002792 if (button != null) {
2793 button.setCompoundDrawables(toolbarIcon, null, null, null);
2794 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002795 return null;
2796 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002797 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002798 if (button != null) {
2799 button.setCompoundDrawables(toolbarIcon, null, null, null);
2800 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002801 return toolbarIcon.getConstantState();
2802 }
2803 }
2804
2805 // if successful in getting icon, return it; otherwise, set button to use default drawable
2806 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002807 int buttonId, ComponentName activityName, int fallbackDrawableId,
2808 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002809 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002810 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002811
Michael Jurka19e0fc52011-07-22 18:00:21 -07002812 if (button != null) {
2813 // If we were unable to find the icon via the meta-data, use a
2814 // generic one
2815 if (toolbarIcon == null) {
2816 button.setImageResource(fallbackDrawableId);
2817 } else {
2818 button.setImageDrawable(toolbarIcon);
2819 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002820 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002821
2822 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2823
Michael Jurka0423dcf2010-10-05 14:56:18 -07002824 }
2825
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002826 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2827 TextView button = (TextView) findViewById(buttonId);
2828 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2829 }
2830
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002831 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002832 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002833 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002834 }
2835
Winson Chungbb185bd2011-11-21 12:31:42 -08002836 private void invalidatePressedFocusedStates(View container, View button) {
2837 if (container instanceof HolographicLinearLayout) {
2838 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2839 layout.invalidatePressedFocusedStates();
2840 } else if (button instanceof HolographicImageView) {
2841 HolographicImageView view = (HolographicImageView) button;
2842 view.invalidatePressedFocusedStates();
2843 }
2844 }
2845
Winson Chungc51db6a2011-10-05 11:44:49 -07002846 private boolean updateGlobalSearchIcon() {
2847 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002848 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2849 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002850 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002851 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002852 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002853
Winson Chung1cad91e2011-05-25 17:41:01 -07002854 final SearchManager searchManager =
2855 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2856 ComponentName activityName = searchManager.getGlobalSearchActivity();
2857 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002858 int coi = getCurrentOrientationIndexForGlobalIcons();
2859 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002860 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2861 TOOLBAR_SEARCH_ICON_METADATA_NAME);
2862 if (sGlobalSearchIcon[coi] == null) {
2863 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2864 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2865 TOOLBAR_ICON_METADATA_NAME);
2866 }
2867
Winson Chung649723c2011-07-06 20:41:23 -07002868 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002869 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2870 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002871 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002872 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002873 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002874 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002875 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002876 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2877 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2878 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002879 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07002880 if (voiceButtonProxy != null) {
2881 voiceButtonProxy.setVisibility(View.GONE);
2882 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002883 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002884 }
2885 }
2886
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002887 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002888 final View searchButtonContainer = findViewById(R.id.search_button_container);
2889 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002890 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002891 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002892 }
2893
Winson Chungc51db6a2011-10-05 11:44:49 -07002894 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002895 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002896 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002897 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002898 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002899
Winson Chungc51db6a2011-10-05 11:44:49 -07002900 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07002901 final SearchManager searchManager =
2902 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2903 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2904
2905 ComponentName activityName = null;
2906 if (globalSearchActivity != null) {
2907 // Check if the global search activity handles voice search
2908 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2909 intent.setPackage(globalSearchActivity.getPackageName());
2910 activityName = intent.resolveActivity(getPackageManager());
2911 }
2912
2913 if (activityName == null) {
2914 // Fallback: check if an activity other than the global search activity
2915 // resolves this
2916 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2917 activityName = intent.resolveActivity(getPackageManager());
2918 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002919 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002920 int coi = getCurrentOrientationIndexForGlobalIcons();
2921 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002922 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2923 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
2924 if (sVoiceSearchIcon[coi] == null) {
2925 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2926 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2927 TOOLBAR_ICON_METADATA_NAME);
2928 }
Winson Chung649723c2011-07-06 20:41:23 -07002929 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002930 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002931 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07002932 if (voiceButtonProxy != null) {
2933 voiceButtonProxy.setVisibility(View.VISIBLE);
2934 }
Winson Chungbb185bd2011-11-21 12:31:42 -08002935 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002936 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002937 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002938 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002939 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002940 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07002941 if (voiceButtonProxy != null) {
2942 voiceButtonProxy.setVisibility(View.GONE);
2943 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002944 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002945 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002946 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002947
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002948 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002949 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2950 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002951 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002952 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002953 }
2954
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002955 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002956 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002957 */
2958 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002959 final View marketButton = findViewById(R.id.market_button);
2960 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2961 // Find the app market activity by resolving an intent.
2962 // (If multiple app markets are installed, it will return the ResolverActivity.)
2963 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002964 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002965 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002966 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002967 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002968 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
2969 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07002970 marketButton.setVisibility(View.VISIBLE);
2971 } else {
2972 // We should hide and disable the view so that we don't try and restore the visibility
2973 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2974 marketButton.setVisibility(View.GONE);
2975 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002976 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002977 }
2978
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002979 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002980 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002981 }
2982
Michael Jurkad7c28052012-04-27 15:43:36 -07002983 @Override
2984 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2985 boolean result = super.dispatchPopulateAccessibilityEvent(event);
2986 final List<CharSequence> text = event.getText();
2987 text.clear();
2988 text.add(getString(R.string.home));
2989 return result;
2990 }
2991
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002992 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002993 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002994 */
2995 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2996 @Override
2997 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002998 closeSystemDialogs();
2999 }
3000 }
3001
3002 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003003 * Receives notifications whenever the appwidgets are reset.
3004 */
3005 private class AppWidgetResetObserver extends ContentObserver {
3006 public AppWidgetResetObserver() {
3007 super(new Handler());
3008 }
3009
3010 @Override
3011 public void onChange(boolean selfChange) {
3012 onAppWidgetReset();
3013 }
3014 }
3015
3016 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003017 * If the activity is currently paused, signal that we need to re-run the loader
3018 * in onResume.
3019 *
3020 * This needs to be called from incoming places where resources might have been loaded
3021 * while we are paused. That is becaues the Configuration might be wrong
3022 * when we're not running, and if it comes back to what it was when we
3023 * were paused, we are not restarted.
3024 *
3025 * Implementation of the method from LauncherModel.Callbacks.
3026 *
3027 * @return true if we are currently paused. The caller might be able to
3028 * skip some work in that case since we will come back again.
3029 */
3030 public boolean setLoadOnResume() {
3031 if (mPaused) {
3032 Log.i(TAG, "setLoadOnResume");
3033 mOnResumeNeedsLoad = true;
3034 return true;
3035 } else {
3036 return false;
3037 }
3038 }
3039
3040 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003041 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003042 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003043 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003044 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003045 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003046 } else {
3047 return SCREEN_COUNT / 2;
3048 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003049 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003050
Joe Onorato9c1289c2009-08-17 11:03:03 -04003051 /**
3052 * Refreshes the shortcuts shown on the workspace.
3053 *
3054 * Implementation of the method from LauncherModel.Callbacks.
3055 */
3056 public void startBinding() {
3057 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003058
Winson Chungf0c6ae02012-03-21 16:10:31 -07003059 mNewShortcutAnimatePage = -1;
3060 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003061 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003062 int count = workspace.getChildCount();
3063 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003064 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003065 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3066 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003067 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003068 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003069 if (mHotseat != null) {
3070 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003071 }
3072 }
3073
3074 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003075 * Bind the items start-end from the list.
3076 *
3077 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003078 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003079 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003080 setLoadOnResume();
3081
Winson Chungf0c6ae02012-03-21 16:10:31 -07003082 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3083 Set<String> newApps = new HashSet<String>();
3084 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3085
3086 Workspace workspace = mWorkspace;
3087 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003088 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003089
3090 // Short circuit if we are loading dock items for a configuration which has no dock
3091 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3092 mHotseat == null) {
3093 continue;
3094 }
3095
Joe Onorato9c1289c2009-08-17 11:03:03 -04003096 switch (item.itemType) {
3097 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3098 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003099 ShortcutInfo info = (ShortcutInfo) item;
3100 String uri = info.intent.toUri(0).toString();
3101 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003102 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3103 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003104 if (newApps.contains(uri)) {
3105 newApps.remove(uri);
3106
3107 // Prepare the view to be animated up
3108 shortcut.setAlpha(0f);
3109 shortcut.setScaleX(0f);
3110 shortcut.setScaleY(0f);
3111 mNewShortcutAnimatePage = item.screen;
3112 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3113 mNewShortcutAnimateViews.add(shortcut);
3114 }
3115 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003116 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003117 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003118 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003119 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003120 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003121 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3122 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003123 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003125 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003126
Joe Onorato9c1289c2009-08-17 11:03:03 -04003127 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003128 }
3129
Joe Onorato9c1289c2009-08-17 11:03:03 -04003130 /**
3131 * Implementation of the method from LauncherModel.Callbacks.
3132 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003133 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003134 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003135 sFolders.clear();
3136 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003138
3139 /**
3140 * Add the views for a widget to the workspace.
3141 *
3142 * Implementation of the method from LauncherModel.Callbacks.
3143 */
3144 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003145 setLoadOnResume();
3146
Daniel Sandler843e8602010-06-07 14:59:01 -04003147 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3148 if (DEBUG_WIDGETS) {
3149 Log.d(TAG, "bindAppWidget: " + item);
3150 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003151 final Workspace workspace = mWorkspace;
3152
3153 final int appWidgetId = item.appWidgetId;
3154 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003155 if (DEBUG_WIDGETS) {
3156 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3157 }
3158
Joe Onorato9c1289c2009-08-17 11:03:03 -04003159 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3160
Joe Onorato9c1289c2009-08-17 11:03:03 -04003161 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3162 item.hostView.setTag(item);
3163
Winson Chung3d503fb2011-07-13 17:25:49 -07003164 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003165 item.cellY, item.spanX, item.spanY, false);
3166
Adam Cohended9f8d2010-11-03 13:25:16 -07003167 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3168
Joe Onorato9c1289c2009-08-17 11:03:03 -04003169 workspace.requestLayout();
3170
Winson Chungf51025f2012-05-03 15:21:11 -07003171 AppWidgetResizeFrame.updateWidgetSizeRanges(item.hostView,
3172 this, item.spanX, item.spanY);
3173
Daniel Sandler843e8602010-06-07 14:59:01 -04003174 if (DEBUG_WIDGETS) {
3175 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3176 + (SystemClock.uptimeMillis()-start) + "ms");
3177 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003178 }
3179
3180 /**
3181 * Callback saying that there aren't any more items to bind.
3182 *
3183 * Implementation of the method from LauncherModel.Callbacks.
3184 */
3185 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003186 setLoadOnResume();
3187
Joe Onorato9c1289c2009-08-17 11:03:03 -04003188 if (mSavedState != null) {
3189 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003190 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003191 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003192 mSavedState = null;
3193 }
3194
3195 if (mSavedInstanceState != null) {
3196 super.onRestoreInstanceState(mSavedInstanceState);
3197 mSavedInstanceState = null;
3198 }
3199
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003200 // If we received the result of any pending adds while the loader was running (e.g. the
3201 // widget configuration forced an orientation change), process them now.
3202 for (int i = 0; i < sPendingAddList.size(); i++) {
3203 completeAdd(sPendingAddList.get(i));
3204 }
3205 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003206
Winson Chungc51db6a2011-10-05 11:44:49 -07003207 // Update the market app icon as necessary (the other icons will be managed in response to
3208 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003209 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003210
Winson Chungf0c6ae02012-03-21 16:10:31 -07003211 // Animate up any icons as necessary
3212 if (mVisible || mWorkspaceLoading) {
3213 Runnable newAppsRunnable = new Runnable() {
3214 @Override
3215 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003216 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003217 }
3218 };
Winson Chunga2413752012-04-03 14:22:34 -07003219
3220 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3221 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3222 if (canRunNewAppsAnimation()) {
3223 // If the user has not interacted recently, then either snap to the new page to show
3224 // the new-apps animation or just run them if they are to appear on the current page
3225 if (willSnapPage) {
3226 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3227 } else {
3228 runNewAppsAnimation(false);
3229 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003230 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003231 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003232 // are on another page (or just normally if they are added to the current page)
3233 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003234 }
3235 }
3236
3237 mWorkspaceLoading = false;
3238 }
3239
Winson Chunga2413752012-04-03 14:22:34 -07003240 private boolean canRunNewAppsAnimation() {
3241 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3242 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3243 }
3244
Winson Chungf0c6ae02012-03-21 16:10:31 -07003245 /**
3246 * Runs a new animation that scales up icons that were added while Launcher was in the
3247 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003248 *
3249 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003250 */
Winson Chunga2413752012-04-03 14:22:34 -07003251 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003252 AnimatorSet anim = new AnimatorSet();
3253 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003254
3255 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003256 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3257 @Override
3258 public int compare(View a, View b) {
3259 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3260 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3261 int cellCountX = LauncherModel.getCellCountX();
3262 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3263 }
3264 });
Winson Chunga2413752012-04-03 14:22:34 -07003265
3266 // Animate each of the views in place (or show them immediately if requested)
3267 if (immediate) {
3268 for (View v : mNewShortcutAnimateViews) {
3269 v.setAlpha(1f);
3270 v.setScaleX(1f);
3271 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003272 }
Winson Chunga2413752012-04-03 14:22:34 -07003273 } else {
3274 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3275 View v = mNewShortcutAnimateViews.get(i);
3276 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3277 PropertyValuesHolder.ofFloat("alpha", 1f),
3278 PropertyValuesHolder.ofFloat("scaleX", 1f),
3279 PropertyValuesHolder.ofFloat("scaleY", 1f));
3280 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3281 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3282 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3283 bounceAnims.add(bounceAnim);
3284 }
3285 anim.playTogether(bounceAnims);
3286 anim.addListener(new AnimatorListenerAdapter() {
3287 @Override
3288 public void onAnimationEnd(Animator animation) {
3289 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3290 }
3291 });
3292 anim.start();
3293 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003294
3295 // Clean up
3296 mNewShortcutAnimatePage = -1;
3297 mNewShortcutAnimateViews.clear();
3298 new Thread("clearNewAppsThread") {
3299 public void run() {
3300 mSharedPrefs.edit()
3301 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3302 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3303 .commit();
3304 }
3305 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003306 }
3307
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003308 @Override
3309 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003310 boolean searchVisible = updateGlobalSearchIcon();
3311 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3312 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003313 }
3314
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003315 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003316 * Add the icons for all apps.
3317 *
3318 * Implementation of the method from LauncherModel.Callbacks.
3319 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003320 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3321 // Remove the progress bar entirely; we could also make it GONE
3322 // but better to remove it since we know it's not going to be used
3323 View progressBar = mAppsCustomizeTabHost.
3324 findViewById(R.id.apps_customize_progress_bar);
3325 if (progressBar != null) {
3326 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003327 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003328 // We just post the call to setApps so the user sees the progress bar
3329 // disappear-- otherwise, it just looks like the progress bar froze
3330 // which doesn't look great
3331 mAppsCustomizeTabHost.post(new Runnable() {
3332 public void run() {
3333 if (mAppsCustomizeContent != null) {
3334 mAppsCustomizeContent.setApps(apps);
3335 }
3336 }
3337 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003338 }
3339
3340 /**
3341 * A package was installed.
3342 *
3343 * Implementation of the method from LauncherModel.Callbacks.
3344 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003345 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003346 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003347
Winson Chung785d2eb2011-04-14 16:08:02 -07003348 if (mAppsCustomizeContent != null) {
3349 mAppsCustomizeContent.addApps(apps);
3350 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003351 }
3352
3353 /**
3354 * A package was updated.
3355 *
3356 * Implementation of the method from LauncherModel.Callbacks.
3357 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003358 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003359 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003360 if (mWorkspace != null) {
3361 mWorkspace.updateShortcuts(apps);
3362 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003363
Winson Chung785d2eb2011-04-14 16:08:02 -07003364 if (mAppsCustomizeContent != null) {
3365 mAppsCustomizeContent.updateApps(apps);
3366 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003367 }
3368
3369 /**
3370 * A package was uninstalled.
3371 *
3372 * Implementation of the method from LauncherModel.Callbacks.
3373 */
Joe Onorato36115782010-06-17 13:28:48 -04003374 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003375 if (permanent) {
3376 mWorkspace.removeItems(apps);
3377 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003378
Winson Chung785d2eb2011-04-14 16:08:02 -07003379 if (mAppsCustomizeContent != null) {
3380 mAppsCustomizeContent.removeApps(apps);
3381 }
Winson Chunga1820962011-10-03 16:31:06 -07003382
3383 // Notify the drag controller
3384 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003385 }
Joe Onoratobe386092009-11-17 17:32:16 -08003386
3387 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003388 * A number of packages were updated.
3389 */
3390 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003391 if (mAppsCustomizeContent != null) {
3392 mAppsCustomizeContent.onPackagesUpdated();
3393 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003394 }
3395
Winson Chung400438b2011-01-16 17:53:48 -08003396 private int mapConfigurationOriActivityInfoOri(int configOri) {
3397 final Display d = getWindowManager().getDefaultDisplay();
3398 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3399 switch (d.getRotation()) {
3400 case Surface.ROTATION_0:
3401 case Surface.ROTATION_180:
3402 // We are currently in the same basic orientation as the natural orientation
3403 naturalOri = configOri;
3404 break;
3405 case Surface.ROTATION_90:
3406 case Surface.ROTATION_270:
3407 // We are currently in the other basic orientation to the natural orientation
3408 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3409 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3410 break;
3411 }
3412
3413 int[] oriMap = {
3414 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3415 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3416 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3417 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3418 };
3419 // Since the map starts at portrait, we need to offset if this device's natural orientation
3420 // is landscape.
3421 int indexOffset = 0;
3422 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3423 indexOffset = 1;
3424 }
3425 return oriMap[(d.getRotation() + indexOffset) % 4];
3426 }
Adam Cohen446e9402011-09-15 18:21:21 -07003427
Winson Chung4b919f82012-05-01 10:44:08 -07003428 public boolean isRotationEnabled() {
3429 boolean forceEnableRotation = "true".equalsIgnoreCase(SystemProperties.get(
3430 FORCE_ENABLE_ROTATION_PROPERTY, "false"));
3431 boolean enableRotation = forceEnableRotation ||
3432 getResources().getBoolean(R.bool.allow_rotation);
3433 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003434 }
Winson Chung4b919f82012-05-01 10:44:08 -07003435 public void lockScreenOrientation() {
3436 if (isRotationEnabled()) {
3437 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3438 .getConfiguration().orientation));
3439 }
3440 }
3441 public void unlockScreenOrientation(boolean immediate) {
3442 if (isRotationEnabled()) {
3443 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003444 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003445 } else {
3446 mHandler.postDelayed(new Runnable() {
3447 public void run() {
3448 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3449 }
3450 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003451 }
Winson Chung4b919f82012-05-01 10:44:08 -07003452 }
Winson Chung400438b2011-01-16 17:53:48 -08003453 }
3454
Winson Chung82f55532011-08-09 14:14:23 -07003455 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003456 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003457 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003458 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003459
Winson Chung7d7541e2011-09-16 20:14:36 -07003460 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003461 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003462 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3463 Cling cling = (Cling) findViewById(clingId);
3464 if (cling != null) {
3465 cling.init(this, positionData);
3466 cling.setVisibility(View.VISIBLE);
3467 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3468 if (animate) {
3469 cling.buildLayer();
3470 cling.setAlpha(0f);
3471 cling.animate()
3472 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003473 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003474 .setDuration(SHOW_CLING_DURATION)
3475 .setStartDelay(delay)
3476 .start();
3477 } else {
3478 cling.setAlpha(1f);
3479 }
3480 }
3481 return cling;
3482 }
3483 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003484 if (cling != null) {
3485 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003486 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003487 anim.addListener(new AnimatorListenerAdapter() {
3488 public void onAnimationEnd(Animator animation) {
3489 cling.setVisibility(View.GONE);
3490 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003491 // We should update the shared preferences on a background thread
3492 new Thread("dismissClingThread") {
3493 public void run() {
3494 SharedPreferences.Editor editor = mSharedPrefs.edit();
3495 editor.putBoolean(flag, true);
3496 editor.commit();
3497 }
3498 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003499 };
3500 });
3501 anim.start();
3502 }
3503 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003504 private void removeCling(int id) {
3505 final View cling = findViewById(id);
3506 if (cling != null) {
3507 final ViewGroup parent = (ViewGroup) cling.getParent();
3508 parent.post(new Runnable() {
3509 @Override
3510 public void run() {
3511 parent.removeView(cling);
3512 }
3513 });
3514 }
3515 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003516 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003517 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003518 if (isClingsEnabled() &&
3519 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003520 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003521 } else {
3522 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003523 }
3524 }
3525 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003526 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003527 if (isClingsEnabled() &&
3528 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003529 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003530 } else {
3531 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003532 }
3533 }
3534 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003535 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003536 if (isClingsEnabled() &&
3537 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3538 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003539 } else {
3540 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003541 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003542 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003543 }
3544 public boolean isFolderClingVisible() {
3545 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003546 if (cling != null) {
3547 return cling.getVisibility() == View.VISIBLE;
3548 }
3549 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003550 }
Winson Chung82f55532011-08-09 14:14:23 -07003551 public void dismissWorkspaceCling(View v) {
3552 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003553 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003554 }
3555 public void dismissAllAppsCling(View v) {
3556 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003557 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3558 }
3559 public void dismissFolderCling(View v) {
3560 Cling cling = (Cling) findViewById(R.id.folder_cling);
3561 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003562 }
3563
Winson Chung80baf5a2010-08-09 16:03:15 -07003564 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003565 * Prints out out state for debugging.
3566 */
3567 public void dumpState() {
3568 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003569 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003570 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3571 Log.d(TAG, "mRestoring=" + mRestoring);
3572 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3573 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003574 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003575 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003576
Winson Chung785d2eb2011-04-14 16:08:02 -07003577 if (mAppsCustomizeContent != null) {
3578 mAppsCustomizeContent.dumpState();
3579 }
Joe Onoratobe386092009-11-17 17:32:16 -08003580 Log.d(TAG, "END launcher2 dump state");
3581 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003582
3583 @Override
3584 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3585 super.dump(prefix, fd, writer, args);
3586 writer.println(" ");
3587 writer.println("Debug logs: ");
3588 for (int i = 0; i < sDumpLogs.size(); i++) {
3589 writer.println(" " + sDumpLogs.get(i));
3590 }
3591 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003592}
Michael Jurka2763be32011-02-24 11:19:57 -08003593
Michael Jurkaabded662011-03-04 12:06:57 -08003594interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003595 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003596 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003597 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003598 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003599 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003600}