blob: d01de9c77bbc69839898e8eb663df575fd4bdab6 [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
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040053import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070054import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020056import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080057import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070058import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070059import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040060import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080061import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070062import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080063import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.Selection;
65import android.text.SpannableStringBuilder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070067import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080068import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080069import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.KeyEvent;
71import android.view.LayoutInflater;
72import android.view.Menu;
73import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070074import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080075import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080077import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070078import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080079import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080080import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070081import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080082import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070083import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070084import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080085import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070087import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070088import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070089import android.widget.TextView;
90import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070091
Adam Cohendf2cc412011-04-27 16:56:57 -070092import com.android.common.Search;
93import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070094import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080095
Adam Cohenc0dcf592011-06-01 15:30:43 -070096import java.io.DataInputStream;
97import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070098import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.FileNotFoundException;
100import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700101import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700103import java.util.Collection;
104import java.util.Collections;
105import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700108import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700109import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111/**
112 * Default launcher application.
113 */
Michael Jurka946ad472010-07-09 18:05:18 -0700114public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700115 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
116 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800117 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700118 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Joe Onorato9c1289c2009-08-17 11:03:03 -0400120 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400121 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700122 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700123
Winson Chung70d72102011-08-12 11:24:35 -0700124 private static final int MENU_GROUP_WALLPAPER = 1;
125 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
126 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700127 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
128 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
130 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700131 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int REQUEST_PICK_APPLICATION = 6;
133 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700135 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Michael Jurka8b805b12012-04-18 14:23:14 -0700137 private static final int REQUEST_BIND_APPWIDGET = 11;
138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
140
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800141 static final int SCREEN_COUNT = 5;
142 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Romain Guy98d01652009-06-30 16:21:04 -0700144 private static final String PREFERENCES = "launcher.preferences";
Adam Cohen23a4d302011-12-01 17:26:44 -0800145 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Winson Chung2672ff92012-05-04 16:22:30 -0700147 // The Intent extra that defines whether to ignore the launch animation
148 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
149 "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 // Type: int
152 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700153 // Type: int
154 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700156 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
157 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
159 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700162 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: boolean
164 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
165 // Type: long
166 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
169 // Type: int
170 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
171 // Type: parcelable
172 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700174 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700175 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
176 "com.android.launcher.toolbar_search_icon";
177 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
178 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700179
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700180 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700181 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700182 private State mState = State.WORKSPACE;
183 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800184 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700185
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700187 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
188 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700189 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700190 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800193 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Winson Chunga2413752012-04-03 14:22:34 -0700195 // How long to wait before the new-shortcut animation automatically pans the workspace
196 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
197
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800198 private final BroadcastReceiver mCloseSystemDialogsReceiver
199 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800200 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
201
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private LayoutInflater mInflater;
203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800205 private View mQsbDivider;
206 private View mDockDivider;
207 private DragLayer mDragLayer;
208 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700209
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700210 private AppWidgetManager mAppWidgetManager;
211 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700212
Michael Jurkac9d95c52011-08-29 14:03:34 -0700213 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700214 private AppWidgetProviderInfo mPendingAddWidgetInfo;
215
Michael Jurka0280c3b2010-09-17 15:00:07 -0700216 private int[] mTmpAddItemCellCoordinates = new int[2];
217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 private FolderInfo mFolderInfo;
219
Winson Chung3d503fb2011-07-13 17:25:49 -0700220 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800221 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700222
Winson Chungc51db6a2011-10-05 11:44:49 -0700223 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700224 private AppsCustomizeTabHost mAppsCustomizeTabHost;
225 private AppsCustomizePagedView mAppsCustomizeContent;
226 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 private Bundle mSavedState;
229
230 private SpannableStringBuilder mDefaultKeySsb = null;
231
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232 private boolean mWorkspaceLoading = true;
233
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800234 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 private boolean mRestoring;
236 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700237 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238
239 private Bundle mSavedInstanceState;
240
Joe Onorato9c1289c2009-08-17 11:03:03 -0400241 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800242 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800243 private boolean mUserPresent = true;
244 private boolean mVisible = false;
245 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700247 private static LocaleConfiguration sLocaleConfiguration = null;
248
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700249 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700250
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700251 private Intent mAppMarketIntent = null;
252
Adam Cohended9f8d2010-11-03 13:25:16 -0700253 // Related to the auto-advancing of widgets
254 private final int ADVANCE_MSG = 1;
255 private final int mAdvanceInterval = 20000;
256 private final int mAdvanceStagger = 250;
257 private long mAutoAdvanceSentTime;
258 private long mAutoAdvanceTimeLeft = -1;
259 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
260 new HashMap<View, AppWidgetProviderInfo>();
261
Winson Chung400438b2011-01-16 17:53:48 -0800262 // Determines how long to wait after a rotation before restoring the screen orientation to
263 // match the sensor state.
264 private final int mRestoreScreenOrientationDelay = 500;
265
Michael Jurka4ef207b2010-11-29 17:05:45 -0800266 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700267 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
268 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
269 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800270
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700271 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
272
Winson Chung46353de2012-02-16 14:05:10 -0800273 // We only want to get the SharedPreferences once since it does an FS stat each time we get
274 // it from the context.
275 private SharedPreferences mSharedPrefs;
276
Winson Chungf0c6ae02012-03-21 16:10:31 -0700277 // Holds the page that we need to animate to, and the icon views that we need to animate up
278 // when we scroll to that page on resume.
279 private int mNewShortcutAnimatePage = -1;
280 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700281
Michael Jurkaddd62e92011-02-16 17:49:14 -0800282 private BubbleTextView mWaitingForResume;
283
Michael Jurkac1f5d262011-09-30 19:32:27 -0700284 private Runnable mBuildLayersRunnable = new Runnable() {
285 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700286 if (mWorkspace != null) {
287 mWorkspace.buildPageHardwareLayers();
288 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700289 }
290 };
291
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800292 private static ArrayList<PendingAddArguments> sPendingAddList
293 = new ArrayList<PendingAddArguments>();
294
295 private static class PendingAddArguments {
296 int requestCode;
297 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700298 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800299 int screen;
300 int cellX;
301 int cellY;
302 }
303
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 @Override
305 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700306 if (DEBUG_STRICT_MODE) {
307 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
308 .detectDiskReads()
309 .detectDiskWrites()
310 .detectNetwork() // or .detectAll() for all detectable problems
311 .penaltyLog()
312 .build());
313 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
314 .detectLeakedSqlLiteObjects()
315 .detectLeakedClosableObjects()
316 .penaltyLog()
317 .penaltyDeath()
318 .build());
319 }
320
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800322 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700323 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
324 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800325 mModel = app.setLauncher(this);
326 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400327 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700329
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700330 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700331 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
332 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200335 android.os.Debug.startMethodTracing(
336 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 }
338
339 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 setContentView(R.layout.launcher);
341 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700342 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800344 registerContentObservers();
345
Joe Onorato7c312c12009-08-13 21:36:53 -0700346 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700347
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 mSavedState = savedInstanceState;
349 restoreState(mSavedState);
350
Winson Chunga12a2502010-12-20 14:41:35 -0800351 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700352 if (mAppsCustomizeContent != null) {
353 mAppsCustomizeContent.onPackagesUpdated();
354 }
Winson Chunga12a2502010-12-20 14:41:35 -0800355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 if (PROFILE_STARTUP) {
357 android.os.Debug.stopMethodTracing();
358 }
359
360 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700361 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 }
363
Michael Jurkac57b7a82011-08-09 22:02:20 -0700364 if (!mModel.isAllAppsLoaded()) {
365 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
366 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
367 }
368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 // For handling default keys
370 mDefaultKeySsb = new SpannableStringBuilder();
371 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800372
373 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
374 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800375
Winson Chungc51db6a2011-10-05 11:44:49 -0700376 boolean searchVisible = false;
377 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800378 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700379 int coi = getCurrentOrientationIndexForGlobalIcons();
380 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
381 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700382 updateAppMarketIcon();
383 searchVisible = updateGlobalSearchIcon();
384 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700385 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700386 if (sGlobalSearchIcon[coi] != null) {
387 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700388 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700389 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700390 if (sVoiceSearchIcon[coi] != null) {
391 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700392 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700393 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700394 if (sAppMarketIcon[coi] != null) {
395 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800396 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700397 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700398
399 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Winson Chung4b919f82012-05-01 10:44:08 -0700400 unlockScreenOrientation(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 }
Romain Guycbb89e42009-06-08 15:52:54 -0700402
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700404 if (sLocaleConfiguration == null) {
405 new AsyncTask<Void, Void, LocaleConfiguration>() {
406 @Override
407 protected LocaleConfiguration doInBackground(Void... unused) {
408 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
409 readConfiguration(Launcher.this, localeConfiguration);
410 return localeConfiguration;
411 }
412
413 @Override
414 protected void onPostExecute(LocaleConfiguration result) {
415 sLocaleConfiguration = result;
416 checkForLocaleChange(); // recursive, but now with a locale configuration
417 }
418 }.execute();
419 return;
420 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700421
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 final Configuration configuration = getResources().getConfiguration();
423
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700424 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 final String locale = configuration.locale.toString();
426
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700427 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 final int mcc = configuration.mcc;
429
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700430 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 final int mnc = configuration.mnc;
432
Romain Guy84f296c2009-11-04 15:00:44 -0800433 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434
Romain Guy84f296c2009-11-04 15:00:44 -0800435 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700436 sLocaleConfiguration.locale = locale;
437 sLocaleConfiguration.mcc = mcc;
438 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700439
Joe Onorato0589f0f2010-02-08 13:44:00 -0800440 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700441
442 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
443 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700444 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700445 public void run() {
446 writeConfiguration(Launcher.this, localeConfiguration);
447 }
448 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700449 }
450 }
451
452 private static class LocaleConfiguration {
453 public String locale;
454 public int mcc = -1;
455 public int mnc = -1;
456 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700457
Romain Guy98d01652009-06-30 16:21:04 -0700458 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
459 DataInputStream in = null;
460 try {
461 in = new DataInputStream(context.openFileInput(PREFERENCES));
462 configuration.locale = in.readUTF();
463 configuration.mcc = in.readInt();
464 configuration.mnc = in.readInt();
465 } catch (FileNotFoundException e) {
466 // Ignore
467 } catch (IOException e) {
468 // Ignore
469 } finally {
470 if (in != null) {
471 try {
472 in.close();
473 } catch (IOException e) {
474 // Ignore
475 }
476 }
477 }
478 }
479
480 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
481 DataOutputStream out = null;
482 try {
483 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
484 out.writeUTF(configuration.locale);
485 out.writeInt(configuration.mcc);
486 out.writeInt(configuration.mnc);
487 out.flush();
488 } catch (FileNotFoundException e) {
489 // Ignore
490 } catch (IOException e) {
491 //noinspection ResultOfMethodCallIgnored
492 context.getFileStreamPath(PREFERENCES).delete();
493 } finally {
494 if (out != null) {
495 try {
496 out.close();
497 } catch (IOException e) {
498 // Ignore
499 }
500 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501 }
502 }
503
Adam Cohen716b51e2011-06-30 12:09:54 -0700504 public DragLayer getDragLayer() {
505 return mDragLayer;
506 }
507
Winson Chung36a62fe2012-05-06 18:04:42 -0700508 boolean isDraggingEnabled() {
509 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
510 // that is subsequently removed from the workspace in startBinding().
511 return !mModel.isLoadingWorkspace();
512 }
513
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800514 static int getScreen() {
515 synchronized (sLock) {
516 return sScreen;
517 }
518 }
519
520 static void setScreen(int screen) {
521 synchronized (sLock) {
522 sScreen = screen;
523 }
524 }
525
Winson Chung557d6ed2011-07-08 15:34:52 -0700526 /**
527 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
528 * a configuration step, this allows the proper animations to run after other transitions.
529 */
530 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700531 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800532 switch (args.requestCode) {
533 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700534 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
535 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800536 break;
537 case REQUEST_PICK_SHORTCUT:
538 processShortcut(args.intent);
539 break;
540 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700541 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
542 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700543 result = true;
544 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800545 case REQUEST_CREATE_APPWIDGET:
546 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800547 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700548 result = true;
549 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800550 case REQUEST_PICK_WALLPAPER:
551 // We just wanted the activity result here so we can clear mWaitingForResult
552 break;
553 }
Michael Jurka27614d22012-04-02 06:40:22 -0700554 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
555 // if you turned the screen off and then back while in All Apps, Launcher would not
556 // return to the workspace. Clearing mAddInfo.container here fixes this issue
557 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700558 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800559 }
560
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700562 protected void onActivityResult(
563 final int requestCode, final int resultCode, final Intent data) {
564 if (requestCode == REQUEST_BIND_APPWIDGET) {
565 int appWidgetId = data != null ?
566 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
567 if (resultCode == RESULT_CANCELED) {
568 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
569 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700570 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700571 }
572 return;
573 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700574 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800575 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
576 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700577 mWaitingForResult = false;
578
Adam Cohened66b2b2012-01-23 17:28:51 -0800579 // We have special handling for widgets
580 if (isWidgetDrop) {
581 int appWidgetId = data != null ?
582 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700583 if (appWidgetId < 0) {
584 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
585 "widget configuration activity.");
586 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
587 } else {
588 completeTwoStageWidgetDrop(resultCode, appWidgetId);
589 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800590 return;
591 }
592
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 // The pattern used here is that a user PICKs a specific application,
594 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700595
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
597 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700598 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800599 final PendingAddArguments args = new PendingAddArguments();
600 args.requestCode = requestCode;
601 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700602 args.container = mPendingAddInfo.container;
603 args.screen = mPendingAddInfo.screen;
604 args.cellX = mPendingAddInfo.cellX;
605 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800606 if (isWorkspaceLocked()) {
607 sPendingAddList.add(args);
608 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700609 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800612 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700613 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800614 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
615 null);
616 }
617
618 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700619 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700620 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800621 Runnable onCompleteRunnable = null;
622 int animationType = 0;
623
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700624 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800625 if (resultCode == RESULT_OK) {
626 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
627 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700628 mPendingAddWidgetInfo);
629 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800630 onCompleteRunnable = new Runnable() {
631 @Override
632 public void run() {
633 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
634 mPendingAddInfo.screen, layout, null);
635 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
636 null);
637 }
638 };
639 } else if (resultCode == RESULT_CANCELED) {
640 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
641 onCompleteRunnable = new Runnable() {
642 @Override
643 public void run() {
644 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
645 null);
646 }
647 };
648 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700649 if (mDragLayer.getAnimatedView() != null) {
650 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
651 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
652 animationType, boundWidget, true);
653 } else {
654 // The animated view may be null in the case of a rotation during widget configuration
655 onCompleteRunnable.run();
656 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 }
658
659 @Override
660 protected void onResume() {
661 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700662
Winson Chungde0fb8f2012-05-08 14:37:08 -0700663 // Process any items that were added while Launcher was away
664 InstallShortcutReceiver.flushInstallQueue(this);
665
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800666 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700667 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400668 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700669 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400670 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700671 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 }
Winson Chunge4e50662012-01-23 14:45:13 -0800673
674 // Reset the pressed state of icons that were locked in the press state while activities
675 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800676 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800677 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800678 mWaitingForResume.setStayPressed(false);
679 }
Winson Chunge4e50662012-01-23 14:45:13 -0800680 if (mAppsCustomizeContent != null) {
681 // Resets the previous all apps icon press state
682 mAppsCustomizeContent.resetDrawableState();
683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
685
686 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700687 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700688 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
689 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
690 // when Launcher resumes and we are still in AllApps.
691 updateWallpaperVisibility(true);
692
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700693 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700694 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800695 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700696 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697 }
Romain Guycbb89e42009-06-08 15:52:54 -0700698
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700699 @Override
700 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 // Flag the loader to stop early before switching
702 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700703 if (mAppsCustomizeContent != null) {
704 mAppsCustomizeContent.surrender();
705 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800706 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700707 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700708
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800709 // We can't hide the IME if it was forced open. So don't bother
710 /*
711 @Override
712 public void onWindowFocusChanged(boolean hasFocus) {
713 super.onWindowFocusChanged(hasFocus);
714
715 if (hasFocus) {
716 final InputMethodManager inputManager = (InputMethodManager)
717 getSystemService(Context.INPUT_METHOD_SERVICE);
718 WindowManager.LayoutParams lp = getWindow().getAttributes();
719 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
720 android.os.Handler()) {
721 protected void onReceiveResult(int resultCode, Bundle resultData) {
722 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
723 }
724 });
725 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
726 }
727 }
728 */
729
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 private boolean acceptFilter() {
731 final InputMethodManager inputManager = (InputMethodManager)
732 getSystemService(Context.INPUT_METHOD_SERVICE);
733 return !inputManager.isFullscreenMode();
734 }
735
736 @Override
737 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700738 final int uniChar = event.getUnicodeChar();
739 final boolean handled = super.onKeyDown(keyCode, event);
740 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
741 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
743 keyCode, event);
744 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700745 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700746 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700747 // showSearchDialog()
748 // If there are multiple keystrokes before the search dialog takes focus,
749 // onSearchRequested() will be called for every keystroke,
750 // but it is idempotent, so it's fine.
751 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753 }
754
Joe Onorato8a9625e2010-01-28 15:55:35 -0800755 // Eat the long press event so the keyboard doesn't come up.
756 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
757 return true;
758 }
759
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 return handled;
761 }
762
Karl Rosaen138a0412009-04-23 19:00:21 -0700763 private String getTypedText() {
764 return mDefaultKeySsb.toString();
765 }
766
767 private void clearTypedText() {
768 mDefaultKeySsb.clear();
769 mDefaultKeySsb.clearSpans();
770 Selection.setSelection(mDefaultKeySsb, 0);
771 }
772
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700774 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
775 * State
776 */
777 private static State intToState(int stateOrdinal) {
778 State state = State.WORKSPACE;
779 final State[] stateValues = State.values();
780 for (int i = 0; i < stateValues.length; i++) {
781 if (stateValues[i].ordinal() == stateOrdinal) {
782 state = stateValues[i];
783 break;
784 }
785 }
786 return state;
787 }
788
789 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 * Restores the previous state, if it exists.
791 *
792 * @param savedState The previous state.
793 */
794 private void restoreState(Bundle savedState) {
795 if (savedState == null) {
796 return;
797 }
798
Michael Jurka883f55b2010-10-21 15:47:14 -0700799 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700800 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800801 showAllApps(false);
802 }
803
Winson Chungf0c6ae02012-03-21 16:10:31 -0700804 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700806 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 }
808
Winson Chung3d503fb2011-07-13 17:25:49 -0700809 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
810 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700811
Winson Chung3d503fb2011-07-13 17:25:49 -0700812 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
813 mPendingAddInfo.container = pendingAddContainer;
814 mPendingAddInfo.screen = pendingAddScreen;
815 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
816 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700817 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
818 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
819 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 mRestoring = true;
821 }
822
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700823
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
825 if (renameFolder) {
826 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700827 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 mRestoring = true;
829 }
Winson Chunga12a2502010-12-20 14:41:35 -0800830
Winson Chung785d2eb2011-04-14 16:08:02 -0700831
832 // Restore the AppsCustomize tab
833 if (mAppsCustomizeTabHost != null) {
834 String curTab = savedState.getString("apps_customize_currentTab");
835 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700836 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700837 mAppsCustomizeContent.setContentType(
838 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
839 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700840 mAppsCustomizeContent.loadAssociatedPages(
841 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700842 }
843
Winson Chung5afbf7b2011-07-25 11:53:08 -0700844 int currentIndex = savedState.getInt("apps_customize_currentIndex");
845 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700846 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 }
848
849 /**
850 * Finds all the views we need and configure them properly.
851 */
852 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700853 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400854
Winson Chung4c98d922011-05-31 16:50:48 -0700855 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
856 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800857 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
858 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859
Winson Chung4c98d922011-05-31 16:50:48 -0700860 // Setup the drag layer
861 mDragLayer.setup(this, dragController);
862
Winson Chung3d503fb2011-07-13 17:25:49 -0700863 // Setup the hotseat
864 mHotseat = (Hotseat) findViewById(R.id.hotseat);
865 if (mHotseat != null) {
866 mHotseat.setup(this);
867 }
868
Winson Chung4c98d922011-05-31 16:50:48 -0700869 // Setup the workspace
870 mWorkspace.setHapticFeedbackEnabled(false);
871 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700872 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700873 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700874
Winson Chungf0ea4d32011-06-06 14:27:16 -0700875 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700876 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700877
Winson Chungf0ea4d32011-06-06 14:27:16 -0700878 // Setup AppsCustomize
879 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
880 findViewById(R.id.apps_customize_pane);
881 mAppsCustomizeContent = (AppsCustomizePagedView)
882 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700883 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700884 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400885
Michael Jurka5130e402011-10-13 04:55:35 -0700886 // Get the all apps button
887 mAllAppsButton = findViewById(R.id.all_apps_button);
888 if (mAllAppsButton != null) {
889 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
890 @Override
891 public boolean onTouch(View v, MotionEvent event) {
892 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
893 onTouchDownAllAppsButton(v);
894 }
895 return false;
896 }
897 });
898 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700899 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700900 dragController.setDragScoller(mWorkspace);
901 dragController.setScrollView(mDragLayer);
902 dragController.setMoveTarget(mWorkspace);
903 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700904 if (mSearchDropTargetBar != null) {
905 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800906 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 }
908
909 /**
910 * Creates a view representing a shortcut.
911 *
912 * @param info The data structure describing the shortcut.
913 *
914 * @return A View inflated from R.layout.application.
915 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800916 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700918 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 }
920
921 /**
922 * Creates a view representing a shortcut inflated from the specified resource.
923 *
924 * @param layoutResId The id of the XML layout used to create the shortcut.
925 * @param parent The group the shortcut belongs to.
926 * @param info The data structure describing the shortcut.
927 *
928 * @return A View inflated from layoutResId.
929 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800930 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800931 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
932 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 return favorite;
935 }
936
937 /**
938 * Add an application shortcut to the workspace.
939 *
940 * @param data The intent describing the application.
941 * @param cellInfo The position on screen where to create the shortcut.
942 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700943 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700944 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700945 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700946
Adam Cohenfbba09b2011-07-18 21:43:05 -0700947 // First we check if we already know the exact location where we want to add this item.
948 if (cellX >= 0 && cellY >= 0) {
949 cellXY[0] = cellX;
950 cellXY[1] = cellY;
951 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700952 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700953 return;
954 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800956 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800957
Romain Guy73b979d2009-06-09 12:57:21 -0700958 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800959 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800961 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700962 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700963 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800964 } else {
965 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 }
967 }
Romain Guycbb89e42009-06-08 15:52:54 -0700968
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 /**
970 * Add a shortcut to the workspace.
971 *
972 * @param data The intent describing the shortcut.
973 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700975 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
976 int cellY) {
977 int[] cellXY = mTmpAddItemCellCoordinates;
978 int[] touchXY = mPendingAddInfo.dropPos;
979 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700980
Michael Jurkad3ef3062010-11-23 16:23:58 -0800981 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700982
Adam Cohen558baaf2011-08-15 15:22:57 -0700983 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800984 if (info == null) {
985 return;
986 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700987 final View view = createShortcut(info);
988
Adam Cohenfbba09b2011-07-18 21:43:05 -0700989 // First we check if we already know the exact location where we want to add this item.
990 if (cellX >= 0 && cellY >= 0) {
991 cellXY[0] = cellX;
992 cellXY[1] = cellY;
993 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700994
995 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800996 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700997 true, null,null)) {
998 return;
999 }
1000 DragObject dragObject = new DragObject();
1001 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001002 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1003 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001004 return;
1005 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001006 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001007 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001008 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001009 foundCellSpan = (result != null);
1010 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001011 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001012 }
1013
1014 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001015 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001016 return;
1017 }
1018
Adam Cohen558baaf2011-08-15 15:22:57 -07001019 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020
1021 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001022 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1023 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 }
1025 }
1026
Adam Cohen2f093b62012-04-30 18:59:53 -07001027 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1028 int minHeight) {
1029 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001030 // We want to account for the extra amount of padding that we are adding to the widget
1031 // to ensure that it gets the full amount of space that it has requested
1032 int requiredWidth = minWidth + padding.left + padding.right;
1033 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001034 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001035 }
1036
Adam Cohen2f093b62012-04-30 18:59:53 -07001037 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1038 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001039 }
1040
Adam Cohen2f093b62012-04-30 18:59:53 -07001041 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1042 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001043 }
1044
Adam Cohen2f093b62012-04-30 18:59:53 -07001045 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1046 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001047 }
1048
Adam Cohen2f093b62012-04-30 18:59:53 -07001049 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1050 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001051 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001052 }
1053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001055 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001057 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001058 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001060 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1061 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1062 if (appWidgetInfo == null) {
1063 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1064 }
Romain Guycbb89e42009-06-08 15:52:54 -07001065
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001066 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001067 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001068
Adam Cohen2f093b62012-04-30 18:59:53 -07001069 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1070 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001073 // We have saved the position to which the widget was dragged-- this really only matters
1074 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001075 int[] cellXY = mTmpAddItemCellCoordinates;
1076 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001077 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001078 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001079 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1080 cellXY[0] = mPendingAddInfo.cellX;
1081 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001082 spanXY[0] = mPendingAddInfo.spanX;
1083 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001084 foundCellSpan = true;
1085 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001086 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001087 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001088 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1089 spanXY[1], cellXY, finalSpan);
1090 spanXY[0] = finalSpan[0];
1091 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001092 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001093 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001094 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001095 }
1096
Michael Jurka0280c3b2010-09-17 15:00:07 -07001097 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001098 if (appWidgetId != -1) {
1099 // Deleting an app widget ID is a void call but writes to disk before returning
1100 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001101 new Thread("deleteAppWidgetId") {
1102 public void run() {
1103 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1104 }
1105 }.start();
1106 }
Winson Chung93eef082012-03-23 15:59:27 -07001107 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001108 return;
1109 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001111 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001112 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1113 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001114 launcherInfo.spanX = spanXY[0];
1115 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001116 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1117 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001118
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001120 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121
1122 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001123 if (hostView == null) {
1124 // Perform actual inflation because we're live
1125 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1126 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1127 } else {
1128 // The AppWidgetHostView has already been inflated and instantiated
1129 launcherInfo.hostView = hostView;
1130 }
Romain Guycbb89e42009-06-08 15:52:54 -07001131
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001133 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung211bac32012-05-15 13:43:57 -07001134 launcherInfo.notifyWidgetSizeChanged(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001135 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001136 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001137
1138 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001140 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 }
Romain Guycbb89e42009-06-08 15:52:54 -07001142
Adam Cohended9f8d2010-11-03 13:25:16 -07001143 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1144 @Override
1145 public void onReceive(Context context, Intent intent) {
1146 final String action = intent.getAction();
1147 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1148 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001149 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001150 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001151
Winson Chungc100e8e2011-08-09 16:02:43 -07001152 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001153 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001154 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1155 mAppsCustomizeTabHost.reset();
1156 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001157 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001158 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1159 mUserPresent = true;
1160 updateRunning();
1161 }
1162 }
1163 };
1164
1165 @Override
1166 public void onAttachedToWindow() {
1167 super.onAttachedToWindow();
1168
1169 // Listen for broadcasts related to user-presence
1170 final IntentFilter filter = new IntentFilter();
1171 filter.addAction(Intent.ACTION_SCREEN_OFF);
1172 filter.addAction(Intent.ACTION_USER_PRESENT);
1173 registerReceiver(mReceiver, filter);
1174
Adam Cohend113e0c2010-11-11 10:48:05 -08001175 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001176 mVisible = true;
1177 }
1178
1179 @Override
1180 public void onDetachedFromWindow() {
1181 super.onDetachedFromWindow();
1182 mVisible = false;
1183
Adam Cohend113e0c2010-11-11 10:48:05 -08001184 if (mAttached) {
1185 unregisterReceiver(mReceiver);
1186 mAttached = false;
1187 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001188 updateRunning();
1189 }
1190
1191 public void onWindowVisibilityChanged(int visibility) {
1192 mVisible = visibility == View.VISIBLE;
1193 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001194 // The following code used to be in onResume, but it turns out onResume is called when
1195 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1196 // is a more appropriate event to handle
1197 if (mVisible) {
1198 mAppsCustomizeTabHost.onWindowVisible();
1199 if (!mWorkspaceLoading) {
1200 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001201 // We want to let Launcher draw itself at least once before we force it to build
1202 // layers on all the workspace pages, so that transitioning to Launcher from other
1203 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1204 // a true draw so we wait until the second preDraw call to be safe
1205 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001206 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001207 // We delay the layer building a bit in order to give
1208 // other message processing a time to run. In particular
1209 // this avoids a delay in hiding the IME if it was
1210 // currently shown, because doing that may involve
1211 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001212 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001213
1214 // We had to enable the wallpaper visibility when launching apps from all
1215 // apps (so that the transitions would be the same as when launching from
1216 // workspace) so take this time to see if we need to re-disable the
1217 // wallpaper visibility to ensure performance.
1218 mWorkspace.post(new Runnable() {
1219 @Override
1220 public void run() {
Winson Chung18f41f82012-05-09 13:28:10 -07001221 disableWallpaperIfInAllApps();
Winson Chung31ce0732012-05-06 13:36:43 -07001222 }
1223 });
1224
Michael Jurka6ee21d22012-02-21 18:20:27 -08001225 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001226 return true;
1227 }
1228 });
1229 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001230 // When Launcher comes back to foreground, a different Activity might be responsible for
1231 // the app market intent, so refresh the icon
1232 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001233 clearTypedText();
1234 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001235 }
1236
1237 private void sendAdvanceMessage(long delay) {
1238 mHandler.removeMessages(ADVANCE_MSG);
1239 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1240 mHandler.sendMessageDelayed(msg, delay);
1241 mAutoAdvanceSentTime = System.currentTimeMillis();
1242 }
1243
1244 private void updateRunning() {
1245 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1246 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1247 mAutoAdvanceRunning = autoAdvanceRunning;
1248 if (autoAdvanceRunning) {
1249 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1250 sendAdvanceMessage(delay);
1251 } else {
1252 if (!mWidgetsToAdvance.isEmpty()) {
1253 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1254 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1255 }
1256 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001257 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001258 }
1259 }
1260 }
1261
1262 private final Handler mHandler = new Handler() {
1263 @Override
1264 public void handleMessage(Message msg) {
1265 if (msg.what == ADVANCE_MSG) {
1266 int i = 0;
1267 for (View key: mWidgetsToAdvance.keySet()) {
1268 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1269 final int delay = mAdvanceStagger * i;
1270 if (v instanceof Advanceable) {
1271 postDelayed(new Runnable() {
1272 public void run() {
1273 ((Advanceable) v).advance();
1274 }
1275 }, delay);
1276 }
1277 i++;
1278 }
1279 sendAdvanceMessage(mAdvanceInterval);
1280 }
1281 }
1282 };
1283
1284 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001285 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001286 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1287 if (v instanceof Advanceable) {
1288 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001289 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001290 updateRunning();
1291 }
1292 }
1293
1294 void removeWidgetToAutoAdvance(View hostView) {
1295 if (mWidgetsToAdvance.containsKey(hostView)) {
1296 mWidgetsToAdvance.remove(hostView);
1297 updateRunning();
1298 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001299 }
1300
Joe Onorato9c1289c2009-08-17 11:03:03 -04001301 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001302 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001303 launcherInfo.hostView = null;
1304 }
1305
Winson Chung93eef082012-03-23 15:59:27 -07001306 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1307 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1308 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001309 }
1310
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001311 public LauncherAppWidgetHost getAppWidgetHost() {
1312 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 }
Romain Guycbb89e42009-06-08 15:52:54 -07001314
Winson Chunga9abd0e2010-10-27 17:18:37 -07001315 public LauncherModel getModel() {
1316 return mModel;
1317 }
1318
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001319 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001320 getWindow().closeAllPanels();
1321
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001322 // Whatever we were doing is hereby canceled.
1323 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001324 }
1325
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 @Override
1327 protected void onNewIntent(Intent intent) {
1328 super.onNewIntent(intent);
1329
1330 // Close the menu
1331 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001332 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001333 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001334
Joe Onorato14f122b2009-11-19 14:06:36 -08001335 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1336 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001337
Adam Cohenac56cff2011-09-28 20:45:37 -07001338 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001339 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001340 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001341 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1342 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001343 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001344 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001345
1346 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001347 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001348 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001349
Joe Onorato3a8820b2009-11-10 15:06:42 -08001350 final View v = getWindow().peekDecorView();
1351 if (v != null && v.getWindowToken() != null) {
1352 InputMethodManager imm = (InputMethodManager)getSystemService(
1353 INPUT_METHOD_SERVICE);
1354 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001356
Michael Jurka35aa14d2011-07-07 17:01:08 -07001357 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001358 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001359 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001360 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 }
1362 }
1363
1364 @Override
1365 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1366 // Do not call super here
1367 mSavedInstanceState = savedInstanceState;
1368 }
1369
1370 @Override
1371 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001372 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001373 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374
Michael Jurka883f55b2010-10-21 15:47:14 -07001375 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001376 // We close any open folder since it will not be re-opened, and we need to make sure
1377 // this state is reflected.
1378 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379
Winson Chung3d503fb2011-07-13 17:25:49 -07001380 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1381 mWaitingForResult) {
1382 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1383 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1384 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1385 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001386 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1387 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1388 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 }
1390
1391 if (mFolderInfo != null && mWaitingForResult) {
1392 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1393 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1394 }
Michael Jurka946ad472010-07-09 18:05:18 -07001395
Winson Chung785d2eb2011-04-14 16:08:02 -07001396 // Save the current AppsCustomize tab
1397 if (mAppsCustomizeTabHost != null) {
1398 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1399 if (currentTabTag != null) {
1400 outState.putString("apps_customize_currentTab", currentTabTag);
1401 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001402 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1403 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001404 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 }
1406
1407 @Override
1408 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001410
Winson Chunge7a03942011-08-05 15:05:12 -07001411 // Remove all pending runnables
1412 mHandler.removeMessages(ADVANCE_MSG);
1413 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001414 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001415
Winson Chungcd2b0142011-06-08 16:02:26 -07001416 // Stop callbacks from LauncherModel
1417 LauncherApplication app = ((LauncherApplication) getApplication());
1418 mModel.stopLoader();
1419 app.setLauncher(null);
1420
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001422 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001424 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001426 mAppWidgetHost = null;
1427
1428 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429
1430 TextKeyListener.getInstance().release();
1431
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432
Winson Chung3d503fb2011-07-13 17:25:49 -07001433 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001434
1435 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001436 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001437
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001438 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001439 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1440 mWorkspace.removeAllViews();
1441 mWorkspace = null;
1442 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001443
1444 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 }
1446
Adam Cohena9cf38f2011-05-02 15:36:58 -07001447 public DragController getDragController() {
1448 return mDragController;
1449 }
1450
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 @Override
1452 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001453 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 super.startActivityForResult(intent, requestCode);
1455 }
1456
Winson Chung70d72102011-08-12 11:24:35 -07001457 /**
1458 * Indicates that we want global search for this activity by setting the globalSearch
1459 * argument for {@link #startSearch} to true.
1460 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001462 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001464
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001465 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001466
Karl Rosaen138a0412009-04-23 19:00:21 -07001467 if (initialQuery == null) {
1468 // Use any text typed in the launcher as the initial query
1469 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001470 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 if (appSearchData == null) {
1472 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001473 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001475 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001476
Karl Rosaen138a0412009-04-23 19:00:21 -07001477 final SearchManager searchManager =
1478 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001479 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001480 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 }
1482
Winson Chung70d72102011-08-12 11:24:35 -07001483 @Override
1484 public boolean onCreateOptionsMenu(Menu menu) {
1485 if (isWorkspaceLocked()) {
1486 return false;
1487 }
1488
1489 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001490
1491 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1492 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1493 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001494 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1495 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1496 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001497 String helpUrl = getString(R.string.help_url);
1498 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001499 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1500 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1501
Winson Chung70d72102011-08-12 11:24:35 -07001502 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1503 .setIcon(android.R.drawable.ic_menu_gallery)
1504 .setAlphabeticShortcut('W');
1505 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1506 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001507 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001508 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001509 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1510 .setIcon(android.R.drawable.ic_menu_preferences)
1511 .setIntent(settings)
1512 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001513 if (!helpUrl.isEmpty()) {
1514 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1515 .setIcon(android.R.drawable.ic_menu_help)
1516 .setIntent(help)
1517 .setAlphabeticShortcut('H');
1518 }
Winson Chung70d72102011-08-12 11:24:35 -07001519 return true;
1520 }
1521
1522 @Override
1523 public boolean onPrepareOptionsMenu(Menu menu) {
1524 super.onPrepareOptionsMenu(menu);
1525
1526 if (mAppsCustomizeTabHost.isTransitioning()) {
1527 return false;
1528 }
1529 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1530 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1531
1532 return true;
1533 }
1534
1535 @Override
1536 public boolean onOptionsItemSelected(MenuItem item) {
1537 switch (item.getItemId()) {
1538 case MENU_WALLPAPER_SETTINGS:
1539 startWallpaper();
1540 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001541 }
1542
1543 return super.onOptionsItemSelected(item);
1544 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001546 @Override
1547 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001548 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001549 // Use a custom animation for launching search
1550 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001551 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001552 }
1553
Joe Onorato9c1289c2009-08-17 11:03:03 -04001554 public boolean isWorkspaceLocked() {
1555 return mWorkspaceLoading || mWaitingForResult;
1556 }
1557
Michael Jurka0280c3b2010-09-17 15:00:07 -07001558 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001559 mPendingAddInfo.container = ItemInfo.NO_ID;
1560 mPendingAddInfo.screen = -1;
1561 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1562 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001563 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001564 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001565 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001566
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001567 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1568 AppWidgetProviderInfo appWidgetInfo) {
1569 if (appWidgetInfo.configure != null) {
1570 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001571
Bjorn Bringert7984c942009-12-09 15:38:25 +00001572 // Launch over to configure widget, if needed
1573 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001574 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001575 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001576 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001578 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001579 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1580 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001581 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001582 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 }
1584 }
Romain Guycbb89e42009-06-08 15:52:54 -07001585
Adam Cohenfbba09b2011-07-18 21:43:05 -07001586 /**
1587 * Process a shortcut drop.
1588 *
1589 * @param componentName The name of the component
1590 * @param screen The screen where it should be added
1591 * @param cell The cell it should be added to, optional
1592 * @param position The location on the screen where it was dropped, optional
1593 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001594 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1595 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001596 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001597 mPendingAddInfo.container = container;
1598 mPendingAddInfo.screen = screen;
1599 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001600
1601 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001602 mPendingAddInfo.cellX = cell[0];
1603 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001604 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001605
1606 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1607 createShortcutIntent.setComponent(componentName);
1608 processShortcut(createShortcutIntent);
1609 }
1610
Adam Cohenfbba09b2011-07-18 21:43:05 -07001611 /**
1612 * Process a widget drop.
1613 *
1614 * @param info The PendingAppWidgetInfo of the widget being added.
1615 * @param screen The screen where it should be added
1616 * @param cell The cell it should be added to, optional
1617 * @param position The location on the screen where it was dropped, optional
1618 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001619 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001620 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001621 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001622 mPendingAddInfo.container = info.container = container;
1623 mPendingAddInfo.screen = info.screen = screen;
1624 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001625 mPendingAddInfo.minSpanX = info.minSpanX;
1626 mPendingAddInfo.minSpanY = info.minSpanY;
1627
Adam Cohenfbba09b2011-07-18 21:43:05 -07001628 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001629 mPendingAddInfo.cellX = cell[0];
1630 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001631 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001632 if (span != null) {
1633 mPendingAddInfo.spanX = span[0];
1634 mPendingAddInfo.spanY = span[1];
1635 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001636
Adam Cohened66b2b2012-01-23 17:28:51 -08001637 AppWidgetHostView hostView = info.boundWidget;
1638 int appWidgetId;
1639 if (hostView != null) {
1640 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001641 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001642 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001643 // In this case, we either need to start an activity to get permission to bind
1644 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001645 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001646 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001647 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001648 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001649 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001650 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1651 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1652 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1653 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1654 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001655 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001656 }
1657
Joe Onoratodeb98af2010-02-19 14:59:39 -08001658 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001659 // Handle case where user selected "Applications"
1660 String applicationName = getResources().getString(R.string.group_applications);
1661 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001662
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001663 if (applicationName != null && applicationName.equals(shortcutName)) {
1664 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1665 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001666
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001667 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1668 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001669 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001670 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001671 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001672 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001673 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 }
1675
Winson Chung24ab2f12010-09-16 14:10:47 -07001676 void processWallpaper(Intent intent) {
1677 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1678 }
1679
Winson Chung3d503fb2011-07-13 17:25:49 -07001680 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1681 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001682 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 folderInfo.title = getText(R.string.folder_name);
1684
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001686 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1687 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001688 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689
1690 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001691 FolderIcon newFolder =
1692 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1693 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1694 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001695 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 }
Romain Guycbb89e42009-06-08 15:52:54 -07001697
Joe Onorato9c1289c2009-08-17 11:03:03 -04001698 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001699 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001700 }
1701
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001703 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001705 Intent chooser = Intent.createChooser(pickWallpaper,
1706 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001707 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1708 // Removed in Eclair MR1
1709// WallpaperManager wm = (WallpaperManager)
1710// getSystemService(Context.WALLPAPER_SERVICE);
1711// WallpaperInfo wi = wm.getWallpaperInfo();
1712// if (wi != null && wi.getSettingsActivity() != null) {
1713// LabeledIntent li = new LabeledIntent(getPackageName(),
1714// R.string.configure_wallpaper, 0);
1715// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1716// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1717// }
Mike Clerona0618e42009-10-22 13:55:21 -07001718 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 }
1720
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001721 /**
1722 * Registers various content observers. The current implementation registers
1723 * only a favorites observer to keep track of the favorites applications.
1724 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001725 private void registerContentObservers() {
1726 ContentResolver resolver = getContentResolver();
1727 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1728 true, mWidgetObserver);
1729 }
1730
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 @Override
1732 public boolean dispatchKeyEvent(KeyEvent event) {
1733 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1734 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001736 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001737 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001738 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001739 dumpState();
1740 return true;
1741 }
1742 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001743 }
1744 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1745 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001746 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747 return true;
1748 }
1749 }
1750
1751 return super.dispatchKeyEvent(event);
1752 }
1753
Joe Onorato88ec0992009-11-19 13:16:06 -08001754 @Override
1755 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001756 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001757 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001758 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001759 Folder openFolder = mWorkspace.getOpenFolder();
1760 if (openFolder.isEditingName()) {
1761 openFolder.dismissEditingName();
1762 } else {
1763 closeFolder();
1764 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001765 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001766 mWorkspace.exitWidgetResizeMode();
1767
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001768 // Back button is a no-op here, but give at least some feedback for the button press
1769 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001770 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001771 }
1772
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001774 * Re-listen when widgets are reset.
1775 */
1776 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001777 if (mAppWidgetHost != null) {
1778 mAppWidgetHost.startListening();
1779 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001780 }
1781
1782 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001783 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1784 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001786 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001787 if (mModel != null) {
1788 mModel.unbindWorkspaceItems();
1789 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001791
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 /**
1793 * Launches the intent referred by the clicked shortcut.
1794 *
1795 * @param v The view representing the clicked shortcut.
1796 */
1797 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001798 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1799 // view has detached (it's possible for this to happen if the view is removed mid touch).
1800 if (v.getWindowToken() == null) {
1801 return;
1802 }
1803
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001804 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001805 return;
1806 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001807
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001809 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001811 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001812 int[] pos = new int[2];
1813 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001814 intent.setSourceBounds(new Rect(pos[0], pos[1],
1815 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001816
1817 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001818
1819 if (success && v instanceof BubbleTextView) {
1820 mWaitingForResume = (BubbleTextView) v;
1821 mWaitingForResume.setStayPressed(true);
1822 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001824 if (v instanceof FolderIcon) {
1825 FolderIcon fi = (FolderIcon) v;
1826 handleFolderClick(fi);
1827 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001828 } else if (v == mAllAppsButton) {
1829 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001830 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001831 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001832 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 }
1835 }
1836
Michael Jurka0e260592010-06-30 17:07:39 -07001837 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001838 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001839 // clicking anywhere on the workspace causes the customization drawer to slide down
1840 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001841 return false;
1842 }
1843
Michael Jurkaaf442092010-06-10 17:01:57 -07001844 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001845 * Event handler for the search button
1846 *
1847 * @param v The view that was clicked.
1848 */
1849 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001850 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1851
Amith Yamasania135ba82011-08-09 17:42:01 -07001852 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001853 }
1854
1855 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001856 * Event handler for the voice button
1857 *
1858 * @param v The view that was clicked.
1859 */
1860 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001861 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001862
Michael Jurkaae65ee32012-04-30 11:45:54 -07001863 try {
1864 final SearchManager searchManager =
1865 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1866 ComponentName activityName = searchManager.getGlobalSearchActivity();
1867 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1868 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1869 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1870 if (activityName != null) {
1871 intent.setPackage(activityName.getPackageName());
1872 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001873 startActivity(null, intent, "onClickVoiceButton");
Winson Chung01b0b632012-05-22 10:18:14 -07001874 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001875 } catch (ActivityNotFoundException e) {
1876 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1877 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1878 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1879 startActivitySafely(null, intent, "onClickVoiceButton");
1880 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001881 }
1882
1883 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001884 * Event handler for the "grid" button that appears on the home screen, which
1885 * enters all apps mode.
1886 *
1887 * @param v The view that was clicked.
1888 */
1889 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001890 showAllApps(true);
1891 }
1892
1893 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001894 // Provide the same haptic feedback that the system offers for virtual keys.
1895 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001896 }
1897
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001898 public void onClickAppMarketButton(View v) {
1899 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001900 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001901 } else {
1902 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001903 }
1904 }
1905
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001906 void startApplicationDetailsActivity(ComponentName componentName) {
1907 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001908 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1909 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001910 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001911 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001912 }
1913
Patrick Dubroy5539af72010-09-07 15:22:01 -07001914 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1915 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1916 // System applications cannot be installed. For now, show a toast explaining that.
1917 // We may give them the option of disabling apps this way.
1918 int messageId = R.string.uninstall_system_app_text;
1919 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1920 } else {
1921 String packageName = appInfo.componentName.getPackageName();
1922 String className = appInfo.componentName.getClassName();
1923 Intent intent = new Intent(
1924 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001925 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1926 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001927 startActivity(intent);
1928 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001929 }
1930
Michael Jurka86a720e2012-05-09 11:23:23 -07001931 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001933
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 try {
Winson Chung2672ff92012-05-04 16:22:30 -07001935 // Only launch using the new animation if the shortcut has not opted out (this is a
1936 // private contract between launcher and may be ignored in the future).
1937 boolean useLaunchAnimation = (v != null) &&
1938 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
1939 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07001940 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1941 v.getMeasuredWidth(), v.getMeasuredHeight());
1942
1943 startActivity(intent, opts.toBundle());
1944 } else {
1945 startActivity(intent);
1946 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001947 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 } catch (SecurityException e) {
1949 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001950 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001952 "or use the exported attribute for this activity. "
1953 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001955 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001957
Michael Jurka86a720e2012-05-09 11:23:23 -07001958 boolean startActivitySafely(View v, Intent intent, Object tag) {
1959 boolean success = false;
1960 try {
1961 success = startActivity(v, intent, tag);
1962 } catch (ActivityNotFoundException e) {
1963 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1964 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
1965 }
1966 return success;
1967 }
1968
Romain Guy8e633c52010-03-04 12:51:36 -08001969 void startActivityForResultSafely(Intent intent, int requestCode) {
1970 try {
1971 startActivityForResult(intent, requestCode);
1972 } catch (ActivityNotFoundException e) {
1973 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1974 } catch (SecurityException e) {
1975 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1976 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1977 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1978 "or use the exported attribute for this activity.", e);
1979 }
1980 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981
Adam Cohena9cf38f2011-05-02 15:36:58 -07001982 private void handleFolderClick(FolderIcon folderIcon) {
1983 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001984 Folder openFolder = mWorkspace.getFolderForTag(info);
1985
1986 // If the folder info reports that the associated folder is open, then verify that
1987 // it is actually opened. There have been a few instances where this gets out of sync.
1988 if (info.opened && openFolder == null) {
1989 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1990 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1991 info.opened = false;
1992 }
1993
Adam Cohena9cf38f2011-05-02 15:36:58 -07001994 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 // Close any open folder
1996 closeFolder();
1997 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001998 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 } else {
2000 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 int folderScreen;
2002 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002003 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 // .. and close it
2005 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002006 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 // Close any folder open on the current screen
2008 closeFolder();
2009 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002010 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 }
2012 }
2013 }
2014 }
2015
Adam Cohen2801caf2011-05-13 20:57:39 -07002016 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002017 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002018 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2019 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2020 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2021
Adam Cohenc51934b2011-07-26 21:07:43 -07002022 FolderInfo info = (FolderInfo) fi.getTag();
2023 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2024 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002025 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2026 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002027 }
2028
Adam Cohen2801caf2011-05-13 20:57:39 -07002029 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2030 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2031 oa.start();
2032 }
2033
2034 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002035 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002036 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2037 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2038 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2039
Adam Cohenc51934b2011-07-26 21:07:43 -07002040 FolderInfo info = (FolderInfo) fi.getTag();
2041 CellLayout cl = null;
2042 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2043 cl = (CellLayout) fi.getParent().getParent();
2044 }
2045
2046 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07002047 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2048 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002049 oa.addListener(new AnimatorListenerAdapter() {
2050 @Override
2051 public void onAnimationEnd(Animator animation) {
2052 if (layout != null) {
2053 layout.clearFolderLeaveBehind();
2054 }
2055 }
2056 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002057 oa.start();
2058 }
2059
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002061 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 * is animated relative to the specified View. If the View is null, no animation
2063 * is played.
2064 *
2065 * @param folderInfo The FolderInfo describing the folder to open.
2066 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002067 public void openFolder(FolderIcon folderIcon) {
2068 Folder folder = folderIcon.mFolder;
2069 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070
Adam Cohen2801caf2011-05-13 20:57:39 -07002071 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002072 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073
Adam Cohen4554ee12011-08-03 16:13:21 -07002074 // Just verify that the folder hasn't already been added to the DragLayer.
2075 // There was a one-off crash where the folder had a parent already.
2076 if (folder.getParent() == null) {
2077 mDragLayer.addView(folder);
2078 mDragController.addDropTarget((DropTarget) folder);
2079 } else {
2080 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2081 folder.getParent() + ").");
2082 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002083 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002084 }
2085
2086 public void closeFolder() {
2087 Folder folder = mWorkspace.getOpenFolder();
2088 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002089 if (folder.isEditingName()) {
2090 folder.dismissEditingName();
2091 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002092 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002093
2094 // Dismiss the folder cling
2095 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002096 }
2097 }
2098
2099 void closeFolder(Folder folder) {
2100 folder.getInfo().opened = false;
2101
2102 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2103 if (parent != null) {
2104 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2105 shrinkAndFadeInFolderIcon(fi);
2106 }
2107 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 }
2109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002111 if (!isDraggingEnabled()) return false;
2112 if (isWorkspaceLocked()) return false;
2113 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114
2115 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002116 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 }
2118
Michael Jurka0280c3b2010-09-17 15:00:07 -07002119 resetAddInfo();
2120 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002122 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 return true;
2124 }
2125
Winson Chung3d503fb2011-07-13 17:25:49 -07002126 // The hotseat touch handling does not go through Workspace, and we always allow long press
2127 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002128 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002129 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2130 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002131 if (itemUnderLongClick == null) {
2132 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002133 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2134 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002135 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002137 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002139 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 }
2141 }
2142 }
2143 return true;
2144 }
2145
Winson Chung3d503fb2011-07-13 17:25:49 -07002146 boolean isHotseatLayout(View layout) {
2147 return mHotseat != null && layout != null &&
2148 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2149 }
2150 Hotseat getHotseat() {
2151 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002152 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002153 SearchDropTargetBar getSearchBar() {
2154 return mSearchDropTargetBar;
2155 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002156
Winson Chung3d503fb2011-07-13 17:25:49 -07002157 /**
2158 * Returns the CellLayout of the specified container at the specified screen.
2159 */
2160 CellLayout getCellLayout(long container, int screen) {
2161 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2162 if (mHotseat != null) {
2163 return mHotseat.getLayout();
2164 } else {
2165 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002166 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002167 } else {
2168 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002169 }
2170 }
2171
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 Workspace getWorkspace() {
2173 return mWorkspace;
2174 }
2175
Daniel Sandler843e8602010-06-07 14:59:01 -04002176 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2177 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002178 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002179 }
2180
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002181 public boolean isAllAppsButtonRank(int rank) {
2182 return mHotseat.isAllAppsButtonRank(rank);
2183 }
2184
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002185 // AllAppsView.Watcher
2186 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002187 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002188 mWorkspace.setVisibility(View.GONE);
2189 }
2190 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002191
2192 /**
2193 * Helper method for the cameraZoomIn/cameraZoomOut animations
2194 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002195 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002196 * @param scaleFactor The scale factor used for the zoom
2197 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002198 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002199 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002200 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002201 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002202
Winson Chung18f41f82012-05-09 13:28:10 -07002203 void disableWallpaperIfInAllApps() {
2204 // Only disable it if we are in all apps
2205 if (mState == State.APPS_CUSTOMIZE) {
2206 if (mAppsCustomizeTabHost != null &&
2207 !mAppsCustomizeTabHost.isTransitioning()) {
2208 updateWallpaperVisibility(false);
2209 }
2210 }
2211 }
2212
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002213 void updateWallpaperVisibility(boolean visible) {
2214 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2215 int curflags = getWindow().getAttributes().flags
2216 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2217 if (wpflags != curflags) {
2218 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2219 }
2220 }
2221
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002222 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2223 if (v instanceof LauncherTransitionable) {
2224 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2225 }
2226 }
2227
Michael Jurkabed61d22012-02-14 22:51:29 -08002228 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2229 if (v instanceof LauncherTransitionable) {
2230 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2231 }
Winson Chung70442722012-02-10 15:43:22 -08002232
2233 // Update the workspace transition step as well
2234 dispatchOnLauncherTransitionStep(v, 0f);
2235 }
2236
2237 private void dispatchOnLauncherTransitionStep(View v, float t) {
2238 if (v instanceof LauncherTransitionable) {
2239 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2240 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002241 }
2242
2243 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2244 if (v instanceof LauncherTransitionable) {
2245 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2246 }
Winson Chung70442722012-02-10 15:43:22 -08002247
2248 // Update the workspace transition step as well
2249 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002250 }
2251
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002252 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002253 * Things to test when changing the following seven functions.
2254 * - Home from workspace
2255 * - from center screen
2256 * - from other screens
2257 * - Home from all apps
2258 * - from center screen
2259 * - from other screens
2260 * - Back from all apps
2261 * - from center screen
2262 * - from other screens
2263 * - Launch app from workspace and quit
2264 * - with back
2265 * - with home
2266 * - Launch app from all apps and quit
2267 * - with back
2268 * - with home
2269 * - Go to a screen that's not the default, then all
2270 * apps, and launch and app, and go back
2271 * - with back
2272 * -with home
2273 * - On workspace, long press power and go back
2274 * - with back
2275 * - with home
2276 * - On all apps, long press power and go back
2277 * - with back
2278 * - with home
2279 * - On workspace, power off
2280 * - On all apps, power off
2281 * - Launch an app and turn off the screen while in that app
2282 * - Go back with home key
2283 * - Go back with back key TODO: make this not go to workspace
2284 * - From all apps
2285 * - From workspace
2286 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2287 * - From all apps
2288 * - From the center workspace
2289 * - From another workspace
2290 */
2291
2292 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002293 * Zoom the camera out from the workspace to reveal 'toView'.
2294 * Assumes that the view to show is anchored at either the very top or very bottom
2295 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002296 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002297 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002298 if (mStateAnimation != null) {
2299 mStateAnimation.cancel();
2300 mStateAnimation = null;
2301 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002302 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002303
Winson Chungf0ea4d32011-06-06 14:27:16 -07002304 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2305 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2306 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002307 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002308 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002309 final int startDelay =
2310 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002311
Michael Jurkab3e22d92011-10-31 15:58:33 -07002312 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002313
Michael Jurkad74c9842011-07-10 12:44:21 -07002314 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002315 Animator workspaceAnim =
2316 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002317
2318 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002319 toView.setScaleX(scale);
2320 toView.setScaleY(scale);
2321 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2322 scaleAnim.
2323 scaleX(1f).scaleY(1f).
2324 setDuration(duration).
2325 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002326
Winson Chungf0ea4d32011-06-06 14:27:16 -07002327 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002328 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002329 final ObjectAnimator alphaAnim = ObjectAnimator
2330 .ofFloat(toView, "alpha", 0f, 1f)
2331 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002332 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002333 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2334 @Override
2335 public void onAnimationUpdate(ValueAnimator animation) {
2336 float t = (Float) animation.getAnimatedValue();
2337 dispatchOnLauncherTransitionStep(fromView, t);
2338 dispatchOnLauncherTransitionStep(toView, t);
2339 }
2340 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002341
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002342 // toView should appear right at the end of the workspace shrink
2343 // animation
2344 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002345 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002346 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002347
2348 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002349 boolean animationCancelled = false;
2350
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002351 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002352 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002353 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002354 // Prepare the position
2355 toView.setTranslationX(0.0f);
2356 toView.setTranslationY(0.0f);
2357 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002358 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002359 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002360 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002361 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002362 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2363 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002364
2365 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2366 // Hide the workspace scrollbar
2367 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002368 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002369 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002370 if (!animationCancelled) {
2371 updateWallpaperVisibility(false);
2372 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002373
2374 // Hide the search bar
2375 mSearchDropTargetBar.hideSearchBar(false);
Adam Cohenf4ddea32011-09-12 13:46:42 -07002376 }
2377
Adam Cohencff6af82011-09-13 14:51:53 -07002378 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002379 public void onAnimationCancel(Animator animation) {
2380 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002381 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002382 });
2383
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002384 if (workspaceAnim != null) {
2385 mStateAnimation.play(workspaceAnim);
2386 }
Michael Jurka1899a362011-11-03 13:50:45 -07002387
2388 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002389 final ViewTreeObserver observer;
2390
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002391 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2392 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002393
2394 // If any of the objects being animated haven't been measured/laid out
2395 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002396 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002397 (mWorkspace.getMeasuredWidth() == 0) ||
2398 (toView.getMeasuredWidth() == 0)) {
2399 observer = mWorkspace.getViewTreeObserver();
2400 delayAnim = true;
2401 } else {
2402 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002403 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002404
2405 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002406 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002407 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2408 public void onGlobalLayout() {
2409 mWorkspace.post(new Runnable() {
2410 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002411 // Check that mStateAnimation hasn't changed while
2412 // we waited for a layout pass
2413 if (mStateAnimation == stateAnimation) {
2414 // Need to update pivots for zoom if layout changed
2415 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002416 dispatchOnLauncherTransitionStart(fromView, animated, false);
2417 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka49779a12012-01-16 04:10:08 -08002418 mStateAnimation.start();
2419 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002420 }
2421 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002422 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002423 }
2424 };
2425 observer.addOnGlobalLayoutListener(delayedStart);
2426 } else {
2427 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002428 dispatchOnLauncherTransitionStart(fromView, animated, false);
2429 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka1899a362011-11-03 13:50:45 -07002430 mStateAnimation.start();
2431 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002432 } else {
2433 toView.setTranslationX(0.0f);
2434 toView.setTranslationY(0.0f);
2435 toView.setScaleX(1.0f);
2436 toView.setScaleY(1.0f);
2437 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002438 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002439
Michael Jurkabed61d22012-02-14 22:51:29 -08002440 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2441 // Hide the workspace scrollbar
2442 mWorkspace.hideScrollingIndicator(true);
2443 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002444
2445 // Hide the search bar
2446 mSearchDropTargetBar.hideSearchBar(false);
Michael Jurkaabded662011-03-04 12:06:57 -08002447 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002448 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002449 dispatchOnLauncherTransitionStart(fromView, animated, false);
2450 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002451 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002452 dispatchOnLauncherTransitionStart(toView, animated, false);
2453 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002454 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002455 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002456 }
2457
2458 /**
2459 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002460 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002461 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002462 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002463 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2464 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002465
Michael Jurkab3e22d92011-10-31 15:58:33 -07002466 if (mStateAnimation != null) {
2467 mStateAnimation.cancel();
2468 mStateAnimation = null;
2469 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002470 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002471
Winson Chungf0ea4d32011-06-06 14:27:16 -07002472 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002473 final int fadeOutDuration =
2474 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002475 final float scaleFactor = (float)
2476 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2477 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002478 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002479 Animator workspaceAnim = null;
2480
2481 if (toState == State.WORKSPACE) {
2482 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2483 workspaceAnim = mWorkspace.getChangeStateAnimation(
2484 Workspace.State.NORMAL, animated, stagger);
2485 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2486 workspaceAnim = mWorkspace.getChangeStateAnimation(
2487 Workspace.State.SPRING_LOADED, animated);
2488 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002489
Michael Jurkab3e22d92011-10-31 15:58:33 -07002490 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002491 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002492 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002493 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002494 final LauncherViewPropertyAnimator scaleAnim =
2495 new LauncherViewPropertyAnimator(fromView);
2496 scaleAnim.
2497 scaleX(scaleFactor).scaleY(scaleFactor).
2498 setDuration(duration).
2499 setInterpolator(new Workspace.ZoomInInterpolator());
2500
2501 final ObjectAnimator alphaAnim = ObjectAnimator
2502 .ofFloat(fromView, "alpha", 1f, 0f)
2503 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002504 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002505 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2506 @Override
2507 public void onAnimationUpdate(ValueAnimator animation) {
2508 float t = 1f - (Float) animation.getAnimatedValue();
2509 dispatchOnLauncherTransitionStep(fromView, t);
2510 dispatchOnLauncherTransitionStep(toView, t);
2511 }
2512 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002513
Michael Jurkabed61d22012-02-14 22:51:29 -08002514 mStateAnimation = new AnimatorSet();
2515
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002516 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2517 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002518
2519 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002520 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002521 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002522 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002523 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002524 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2525 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002526 if (mWorkspace != null) {
2527 mWorkspace.hideScrollingIndicator(false);
2528 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002529 if (onCompleteRunnable != null) {
2530 onCompleteRunnable.run();
2531 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002532 }
2533 });
2534
Winson Chungf0ea4d32011-06-06 14:27:16 -07002535 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002536 if (workspaceAnim != null) {
2537 mStateAnimation.play(workspaceAnim);
2538 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002539 dispatchOnLauncherTransitionStart(fromView, animated, true);
2540 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002541 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002542 } else {
2543 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002544 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002545 dispatchOnLauncherTransitionStart(fromView, animated, true);
2546 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002547 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002548 dispatchOnLauncherTransitionStart(toView, animated, true);
2549 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002550 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002551 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002552 }
2553
Michael Jurkae326f182011-11-21 14:05:46 -08002554 @Override
2555 public void onTrimMemory(int level) {
2556 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002557 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002558 mAppsCustomizeTabHost.onTrimMemory();
2559 }
2560 }
2561
Winson Chung18f41f82012-05-09 13:28:10 -07002562 @Override
2563 public void onWindowFocusChanged(boolean hasFocus) {
2564 if (!hasFocus) {
2565 // When another window occludes launcher (like the notification shade, or recents),
2566 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2567 updateWallpaperVisibility(true);
2568 } else {
2569 // When launcher has focus again, disable the wallpaper if we are in AllApps
2570 disableWallpaperIfInAllApps();
2571 }
2572 }
2573
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002574 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002575 showWorkspace(animated, null);
2576 }
2577
2578 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002579 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002580 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002581 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002582 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002583
Winson Chungc7d2b602012-05-16 17:02:20 -07002584 // Show the search bar (only animate if we were showing the drop target bar in spring
2585 // loaded mode)
2586 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2587
Michael Jurkab737ee62011-11-15 15:57:22 -08002588 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002589 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002590
2591 // Set focus to the AppsCustomize button
2592 if (mAllAppsButton != null) {
2593 mAllAppsButton.requestFocus();
2594 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002595 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002596
Michael Jurkab737ee62011-11-15 15:57:22 -08002597 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002598
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002599 // Change the state *after* we've called all the transition code
2600 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002601
Winson Chung5fb63472011-02-02 17:03:37 -08002602 // Resume the auto-advance of widgets
2603 mUserPresent = true;
2604 updateRunning();
2605
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002606 // send an accessibility event to announce the context change
2607 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002608 }
2609
Michael Jurkab3e22d92011-10-31 15:58:33 -07002610 void showAllApps(boolean animated) {
2611 if (mState != State.WORKSPACE) return;
2612
2613 showAppsCustomizeHelper(animated, false);
2614 mAppsCustomizeTabHost.requestFocus();
2615
Michael Jurkab3e22d92011-10-31 15:58:33 -07002616 // Change the state *after* we've called all the transition code
2617 mState = State.APPS_CUSTOMIZE;
2618
2619 // Pause the auto-advance of widgets until we are out of AllApps
2620 mUserPresent = false;
2621 updateRunning();
2622 closeFolder();
2623
2624 // Send an accessibility event to announce the context change
2625 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2626 }
2627
Adam Cohen7777d962011-08-18 18:58:38 -07002628 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002629 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002630 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002631 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002632 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002633 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002634 }
Adam Cohen7777d962011-08-18 18:58:38 -07002635
Adam Cohened66b2b2012-01-23 17:28:51 -08002636 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2637 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002638 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2639
Winson Chunge7a03942011-08-05 15:05:12 -07002640 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002641 @Override
2642 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002643 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002644 // Before we show workspace, hide all apps again because
2645 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2646 // clean up our state transition functions
2647 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002648 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002649 } else {
2650 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002651 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002652 }
2653 }, (extendedDelay ?
2654 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2655 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2656 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002657
Michael Jurkad3ef3062010-11-23 16:23:58 -08002658 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002659 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002660 final boolean animated = true;
2661 final boolean springLoaded = true;
2662 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002663 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002664 }
2665 // Otherwise, we are not in spring loaded mode, so don't do anything.
2666 }
2667
Michael Jurkab737ee62011-11-15 15:57:22 -08002668 void hideDockDivider() {
2669 if (mQsbDivider != null && mDockDivider != null) {
2670 mQsbDivider.setVisibility(View.INVISIBLE);
2671 mDockDivider.setVisibility(View.INVISIBLE);
2672 }
2673 }
2674
2675 void showDockDivider(boolean animated) {
2676 if (mQsbDivider != null && mDockDivider != null) {
2677 mQsbDivider.setVisibility(View.VISIBLE);
2678 mDockDivider.setVisibility(View.VISIBLE);
2679 if (mDividerAnimator != null) {
2680 mDividerAnimator.cancel();
2681 mQsbDivider.setAlpha(1f);
2682 mDockDivider.setAlpha(1f);
2683 mDividerAnimator = null;
2684 }
2685 if (animated) {
2686 mDividerAnimator = new AnimatorSet();
2687 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2688 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2689 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2690 mDividerAnimator.start();
2691 }
2692 }
2693 }
2694
Michael Jurkab3e22d92011-10-31 15:58:33 -07002695 void lockAllApps() {
2696 // TODO
2697 }
2698
2699 void unlockAllApps() {
2700 // TODO
2701 }
2702
Adam Cohenfc53cd22011-07-20 15:45:11 -07002703 public boolean isAllAppsCustomizeOpen() {
2704 return mState == State.APPS_CUSTOMIZE;
2705 }
2706
Winson Chungf0ea4d32011-06-06 14:27:16 -07002707 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002708 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002709 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002710 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002711 if (!LauncherApplication.isScreenLarge()) {
2712 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002713 if (mHotseat.getAlpha() != 1f) {
2714 int duration = mSearchDropTargetBar.getTransitionInDuration();
2715 mHotseat.animate().alpha(1f).setDuration(duration);
2716 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002717 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002718 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002719 }
2720 }
2721 }
2722
2723 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002724 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002725 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002726 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002727 if (!LauncherApplication.isScreenLarge()) {
2728 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002729 if (mHotseat.getAlpha() != 0f) {
2730 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2731 mHotseat.animate().alpha(0f).setDuration(duration);
2732 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002733 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002734 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002735 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002736 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002737 }
2738
Patrick Dubroy5f445422011-02-18 14:35:21 -08002739 /**
2740 * Add an item from all apps or customize onto the given workspace screen.
2741 * If layout is null, add to the current screen.
2742 */
2743 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002744 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002745 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002746 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002747 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002748
Winson Chungdff8ebb2011-09-08 17:25:31 -07002749 /** Maps the current orientation to an index for referencing orientation correct global icons */
2750 private int getCurrentOrientationIndexForGlobalIcons() {
2751 // default - 0, landscape - 1
2752 switch (getResources().getConfiguration().orientation) {
2753 case Configuration.ORIENTATION_LANDSCAPE:
2754 return 1;
2755 default:
2756 return 0;
2757 }
2758 }
2759
Michael Jurkaae65ee32012-04-30 11:45:54 -07002760 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002761 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002762 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002763 // Look for the toolbar icon specified in the activity meta-data
2764 Bundle metaData = packageManager.getActivityInfo(
2765 activityName, PackageManager.GET_META_DATA).metaData;
2766 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002767 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002768 if (iconResId != 0) {
2769 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002770 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002771 }
2772 }
2773 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002774 // This can happen if the activity defines an invalid drawable
2775 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2776 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002777 } catch (Resources.NotFoundException nfe) {
2778 // This can happen if the activity defines an invalid drawable
2779 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2780 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002781 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002782 return null;
2783 }
2784
2785 // if successful in getting icon, return it; otherwise, set button to use default drawable
2786 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002787 int buttonId, ComponentName activityName, int fallbackDrawableId,
2788 String toolbarResourceName) {
2789 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002790 Resources r = getResources();
2791 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2792 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002793
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002794 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002795 // If we were unable to find the icon via the meta-data, use a generic one
2796 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002797 toolbarIcon = r.getDrawable(fallbackDrawableId);
2798 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002799 if (button != null) {
2800 button.setCompoundDrawables(toolbarIcon, null, null, null);
2801 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002802 return null;
2803 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002804 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002805 if (button != null) {
2806 button.setCompoundDrawables(toolbarIcon, null, null, null);
2807 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002808 return toolbarIcon.getConstantState();
2809 }
2810 }
2811
2812 // if successful in getting icon, return it; otherwise, set button to use default drawable
2813 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002814 int buttonId, ComponentName activityName, int fallbackDrawableId,
2815 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002816 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002817 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002818
Michael Jurka19e0fc52011-07-22 18:00:21 -07002819 if (button != null) {
2820 // If we were unable to find the icon via the meta-data, use a
2821 // generic one
2822 if (toolbarIcon == null) {
2823 button.setImageResource(fallbackDrawableId);
2824 } else {
2825 button.setImageDrawable(toolbarIcon);
2826 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002827 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002828
2829 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2830
Michael Jurka0423dcf2010-10-05 14:56:18 -07002831 }
2832
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002833 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2834 TextView button = (TextView) findViewById(buttonId);
2835 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2836 }
2837
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002838 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002839 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002840 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002841 }
2842
Winson Chungbb185bd2011-11-21 12:31:42 -08002843 private void invalidatePressedFocusedStates(View container, View button) {
2844 if (container instanceof HolographicLinearLayout) {
2845 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2846 layout.invalidatePressedFocusedStates();
2847 } else if (button instanceof HolographicImageView) {
2848 HolographicImageView view = (HolographicImageView) button;
2849 view.invalidatePressedFocusedStates();
2850 }
2851 }
2852
Winson Chungc51db6a2011-10-05 11:44:49 -07002853 private boolean updateGlobalSearchIcon() {
2854 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002855 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2856 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002857 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002858 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002859 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002860
Winson Chung1cad91e2011-05-25 17:41:01 -07002861 final SearchManager searchManager =
2862 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2863 ComponentName activityName = searchManager.getGlobalSearchActivity();
2864 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002865 int coi = getCurrentOrientationIndexForGlobalIcons();
2866 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002867 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2868 TOOLBAR_SEARCH_ICON_METADATA_NAME);
2869 if (sGlobalSearchIcon[coi] == null) {
2870 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2871 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2872 TOOLBAR_ICON_METADATA_NAME);
2873 }
2874
Winson Chung649723c2011-07-06 20:41:23 -07002875 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002876 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2877 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002878 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002879 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002880 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002881 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002882 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002883 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2884 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2885 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002886 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07002887 if (voiceButtonProxy != null) {
2888 voiceButtonProxy.setVisibility(View.GONE);
2889 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002890 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002891 }
2892 }
2893
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002894 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002895 final View searchButtonContainer = findViewById(R.id.search_button_container);
2896 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002897 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002898 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002899 }
2900
Winson Chungc51db6a2011-10-05 11:44:49 -07002901 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002902 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002903 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002904 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002905 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002906
Winson Chungc51db6a2011-10-05 11:44:49 -07002907 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07002908 final SearchManager searchManager =
2909 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2910 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2911
2912 ComponentName activityName = null;
2913 if (globalSearchActivity != null) {
2914 // Check if the global search activity handles voice search
2915 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2916 intent.setPackage(globalSearchActivity.getPackageName());
2917 activityName = intent.resolveActivity(getPackageManager());
2918 }
2919
2920 if (activityName == null) {
2921 // Fallback: check if an activity other than the global search activity
2922 // resolves this
2923 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2924 activityName = intent.resolveActivity(getPackageManager());
2925 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002926 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002927 int coi = getCurrentOrientationIndexForGlobalIcons();
2928 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002929 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2930 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
2931 if (sVoiceSearchIcon[coi] == null) {
2932 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2933 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2934 TOOLBAR_ICON_METADATA_NAME);
2935 }
Winson Chung649723c2011-07-06 20:41:23 -07002936 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002937 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002938 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07002939 if (voiceButtonProxy != null) {
2940 voiceButtonProxy.setVisibility(View.VISIBLE);
2941 }
Winson Chungbb185bd2011-11-21 12:31:42 -08002942 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002943 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002944 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002945 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002946 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002947 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07002948 if (voiceButtonProxy != null) {
2949 voiceButtonProxy.setVisibility(View.GONE);
2950 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002951 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002952 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002953 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002954
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002955 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002956 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2957 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002958 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002959 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002960 }
2961
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002962 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002963 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002964 */
2965 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002966 final View marketButton = findViewById(R.id.market_button);
2967 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2968 // Find the app market activity by resolving an intent.
2969 // (If multiple app markets are installed, it will return the ResolverActivity.)
2970 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002971 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002972 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002973 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002974 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002975 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
2976 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07002977 marketButton.setVisibility(View.VISIBLE);
2978 } else {
2979 // We should hide and disable the view so that we don't try and restore the visibility
2980 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2981 marketButton.setVisibility(View.GONE);
2982 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002983 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002984 }
2985
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002986 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002987 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002988 }
2989
Michael Jurkad7c28052012-04-27 15:43:36 -07002990 @Override
2991 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2992 boolean result = super.dispatchPopulateAccessibilityEvent(event);
2993 final List<CharSequence> text = event.getText();
2994 text.clear();
2995 text.add(getString(R.string.home));
2996 return result;
2997 }
2998
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002999 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003000 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003001 */
3002 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3003 @Override
3004 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003005 closeSystemDialogs();
3006 }
3007 }
3008
3009 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003010 * Receives notifications whenever the appwidgets are reset.
3011 */
3012 private class AppWidgetResetObserver extends ContentObserver {
3013 public AppWidgetResetObserver() {
3014 super(new Handler());
3015 }
3016
3017 @Override
3018 public void onChange(boolean selfChange) {
3019 onAppWidgetReset();
3020 }
3021 }
3022
3023 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003024 * If the activity is currently paused, signal that we need to re-run the loader
3025 * in onResume.
3026 *
3027 * This needs to be called from incoming places where resources might have been loaded
3028 * while we are paused. That is becaues the Configuration might be wrong
3029 * when we're not running, and if it comes back to what it was when we
3030 * were paused, we are not restarted.
3031 *
3032 * Implementation of the method from LauncherModel.Callbacks.
3033 *
3034 * @return true if we are currently paused. The caller might be able to
3035 * skip some work in that case since we will come back again.
3036 */
3037 public boolean setLoadOnResume() {
3038 if (mPaused) {
3039 Log.i(TAG, "setLoadOnResume");
3040 mOnResumeNeedsLoad = true;
3041 return true;
3042 } else {
3043 return false;
3044 }
3045 }
3046
3047 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003048 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003049 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003050 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003051 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003052 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003053 } else {
3054 return SCREEN_COUNT / 2;
3055 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003056 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003057
Joe Onorato9c1289c2009-08-17 11:03:03 -04003058 /**
3059 * Refreshes the shortcuts shown on the workspace.
3060 *
3061 * Implementation of the method from LauncherModel.Callbacks.
3062 */
3063 public void startBinding() {
3064 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003065
Winson Chungf0c6ae02012-03-21 16:10:31 -07003066 mNewShortcutAnimatePage = -1;
3067 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003068 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003069 int count = workspace.getChildCount();
3070 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003071 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003072 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3073 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003074 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003075 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003076 if (mHotseat != null) {
3077 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003078 }
3079 }
3080
3081 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003082 * Bind the items start-end from the list.
3083 *
3084 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003085 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003086 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003087 setLoadOnResume();
3088
Winson Chungf0c6ae02012-03-21 16:10:31 -07003089 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3090 Set<String> newApps = new HashSet<String>();
3091 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3092
3093 Workspace workspace = mWorkspace;
3094 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003095 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003096
3097 // Short circuit if we are loading dock items for a configuration which has no dock
3098 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3099 mHotseat == null) {
3100 continue;
3101 }
3102
Joe Onorato9c1289c2009-08-17 11:03:03 -04003103 switch (item.itemType) {
3104 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3105 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003106 ShortcutInfo info = (ShortcutInfo) item;
3107 String uri = info.intent.toUri(0).toString();
3108 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003109 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3110 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003111 if (newApps.contains(uri)) {
3112 newApps.remove(uri);
3113
3114 // Prepare the view to be animated up
3115 shortcut.setAlpha(0f);
3116 shortcut.setScaleX(0f);
3117 shortcut.setScaleY(0f);
3118 mNewShortcutAnimatePage = item.screen;
3119 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3120 mNewShortcutAnimateViews.add(shortcut);
3121 }
3122 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003123 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003124 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003125 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003126 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003127 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003128 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3129 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003130 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003132 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003133
Joe Onorato9c1289c2009-08-17 11:03:03 -04003134 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003135 }
3136
Joe Onorato9c1289c2009-08-17 11:03:03 -04003137 /**
3138 * Implementation of the method from LauncherModel.Callbacks.
3139 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003140 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003141 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003142 sFolders.clear();
3143 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003144 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003145
3146 /**
3147 * Add the views for a widget to the workspace.
3148 *
3149 * Implementation of the method from LauncherModel.Callbacks.
3150 */
3151 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003152 setLoadOnResume();
3153
Daniel Sandler843e8602010-06-07 14:59:01 -04003154 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3155 if (DEBUG_WIDGETS) {
3156 Log.d(TAG, "bindAppWidget: " + item);
3157 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003158 final Workspace workspace = mWorkspace;
3159
3160 final int appWidgetId = item.appWidgetId;
3161 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003162 if (DEBUG_WIDGETS) {
3163 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3164 }
3165
Joe Onorato9c1289c2009-08-17 11:03:03 -04003166 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3167
Joe Onorato9c1289c2009-08-17 11:03:03 -04003168 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3169 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003170 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003171
Winson Chung3d503fb2011-07-13 17:25:49 -07003172 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003173 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003174 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3175
Joe Onorato9c1289c2009-08-17 11:03:03 -04003176 workspace.requestLayout();
3177
Daniel Sandler843e8602010-06-07 14:59:01 -04003178 if (DEBUG_WIDGETS) {
3179 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3180 + (SystemClock.uptimeMillis()-start) + "ms");
3181 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003182 }
3183
3184 /**
3185 * Callback saying that there aren't any more items to bind.
3186 *
3187 * Implementation of the method from LauncherModel.Callbacks.
3188 */
3189 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003190 setLoadOnResume();
3191
Joe Onorato9c1289c2009-08-17 11:03:03 -04003192 if (mSavedState != null) {
3193 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003194 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003195 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003196 mSavedState = null;
3197 }
3198
3199 if (mSavedInstanceState != null) {
3200 super.onRestoreInstanceState(mSavedInstanceState);
3201 mSavedInstanceState = null;
3202 }
3203
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003204 // If we received the result of any pending adds while the loader was running (e.g. the
3205 // widget configuration forced an orientation change), process them now.
3206 for (int i = 0; i < sPendingAddList.size(); i++) {
3207 completeAdd(sPendingAddList.get(i));
3208 }
3209 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003210
Winson Chungc51db6a2011-10-05 11:44:49 -07003211 // Update the market app icon as necessary (the other icons will be managed in response to
3212 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003213 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003214
Winson Chungf0c6ae02012-03-21 16:10:31 -07003215 // Animate up any icons as necessary
3216 if (mVisible || mWorkspaceLoading) {
3217 Runnable newAppsRunnable = new Runnable() {
3218 @Override
3219 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003220 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003221 }
3222 };
Winson Chunga2413752012-04-03 14:22:34 -07003223
3224 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3225 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3226 if (canRunNewAppsAnimation()) {
3227 // If the user has not interacted recently, then either snap to the new page to show
3228 // the new-apps animation or just run them if they are to appear on the current page
3229 if (willSnapPage) {
3230 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3231 } else {
3232 runNewAppsAnimation(false);
3233 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003234 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003235 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003236 // are on another page (or just normally if they are added to the current page)
3237 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003238 }
3239 }
3240
3241 mWorkspaceLoading = false;
3242 }
3243
Winson Chunga2413752012-04-03 14:22:34 -07003244 private boolean canRunNewAppsAnimation() {
3245 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3246 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3247 }
3248
Winson Chungf0c6ae02012-03-21 16:10:31 -07003249 /**
3250 * Runs a new animation that scales up icons that were added while Launcher was in the
3251 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003252 *
3253 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003254 */
Winson Chunga2413752012-04-03 14:22:34 -07003255 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003256 AnimatorSet anim = new AnimatorSet();
3257 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003258
3259 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003260 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3261 @Override
3262 public int compare(View a, View b) {
3263 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3264 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3265 int cellCountX = LauncherModel.getCellCountX();
3266 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3267 }
3268 });
Winson Chunga2413752012-04-03 14:22:34 -07003269
3270 // Animate each of the views in place (or show them immediately if requested)
3271 if (immediate) {
3272 for (View v : mNewShortcutAnimateViews) {
3273 v.setAlpha(1f);
3274 v.setScaleX(1f);
3275 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003276 }
Winson Chunga2413752012-04-03 14:22:34 -07003277 } else {
3278 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3279 View v = mNewShortcutAnimateViews.get(i);
3280 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3281 PropertyValuesHolder.ofFloat("alpha", 1f),
3282 PropertyValuesHolder.ofFloat("scaleX", 1f),
3283 PropertyValuesHolder.ofFloat("scaleY", 1f));
3284 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3285 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3286 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3287 bounceAnims.add(bounceAnim);
3288 }
3289 anim.playTogether(bounceAnims);
3290 anim.addListener(new AnimatorListenerAdapter() {
3291 @Override
3292 public void onAnimationEnd(Animator animation) {
3293 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3294 }
3295 });
3296 anim.start();
3297 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003298
3299 // Clean up
3300 mNewShortcutAnimatePage = -1;
3301 mNewShortcutAnimateViews.clear();
3302 new Thread("clearNewAppsThread") {
3303 public void run() {
3304 mSharedPrefs.edit()
3305 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3306 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3307 .commit();
3308 }
3309 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003310 }
3311
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003312 @Override
3313 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003314 boolean searchVisible = updateGlobalSearchIcon();
3315 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3316 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003317 }
3318
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003319 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003320 * Add the icons for all apps.
3321 *
3322 * Implementation of the method from LauncherModel.Callbacks.
3323 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003324 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3325 // Remove the progress bar entirely; we could also make it GONE
3326 // but better to remove it since we know it's not going to be used
3327 View progressBar = mAppsCustomizeTabHost.
3328 findViewById(R.id.apps_customize_progress_bar);
3329 if (progressBar != null) {
3330 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003331 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003332 // We just post the call to setApps so the user sees the progress bar
3333 // disappear-- otherwise, it just looks like the progress bar froze
3334 // which doesn't look great
3335 mAppsCustomizeTabHost.post(new Runnable() {
3336 public void run() {
3337 if (mAppsCustomizeContent != null) {
3338 mAppsCustomizeContent.setApps(apps);
3339 }
3340 }
3341 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003342 }
3343
3344 /**
3345 * A package was installed.
3346 *
3347 * Implementation of the method from LauncherModel.Callbacks.
3348 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003349 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003350 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003351
Winson Chung785d2eb2011-04-14 16:08:02 -07003352 if (mAppsCustomizeContent != null) {
3353 mAppsCustomizeContent.addApps(apps);
3354 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003355 }
3356
3357 /**
3358 * A package was updated.
3359 *
3360 * Implementation of the method from LauncherModel.Callbacks.
3361 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003362 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003363 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003364 if (mWorkspace != null) {
3365 mWorkspace.updateShortcuts(apps);
3366 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003367
Winson Chung785d2eb2011-04-14 16:08:02 -07003368 if (mAppsCustomizeContent != null) {
3369 mAppsCustomizeContent.updateApps(apps);
3370 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003371 }
3372
3373 /**
3374 * A package was uninstalled.
3375 *
3376 * Implementation of the method from LauncherModel.Callbacks.
3377 */
Joe Onorato36115782010-06-17 13:28:48 -04003378 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003379 if (permanent) {
3380 mWorkspace.removeItems(apps);
3381 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003382
Winson Chung785d2eb2011-04-14 16:08:02 -07003383 if (mAppsCustomizeContent != null) {
3384 mAppsCustomizeContent.removeApps(apps);
3385 }
Winson Chunga1820962011-10-03 16:31:06 -07003386
3387 // Notify the drag controller
3388 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003389 }
Joe Onoratobe386092009-11-17 17:32:16 -08003390
3391 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003392 * A number of packages were updated.
3393 */
3394 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003395 if (mAppsCustomizeContent != null) {
3396 mAppsCustomizeContent.onPackagesUpdated();
3397 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003398 }
3399
Winson Chung400438b2011-01-16 17:53:48 -08003400 private int mapConfigurationOriActivityInfoOri(int configOri) {
3401 final Display d = getWindowManager().getDefaultDisplay();
3402 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3403 switch (d.getRotation()) {
3404 case Surface.ROTATION_0:
3405 case Surface.ROTATION_180:
3406 // We are currently in the same basic orientation as the natural orientation
3407 naturalOri = configOri;
3408 break;
3409 case Surface.ROTATION_90:
3410 case Surface.ROTATION_270:
3411 // We are currently in the other basic orientation to the natural orientation
3412 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3413 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3414 break;
3415 }
3416
3417 int[] oriMap = {
3418 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3419 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3420 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3421 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3422 };
3423 // Since the map starts at portrait, we need to offset if this device's natural orientation
3424 // is landscape.
3425 int indexOffset = 0;
3426 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3427 indexOffset = 1;
3428 }
3429 return oriMap[(d.getRotation() + indexOffset) % 4];
3430 }
Adam Cohen446e9402011-09-15 18:21:21 -07003431
Winson Chung4b919f82012-05-01 10:44:08 -07003432 public boolean isRotationEnabled() {
3433 boolean forceEnableRotation = "true".equalsIgnoreCase(SystemProperties.get(
3434 FORCE_ENABLE_ROTATION_PROPERTY, "false"));
3435 boolean enableRotation = forceEnableRotation ||
3436 getResources().getBoolean(R.bool.allow_rotation);
3437 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003438 }
Winson Chung4b919f82012-05-01 10:44:08 -07003439 public void lockScreenOrientation() {
3440 if (isRotationEnabled()) {
3441 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3442 .getConfiguration().orientation));
3443 }
3444 }
3445 public void unlockScreenOrientation(boolean immediate) {
3446 if (isRotationEnabled()) {
3447 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003448 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003449 } else {
3450 mHandler.postDelayed(new Runnable() {
3451 public void run() {
3452 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3453 }
3454 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003455 }
Winson Chung4b919f82012-05-01 10:44:08 -07003456 }
Winson Chung400438b2011-01-16 17:53:48 -08003457 }
3458
Winson Chung82f55532011-08-09 14:14:23 -07003459 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003460 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003461 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003462 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003463
Winson Chung7d7541e2011-09-16 20:14:36 -07003464 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003465 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003466 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3467 Cling cling = (Cling) findViewById(clingId);
3468 if (cling != null) {
3469 cling.init(this, positionData);
3470 cling.setVisibility(View.VISIBLE);
3471 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Svetoslav Ganov55d225d2012-05-22 15:19:14 -07003472 cling.requestAccessibilityFocus();
Winson Chung7d7541e2011-09-16 20:14:36 -07003473 if (animate) {
3474 cling.buildLayer();
3475 cling.setAlpha(0f);
3476 cling.animate()
3477 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003478 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003479 .setDuration(SHOW_CLING_DURATION)
3480 .setStartDelay(delay)
3481 .start();
3482 } else {
3483 cling.setAlpha(1f);
3484 }
3485 }
3486 return cling;
3487 }
3488 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003489 if (cling != null) {
3490 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003491 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003492 anim.addListener(new AnimatorListenerAdapter() {
3493 public void onAnimationEnd(Animator animation) {
3494 cling.setVisibility(View.GONE);
3495 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003496 // We should update the shared preferences on a background thread
3497 new Thread("dismissClingThread") {
3498 public void run() {
3499 SharedPreferences.Editor editor = mSharedPrefs.edit();
3500 editor.putBoolean(flag, true);
3501 editor.commit();
3502 }
3503 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003504 };
3505 });
3506 anim.start();
3507 }
3508 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003509 private void removeCling(int id) {
3510 final View cling = findViewById(id);
3511 if (cling != null) {
3512 final ViewGroup parent = (ViewGroup) cling.getParent();
3513 parent.post(new Runnable() {
3514 @Override
3515 public void run() {
3516 parent.removeView(cling);
3517 }
3518 });
3519 }
3520 }
Michael Jurka974c3862012-05-22 22:00:31 -07003521
3522 private boolean skipCustomClingIfNoAccounts() {
3523 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3524 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3525 if (customCling) {
3526 AccountManager am = AccountManager.get(this);
3527 Account[] accounts = am.getAccountsByType("com.google");
3528 return accounts.length == 0;
3529 }
3530 return false;
3531 }
3532
Winson Chung7d7541e2011-09-16 20:14:36 -07003533 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003534 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003535 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003536 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3537 !skipCustomClingIfNoAccounts() ) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003538 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003539 } else {
3540 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003541 }
3542 }
3543 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003544 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003545 if (isClingsEnabled() &&
3546 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003547 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003548 } else {
3549 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003550 }
3551 }
3552 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003553 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003554 if (isClingsEnabled() &&
3555 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3556 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003557 } else {
3558 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003559 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003560 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003561 }
3562 public boolean isFolderClingVisible() {
3563 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003564 if (cling != null) {
3565 return cling.getVisibility() == View.VISIBLE;
3566 }
3567 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003568 }
Winson Chung82f55532011-08-09 14:14:23 -07003569 public void dismissWorkspaceCling(View v) {
3570 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003571 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003572 }
3573 public void dismissAllAppsCling(View v) {
3574 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003575 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3576 }
3577 public void dismissFolderCling(View v) {
3578 Cling cling = (Cling) findViewById(R.id.folder_cling);
3579 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003580 }
3581
Winson Chung80baf5a2010-08-09 16:03:15 -07003582 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003583 * Prints out out state for debugging.
3584 */
3585 public void dumpState() {
3586 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003587 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003588 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3589 Log.d(TAG, "mRestoring=" + mRestoring);
3590 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3591 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003592 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003593 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003594
Winson Chung785d2eb2011-04-14 16:08:02 -07003595 if (mAppsCustomizeContent != null) {
3596 mAppsCustomizeContent.dumpState();
3597 }
Joe Onoratobe386092009-11-17 17:32:16 -08003598 Log.d(TAG, "END launcher2 dump state");
3599 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003600
3601 @Override
3602 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3603 super.dump(prefix, fd, writer, args);
3604 writer.println(" ");
3605 writer.println("Debug logs: ");
3606 for (int i = 0; i < sDumpLogs.size(); i++) {
3607 writer.println(" " + sDumpLogs.get(i));
3608 }
3609 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003610}
Michael Jurka2763be32011-02-24 11:19:57 -08003611
Michael Jurkaabded662011-03-04 12:06:57 -08003612interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003613 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003614 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003615 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003616 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003617 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003618}