blob: 91184c9f6e9d35d326c09e476e9f8cbeb45a98f5 [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);
Winson Chung211bac32012-05-15 13:43:57 -07001132 launcherInfo.notifyWidgetSizeChanged(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001133 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001134 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001135
1136 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001138 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 }
Romain Guycbb89e42009-06-08 15:52:54 -07001140
Adam Cohended9f8d2010-11-03 13:25:16 -07001141 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1142 @Override
1143 public void onReceive(Context context, Intent intent) {
1144 final String action = intent.getAction();
1145 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1146 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001147 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001148 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001149
Winson Chungc100e8e2011-08-09 16:02:43 -07001150 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001151 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001152 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1153 mAppsCustomizeTabHost.reset();
1154 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001155 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001156 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1157 mUserPresent = true;
1158 updateRunning();
1159 }
1160 }
1161 };
1162
1163 @Override
1164 public void onAttachedToWindow() {
1165 super.onAttachedToWindow();
1166
1167 // Listen for broadcasts related to user-presence
1168 final IntentFilter filter = new IntentFilter();
1169 filter.addAction(Intent.ACTION_SCREEN_OFF);
1170 filter.addAction(Intent.ACTION_USER_PRESENT);
1171 registerReceiver(mReceiver, filter);
1172
Adam Cohend113e0c2010-11-11 10:48:05 -08001173 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001174 mVisible = true;
1175 }
1176
1177 @Override
1178 public void onDetachedFromWindow() {
1179 super.onDetachedFromWindow();
1180 mVisible = false;
1181
Adam Cohend113e0c2010-11-11 10:48:05 -08001182 if (mAttached) {
1183 unregisterReceiver(mReceiver);
1184 mAttached = false;
1185 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001186 updateRunning();
1187 }
1188
1189 public void onWindowVisibilityChanged(int visibility) {
1190 mVisible = visibility == View.VISIBLE;
1191 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001192 // The following code used to be in onResume, but it turns out onResume is called when
1193 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1194 // is a more appropriate event to handle
1195 if (mVisible) {
1196 mAppsCustomizeTabHost.onWindowVisible();
1197 if (!mWorkspaceLoading) {
1198 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001199 // We want to let Launcher draw itself at least once before we force it to build
1200 // layers on all the workspace pages, so that transitioning to Launcher from other
1201 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1202 // a true draw so we wait until the second preDraw call to be safe
1203 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001204 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001205 // We delay the layer building a bit in order to give
1206 // other message processing a time to run. In particular
1207 // this avoids a delay in hiding the IME if it was
1208 // currently shown, because doing that may involve
1209 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001210 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001211
1212 // We had to enable the wallpaper visibility when launching apps from all
1213 // apps (so that the transitions would be the same as when launching from
1214 // workspace) so take this time to see if we need to re-disable the
1215 // wallpaper visibility to ensure performance.
1216 mWorkspace.post(new Runnable() {
1217 @Override
1218 public void run() {
Winson Chung18f41f82012-05-09 13:28:10 -07001219 disableWallpaperIfInAllApps();
Winson Chung31ce0732012-05-06 13:36:43 -07001220 }
1221 });
1222
Michael Jurka6ee21d22012-02-21 18:20:27 -08001223 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001224 return true;
1225 }
1226 });
1227 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001228 // When Launcher comes back to foreground, a different Activity might be responsible for
1229 // the app market intent, so refresh the icon
1230 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001231 clearTypedText();
1232 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001233 }
1234
1235 private void sendAdvanceMessage(long delay) {
1236 mHandler.removeMessages(ADVANCE_MSG);
1237 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1238 mHandler.sendMessageDelayed(msg, delay);
1239 mAutoAdvanceSentTime = System.currentTimeMillis();
1240 }
1241
1242 private void updateRunning() {
1243 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1244 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1245 mAutoAdvanceRunning = autoAdvanceRunning;
1246 if (autoAdvanceRunning) {
1247 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1248 sendAdvanceMessage(delay);
1249 } else {
1250 if (!mWidgetsToAdvance.isEmpty()) {
1251 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1252 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1253 }
1254 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001255 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001256 }
1257 }
1258 }
1259
1260 private final Handler mHandler = new Handler() {
1261 @Override
1262 public void handleMessage(Message msg) {
1263 if (msg.what == ADVANCE_MSG) {
1264 int i = 0;
1265 for (View key: mWidgetsToAdvance.keySet()) {
1266 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1267 final int delay = mAdvanceStagger * i;
1268 if (v instanceof Advanceable) {
1269 postDelayed(new Runnable() {
1270 public void run() {
1271 ((Advanceable) v).advance();
1272 }
1273 }, delay);
1274 }
1275 i++;
1276 }
1277 sendAdvanceMessage(mAdvanceInterval);
1278 }
1279 }
1280 };
1281
1282 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001283 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001284 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1285 if (v instanceof Advanceable) {
1286 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001287 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001288 updateRunning();
1289 }
1290 }
1291
1292 void removeWidgetToAutoAdvance(View hostView) {
1293 if (mWidgetsToAdvance.containsKey(hostView)) {
1294 mWidgetsToAdvance.remove(hostView);
1295 updateRunning();
1296 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001297 }
1298
Joe Onorato9c1289c2009-08-17 11:03:03 -04001299 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001300 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001301 launcherInfo.hostView = null;
1302 }
1303
Winson Chung93eef082012-03-23 15:59:27 -07001304 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1305 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1306 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001307 }
1308
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001309 public LauncherAppWidgetHost getAppWidgetHost() {
1310 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 }
Romain Guycbb89e42009-06-08 15:52:54 -07001312
Winson Chunga9abd0e2010-10-27 17:18:37 -07001313 public LauncherModel getModel() {
1314 return mModel;
1315 }
1316
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001317 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001318 getWindow().closeAllPanels();
1319
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001320 // Whatever we were doing is hereby canceled.
1321 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001322 }
1323
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 @Override
1325 protected void onNewIntent(Intent intent) {
1326 super.onNewIntent(intent);
1327
1328 // Close the menu
1329 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001330 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001331 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001332
Joe Onorato14f122b2009-11-19 14:06:36 -08001333 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1334 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001335
Adam Cohenac56cff2011-09-28 20:45:37 -07001336 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001337 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001338 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001339 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1340 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001341 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001342 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001343
1344 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001345 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001346 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001347
Joe Onorato3a8820b2009-11-10 15:06:42 -08001348 final View v = getWindow().peekDecorView();
1349 if (v != null && v.getWindowToken() != null) {
1350 InputMethodManager imm = (InputMethodManager)getSystemService(
1351 INPUT_METHOD_SERVICE);
1352 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001354
Michael Jurka35aa14d2011-07-07 17:01:08 -07001355 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001356 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001357 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001358 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
1360 }
1361
1362 @Override
1363 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1364 // Do not call super here
1365 mSavedInstanceState = savedInstanceState;
1366 }
1367
1368 @Override
1369 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001370 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001371 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372
Michael Jurka883f55b2010-10-21 15:47:14 -07001373 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001374 // We close any open folder since it will not be re-opened, and we need to make sure
1375 // this state is reflected.
1376 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377
Winson Chung3d503fb2011-07-13 17:25:49 -07001378 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1379 mWaitingForResult) {
1380 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1381 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1382 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1383 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001384 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1385 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1386 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 }
1388
1389 if (mFolderInfo != null && mWaitingForResult) {
1390 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1391 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1392 }
Michael Jurka946ad472010-07-09 18:05:18 -07001393
Winson Chung785d2eb2011-04-14 16:08:02 -07001394 // Save the current AppsCustomize tab
1395 if (mAppsCustomizeTabHost != null) {
1396 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1397 if (currentTabTag != null) {
1398 outState.putString("apps_customize_currentTab", currentTabTag);
1399 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001400 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1401 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001402 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 }
1404
1405 @Override
1406 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001408
Winson Chunge7a03942011-08-05 15:05:12 -07001409 // Remove all pending runnables
1410 mHandler.removeMessages(ADVANCE_MSG);
1411 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001412 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001413
Winson Chungcd2b0142011-06-08 16:02:26 -07001414 // Stop callbacks from LauncherModel
1415 LauncherApplication app = ((LauncherApplication) getApplication());
1416 mModel.stopLoader();
1417 app.setLauncher(null);
1418
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001420 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001422 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001424 mAppWidgetHost = null;
1425
1426 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427
1428 TextKeyListener.getInstance().release();
1429
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430
Winson Chung3d503fb2011-07-13 17:25:49 -07001431 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001432
1433 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001434 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001435
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001436 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001437 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1438 mWorkspace.removeAllViews();
1439 mWorkspace = null;
1440 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001441
1442 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 }
1444
Adam Cohena9cf38f2011-05-02 15:36:58 -07001445 public DragController getDragController() {
1446 return mDragController;
1447 }
1448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 @Override
1450 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001451 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 super.startActivityForResult(intent, requestCode);
1453 }
1454
Winson Chung70d72102011-08-12 11:24:35 -07001455 /**
1456 * Indicates that we want global search for this activity by setting the globalSearch
1457 * argument for {@link #startSearch} to true.
1458 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001460 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001462
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001463 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001464
Karl Rosaen138a0412009-04-23 19:00:21 -07001465 if (initialQuery == null) {
1466 // Use any text typed in the launcher as the initial query
1467 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 if (appSearchData == null) {
1470 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001471 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001473 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001474
Karl Rosaen138a0412009-04-23 19:00:21 -07001475 final SearchManager searchManager =
1476 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001477 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001478 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
1480
Winson Chung70d72102011-08-12 11:24:35 -07001481 @Override
1482 public boolean onCreateOptionsMenu(Menu menu) {
1483 if (isWorkspaceLocked()) {
1484 return false;
1485 }
1486
1487 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001488
1489 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1490 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1491 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001492 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1493 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1494 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001495 String helpUrl = getString(R.string.help_url);
1496 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001497 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1498 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1499
Winson Chung70d72102011-08-12 11:24:35 -07001500 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1501 .setIcon(android.R.drawable.ic_menu_gallery)
1502 .setAlphabeticShortcut('W');
1503 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1504 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001505 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001506 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001507 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1508 .setIcon(android.R.drawable.ic_menu_preferences)
1509 .setIntent(settings)
1510 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001511 if (!helpUrl.isEmpty()) {
1512 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1513 .setIcon(android.R.drawable.ic_menu_help)
1514 .setIntent(help)
1515 .setAlphabeticShortcut('H');
1516 }
Winson Chung70d72102011-08-12 11:24:35 -07001517 return true;
1518 }
1519
1520 @Override
1521 public boolean onPrepareOptionsMenu(Menu menu) {
1522 super.onPrepareOptionsMenu(menu);
1523
1524 if (mAppsCustomizeTabHost.isTransitioning()) {
1525 return false;
1526 }
1527 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1528 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1529
1530 return true;
1531 }
1532
1533 @Override
1534 public boolean onOptionsItemSelected(MenuItem item) {
1535 switch (item.getItemId()) {
1536 case MENU_WALLPAPER_SETTINGS:
1537 startWallpaper();
1538 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001539 }
1540
1541 return super.onOptionsItemSelected(item);
1542 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001544 @Override
1545 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001546 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001547 // Use a custom animation for launching search
1548 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001549 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001550 }
1551
Joe Onorato9c1289c2009-08-17 11:03:03 -04001552 public boolean isWorkspaceLocked() {
1553 return mWorkspaceLoading || mWaitingForResult;
1554 }
1555
Michael Jurka0280c3b2010-09-17 15:00:07 -07001556 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001557 mPendingAddInfo.container = ItemInfo.NO_ID;
1558 mPendingAddInfo.screen = -1;
1559 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1560 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001561 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001562 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001563 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001564
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001565 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1566 AppWidgetProviderInfo appWidgetInfo) {
1567 if (appWidgetInfo.configure != null) {
1568 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001569
Bjorn Bringert7984c942009-12-09 15:38:25 +00001570 // Launch over to configure widget, if needed
1571 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001572 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001573 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001574 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001576 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001577 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1578 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001579 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001580 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 }
1582 }
Romain Guycbb89e42009-06-08 15:52:54 -07001583
Adam Cohenfbba09b2011-07-18 21:43:05 -07001584 /**
1585 * Process a shortcut drop.
1586 *
1587 * @param componentName The name of the component
1588 * @param screen The screen where it should be added
1589 * @param cell The cell it should be added to, optional
1590 * @param position The location on the screen where it was dropped, optional
1591 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001592 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1593 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001594 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001595 mPendingAddInfo.container = container;
1596 mPendingAddInfo.screen = screen;
1597 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001598
1599 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001600 mPendingAddInfo.cellX = cell[0];
1601 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001602 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001603
1604 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1605 createShortcutIntent.setComponent(componentName);
1606 processShortcut(createShortcutIntent);
1607 }
1608
Adam Cohenfbba09b2011-07-18 21:43:05 -07001609 /**
1610 * Process a widget drop.
1611 *
1612 * @param info The PendingAppWidgetInfo of the widget being added.
1613 * @param screen The screen where it should be added
1614 * @param cell The cell it should be added to, optional
1615 * @param position The location on the screen where it was dropped, optional
1616 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001617 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001618 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001619 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001620 mPendingAddInfo.container = info.container = container;
1621 mPendingAddInfo.screen = info.screen = screen;
1622 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001623 mPendingAddInfo.minSpanX = info.minSpanX;
1624 mPendingAddInfo.minSpanY = info.minSpanY;
1625
Adam Cohenfbba09b2011-07-18 21:43:05 -07001626 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001627 mPendingAddInfo.cellX = cell[0];
1628 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001629 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001630 if (span != null) {
1631 mPendingAddInfo.spanX = span[0];
1632 mPendingAddInfo.spanY = span[1];
1633 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001634
Adam Cohened66b2b2012-01-23 17:28:51 -08001635 AppWidgetHostView hostView = info.boundWidget;
1636 int appWidgetId;
1637 if (hostView != null) {
1638 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001639 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001640 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001641 // In this case, we either need to start an activity to get permission to bind
1642 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001643 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001644 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001645 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001646 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001647 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001648 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1649 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1650 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1651 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1652 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001653 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001654 }
1655
Joe Onoratodeb98af2010-02-19 14:59:39 -08001656 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001657 // Handle case where user selected "Applications"
1658 String applicationName = getResources().getString(R.string.group_applications);
1659 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001660
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001661 if (applicationName != null && applicationName.equals(shortcutName)) {
1662 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1663 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001664
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001665 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1666 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001667 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001668 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001669 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001670 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001671 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 }
1673
Winson Chung24ab2f12010-09-16 14:10:47 -07001674 void processWallpaper(Intent intent) {
1675 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1676 }
1677
Winson Chung3d503fb2011-07-13 17:25:49 -07001678 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1679 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001680 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 folderInfo.title = getText(R.string.folder_name);
1682
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001684 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1685 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001686 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687
1688 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001689 FolderIcon newFolder =
1690 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1691 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1692 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001693 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 }
Romain Guycbb89e42009-06-08 15:52:54 -07001695
Joe Onorato9c1289c2009-08-17 11:03:03 -04001696 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001697 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001698 }
1699
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001701 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001703 Intent chooser = Intent.createChooser(pickWallpaper,
1704 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001705 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1706 // Removed in Eclair MR1
1707// WallpaperManager wm = (WallpaperManager)
1708// getSystemService(Context.WALLPAPER_SERVICE);
1709// WallpaperInfo wi = wm.getWallpaperInfo();
1710// if (wi != null && wi.getSettingsActivity() != null) {
1711// LabeledIntent li = new LabeledIntent(getPackageName(),
1712// R.string.configure_wallpaper, 0);
1713// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1714// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1715// }
Mike Clerona0618e42009-10-22 13:55:21 -07001716 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 }
1718
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001719 /**
1720 * Registers various content observers. The current implementation registers
1721 * only a favorites observer to keep track of the favorites applications.
1722 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001723 private void registerContentObservers() {
1724 ContentResolver resolver = getContentResolver();
1725 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1726 true, mWidgetObserver);
1727 }
1728
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 @Override
1730 public boolean dispatchKeyEvent(KeyEvent event) {
1731 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1732 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001734 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001735 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001736 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001737 dumpState();
1738 return true;
1739 }
1740 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001741 }
1742 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1743 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001744 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 return true;
1746 }
1747 }
1748
1749 return super.dispatchKeyEvent(event);
1750 }
1751
Joe Onorato88ec0992009-11-19 13:16:06 -08001752 @Override
1753 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001754 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001755 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001756 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001757 Folder openFolder = mWorkspace.getOpenFolder();
1758 if (openFolder.isEditingName()) {
1759 openFolder.dismissEditingName();
1760 } else {
1761 closeFolder();
1762 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001763 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001764 mWorkspace.exitWidgetResizeMode();
1765
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001766 // Back button is a no-op here, but give at least some feedback for the button press
1767 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001768 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001769 }
1770
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001772 * Re-listen when widgets are reset.
1773 */
1774 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001775 if (mAppWidgetHost != null) {
1776 mAppWidgetHost.startListening();
1777 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001778 }
1779
1780 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001781 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1782 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001784 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001785 if (mModel != null) {
1786 mModel.unbindWorkspaceItems();
1787 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001789
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 /**
1791 * Launches the intent referred by the clicked shortcut.
1792 *
1793 * @param v The view representing the clicked shortcut.
1794 */
1795 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001796 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1797 // view has detached (it's possible for this to happen if the view is removed mid touch).
1798 if (v.getWindowToken() == null) {
1799 return;
1800 }
1801
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001802 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001803 return;
1804 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001805
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001807 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001809 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001810 int[] pos = new int[2];
1811 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001812 intent.setSourceBounds(new Rect(pos[0], pos[1],
1813 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001814
1815 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001816
1817 if (success && v instanceof BubbleTextView) {
1818 mWaitingForResume = (BubbleTextView) v;
1819 mWaitingForResume.setStayPressed(true);
1820 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001822 if (v instanceof FolderIcon) {
1823 FolderIcon fi = (FolderIcon) v;
1824 handleFolderClick(fi);
1825 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001826 } else if (v == mAllAppsButton) {
1827 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001828 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001829 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001830 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 }
1833 }
1834
Michael Jurka0e260592010-06-30 17:07:39 -07001835 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001836 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001837 // clicking anywhere on the workspace causes the customization drawer to slide down
1838 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001839 return false;
1840 }
1841
Michael Jurkaaf442092010-06-10 17:01:57 -07001842 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001843 * Event handler for the search button
1844 *
1845 * @param v The view that was clicked.
1846 */
1847 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001848 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1849
Amith Yamasania135ba82011-08-09 17:42:01 -07001850 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001851 }
1852
1853 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001854 * Event handler for the voice button
1855 *
1856 * @param v The view that was clicked.
1857 */
1858 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001859 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001860
Michael Jurkaae65ee32012-04-30 11:45:54 -07001861 try {
1862 final SearchManager searchManager =
1863 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1864 ComponentName activityName = searchManager.getGlobalSearchActivity();
1865 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1866 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1867 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1868 if (activityName != null) {
1869 intent.setPackage(activityName.getPackageName());
1870 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001871 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07001872 } catch (ActivityNotFoundException e) {
1873 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1874 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1875 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1876 startActivitySafely(null, intent, "onClickVoiceButton");
1877 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001878 }
1879
1880 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001881 * Event handler for the "grid" button that appears on the home screen, which
1882 * enters all apps mode.
1883 *
1884 * @param v The view that was clicked.
1885 */
1886 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001887 showAllApps(true);
1888 }
1889
1890 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001891 // Provide the same haptic feedback that the system offers for virtual keys.
1892 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001893 }
1894
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001895 public void onClickAppMarketButton(View v) {
1896 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001897 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001898 } else {
1899 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001900 }
1901 }
1902
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001903 void startApplicationDetailsActivity(ComponentName componentName) {
1904 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001905 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1906 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001907 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001908 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001909 }
1910
Patrick Dubroy5539af72010-09-07 15:22:01 -07001911 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1912 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1913 // System applications cannot be installed. For now, show a toast explaining that.
1914 // We may give them the option of disabling apps this way.
1915 int messageId = R.string.uninstall_system_app_text;
1916 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1917 } else {
1918 String packageName = appInfo.componentName.getPackageName();
1919 String className = appInfo.componentName.getClassName();
1920 Intent intent = new Intent(
1921 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001922 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1923 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001924 startActivity(intent);
1925 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001926 }
1927
Michael Jurka86a720e2012-05-09 11:23:23 -07001928 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001930
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 try {
Winson Chung2672ff92012-05-04 16:22:30 -07001932 // Only launch using the new animation if the shortcut has not opted out (this is a
1933 // private contract between launcher and may be ignored in the future).
1934 boolean useLaunchAnimation = (v != null) &&
1935 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
1936 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07001937 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1938 v.getMeasuredWidth(), v.getMeasuredHeight());
1939
1940 startActivity(intent, opts.toBundle());
1941 } else {
1942 startActivity(intent);
1943 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001944 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 } catch (SecurityException e) {
1946 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001947 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001949 "or use the exported attribute for this activity. "
1950 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001952 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001954
Michael Jurka86a720e2012-05-09 11:23:23 -07001955 boolean startActivitySafely(View v, Intent intent, Object tag) {
1956 boolean success = false;
1957 try {
1958 success = startActivity(v, intent, tag);
1959 } catch (ActivityNotFoundException e) {
1960 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1961 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
1962 }
1963 return success;
1964 }
1965
Romain Guy8e633c52010-03-04 12:51:36 -08001966 void startActivityForResultSafely(Intent intent, int requestCode) {
1967 try {
1968 startActivityForResult(intent, requestCode);
1969 } catch (ActivityNotFoundException e) {
1970 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1971 } catch (SecurityException e) {
1972 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1973 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1974 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1975 "or use the exported attribute for this activity.", e);
1976 }
1977 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978
Adam Cohena9cf38f2011-05-02 15:36:58 -07001979 private void handleFolderClick(FolderIcon folderIcon) {
1980 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001981 Folder openFolder = mWorkspace.getFolderForTag(info);
1982
1983 // If the folder info reports that the associated folder is open, then verify that
1984 // it is actually opened. There have been a few instances where this gets out of sync.
1985 if (info.opened && openFolder == null) {
1986 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1987 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1988 info.opened = false;
1989 }
1990
Adam Cohena9cf38f2011-05-02 15:36:58 -07001991 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 // Close any open folder
1993 closeFolder();
1994 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001995 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 } else {
1997 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 int folderScreen;
1999 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002000 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 // .. and close it
2002 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002003 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 // Close any folder open on the current screen
2005 closeFolder();
2006 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002007 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 }
2009 }
2010 }
2011 }
2012
Adam Cohen2801caf2011-05-13 20:57:39 -07002013 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002014 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002015 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2016 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2017 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2018
Adam Cohenc51934b2011-07-26 21:07:43 -07002019 FolderInfo info = (FolderInfo) fi.getTag();
2020 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2021 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002022 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2023 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002024 }
2025
Adam Cohen2801caf2011-05-13 20:57:39 -07002026 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2027 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2028 oa.start();
2029 }
2030
2031 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002032 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002033 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2034 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2035 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2036
Adam Cohenc51934b2011-07-26 21:07:43 -07002037 FolderInfo info = (FolderInfo) fi.getTag();
2038 CellLayout cl = null;
2039 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2040 cl = (CellLayout) fi.getParent().getParent();
2041 }
2042
2043 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07002044 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2045 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002046 oa.addListener(new AnimatorListenerAdapter() {
2047 @Override
2048 public void onAnimationEnd(Animator animation) {
2049 if (layout != null) {
2050 layout.clearFolderLeaveBehind();
2051 }
2052 }
2053 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002054 oa.start();
2055 }
2056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002058 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 * is animated relative to the specified View. If the View is null, no animation
2060 * is played.
2061 *
2062 * @param folderInfo The FolderInfo describing the folder to open.
2063 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002064 public void openFolder(FolderIcon folderIcon) {
2065 Folder folder = folderIcon.mFolder;
2066 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067
Adam Cohen2801caf2011-05-13 20:57:39 -07002068 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002069 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070
Adam Cohen4554ee12011-08-03 16:13:21 -07002071 // Just verify that the folder hasn't already been added to the DragLayer.
2072 // There was a one-off crash where the folder had a parent already.
2073 if (folder.getParent() == null) {
2074 mDragLayer.addView(folder);
2075 mDragController.addDropTarget((DropTarget) folder);
2076 } else {
2077 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2078 folder.getParent() + ").");
2079 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002080 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002081 }
2082
2083 public void closeFolder() {
2084 Folder folder = mWorkspace.getOpenFolder();
2085 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002086 if (folder.isEditingName()) {
2087 folder.dismissEditingName();
2088 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002089 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002090
2091 // Dismiss the folder cling
2092 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002093 }
2094 }
2095
2096 void closeFolder(Folder folder) {
2097 folder.getInfo().opened = false;
2098
2099 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2100 if (parent != null) {
2101 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2102 shrinkAndFadeInFolderIcon(fi);
2103 }
2104 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 }
2106
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002108 if (!isDraggingEnabled()) return false;
2109 if (isWorkspaceLocked()) return false;
2110 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111
2112 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002113 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 }
2115
Michael Jurka0280c3b2010-09-17 15:00:07 -07002116 resetAddInfo();
2117 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002119 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 return true;
2121 }
2122
Winson Chung3d503fb2011-07-13 17:25:49 -07002123 // The hotseat touch handling does not go through Workspace, and we always allow long press
2124 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002125 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002126 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2127 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002128 if (itemUnderLongClick == null) {
2129 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002130 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2131 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002132 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002134 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002136 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 }
2138 }
2139 }
2140 return true;
2141 }
2142
Winson Chung3d503fb2011-07-13 17:25:49 -07002143 boolean isHotseatLayout(View layout) {
2144 return mHotseat != null && layout != null &&
2145 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2146 }
2147 Hotseat getHotseat() {
2148 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002149 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002150 SearchDropTargetBar getSearchBar() {
2151 return mSearchDropTargetBar;
2152 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002153
Winson Chung3d503fb2011-07-13 17:25:49 -07002154 /**
2155 * Returns the CellLayout of the specified container at the specified screen.
2156 */
2157 CellLayout getCellLayout(long container, int screen) {
2158 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2159 if (mHotseat != null) {
2160 return mHotseat.getLayout();
2161 } else {
2162 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002163 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002164 } else {
2165 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002166 }
2167 }
2168
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 Workspace getWorkspace() {
2170 return mWorkspace;
2171 }
2172
Daniel Sandler843e8602010-06-07 14:59:01 -04002173 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2174 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002175 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002176 }
2177
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002178 public boolean isAllAppsButtonRank(int rank) {
2179 return mHotseat.isAllAppsButtonRank(rank);
2180 }
2181
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002182 // AllAppsView.Watcher
2183 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002184 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002185 mWorkspace.setVisibility(View.GONE);
2186 }
2187 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002188
2189 /**
2190 * Helper method for the cameraZoomIn/cameraZoomOut animations
2191 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002192 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002193 * @param scaleFactor The scale factor used for the zoom
2194 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002195 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002196 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002197 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002198 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002199
Winson Chung18f41f82012-05-09 13:28:10 -07002200 void disableWallpaperIfInAllApps() {
2201 // Only disable it if we are in all apps
2202 if (mState == State.APPS_CUSTOMIZE) {
2203 if (mAppsCustomizeTabHost != null &&
2204 !mAppsCustomizeTabHost.isTransitioning()) {
2205 updateWallpaperVisibility(false);
2206 }
2207 }
2208 }
2209
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002210 void updateWallpaperVisibility(boolean visible) {
2211 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2212 int curflags = getWindow().getAttributes().flags
2213 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2214 if (wpflags != curflags) {
2215 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2216 }
2217 }
2218
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002219 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2220 if (v instanceof LauncherTransitionable) {
2221 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2222 }
2223 }
2224
Michael Jurkabed61d22012-02-14 22:51:29 -08002225 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2226 if (v instanceof LauncherTransitionable) {
2227 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2228 }
Winson Chung70442722012-02-10 15:43:22 -08002229
2230 // Update the workspace transition step as well
2231 dispatchOnLauncherTransitionStep(v, 0f);
2232 }
2233
2234 private void dispatchOnLauncherTransitionStep(View v, float t) {
2235 if (v instanceof LauncherTransitionable) {
2236 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2237 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002238 }
2239
2240 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2241 if (v instanceof LauncherTransitionable) {
2242 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2243 }
Winson Chung70442722012-02-10 15:43:22 -08002244
2245 // Update the workspace transition step as well
2246 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002247 }
2248
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002249 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002250 * Things to test when changing the following seven functions.
2251 * - Home from workspace
2252 * - from center screen
2253 * - from other screens
2254 * - Home from all apps
2255 * - from center screen
2256 * - from other screens
2257 * - Back from all apps
2258 * - from center screen
2259 * - from other screens
2260 * - Launch app from workspace and quit
2261 * - with back
2262 * - with home
2263 * - Launch app from all apps and quit
2264 * - with back
2265 * - with home
2266 * - Go to a screen that's not the default, then all
2267 * apps, and launch and app, and go back
2268 * - with back
2269 * -with home
2270 * - On workspace, long press power and go back
2271 * - with back
2272 * - with home
2273 * - On all apps, long press power and go back
2274 * - with back
2275 * - with home
2276 * - On workspace, power off
2277 * - On all apps, power off
2278 * - Launch an app and turn off the screen while in that app
2279 * - Go back with home key
2280 * - Go back with back key TODO: make this not go to workspace
2281 * - From all apps
2282 * - From workspace
2283 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2284 * - From all apps
2285 * - From the center workspace
2286 * - From another workspace
2287 */
2288
2289 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002290 * Zoom the camera out from the workspace to reveal 'toView'.
2291 * Assumes that the view to show is anchored at either the very top or very bottom
2292 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002293 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002294 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002295 if (mStateAnimation != null) {
2296 mStateAnimation.cancel();
2297 mStateAnimation = null;
2298 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002299 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002300
Winson Chungf0ea4d32011-06-06 14:27:16 -07002301 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2302 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2303 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002304 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002305 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002306 final int startDelay =
2307 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002308
Michael Jurkab3e22d92011-10-31 15:58:33 -07002309 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002310
Michael Jurkad74c9842011-07-10 12:44:21 -07002311 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002312 Animator workspaceAnim =
2313 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002314
2315 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002316 toView.setScaleX(scale);
2317 toView.setScaleY(scale);
2318 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2319 scaleAnim.
2320 scaleX(1f).scaleY(1f).
2321 setDuration(duration).
2322 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002323
Winson Chungf0ea4d32011-06-06 14:27:16 -07002324 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002325 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002326 final ObjectAnimator alphaAnim = ObjectAnimator
2327 .ofFloat(toView, "alpha", 0f, 1f)
2328 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002329 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002330 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2331 @Override
2332 public void onAnimationUpdate(ValueAnimator animation) {
2333 float t = (Float) animation.getAnimatedValue();
2334 dispatchOnLauncherTransitionStep(fromView, t);
2335 dispatchOnLauncherTransitionStep(toView, t);
2336 }
2337 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002338
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002339 // toView should appear right at the end of the workspace shrink
2340 // animation
2341 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002342 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002343 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002344
2345 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002346 boolean animationCancelled = false;
2347
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002348 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002349 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002350 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002351 // Prepare the position
2352 toView.setTranslationX(0.0f);
2353 toView.setTranslationY(0.0f);
2354 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002355 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002356 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002357 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002358 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002359 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2360 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002361
2362 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2363 // Hide the workspace scrollbar
2364 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002365 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002366 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002367 if (!animationCancelled) {
2368 updateWallpaperVisibility(false);
2369 }
2370 }
2371
Adam Cohencff6af82011-09-13 14:51:53 -07002372 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002373 public void onAnimationCancel(Animator animation) {
2374 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002375 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002376 });
2377
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002378 if (workspaceAnim != null) {
2379 mStateAnimation.play(workspaceAnim);
2380 }
Michael Jurka1899a362011-11-03 13:50:45 -07002381
2382 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002383 final ViewTreeObserver observer;
2384
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002385 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2386 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002387
2388 // If any of the objects being animated haven't been measured/laid out
2389 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002390 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002391 (mWorkspace.getMeasuredWidth() == 0) ||
2392 (toView.getMeasuredWidth() == 0)) {
2393 observer = mWorkspace.getViewTreeObserver();
2394 delayAnim = true;
2395 } else {
2396 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002397 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002398
2399 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002400 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002401 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2402 public void onGlobalLayout() {
2403 mWorkspace.post(new Runnable() {
2404 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002405 // Check that mStateAnimation hasn't changed while
2406 // we waited for a layout pass
2407 if (mStateAnimation == stateAnimation) {
2408 // Need to update pivots for zoom if layout changed
2409 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002410 dispatchOnLauncherTransitionStart(fromView, animated, false);
2411 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka49779a12012-01-16 04:10:08 -08002412 mStateAnimation.start();
2413 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002414 }
2415 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002416 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002417 }
2418 };
2419 observer.addOnGlobalLayoutListener(delayedStart);
2420 } else {
2421 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002422 dispatchOnLauncherTransitionStart(fromView, animated, false);
2423 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka1899a362011-11-03 13:50:45 -07002424 mStateAnimation.start();
2425 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002426 } else {
2427 toView.setTranslationX(0.0f);
2428 toView.setTranslationY(0.0f);
2429 toView.setScaleX(1.0f);
2430 toView.setScaleY(1.0f);
2431 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002432 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002433
Michael Jurkabed61d22012-02-14 22:51:29 -08002434 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2435 // Hide the workspace scrollbar
2436 mWorkspace.hideScrollingIndicator(true);
2437 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002438 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002439 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002440 dispatchOnLauncherTransitionStart(fromView, animated, false);
2441 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002442 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002443 dispatchOnLauncherTransitionStart(toView, animated, false);
2444 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002445 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002446 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002447 }
2448
2449 /**
2450 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002451 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002452 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002453 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002454 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2455 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002456
Michael Jurkab3e22d92011-10-31 15:58:33 -07002457 if (mStateAnimation != null) {
2458 mStateAnimation.cancel();
2459 mStateAnimation = null;
2460 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002461 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002462
Winson Chungf0ea4d32011-06-06 14:27:16 -07002463 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002464 final int fadeOutDuration =
2465 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002466 final float scaleFactor = (float)
2467 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2468 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002469 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002470 Animator workspaceAnim = null;
2471
2472 if (toState == State.WORKSPACE) {
2473 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2474 workspaceAnim = mWorkspace.getChangeStateAnimation(
2475 Workspace.State.NORMAL, animated, stagger);
2476 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2477 workspaceAnim = mWorkspace.getChangeStateAnimation(
2478 Workspace.State.SPRING_LOADED, animated);
2479 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002480
Michael Jurkab3e22d92011-10-31 15:58:33 -07002481 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002482 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002483 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002484 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002485 final LauncherViewPropertyAnimator scaleAnim =
2486 new LauncherViewPropertyAnimator(fromView);
2487 scaleAnim.
2488 scaleX(scaleFactor).scaleY(scaleFactor).
2489 setDuration(duration).
2490 setInterpolator(new Workspace.ZoomInInterpolator());
2491
2492 final ObjectAnimator alphaAnim = ObjectAnimator
2493 .ofFloat(fromView, "alpha", 1f, 0f)
2494 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002495 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002496 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2497 @Override
2498 public void onAnimationUpdate(ValueAnimator animation) {
2499 float t = 1f - (Float) animation.getAnimatedValue();
2500 dispatchOnLauncherTransitionStep(fromView, t);
2501 dispatchOnLauncherTransitionStep(toView, t);
2502 }
2503 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002504
Michael Jurkabed61d22012-02-14 22:51:29 -08002505 mStateAnimation = new AnimatorSet();
2506
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002507 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2508 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002509
2510 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002511 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002512 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002513 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002514 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002515 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2516 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002517 if (mWorkspace != null) {
2518 mWorkspace.hideScrollingIndicator(false);
2519 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002520 if (onCompleteRunnable != null) {
2521 onCompleteRunnable.run();
2522 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002523 }
2524 });
2525
Winson Chungf0ea4d32011-06-06 14:27:16 -07002526 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002527 if (workspaceAnim != null) {
2528 mStateAnimation.play(workspaceAnim);
2529 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002530 dispatchOnLauncherTransitionStart(fromView, animated, true);
2531 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002532 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002533 } else {
2534 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002535 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002536 dispatchOnLauncherTransitionStart(fromView, animated, true);
2537 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002538 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002539 dispatchOnLauncherTransitionStart(toView, animated, true);
2540 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002541 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002542 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002543 }
2544
Michael Jurkae326f182011-11-21 14:05:46 -08002545 @Override
2546 public void onTrimMemory(int level) {
2547 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002548 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002549 mAppsCustomizeTabHost.onTrimMemory();
2550 }
2551 }
2552
Winson Chung18f41f82012-05-09 13:28:10 -07002553 @Override
2554 public void onWindowFocusChanged(boolean hasFocus) {
2555 if (!hasFocus) {
2556 // When another window occludes launcher (like the notification shade, or recents),
2557 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2558 updateWallpaperVisibility(true);
2559 } else {
2560 // When launcher has focus again, disable the wallpaper if we are in AllApps
2561 disableWallpaperIfInAllApps();
2562 }
2563 }
2564
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002565 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002566 showWorkspace(animated, null);
2567 }
2568
2569 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002570 if (mState != State.WORKSPACE) {
2571 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002572 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002573
2574 // Show the search bar and hotseat
2575 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002576 // We only need to animate in the dock divider if we're going from spring loaded mode
2577 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002578
2579 // Set focus to the AppsCustomize button
2580 if (mAllAppsButton != null) {
2581 mAllAppsButton.requestFocus();
2582 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002583 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002584
Michael Jurkab737ee62011-11-15 15:57:22 -08002585 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002586
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002587 // Change the state *after* we've called all the transition code
2588 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002589
Winson Chung5fb63472011-02-02 17:03:37 -08002590 // Resume the auto-advance of widgets
2591 mUserPresent = true;
2592 updateRunning();
2593
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002594 // send an accessibility event to announce the context change
2595 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002596 }
2597
Michael Jurkab3e22d92011-10-31 15:58:33 -07002598 void showAllApps(boolean animated) {
2599 if (mState != State.WORKSPACE) return;
2600
2601 showAppsCustomizeHelper(animated, false);
2602 mAppsCustomizeTabHost.requestFocus();
2603
2604 // Hide the search bar and hotseat
2605 mSearchDropTargetBar.hideSearchBar(animated);
2606
2607 // Change the state *after* we've called all the transition code
2608 mState = State.APPS_CUSTOMIZE;
2609
2610 // Pause the auto-advance of widgets until we are out of AllApps
2611 mUserPresent = false;
2612 updateRunning();
2613 closeFolder();
2614
2615 // Send an accessibility event to announce the context change
2616 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2617 }
2618
Adam Cohen7777d962011-08-18 18:58:38 -07002619 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002620 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002621 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002622 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002623 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002624 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002625 }
Adam Cohen7777d962011-08-18 18:58:38 -07002626
Adam Cohened66b2b2012-01-23 17:28:51 -08002627 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2628 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002629 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2630
Winson Chunge7a03942011-08-05 15:05:12 -07002631 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002632 @Override
2633 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002634 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002635 // Before we show workspace, hide all apps again because
2636 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2637 // clean up our state transition functions
2638 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002639 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002640 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002641 } else {
2642 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002643 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002644 }
2645 }, (extendedDelay ?
2646 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2647 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2648 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002649
Michael Jurkad3ef3062010-11-23 16:23:58 -08002650 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002651 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002652 final boolean animated = true;
2653 final boolean springLoaded = true;
2654 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002655 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002656 }
2657 // Otherwise, we are not in spring loaded mode, so don't do anything.
2658 }
2659
Michael Jurkab737ee62011-11-15 15:57:22 -08002660 void hideDockDivider() {
2661 if (mQsbDivider != null && mDockDivider != null) {
2662 mQsbDivider.setVisibility(View.INVISIBLE);
2663 mDockDivider.setVisibility(View.INVISIBLE);
2664 }
2665 }
2666
2667 void showDockDivider(boolean animated) {
2668 if (mQsbDivider != null && mDockDivider != null) {
2669 mQsbDivider.setVisibility(View.VISIBLE);
2670 mDockDivider.setVisibility(View.VISIBLE);
2671 if (mDividerAnimator != null) {
2672 mDividerAnimator.cancel();
2673 mQsbDivider.setAlpha(1f);
2674 mDockDivider.setAlpha(1f);
2675 mDividerAnimator = null;
2676 }
2677 if (animated) {
2678 mDividerAnimator = new AnimatorSet();
2679 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2680 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2681 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2682 mDividerAnimator.start();
2683 }
2684 }
2685 }
2686
Michael Jurkab3e22d92011-10-31 15:58:33 -07002687 void lockAllApps() {
2688 // TODO
2689 }
2690
2691 void unlockAllApps() {
2692 // TODO
2693 }
2694
Adam Cohenfc53cd22011-07-20 15:45:11 -07002695 public boolean isAllAppsCustomizeOpen() {
2696 return mState == State.APPS_CUSTOMIZE;
2697 }
2698
Winson Chungf0ea4d32011-06-06 14:27:16 -07002699 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002700 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002701 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002702 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002703 if (!LauncherApplication.isScreenLarge()) {
2704 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002705 if (mHotseat.getAlpha() != 1f) {
2706 int duration = mSearchDropTargetBar.getTransitionInDuration();
2707 mHotseat.animate().alpha(1f).setDuration(duration);
2708 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002709 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002710 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002711 }
2712 }
2713 }
2714
2715 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002716 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002717 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002718 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002719 if (!LauncherApplication.isScreenLarge()) {
2720 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002721 if (mHotseat.getAlpha() != 0f) {
2722 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2723 mHotseat.animate().alpha(0f).setDuration(duration);
2724 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002725 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002726 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002727 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002728 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002729 }
2730
Patrick Dubroy5f445422011-02-18 14:35:21 -08002731 /**
2732 * Add an item from all apps or customize onto the given workspace screen.
2733 * If layout is null, add to the current screen.
2734 */
2735 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002736 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002737 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002738 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002739 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002740
Winson Chungdff8ebb2011-09-08 17:25:31 -07002741 /** Maps the current orientation to an index for referencing orientation correct global icons */
2742 private int getCurrentOrientationIndexForGlobalIcons() {
2743 // default - 0, landscape - 1
2744 switch (getResources().getConfiguration().orientation) {
2745 case Configuration.ORIENTATION_LANDSCAPE:
2746 return 1;
2747 default:
2748 return 0;
2749 }
2750 }
2751
Michael Jurkaae65ee32012-04-30 11:45:54 -07002752 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002753 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002754 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002755 // Look for the toolbar icon specified in the activity meta-data
2756 Bundle metaData = packageManager.getActivityInfo(
2757 activityName, PackageManager.GET_META_DATA).metaData;
2758 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002759 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002760 if (iconResId != 0) {
2761 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002762 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002763 }
2764 }
2765 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002766 // This can happen if the activity defines an invalid drawable
2767 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2768 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002769 } catch (Resources.NotFoundException nfe) {
2770 // This can happen if the activity defines an invalid drawable
2771 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2772 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002773 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002774 return null;
2775 }
2776
2777 // if successful in getting icon, return it; otherwise, set button to use default drawable
2778 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002779 int buttonId, ComponentName activityName, int fallbackDrawableId,
2780 String toolbarResourceName) {
2781 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002782 Resources r = getResources();
2783 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2784 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002785
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002786 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002787 // If we were unable to find the icon via the meta-data, use a generic one
2788 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002789 toolbarIcon = r.getDrawable(fallbackDrawableId);
2790 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002791 if (button != null) {
2792 button.setCompoundDrawables(toolbarIcon, null, null, null);
2793 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002794 return null;
2795 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002796 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002797 if (button != null) {
2798 button.setCompoundDrawables(toolbarIcon, null, null, null);
2799 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002800 return toolbarIcon.getConstantState();
2801 }
2802 }
2803
2804 // if successful in getting icon, return it; otherwise, set button to use default drawable
2805 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002806 int buttonId, ComponentName activityName, int fallbackDrawableId,
2807 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002808 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002809 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002810
Michael Jurka19e0fc52011-07-22 18:00:21 -07002811 if (button != null) {
2812 // If we were unable to find the icon via the meta-data, use a
2813 // generic one
2814 if (toolbarIcon == null) {
2815 button.setImageResource(fallbackDrawableId);
2816 } else {
2817 button.setImageDrawable(toolbarIcon);
2818 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002819 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002820
2821 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2822
Michael Jurka0423dcf2010-10-05 14:56:18 -07002823 }
2824
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002825 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2826 TextView button = (TextView) findViewById(buttonId);
2827 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2828 }
2829
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002830 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002831 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002832 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002833 }
2834
Winson Chungbb185bd2011-11-21 12:31:42 -08002835 private void invalidatePressedFocusedStates(View container, View button) {
2836 if (container instanceof HolographicLinearLayout) {
2837 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2838 layout.invalidatePressedFocusedStates();
2839 } else if (button instanceof HolographicImageView) {
2840 HolographicImageView view = (HolographicImageView) button;
2841 view.invalidatePressedFocusedStates();
2842 }
2843 }
2844
Winson Chungc51db6a2011-10-05 11:44:49 -07002845 private boolean updateGlobalSearchIcon() {
2846 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002847 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2848 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002849 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002850 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002851 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002852
Winson Chung1cad91e2011-05-25 17:41:01 -07002853 final SearchManager searchManager =
2854 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2855 ComponentName activityName = searchManager.getGlobalSearchActivity();
2856 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002857 int coi = getCurrentOrientationIndexForGlobalIcons();
2858 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002859 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2860 TOOLBAR_SEARCH_ICON_METADATA_NAME);
2861 if (sGlobalSearchIcon[coi] == null) {
2862 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2863 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2864 TOOLBAR_ICON_METADATA_NAME);
2865 }
2866
Winson Chung649723c2011-07-06 20:41:23 -07002867 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002868 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2869 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002870 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002871 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002872 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002873 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002874 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002875 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2876 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2877 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002878 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07002879 if (voiceButtonProxy != null) {
2880 voiceButtonProxy.setVisibility(View.GONE);
2881 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002882 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002883 }
2884 }
2885
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002886 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002887 final View searchButtonContainer = findViewById(R.id.search_button_container);
2888 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002889 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002890 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002891 }
2892
Winson Chungc51db6a2011-10-05 11:44:49 -07002893 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002894 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002895 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002896 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002897 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002898
Winson Chungc51db6a2011-10-05 11:44:49 -07002899 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07002900 final SearchManager searchManager =
2901 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2902 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2903
2904 ComponentName activityName = null;
2905 if (globalSearchActivity != null) {
2906 // Check if the global search activity handles voice search
2907 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2908 intent.setPackage(globalSearchActivity.getPackageName());
2909 activityName = intent.resolveActivity(getPackageManager());
2910 }
2911
2912 if (activityName == null) {
2913 // Fallback: check if an activity other than the global search activity
2914 // resolves this
2915 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2916 activityName = intent.resolveActivity(getPackageManager());
2917 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002918 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002919 int coi = getCurrentOrientationIndexForGlobalIcons();
2920 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002921 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2922 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
2923 if (sVoiceSearchIcon[coi] == null) {
2924 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2925 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2926 TOOLBAR_ICON_METADATA_NAME);
2927 }
Winson Chung649723c2011-07-06 20:41:23 -07002928 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002929 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002930 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07002931 if (voiceButtonProxy != null) {
2932 voiceButtonProxy.setVisibility(View.VISIBLE);
2933 }
Winson Chungbb185bd2011-11-21 12:31:42 -08002934 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002935 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002936 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002937 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002938 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002939 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07002940 if (voiceButtonProxy != null) {
2941 voiceButtonProxy.setVisibility(View.GONE);
2942 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002943 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002944 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002945 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002946
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002947 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002948 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2949 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002950 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002951 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002952 }
2953
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002954 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002955 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002956 */
2957 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002958 final View marketButton = findViewById(R.id.market_button);
2959 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2960 // Find the app market activity by resolving an intent.
2961 // (If multiple app markets are installed, it will return the ResolverActivity.)
2962 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002963 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002964 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002965 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002966 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002967 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
2968 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07002969 marketButton.setVisibility(View.VISIBLE);
2970 } else {
2971 // We should hide and disable the view so that we don't try and restore the visibility
2972 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2973 marketButton.setVisibility(View.GONE);
2974 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002975 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002976 }
2977
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002978 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002979 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002980 }
2981
Michael Jurkad7c28052012-04-27 15:43:36 -07002982 @Override
2983 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2984 boolean result = super.dispatchPopulateAccessibilityEvent(event);
2985 final List<CharSequence> text = event.getText();
2986 text.clear();
2987 text.add(getString(R.string.home));
2988 return result;
2989 }
2990
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002991 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002992 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002993 */
2994 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2995 @Override
2996 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002997 closeSystemDialogs();
2998 }
2999 }
3000
3001 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003002 * Receives notifications whenever the appwidgets are reset.
3003 */
3004 private class AppWidgetResetObserver extends ContentObserver {
3005 public AppWidgetResetObserver() {
3006 super(new Handler());
3007 }
3008
3009 @Override
3010 public void onChange(boolean selfChange) {
3011 onAppWidgetReset();
3012 }
3013 }
3014
3015 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003016 * If the activity is currently paused, signal that we need to re-run the loader
3017 * in onResume.
3018 *
3019 * This needs to be called from incoming places where resources might have been loaded
3020 * while we are paused. That is becaues the Configuration might be wrong
3021 * when we're not running, and if it comes back to what it was when we
3022 * were paused, we are not restarted.
3023 *
3024 * Implementation of the method from LauncherModel.Callbacks.
3025 *
3026 * @return true if we are currently paused. The caller might be able to
3027 * skip some work in that case since we will come back again.
3028 */
3029 public boolean setLoadOnResume() {
3030 if (mPaused) {
3031 Log.i(TAG, "setLoadOnResume");
3032 mOnResumeNeedsLoad = true;
3033 return true;
3034 } else {
3035 return false;
3036 }
3037 }
3038
3039 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003040 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003041 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003042 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003043 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003044 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003045 } else {
3046 return SCREEN_COUNT / 2;
3047 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003048 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003049
Joe Onorato9c1289c2009-08-17 11:03:03 -04003050 /**
3051 * Refreshes the shortcuts shown on the workspace.
3052 *
3053 * Implementation of the method from LauncherModel.Callbacks.
3054 */
3055 public void startBinding() {
3056 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003057
Winson Chungf0c6ae02012-03-21 16:10:31 -07003058 mNewShortcutAnimatePage = -1;
3059 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003060 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003061 int count = workspace.getChildCount();
3062 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003063 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003064 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3065 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003066 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003067 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003068 if (mHotseat != null) {
3069 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003070 }
3071 }
3072
3073 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003074 * Bind the items start-end from the list.
3075 *
3076 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003077 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003078 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003079 setLoadOnResume();
3080
Winson Chungf0c6ae02012-03-21 16:10:31 -07003081 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3082 Set<String> newApps = new HashSet<String>();
3083 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3084
3085 Workspace workspace = mWorkspace;
3086 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003087 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003088
3089 // Short circuit if we are loading dock items for a configuration which has no dock
3090 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3091 mHotseat == null) {
3092 continue;
3093 }
3094
Joe Onorato9c1289c2009-08-17 11:03:03 -04003095 switch (item.itemType) {
3096 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3097 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003098 ShortcutInfo info = (ShortcutInfo) item;
3099 String uri = info.intent.toUri(0).toString();
3100 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003101 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3102 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003103 if (newApps.contains(uri)) {
3104 newApps.remove(uri);
3105
3106 // Prepare the view to be animated up
3107 shortcut.setAlpha(0f);
3108 shortcut.setScaleX(0f);
3109 shortcut.setScaleY(0f);
3110 mNewShortcutAnimatePage = item.screen;
3111 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3112 mNewShortcutAnimateViews.add(shortcut);
3113 }
3114 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003115 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003116 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003117 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003118 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003119 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003120 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3121 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003122 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003123 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003124 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003125
Joe Onorato9c1289c2009-08-17 11:03:03 -04003126 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003127 }
3128
Joe Onorato9c1289c2009-08-17 11:03:03 -04003129 /**
3130 * Implementation of the method from LauncherModel.Callbacks.
3131 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003132 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003133 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003134 sFolders.clear();
3135 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003136 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003137
3138 /**
3139 * Add the views for a widget to the workspace.
3140 *
3141 * Implementation of the method from LauncherModel.Callbacks.
3142 */
3143 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003144 setLoadOnResume();
3145
Daniel Sandler843e8602010-06-07 14:59:01 -04003146 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3147 if (DEBUG_WIDGETS) {
3148 Log.d(TAG, "bindAppWidget: " + item);
3149 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003150 final Workspace workspace = mWorkspace;
3151
3152 final int appWidgetId = item.appWidgetId;
3153 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003154 if (DEBUG_WIDGETS) {
3155 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3156 }
3157
Joe Onorato9c1289c2009-08-17 11:03:03 -04003158 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3159
Joe Onorato9c1289c2009-08-17 11:03:03 -04003160 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3161 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003162 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003163
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);
Adam Cohended9f8d2010-11-03 13:25:16 -07003166 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3167
Joe Onorato9c1289c2009-08-17 11:03:03 -04003168 workspace.requestLayout();
3169
Daniel Sandler843e8602010-06-07 14:59:01 -04003170 if (DEBUG_WIDGETS) {
3171 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3172 + (SystemClock.uptimeMillis()-start) + "ms");
3173 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003174 }
3175
3176 /**
3177 * Callback saying that there aren't any more items to bind.
3178 *
3179 * Implementation of the method from LauncherModel.Callbacks.
3180 */
3181 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003182 setLoadOnResume();
3183
Joe Onorato9c1289c2009-08-17 11:03:03 -04003184 if (mSavedState != null) {
3185 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003186 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003187 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003188 mSavedState = null;
3189 }
3190
3191 if (mSavedInstanceState != null) {
3192 super.onRestoreInstanceState(mSavedInstanceState);
3193 mSavedInstanceState = null;
3194 }
3195
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003196 // If we received the result of any pending adds while the loader was running (e.g. the
3197 // widget configuration forced an orientation change), process them now.
3198 for (int i = 0; i < sPendingAddList.size(); i++) {
3199 completeAdd(sPendingAddList.get(i));
3200 }
3201 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003202
Winson Chungc51db6a2011-10-05 11:44:49 -07003203 // Update the market app icon as necessary (the other icons will be managed in response to
3204 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003205 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003206
Winson Chungf0c6ae02012-03-21 16:10:31 -07003207 // Animate up any icons as necessary
3208 if (mVisible || mWorkspaceLoading) {
3209 Runnable newAppsRunnable = new Runnable() {
3210 @Override
3211 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003212 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003213 }
3214 };
Winson Chunga2413752012-04-03 14:22:34 -07003215
3216 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3217 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3218 if (canRunNewAppsAnimation()) {
3219 // If the user has not interacted recently, then either snap to the new page to show
3220 // the new-apps animation or just run them if they are to appear on the current page
3221 if (willSnapPage) {
3222 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3223 } else {
3224 runNewAppsAnimation(false);
3225 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003226 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003227 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003228 // are on another page (or just normally if they are added to the current page)
3229 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003230 }
3231 }
3232
3233 mWorkspaceLoading = false;
3234 }
3235
Winson Chunga2413752012-04-03 14:22:34 -07003236 private boolean canRunNewAppsAnimation() {
3237 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3238 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3239 }
3240
Winson Chungf0c6ae02012-03-21 16:10:31 -07003241 /**
3242 * Runs a new animation that scales up icons that were added while Launcher was in the
3243 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003244 *
3245 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003246 */
Winson Chunga2413752012-04-03 14:22:34 -07003247 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003248 AnimatorSet anim = new AnimatorSet();
3249 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003250
3251 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003252 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3253 @Override
3254 public int compare(View a, View b) {
3255 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3256 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3257 int cellCountX = LauncherModel.getCellCountX();
3258 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3259 }
3260 });
Winson Chunga2413752012-04-03 14:22:34 -07003261
3262 // Animate each of the views in place (or show them immediately if requested)
3263 if (immediate) {
3264 for (View v : mNewShortcutAnimateViews) {
3265 v.setAlpha(1f);
3266 v.setScaleX(1f);
3267 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003268 }
Winson Chunga2413752012-04-03 14:22:34 -07003269 } else {
3270 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3271 View v = mNewShortcutAnimateViews.get(i);
3272 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3273 PropertyValuesHolder.ofFloat("alpha", 1f),
3274 PropertyValuesHolder.ofFloat("scaleX", 1f),
3275 PropertyValuesHolder.ofFloat("scaleY", 1f));
3276 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3277 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3278 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3279 bounceAnims.add(bounceAnim);
3280 }
3281 anim.playTogether(bounceAnims);
3282 anim.addListener(new AnimatorListenerAdapter() {
3283 @Override
3284 public void onAnimationEnd(Animator animation) {
3285 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3286 }
3287 });
3288 anim.start();
3289 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003290
3291 // Clean up
3292 mNewShortcutAnimatePage = -1;
3293 mNewShortcutAnimateViews.clear();
3294 new Thread("clearNewAppsThread") {
3295 public void run() {
3296 mSharedPrefs.edit()
3297 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3298 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3299 .commit();
3300 }
3301 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003302 }
3303
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003304 @Override
3305 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003306 boolean searchVisible = updateGlobalSearchIcon();
3307 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3308 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003309 }
3310
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003311 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003312 * Add the icons for all apps.
3313 *
3314 * Implementation of the method from LauncherModel.Callbacks.
3315 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003316 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3317 // Remove the progress bar entirely; we could also make it GONE
3318 // but better to remove it since we know it's not going to be used
3319 View progressBar = mAppsCustomizeTabHost.
3320 findViewById(R.id.apps_customize_progress_bar);
3321 if (progressBar != null) {
3322 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003323 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003324 // We just post the call to setApps so the user sees the progress bar
3325 // disappear-- otherwise, it just looks like the progress bar froze
3326 // which doesn't look great
3327 mAppsCustomizeTabHost.post(new Runnable() {
3328 public void run() {
3329 if (mAppsCustomizeContent != null) {
3330 mAppsCustomizeContent.setApps(apps);
3331 }
3332 }
3333 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003334 }
3335
3336 /**
3337 * A package was installed.
3338 *
3339 * Implementation of the method from LauncherModel.Callbacks.
3340 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003341 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003342 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003343
Winson Chung785d2eb2011-04-14 16:08:02 -07003344 if (mAppsCustomizeContent != null) {
3345 mAppsCustomizeContent.addApps(apps);
3346 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003347 }
3348
3349 /**
3350 * A package was updated.
3351 *
3352 * Implementation of the method from LauncherModel.Callbacks.
3353 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003354 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003355 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003356 if (mWorkspace != null) {
3357 mWorkspace.updateShortcuts(apps);
3358 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003359
Winson Chung785d2eb2011-04-14 16:08:02 -07003360 if (mAppsCustomizeContent != null) {
3361 mAppsCustomizeContent.updateApps(apps);
3362 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003363 }
3364
3365 /**
3366 * A package was uninstalled.
3367 *
3368 * Implementation of the method from LauncherModel.Callbacks.
3369 */
Joe Onorato36115782010-06-17 13:28:48 -04003370 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003371 if (permanent) {
3372 mWorkspace.removeItems(apps);
3373 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003374
Winson Chung785d2eb2011-04-14 16:08:02 -07003375 if (mAppsCustomizeContent != null) {
3376 mAppsCustomizeContent.removeApps(apps);
3377 }
Winson Chunga1820962011-10-03 16:31:06 -07003378
3379 // Notify the drag controller
3380 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003381 }
Joe Onoratobe386092009-11-17 17:32:16 -08003382
3383 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003384 * A number of packages were updated.
3385 */
3386 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003387 if (mAppsCustomizeContent != null) {
3388 mAppsCustomizeContent.onPackagesUpdated();
3389 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003390 }
3391
Winson Chung400438b2011-01-16 17:53:48 -08003392 private int mapConfigurationOriActivityInfoOri(int configOri) {
3393 final Display d = getWindowManager().getDefaultDisplay();
3394 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3395 switch (d.getRotation()) {
3396 case Surface.ROTATION_0:
3397 case Surface.ROTATION_180:
3398 // We are currently in the same basic orientation as the natural orientation
3399 naturalOri = configOri;
3400 break;
3401 case Surface.ROTATION_90:
3402 case Surface.ROTATION_270:
3403 // We are currently in the other basic orientation to the natural orientation
3404 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3405 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3406 break;
3407 }
3408
3409 int[] oriMap = {
3410 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3411 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3412 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3413 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3414 };
3415 // Since the map starts at portrait, we need to offset if this device's natural orientation
3416 // is landscape.
3417 int indexOffset = 0;
3418 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3419 indexOffset = 1;
3420 }
3421 return oriMap[(d.getRotation() + indexOffset) % 4];
3422 }
Adam Cohen446e9402011-09-15 18:21:21 -07003423
Winson Chung4b919f82012-05-01 10:44:08 -07003424 public boolean isRotationEnabled() {
3425 boolean forceEnableRotation = "true".equalsIgnoreCase(SystemProperties.get(
3426 FORCE_ENABLE_ROTATION_PROPERTY, "false"));
3427 boolean enableRotation = forceEnableRotation ||
3428 getResources().getBoolean(R.bool.allow_rotation);
3429 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003430 }
Winson Chung4b919f82012-05-01 10:44:08 -07003431 public void lockScreenOrientation() {
3432 if (isRotationEnabled()) {
3433 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3434 .getConfiguration().orientation));
3435 }
3436 }
3437 public void unlockScreenOrientation(boolean immediate) {
3438 if (isRotationEnabled()) {
3439 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003440 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003441 } else {
3442 mHandler.postDelayed(new Runnable() {
3443 public void run() {
3444 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3445 }
3446 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003447 }
Winson Chung4b919f82012-05-01 10:44:08 -07003448 }
Winson Chung400438b2011-01-16 17:53:48 -08003449 }
3450
Winson Chung82f55532011-08-09 14:14:23 -07003451 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003452 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003453 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003454 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003455
Winson Chung7d7541e2011-09-16 20:14:36 -07003456 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003457 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003458 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3459 Cling cling = (Cling) findViewById(clingId);
3460 if (cling != null) {
3461 cling.init(this, positionData);
3462 cling.setVisibility(View.VISIBLE);
3463 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3464 if (animate) {
3465 cling.buildLayer();
3466 cling.setAlpha(0f);
3467 cling.animate()
3468 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003469 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003470 .setDuration(SHOW_CLING_DURATION)
3471 .setStartDelay(delay)
3472 .start();
3473 } else {
3474 cling.setAlpha(1f);
3475 }
3476 }
3477 return cling;
3478 }
3479 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003480 if (cling != null) {
3481 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003482 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003483 anim.addListener(new AnimatorListenerAdapter() {
3484 public void onAnimationEnd(Animator animation) {
3485 cling.setVisibility(View.GONE);
3486 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003487 // We should update the shared preferences on a background thread
3488 new Thread("dismissClingThread") {
3489 public void run() {
3490 SharedPreferences.Editor editor = mSharedPrefs.edit();
3491 editor.putBoolean(flag, true);
3492 editor.commit();
3493 }
3494 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003495 };
3496 });
3497 anim.start();
3498 }
3499 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003500 private void removeCling(int id) {
3501 final View cling = findViewById(id);
3502 if (cling != null) {
3503 final ViewGroup parent = (ViewGroup) cling.getParent();
3504 parent.post(new Runnable() {
3505 @Override
3506 public void run() {
3507 parent.removeView(cling);
3508 }
3509 });
3510 }
3511 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003512 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003513 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003514 if (isClingsEnabled() &&
3515 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003516 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003517 } else {
3518 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003519 }
3520 }
3521 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003522 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003523 if (isClingsEnabled() &&
3524 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003525 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003526 } else {
3527 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003528 }
3529 }
3530 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003531 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003532 if (isClingsEnabled() &&
3533 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3534 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003535 } else {
3536 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003537 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003538 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003539 }
3540 public boolean isFolderClingVisible() {
3541 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003542 if (cling != null) {
3543 return cling.getVisibility() == View.VISIBLE;
3544 }
3545 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003546 }
Winson Chung82f55532011-08-09 14:14:23 -07003547 public void dismissWorkspaceCling(View v) {
3548 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003549 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003550 }
3551 public void dismissAllAppsCling(View v) {
3552 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003553 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3554 }
3555 public void dismissFolderCling(View v) {
3556 Cling cling = (Cling) findViewById(R.id.folder_cling);
3557 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003558 }
3559
Winson Chung80baf5a2010-08-09 16:03:15 -07003560 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003561 * Prints out out state for debugging.
3562 */
3563 public void dumpState() {
3564 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003565 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003566 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3567 Log.d(TAG, "mRestoring=" + mRestoring);
3568 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3569 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003570 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003571 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003572
Winson Chung785d2eb2011-04-14 16:08:02 -07003573 if (mAppsCustomizeContent != null) {
3574 mAppsCustomizeContent.dumpState();
3575 }
Joe Onoratobe386092009-11-17 17:32:16 -08003576 Log.d(TAG, "END launcher2 dump state");
3577 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003578
3579 @Override
3580 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3581 super.dump(prefix, fd, writer, args);
3582 writer.println(" ");
3583 writer.println("Debug logs: ");
3584 for (int i = 0; i < sDumpLogs.size(); i++) {
3585 writer.println(" " + sDumpLogs.get(i));
3586 }
3587 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003588}
Michael Jurka2763be32011-02-24 11:19:57 -08003589
Michael Jurkaabded662011-03-04 12:06:57 -08003590interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003591 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003592 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003593 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003594 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003595 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003596}