blob: 984cf3162f86edb1c9059b808683af0d765b1a0a [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070041import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070042import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070045import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070047import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080048import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070049import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070050import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040051import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070052import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020054import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080055import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070056import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070057import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040058import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080059import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070060import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080061import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.text.Selection;
63import android.text.SpannableStringBuilder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070065import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080066import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080067import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.view.KeyEvent;
69import android.view.LayoutInflater;
70import android.view.Menu;
71import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070072import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080073import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080075import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070076import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080077import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080078import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070079import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080080import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070081import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070082import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080083import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070085import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070086import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070087import android.widget.TextView;
88import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070089
Adam Cohendf2cc412011-04-27 16:56:57 -070090import com.android.common.Search;
91import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070092import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080093
Adam Cohenc0dcf592011-06-01 15:30:43 -070094import java.io.DataInputStream;
95import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070096import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070097import java.io.FileNotFoundException;
98import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070099import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700101import java.util.Collection;
102import java.util.Collections;
103import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700104import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700105import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700106import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109/**
110 * Default launcher application.
111 */
Michael Jurka946ad472010-07-09 18:05:18 -0700112public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700113 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
114 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800115 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700116 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
Joe Onorato9c1289c2009-08-17 11:03:03 -0400118 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400119 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700120 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700121
Winson Chung70d72102011-08-12 11:24:35 -0700122 private static final int MENU_GROUP_WALLPAPER = 1;
123 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
124 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700125 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
126 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
128 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700129 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 private static final int REQUEST_PICK_APPLICATION = 6;
131 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700132 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700133 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Michael Jurka8b805b12012-04-18 14:23:14 -0700135 private static final int REQUEST_BIND_APPWIDGET = 11;
136
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
138
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800139 static final int SCREEN_COUNT = 5;
140 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Romain Guy98d01652009-06-30 16:21:04 -0700142 private static final String PREFERENCES = "launcher.preferences";
Adam Cohen23a4d302011-12-01 17:26:44 -0800143 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Winson Chung2672ff92012-05-04 16:22:30 -0700145 // The Intent extra that defines whether to ignore the launch animation
146 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
147 "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
148
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 // Type: int
150 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700151 // Type: int
152 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700154 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
155 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
157 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700158 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 // Type: boolean
162 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
163 // Type: long
164 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
165
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700166 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700167 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
168 "com.android.launcher.toolbar_search_icon";
169 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
170 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700171
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700172 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700173 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700174 private State mState = State.WORKSPACE;
175 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800176 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700177
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700179 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
180 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700181 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700182 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800185 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Winson Chunga2413752012-04-03 14:22:34 -0700187 // How long to wait before the new-shortcut animation automatically pans the workspace
188 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
189
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800190 private final BroadcastReceiver mCloseSystemDialogsReceiver
191 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800192 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private LayoutInflater mInflater;
195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800197 private View mQsbDivider;
198 private View mDockDivider;
199 private DragLayer mDragLayer;
200 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700201
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700202 private AppWidgetManager mAppWidgetManager;
203 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700204
Michael Jurkac9d95c52011-08-29 14:03:34 -0700205 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700206 private int[] mTmpAddItemCellCoordinates = new int[2];
207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private FolderInfo mFolderInfo;
209
Winson Chung3d503fb2011-07-13 17:25:49 -0700210 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800211 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700212
Winson Chungc51db6a2011-10-05 11:44:49 -0700213 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700214 private AppsCustomizeTabHost mAppsCustomizeTabHost;
215 private AppsCustomizePagedView mAppsCustomizeContent;
216 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 private Bundle mSavedState;
219
220 private SpannableStringBuilder mDefaultKeySsb = null;
221
Joe Onorato9c1289c2009-08-17 11:03:03 -0400222 private boolean mWorkspaceLoading = true;
223
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800224 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 private boolean mRestoring;
226 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700227 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228
229 private Bundle mSavedInstanceState;
230
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800232 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800233 private boolean mUserPresent = true;
234 private boolean mVisible = false;
235 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700237 private static LocaleConfiguration sLocaleConfiguration = null;
238
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700239 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700240
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700241 private Intent mAppMarketIntent = null;
242
Adam Cohended9f8d2010-11-03 13:25:16 -0700243 // Related to the auto-advancing of widgets
244 private final int ADVANCE_MSG = 1;
245 private final int mAdvanceInterval = 20000;
246 private final int mAdvanceStagger = 250;
247 private long mAutoAdvanceSentTime;
248 private long mAutoAdvanceTimeLeft = -1;
249 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
250 new HashMap<View, AppWidgetProviderInfo>();
251
Winson Chung400438b2011-01-16 17:53:48 -0800252 // Determines how long to wait after a rotation before restoring the screen orientation to
253 // match the sensor state.
254 private final int mRestoreScreenOrientationDelay = 500;
255
Michael Jurka4ef207b2010-11-29 17:05:45 -0800256 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700257 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
258 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
259 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800260
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700261 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Michael Jurka8b805b12012-04-18 14:23:14 -0700262 PendingAddWidgetInfo mWidgetBeingBoundOrConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700263
Winson Chung46353de2012-02-16 14:05:10 -0800264 // We only want to get the SharedPreferences once since it does an FS stat each time we get
265 // it from the context.
266 private SharedPreferences mSharedPrefs;
267
Winson Chungf0c6ae02012-03-21 16:10:31 -0700268 // Holds the page that we need to animate to, and the icon views that we need to animate up
269 // when we scroll to that page on resume.
270 private int mNewShortcutAnimatePage = -1;
271 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700272
Michael Jurkaddd62e92011-02-16 17:49:14 -0800273 private BubbleTextView mWaitingForResume;
274
Michael Jurkac1f5d262011-09-30 19:32:27 -0700275 private Runnable mBuildLayersRunnable = new Runnable() {
276 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700277 if (mWorkspace != null) {
278 mWorkspace.buildPageHardwareLayers();
279 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700280 }
281 };
282
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800283 private static ArrayList<PendingAddArguments> sPendingAddList
284 = new ArrayList<PendingAddArguments>();
285
286 private static class PendingAddArguments {
287 int requestCode;
288 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700289 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800290 int screen;
291 int cellX;
292 int cellY;
293 }
294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 @Override
296 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700297 if (DEBUG_STRICT_MODE) {
298 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
299 .detectDiskReads()
300 .detectDiskWrites()
301 .detectNetwork() // or .detectAll() for all detectable problems
302 .penaltyLog()
303 .build());
304 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
305 .detectLeakedSqlLiteObjects()
306 .detectLeakedClosableObjects()
307 .penaltyLog()
308 .penaltyDeath()
309 .build());
310 }
311
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800313 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700314 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
315 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800316 mModel = app.setLauncher(this);
317 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400318 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700320
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700321 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700322 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
323 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700324
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200326 android.os.Debug.startMethodTracing(
327 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 }
329
330 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 setContentView(R.layout.launcher);
332 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700333 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800335 registerContentObservers();
336
Joe Onorato7c312c12009-08-13 21:36:53 -0700337 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700338
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 mSavedState = savedInstanceState;
340 restoreState(mSavedState);
341
Winson Chunga12a2502010-12-20 14:41:35 -0800342 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700343 if (mAppsCustomizeContent != null) {
344 mAppsCustomizeContent.onPackagesUpdated();
345 }
Winson Chunga12a2502010-12-20 14:41:35 -0800346
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 if (PROFILE_STARTUP) {
348 android.os.Debug.stopMethodTracing();
349 }
350
351 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700352 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 }
354
Michael Jurkac57b7a82011-08-09 22:02:20 -0700355 if (!mModel.isAllAppsLoaded()) {
356 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
357 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
358 }
359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 // For handling default keys
361 mDefaultKeySsb = new SpannableStringBuilder();
362 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800363
364 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
365 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800366
Winson Chungc51db6a2011-10-05 11:44:49 -0700367 boolean searchVisible = false;
368 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800369 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700370 int coi = getCurrentOrientationIndexForGlobalIcons();
371 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
372 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700373 updateAppMarketIcon();
374 searchVisible = updateGlobalSearchIcon();
375 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700376 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700377 if (sGlobalSearchIcon[coi] != null) {
378 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700379 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700380 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700381 if (sVoiceSearchIcon[coi] != null) {
382 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700383 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700384 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700385 if (sAppMarketIcon[coi] != null) {
386 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800387 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700388 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700389
390 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Winson Chung4b919f82012-05-01 10:44:08 -0700391 unlockScreenOrientation(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 }
Romain Guycbb89e42009-06-08 15:52:54 -0700393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700395 if (sLocaleConfiguration == null) {
396 new AsyncTask<Void, Void, LocaleConfiguration>() {
397 @Override
398 protected LocaleConfiguration doInBackground(Void... unused) {
399 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
400 readConfiguration(Launcher.this, localeConfiguration);
401 return localeConfiguration;
402 }
403
404 @Override
405 protected void onPostExecute(LocaleConfiguration result) {
406 sLocaleConfiguration = result;
407 checkForLocaleChange(); // recursive, but now with a locale configuration
408 }
409 }.execute();
410 return;
411 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700412
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 final Configuration configuration = getResources().getConfiguration();
414
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700415 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 final String locale = configuration.locale.toString();
417
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700418 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 final int mcc = configuration.mcc;
420
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700421 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 final int mnc = configuration.mnc;
423
Romain Guy84f296c2009-11-04 15:00:44 -0800424 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425
Romain Guy84f296c2009-11-04 15:00:44 -0800426 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700427 sLocaleConfiguration.locale = locale;
428 sLocaleConfiguration.mcc = mcc;
429 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700430
Joe Onorato0589f0f2010-02-08 13:44:00 -0800431 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700432
433 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
434 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700435 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700436 public void run() {
437 writeConfiguration(Launcher.this, localeConfiguration);
438 }
439 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700440 }
441 }
442
443 private static class LocaleConfiguration {
444 public String locale;
445 public int mcc = -1;
446 public int mnc = -1;
447 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700448
Romain Guy98d01652009-06-30 16:21:04 -0700449 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
450 DataInputStream in = null;
451 try {
452 in = new DataInputStream(context.openFileInput(PREFERENCES));
453 configuration.locale = in.readUTF();
454 configuration.mcc = in.readInt();
455 configuration.mnc = in.readInt();
456 } catch (FileNotFoundException e) {
457 // Ignore
458 } catch (IOException e) {
459 // Ignore
460 } finally {
461 if (in != null) {
462 try {
463 in.close();
464 } catch (IOException e) {
465 // Ignore
466 }
467 }
468 }
469 }
470
471 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
472 DataOutputStream out = null;
473 try {
474 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
475 out.writeUTF(configuration.locale);
476 out.writeInt(configuration.mcc);
477 out.writeInt(configuration.mnc);
478 out.flush();
479 } catch (FileNotFoundException e) {
480 // Ignore
481 } catch (IOException e) {
482 //noinspection ResultOfMethodCallIgnored
483 context.getFileStreamPath(PREFERENCES).delete();
484 } finally {
485 if (out != null) {
486 try {
487 out.close();
488 } catch (IOException e) {
489 // Ignore
490 }
491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 }
493 }
494
Adam Cohen716b51e2011-06-30 12:09:54 -0700495 public DragLayer getDragLayer() {
496 return mDragLayer;
497 }
498
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 static int getScreen() {
500 synchronized (sLock) {
501 return sScreen;
502 }
503 }
504
505 static void setScreen(int screen) {
506 synchronized (sLock) {
507 sScreen = screen;
508 }
509 }
510
Winson Chung557d6ed2011-07-08 15:34:52 -0700511 /**
512 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
513 * a configuration step, this allows the proper animations to run after other transitions.
514 */
515 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700516 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800517 switch (args.requestCode) {
518 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700519 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
520 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800521 break;
522 case REQUEST_PICK_SHORTCUT:
523 processShortcut(args.intent);
524 break;
525 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700526 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
527 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700528 result = true;
529 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800530 case REQUEST_PICK_APPWIDGET:
531 addAppWidgetFromPick(args.intent);
532 break;
533 case REQUEST_CREATE_APPWIDGET:
534 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800535 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700536 result = true;
537 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800538 case REQUEST_PICK_WALLPAPER:
539 // We just wanted the activity result here so we can clear mWaitingForResult
540 break;
541 }
Michael Jurka27614d22012-04-02 06:40:22 -0700542 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
543 // if you turned the screen off and then back while in All Apps, Launcher would not
544 // return to the workspace. Clearing mAddInfo.container here fixes this issue
545 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700546 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800547 }
548
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700550 protected void onActivityResult(
551 final int requestCode, final int resultCode, final Intent data) {
552 if (requestCode == REQUEST_BIND_APPWIDGET) {
553 int appWidgetId = data != null ?
554 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
555 if (resultCode == RESULT_CANCELED) {
556 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
557 } else if (resultCode == RESULT_OK) {
558 addAppWidgetImpl(appWidgetId, mWidgetBeingBoundOrConfigured);
559 }
560 return;
561 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700562 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800563 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
564 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700565 mWaitingForResult = false;
566
Adam Cohened66b2b2012-01-23 17:28:51 -0800567 // We have special handling for widgets
568 if (isWidgetDrop) {
569 int appWidgetId = data != null ?
570 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700571 if (appWidgetId < 0) {
572 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
573 "widget configuration activity.");
574 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
575 } else {
576 completeTwoStageWidgetDrop(resultCode, appWidgetId);
577 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800578 return;
579 }
580
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 // The pattern used here is that a user PICKs a specific application,
582 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700583
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
585 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700586 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800587 final PendingAddArguments args = new PendingAddArguments();
588 args.requestCode = requestCode;
589 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700590 args.container = mPendingAddInfo.container;
591 args.screen = mPendingAddInfo.screen;
592 args.cellX = mPendingAddInfo.cellX;
593 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800594 if (isWorkspaceLocked()) {
595 sPendingAddList.add(args);
596 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700597 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800600 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700601 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800602 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
603 null);
604 }
605
606 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700607 CellLayout cellLayout =
608 (CellLayout) mWorkspace.getChildAt(mWidgetBeingBoundOrConfigured.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800609 Runnable onCompleteRunnable = null;
610 int animationType = 0;
611
612 if (resultCode == RESULT_OK) {
613 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
614 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Michael Jurka8b805b12012-04-18 14:23:14 -0700615 mWidgetBeingBoundOrConfigured.info);
616 mWidgetBeingBoundOrConfigured.boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800617 onCompleteRunnable = new Runnable() {
618 @Override
619 public void run() {
620 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
621 mPendingAddInfo.screen, layout, null);
622 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
623 null);
624 }
625 };
626 } else if (resultCode == RESULT_CANCELED) {
627 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
628 onCompleteRunnable = new Runnable() {
629 @Override
630 public void run() {
631 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
632 null);
633 }
634 };
635 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700636 mWorkspace.animateWidgetDrop(mWidgetBeingBoundOrConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800637 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Michael Jurka8b805b12012-04-18 14:23:14 -0700638 animationType, mWidgetBeingBoundOrConfigured.boundWidget, true);
639 mWidgetBeingBoundOrConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 }
641
642 @Override
643 protected void onResume() {
644 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700645
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800646 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700647 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700649 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700651 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 }
Winson Chunge4e50662012-01-23 14:45:13 -0800653
654 // Reset the pressed state of icons that were locked in the press state while activities
655 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800656 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800657 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800658 mWaitingForResume.setStayPressed(false);
659 }
Winson Chunge4e50662012-01-23 14:45:13 -0800660 if (mAppsCustomizeContent != null) {
661 // Resets the previous all apps icon press state
662 mAppsCustomizeContent.resetDrawableState();
663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 }
665
666 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700667 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700668 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
669 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
670 // when Launcher resumes and we are still in AllApps.
671 updateWallpaperVisibility(true);
672
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700673 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700674 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800675 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700676 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700677 }
Romain Guycbb89e42009-06-08 15:52:54 -0700678
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700679 @Override
680 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400681 // Flag the loader to stop early before switching
682 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700683 if (mAppsCustomizeContent != null) {
684 mAppsCustomizeContent.surrender();
685 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800686 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700687 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700688
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800689 // We can't hide the IME if it was forced open. So don't bother
690 /*
691 @Override
692 public void onWindowFocusChanged(boolean hasFocus) {
693 super.onWindowFocusChanged(hasFocus);
694
695 if (hasFocus) {
696 final InputMethodManager inputManager = (InputMethodManager)
697 getSystemService(Context.INPUT_METHOD_SERVICE);
698 WindowManager.LayoutParams lp = getWindow().getAttributes();
699 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
700 android.os.Handler()) {
701 protected void onReceiveResult(int resultCode, Bundle resultData) {
702 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
703 }
704 });
705 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
706 }
707 }
708 */
709
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 private boolean acceptFilter() {
711 final InputMethodManager inputManager = (InputMethodManager)
712 getSystemService(Context.INPUT_METHOD_SERVICE);
713 return !inputManager.isFullscreenMode();
714 }
715
716 @Override
717 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700718 final int uniChar = event.getUnicodeChar();
719 final boolean handled = super.onKeyDown(keyCode, event);
720 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
721 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
723 keyCode, event);
724 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700725 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700726 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700727 // showSearchDialog()
728 // If there are multiple keystrokes before the search dialog takes focus,
729 // onSearchRequested() will be called for every keystroke,
730 // but it is idempotent, so it's fine.
731 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 }
733 }
734
Joe Onorato8a9625e2010-01-28 15:55:35 -0800735 // Eat the long press event so the keyboard doesn't come up.
736 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
737 return true;
738 }
739
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 return handled;
741 }
742
Karl Rosaen138a0412009-04-23 19:00:21 -0700743 private String getTypedText() {
744 return mDefaultKeySsb.toString();
745 }
746
747 private void clearTypedText() {
748 mDefaultKeySsb.clear();
749 mDefaultKeySsb.clearSpans();
750 Selection.setSelection(mDefaultKeySsb, 0);
751 }
752
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700754 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
755 * State
756 */
757 private static State intToState(int stateOrdinal) {
758 State state = State.WORKSPACE;
759 final State[] stateValues = State.values();
760 for (int i = 0; i < stateValues.length; i++) {
761 if (stateValues[i].ordinal() == stateOrdinal) {
762 state = stateValues[i];
763 break;
764 }
765 }
766 return state;
767 }
768
769 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 * Restores the previous state, if it exists.
771 *
772 * @param savedState The previous state.
773 */
774 private void restoreState(Bundle savedState) {
775 if (savedState == null) {
776 return;
777 }
778
Michael Jurka883f55b2010-10-21 15:47:14 -0700779 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700780 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800781 showAllApps(false);
782 }
783
Winson Chungf0c6ae02012-03-21 16:10:31 -0700784 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700786 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 }
788
Winson Chung3d503fb2011-07-13 17:25:49 -0700789 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
790 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700791
Winson Chung3d503fb2011-07-13 17:25:49 -0700792 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
793 mPendingAddInfo.container = pendingAddContainer;
794 mPendingAddInfo.screen = pendingAddScreen;
795 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
796 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 mRestoring = true;
798 }
799
800 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
801 if (renameFolder) {
802 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700803 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 mRestoring = true;
805 }
Winson Chunga12a2502010-12-20 14:41:35 -0800806
Winson Chung785d2eb2011-04-14 16:08:02 -0700807
808 // Restore the AppsCustomize tab
809 if (mAppsCustomizeTabHost != null) {
810 String curTab = savedState.getString("apps_customize_currentTab");
811 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700812 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700813 mAppsCustomizeContent.setContentType(
814 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
815 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700816 mAppsCustomizeContent.loadAssociatedPages(
817 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700818 }
819
Winson Chung5afbf7b2011-07-25 11:53:08 -0700820 int currentIndex = savedState.getInt("apps_customize_currentIndex");
821 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700822 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 }
824
825 /**
826 * Finds all the views we need and configure them properly.
827 */
828 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700829 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400830
Winson Chung4c98d922011-05-31 16:50:48 -0700831 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
832 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800833 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
834 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835
Winson Chung4c98d922011-05-31 16:50:48 -0700836 // Setup the drag layer
837 mDragLayer.setup(this, dragController);
838
Winson Chung3d503fb2011-07-13 17:25:49 -0700839 // Setup the hotseat
840 mHotseat = (Hotseat) findViewById(R.id.hotseat);
841 if (mHotseat != null) {
842 mHotseat.setup(this);
843 }
844
Winson Chung4c98d922011-05-31 16:50:48 -0700845 // Setup the workspace
846 mWorkspace.setHapticFeedbackEnabled(false);
847 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700848 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700849 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700850
Winson Chungf0ea4d32011-06-06 14:27:16 -0700851 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700852 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700853
Winson Chungf0ea4d32011-06-06 14:27:16 -0700854 // Setup AppsCustomize
855 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
856 findViewById(R.id.apps_customize_pane);
857 mAppsCustomizeContent = (AppsCustomizePagedView)
858 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700859 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700860 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400861
Michael Jurka5130e402011-10-13 04:55:35 -0700862 // Get the all apps button
863 mAllAppsButton = findViewById(R.id.all_apps_button);
864 if (mAllAppsButton != null) {
865 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
866 @Override
867 public boolean onTouch(View v, MotionEvent event) {
868 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
869 onTouchDownAllAppsButton(v);
870 }
871 return false;
872 }
873 });
874 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700875 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700876 dragController.setDragScoller(mWorkspace);
877 dragController.setScrollView(mDragLayer);
878 dragController.setMoveTarget(mWorkspace);
879 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700880 if (mSearchDropTargetBar != null) {
881 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800882 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 }
884
885 /**
886 * Creates a view representing a shortcut.
887 *
888 * @param info The data structure describing the shortcut.
889 *
890 * @return A View inflated from R.layout.application.
891 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800892 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700894 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 }
896
897 /**
898 * Creates a view representing a shortcut inflated from the specified resource.
899 *
900 * @param layoutResId The id of the XML layout used to create the shortcut.
901 * @param parent The group the shortcut belongs to.
902 * @param info The data structure describing the shortcut.
903 *
904 * @return A View inflated from layoutResId.
905 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800906 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800907 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
908 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 return favorite;
911 }
912
913 /**
914 * Add an application shortcut to the workspace.
915 *
916 * @param data The intent describing the application.
917 * @param cellInfo The position on screen where to create the shortcut.
918 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700919 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700920 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700921 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700922
Adam Cohenfbba09b2011-07-18 21:43:05 -0700923 // First we check if we already know the exact location where we want to add this item.
924 if (cellX >= 0 && cellY >= 0) {
925 cellXY[0] = cellX;
926 cellXY[1] = cellY;
927 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700928 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700929 return;
930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800932 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800933
Romain Guy73b979d2009-06-09 12:57:21 -0700934 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800935 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800937 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700938 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700939 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800940 } else {
941 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943 }
Romain Guycbb89e42009-06-08 15:52:54 -0700944
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 /**
946 * Add a shortcut to the workspace.
947 *
948 * @param data The intent describing the shortcut.
949 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700951 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
952 int cellY) {
953 int[] cellXY = mTmpAddItemCellCoordinates;
954 int[] touchXY = mPendingAddInfo.dropPos;
955 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700956
Michael Jurkad3ef3062010-11-23 16:23:58 -0800957 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700958
Adam Cohen558baaf2011-08-15 15:22:57 -0700959 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800960 if (info == null) {
961 return;
962 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700963 final View view = createShortcut(info);
964
Adam Cohenfbba09b2011-07-18 21:43:05 -0700965 // First we check if we already know the exact location where we want to add this item.
966 if (cellX >= 0 && cellY >= 0) {
967 cellXY[0] = cellX;
968 cellXY[1] = cellY;
969 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700970
971 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800972 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700973 true, null,null)) {
974 return;
975 }
976 DragObject dragObject = new DragObject();
977 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -0800978 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
979 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -0700980 return;
981 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700982 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800983 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700984 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800985 foundCellSpan = (result != null);
986 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700987 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800988 }
989
990 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -0700991 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700992 return;
993 }
994
Adam Cohen558baaf2011-08-15 15:22:57 -0700995 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996
997 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700998 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
999 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 }
1001 }
1002
Adam Cohen2f093b62012-04-30 18:59:53 -07001003 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1004 int minHeight) {
1005 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001006 // We want to account for the extra amount of padding that we are adding to the widget
1007 // to ensure that it gets the full amount of space that it has requested
1008 int requiredWidth = minWidth + padding.left + padding.right;
1009 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001010 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001011 }
1012
Adam Cohen2f093b62012-04-30 18:59:53 -07001013 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1014 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001015 }
1016
Adam Cohen2f093b62012-04-30 18:59:53 -07001017 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1018 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001019 }
1020
Adam Cohen2f093b62012-04-30 18:59:53 -07001021 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1022 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001023 }
1024
Adam Cohen2f093b62012-04-30 18:59:53 -07001025 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1026 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001027 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001028 }
1029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001031 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001033 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001034 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001036 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1037 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1038 if (appWidgetInfo == null) {
1039 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1040 }
Romain Guycbb89e42009-06-08 15:52:54 -07001041
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001042 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001043 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001044
Adam Cohen2f093b62012-04-30 18:59:53 -07001045 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1046 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001047
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001049 // We have saved the position to which the widget was dragged-- this really only matters
1050 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001051 int[] cellXY = mTmpAddItemCellCoordinates;
1052 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001053 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001054 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001055 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1056 cellXY[0] = mPendingAddInfo.cellX;
1057 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001058 spanXY[0] = mPendingAddInfo.spanX;
1059 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001060 foundCellSpan = true;
1061 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001062 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001063 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001064 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1065 spanXY[1], cellXY, finalSpan);
1066 spanXY[0] = finalSpan[0];
1067 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001068 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001069 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001070 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001071 }
1072
Michael Jurka0280c3b2010-09-17 15:00:07 -07001073 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001074 if (appWidgetId != -1) {
1075 // Deleting an app widget ID is a void call but writes to disk before returning
1076 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001077 new Thread("deleteAppWidgetId") {
1078 public void run() {
1079 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1080 }
1081 }.start();
1082 }
Winson Chung93eef082012-03-23 15:59:27 -07001083 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001084 return;
1085 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001087 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001088 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1089 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001090 launcherInfo.spanX = spanXY[0];
1091 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001092 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1093 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001096 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097
1098 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001099 if (hostView == null) {
1100 // Perform actual inflation because we're live
1101 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1102 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1103 } else {
1104 // The AppWidgetHostView has already been inflated and instantiated
1105 launcherInfo.hostView = hostView;
1106 }
Romain Guycbb89e42009-06-08 15:52:54 -07001107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001109 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohena897f392012-04-27 18:12:05 -07001110 AppWidgetResizeFrame.updateWidgetSizeRanges(launcherInfo.hostView,
1111 this, launcherInfo.spanX, launcherInfo.spanY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001112 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001113 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001114
1115 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001117 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 }
Romain Guycbb89e42009-06-08 15:52:54 -07001119
Adam Cohended9f8d2010-11-03 13:25:16 -07001120 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1121 @Override
1122 public void onReceive(Context context, Intent intent) {
1123 final String action = intent.getAction();
1124 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1125 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001126 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001127 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001128
Winson Chungc100e8e2011-08-09 16:02:43 -07001129 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001130 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001131 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1132 mAppsCustomizeTabHost.reset();
1133 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001134 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001135 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1136 mUserPresent = true;
1137 updateRunning();
1138 }
1139 }
1140 };
1141
1142 @Override
1143 public void onAttachedToWindow() {
1144 super.onAttachedToWindow();
1145
1146 // Listen for broadcasts related to user-presence
1147 final IntentFilter filter = new IntentFilter();
1148 filter.addAction(Intent.ACTION_SCREEN_OFF);
1149 filter.addAction(Intent.ACTION_USER_PRESENT);
1150 registerReceiver(mReceiver, filter);
1151
Adam Cohend113e0c2010-11-11 10:48:05 -08001152 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001153 mVisible = true;
1154 }
1155
1156 @Override
1157 public void onDetachedFromWindow() {
1158 super.onDetachedFromWindow();
1159 mVisible = false;
1160
Adam Cohend113e0c2010-11-11 10:48:05 -08001161 if (mAttached) {
1162 unregisterReceiver(mReceiver);
1163 mAttached = false;
1164 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001165 updateRunning();
1166 }
1167
1168 public void onWindowVisibilityChanged(int visibility) {
1169 mVisible = visibility == View.VISIBLE;
1170 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001171 // The following code used to be in onResume, but it turns out onResume is called when
1172 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1173 // is a more appropriate event to handle
1174 if (mVisible) {
1175 mAppsCustomizeTabHost.onWindowVisible();
1176 if (!mWorkspaceLoading) {
1177 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001178 // We want to let Launcher draw itself at least once before we force it to build
1179 // layers on all the workspace pages, so that transitioning to Launcher from other
1180 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1181 // a true draw so we wait until the second preDraw call to be safe
1182 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001183 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001184 // We delay the layer building a bit in order to give
1185 // other message processing a time to run. In particular
1186 // this avoids a delay in hiding the IME if it was
1187 // currently shown, because doing that may involve
1188 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001189 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001190
1191 // We had to enable the wallpaper visibility when launching apps from all
1192 // apps (so that the transitions would be the same as when launching from
1193 // workspace) so take this time to see if we need to re-disable the
1194 // wallpaper visibility to ensure performance.
1195 mWorkspace.post(new Runnable() {
1196 @Override
1197 public void run() {
1198 if (mState == State.APPS_CUSTOMIZE) {
1199 if (mAppsCustomizeTabHost != null &&
1200 !mAppsCustomizeTabHost.isTransitioning()) {
1201 updateWallpaperVisibility(false);
1202 }
1203 }
1204 }
1205 });
1206
Michael Jurka6ee21d22012-02-21 18:20:27 -08001207 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001208 return true;
1209 }
1210 });
1211 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001212 // When Launcher comes back to foreground, a different Activity might be responsible for
1213 // the app market intent, so refresh the icon
1214 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001215 clearTypedText();
1216 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001217 }
1218
1219 private void sendAdvanceMessage(long delay) {
1220 mHandler.removeMessages(ADVANCE_MSG);
1221 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1222 mHandler.sendMessageDelayed(msg, delay);
1223 mAutoAdvanceSentTime = System.currentTimeMillis();
1224 }
1225
1226 private void updateRunning() {
1227 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1228 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1229 mAutoAdvanceRunning = autoAdvanceRunning;
1230 if (autoAdvanceRunning) {
1231 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1232 sendAdvanceMessage(delay);
1233 } else {
1234 if (!mWidgetsToAdvance.isEmpty()) {
1235 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1236 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1237 }
1238 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001239 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001240 }
1241 }
1242 }
1243
1244 private final Handler mHandler = new Handler() {
1245 @Override
1246 public void handleMessage(Message msg) {
1247 if (msg.what == ADVANCE_MSG) {
1248 int i = 0;
1249 for (View key: mWidgetsToAdvance.keySet()) {
1250 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1251 final int delay = mAdvanceStagger * i;
1252 if (v instanceof Advanceable) {
1253 postDelayed(new Runnable() {
1254 public void run() {
1255 ((Advanceable) v).advance();
1256 }
1257 }, delay);
1258 }
1259 i++;
1260 }
1261 sendAdvanceMessage(mAdvanceInterval);
1262 }
1263 }
1264 };
1265
1266 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001267 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001268 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1269 if (v instanceof Advanceable) {
1270 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001271 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001272 updateRunning();
1273 }
1274 }
1275
1276 void removeWidgetToAutoAdvance(View hostView) {
1277 if (mWidgetsToAdvance.containsKey(hostView)) {
1278 mWidgetsToAdvance.remove(hostView);
1279 updateRunning();
1280 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001281 }
1282
Joe Onorato9c1289c2009-08-17 11:03:03 -04001283 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001284 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001285 launcherInfo.hostView = null;
1286 }
1287
Winson Chung93eef082012-03-23 15:59:27 -07001288 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1289 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1290 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001291 }
1292
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001293 public LauncherAppWidgetHost getAppWidgetHost() {
1294 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 }
Romain Guycbb89e42009-06-08 15:52:54 -07001296
Winson Chunga9abd0e2010-10-27 17:18:37 -07001297 public LauncherModel getModel() {
1298 return mModel;
1299 }
1300
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001301 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001302 getWindow().closeAllPanels();
1303
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001304 // Whatever we were doing is hereby canceled.
1305 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001306 }
1307
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 @Override
1309 protected void onNewIntent(Intent intent) {
1310 super.onNewIntent(intent);
1311
1312 // Close the menu
1313 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001314 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001315 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001316
Joe Onorato14f122b2009-11-19 14:06:36 -08001317 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1318 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001319
Adam Cohenac56cff2011-09-28 20:45:37 -07001320 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001321 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001322 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001323 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1324 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001325 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001326 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001327
1328 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001329 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001330 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001331
Joe Onorato3a8820b2009-11-10 15:06:42 -08001332 final View v = getWindow().peekDecorView();
1333 if (v != null && v.getWindowToken() != null) {
1334 InputMethodManager imm = (InputMethodManager)getSystemService(
1335 INPUT_METHOD_SERVICE);
1336 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001338
Michael Jurka35aa14d2011-07-07 17:01:08 -07001339 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001340 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001341 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001342 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 }
1344 }
1345
1346 @Override
1347 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1348 // Do not call super here
1349 mSavedInstanceState = savedInstanceState;
1350 }
1351
1352 @Override
1353 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001354 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001355 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356
Michael Jurka883f55b2010-10-21 15:47:14 -07001357 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001358 // We close any open folder since it will not be re-opened, and we need to make sure
1359 // this state is reflected.
1360 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361
Winson Chung3d503fb2011-07-13 17:25:49 -07001362 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1363 mWaitingForResult) {
1364 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1365 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1366 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1367 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 }
1369
1370 if (mFolderInfo != null && mWaitingForResult) {
1371 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1372 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1373 }
Michael Jurka946ad472010-07-09 18:05:18 -07001374
Winson Chung785d2eb2011-04-14 16:08:02 -07001375 // Save the current AppsCustomize tab
1376 if (mAppsCustomizeTabHost != null) {
1377 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1378 if (currentTabTag != null) {
1379 outState.putString("apps_customize_currentTab", currentTabTag);
1380 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001381 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1382 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001383 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
1385
1386 @Override
1387 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001389
Winson Chunge7a03942011-08-05 15:05:12 -07001390 // Remove all pending runnables
1391 mHandler.removeMessages(ADVANCE_MSG);
1392 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001393 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001394
Winson Chungcd2b0142011-06-08 16:02:26 -07001395 // Stop callbacks from LauncherModel
1396 LauncherApplication app = ((LauncherApplication) getApplication());
1397 mModel.stopLoader();
1398 app.setLauncher(null);
1399
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001401 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001403 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001405 mAppWidgetHost = null;
1406
1407 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408
1409 TextKeyListener.getInstance().release();
1410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411
Winson Chung3d503fb2011-07-13 17:25:49 -07001412 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001413
1414 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001415 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001416
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001417 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001418 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1419 mWorkspace.removeAllViews();
1420 mWorkspace = null;
1421 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001422
1423 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 }
1425
Adam Cohena9cf38f2011-05-02 15:36:58 -07001426 public DragController getDragController() {
1427 return mDragController;
1428 }
1429
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 @Override
1431 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001432 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 super.startActivityForResult(intent, requestCode);
1434 }
1435
Winson Chung70d72102011-08-12 11:24:35 -07001436 /**
1437 * Indicates that we want global search for this activity by setting the globalSearch
1438 * argument for {@link #startSearch} to true.
1439 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001441 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001443
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001444 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001445
Karl Rosaen138a0412009-04-23 19:00:21 -07001446 if (initialQuery == null) {
1447 // Use any text typed in the launcher as the initial query
1448 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001449 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 if (appSearchData == null) {
1451 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001452 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001454 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001455
Karl Rosaen138a0412009-04-23 19:00:21 -07001456 final SearchManager searchManager =
1457 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001458 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001459 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 }
1461
Winson Chung70d72102011-08-12 11:24:35 -07001462 @Override
1463 public boolean onCreateOptionsMenu(Menu menu) {
1464 if (isWorkspaceLocked()) {
1465 return false;
1466 }
1467
1468 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001469
1470 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1471 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1472 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001473 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1474 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1475 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001476 String helpUrl = getString(R.string.help_url);
1477 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001478 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1479 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1480
Winson Chung70d72102011-08-12 11:24:35 -07001481 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1482 .setIcon(android.R.drawable.ic_menu_gallery)
1483 .setAlphabeticShortcut('W');
1484 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1485 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001486 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001487 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001488 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1489 .setIcon(android.R.drawable.ic_menu_preferences)
1490 .setIntent(settings)
1491 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001492 if (!helpUrl.isEmpty()) {
1493 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1494 .setIcon(android.R.drawable.ic_menu_help)
1495 .setIntent(help)
1496 .setAlphabeticShortcut('H');
1497 }
Winson Chung70d72102011-08-12 11:24:35 -07001498 return true;
1499 }
1500
1501 @Override
1502 public boolean onPrepareOptionsMenu(Menu menu) {
1503 super.onPrepareOptionsMenu(menu);
1504
1505 if (mAppsCustomizeTabHost.isTransitioning()) {
1506 return false;
1507 }
1508 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1509 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1510
1511 return true;
1512 }
1513
1514 @Override
1515 public boolean onOptionsItemSelected(MenuItem item) {
1516 switch (item.getItemId()) {
1517 case MENU_WALLPAPER_SETTINGS:
1518 startWallpaper();
1519 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001520 }
1521
1522 return super.onOptionsItemSelected(item);
1523 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001525 @Override
1526 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001527 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001528 // Use a custom animation for launching search
1529 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001530 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001531 }
1532
Joe Onorato9c1289c2009-08-17 11:03:03 -04001533 public boolean isWorkspaceLocked() {
1534 return mWorkspaceLoading || mWaitingForResult;
1535 }
1536
Michael Jurka0280c3b2010-09-17 15:00:07 -07001537 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001538 mPendingAddInfo.container = ItemInfo.NO_ID;
1539 mPendingAddInfo.screen = -1;
1540 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1541 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001542 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001543 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001544 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001545
Michael Jurkaaf442092010-06-10 17:01:57 -07001546 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001547 // TODO: catch bad widget exception when sent
1548 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001549 // TODO: Is this log message meaningful?
1550 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001551 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001552 }
1553
Adam Cohened66b2b2012-01-23 17:28:51 -08001554 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1555 final AppWidgetProviderInfo appWidget = info.info;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001556 if (appWidget.configure != null) {
1557 // Launch over to configure widget, if needed
1558 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1559 intent.setComponent(appWidget.configure);
1560 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001561 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001562 mWidgetBeingBoundOrConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001564 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001565 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001566 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001567 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 }
1569 }
Romain Guycbb89e42009-06-08 15:52:54 -07001570
Adam Cohenfbba09b2011-07-18 21:43:05 -07001571 /**
1572 * Process a shortcut drop.
1573 *
1574 * @param componentName The name of the component
1575 * @param screen The screen where it should be added
1576 * @param cell The cell it should be added to, optional
1577 * @param position The location on the screen where it was dropped, optional
1578 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001579 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1580 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001581 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001582 mPendingAddInfo.container = container;
1583 mPendingAddInfo.screen = screen;
1584 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001585
1586 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001587 mPendingAddInfo.cellX = cell[0];
1588 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001589 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001590
1591 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1592 createShortcutIntent.setComponent(componentName);
1593 processShortcut(createShortcutIntent);
1594 }
1595
Adam Cohenfbba09b2011-07-18 21:43:05 -07001596 /**
1597 * Process a widget drop.
1598 *
1599 * @param info The PendingAppWidgetInfo of the widget being added.
1600 * @param screen The screen where it should be added
1601 * @param cell The cell it should be added to, optional
1602 * @param position The location on the screen where it was dropped, optional
1603 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001604 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001605 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001606 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001607 mPendingAddInfo.container = info.container = container;
1608 mPendingAddInfo.screen = info.screen = screen;
1609 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001610 mPendingAddInfo.minSpanX = info.minSpanX;
1611 mPendingAddInfo.minSpanY = info.minSpanY;
1612
Adam Cohenfbba09b2011-07-18 21:43:05 -07001613 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001614 mPendingAddInfo.cellX = cell[0];
1615 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001616 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001617 if (span != null) {
1618 mPendingAddInfo.spanX = span[0];
1619 mPendingAddInfo.spanY = span[1];
1620 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001621
Adam Cohened66b2b2012-01-23 17:28:51 -08001622 AppWidgetHostView hostView = info.boundWidget;
1623 int appWidgetId;
1624 if (hostView != null) {
1625 appWidgetId = hostView.getAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001626 addAppWidgetImpl(appWidgetId, info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001627 } else {
1628 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001629 mWidgetBeingBoundOrConfigured = info;
1630 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
1631 addAppWidgetImpl(appWidgetId, info);
1632 } else {
1633 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1634 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1635 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1636 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1637 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001638 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001639 }
1640
Joe Onoratodeb98af2010-02-19 14:59:39 -08001641 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001642 // Handle case where user selected "Applications"
1643 String applicationName = getResources().getString(R.string.group_applications);
1644 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001645
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001646 if (applicationName != null && applicationName.equals(shortcutName)) {
1647 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1648 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001649
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001650 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1651 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001652 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001653 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001654 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001655 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001656 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 }
1658
Winson Chung24ab2f12010-09-16 14:10:47 -07001659 void processWallpaper(Intent intent) {
1660 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1661 }
1662
Winson Chung3d503fb2011-07-13 17:25:49 -07001663 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1664 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001665 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 folderInfo.title = getText(R.string.folder_name);
1667
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001669 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1670 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001671 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672
1673 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001674 FolderIcon newFolder =
1675 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1676 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1677 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001678 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 }
Romain Guycbb89e42009-06-08 15:52:54 -07001680
Joe Onorato9c1289c2009-08-17 11:03:03 -04001681 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001682 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001683 }
1684
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001686 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001688 Intent chooser = Intent.createChooser(pickWallpaper,
1689 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001690 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1691 // Removed in Eclair MR1
1692// WallpaperManager wm = (WallpaperManager)
1693// getSystemService(Context.WALLPAPER_SERVICE);
1694// WallpaperInfo wi = wm.getWallpaperInfo();
1695// if (wi != null && wi.getSettingsActivity() != null) {
1696// LabeledIntent li = new LabeledIntent(getPackageName(),
1697// R.string.configure_wallpaper, 0);
1698// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1699// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1700// }
Mike Clerona0618e42009-10-22 13:55:21 -07001701 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 }
1703
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001704 /**
1705 * Registers various content observers. The current implementation registers
1706 * only a favorites observer to keep track of the favorites applications.
1707 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001708 private void registerContentObservers() {
1709 ContentResolver resolver = getContentResolver();
1710 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1711 true, mWidgetObserver);
1712 }
1713
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 @Override
1715 public boolean dispatchKeyEvent(KeyEvent event) {
1716 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1717 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001719 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001720 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001721 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001722 dumpState();
1723 return true;
1724 }
1725 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001726 }
1727 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1728 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001729 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730 return true;
1731 }
1732 }
1733
1734 return super.dispatchKeyEvent(event);
1735 }
1736
Joe Onorato88ec0992009-11-19 13:16:06 -08001737 @Override
1738 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001739 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001740 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001741 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001742 Folder openFolder = mWorkspace.getOpenFolder();
1743 if (openFolder.isEditingName()) {
1744 openFolder.dismissEditingName();
1745 } else {
1746 closeFolder();
1747 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001748 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001749 mWorkspace.exitWidgetResizeMode();
1750
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001751 // Back button is a no-op here, but give at least some feedback for the button press
1752 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001753 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001754 }
1755
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001757 * Re-listen when widgets are reset.
1758 */
1759 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001760 if (mAppWidgetHost != null) {
1761 mAppWidgetHost.startListening();
1762 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001763 }
1764
1765 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001766 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1767 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001769 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001770 if (mModel != null) {
1771 mModel.unbindWorkspaceItems();
1772 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001774
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 /**
1776 * Launches the intent referred by the clicked shortcut.
1777 *
1778 * @param v The view representing the clicked shortcut.
1779 */
1780 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001781 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1782 // view has detached (it's possible for this to happen if the view is removed mid touch).
1783 if (v.getWindowToken() == null) {
1784 return;
1785 }
1786
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001787 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001788 return;
1789 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001790
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001792 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001794 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001795 int[] pos = new int[2];
1796 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001797 intent.setSourceBounds(new Rect(pos[0], pos[1],
1798 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001799
1800 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001801
1802 if (success && v instanceof BubbleTextView) {
1803 mWaitingForResume = (BubbleTextView) v;
1804 mWaitingForResume.setStayPressed(true);
1805 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001807 if (v instanceof FolderIcon) {
1808 FolderIcon fi = (FolderIcon) v;
1809 handleFolderClick(fi);
1810 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001811 } else if (v == mAllAppsButton) {
1812 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001813 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001814 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001815 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001816 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 }
1818 }
1819
Michael Jurka0e260592010-06-30 17:07:39 -07001820 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001821 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001822 // clicking anywhere on the workspace causes the customization drawer to slide down
1823 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001824 return false;
1825 }
1826
Michael Jurkaaf442092010-06-10 17:01:57 -07001827 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001828 * Event handler for the search button
1829 *
1830 * @param v The view that was clicked.
1831 */
1832 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001833 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1834
Amith Yamasania135ba82011-08-09 17:42:01 -07001835 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001836 }
1837
1838 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001839 * Event handler for the voice button
1840 *
1841 * @param v The view that was clicked.
1842 */
1843 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001844 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001845
Michael Jurkaae65ee32012-04-30 11:45:54 -07001846 try {
1847 final SearchManager searchManager =
1848 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1849 ComponentName activityName = searchManager.getGlobalSearchActivity();
1850 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1851 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1852 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1853 if (activityName != null) {
1854 intent.setPackage(activityName.getPackageName());
1855 }
1856 startActivitySafely(null, intent, "onClickVoiceButton");
1857 } catch (ActivityNotFoundException e) {
1858 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1859 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1860 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1861 startActivitySafely(null, intent, "onClickVoiceButton");
1862 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001863 }
1864
1865 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001866 * Event handler for the "grid" button that appears on the home screen, which
1867 * enters all apps mode.
1868 *
1869 * @param v The view that was clicked.
1870 */
1871 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001872 showAllApps(true);
1873 }
1874
1875 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001876 // Provide the same haptic feedback that the system offers for virtual keys.
1877 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001878 }
1879
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001880 public void onClickAppMarketButton(View v) {
1881 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001882 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001883 } else {
1884 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001885 }
1886 }
1887
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001888 void startApplicationDetailsActivity(ComponentName componentName) {
1889 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001890 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1891 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001892 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001893 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001894 }
1895
Patrick Dubroy5539af72010-09-07 15:22:01 -07001896 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1897 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1898 // System applications cannot be installed. For now, show a toast explaining that.
1899 // We may give them the option of disabling apps this way.
1900 int messageId = R.string.uninstall_system_app_text;
1901 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1902 } else {
1903 String packageName = appInfo.componentName.getPackageName();
1904 String className = appInfo.componentName.getClassName();
1905 Intent intent = new Intent(
1906 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001907 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1908 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001909 startActivity(intent);
1910 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001911 }
1912
Winson Chungc7450e32012-04-17 17:34:08 -07001913 boolean startActivitySafely(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001915
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 try {
Winson Chung2672ff92012-05-04 16:22:30 -07001917 // Only launch using the new animation if the shortcut has not opted out (this is a
1918 // private contract between launcher and may be ignored in the future).
1919 boolean useLaunchAnimation = (v != null) &&
1920 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
1921 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07001922 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1923 v.getMeasuredWidth(), v.getMeasuredHeight());
1924
1925 startActivity(intent, opts.toBundle());
1926 } else {
1927 startActivity(intent);
1928 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001929 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 } catch (ActivityNotFoundException e) {
1931 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001932 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 } catch (SecurityException e) {
1934 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001935 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001937 "or use the exported attribute for this activity. "
1938 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001940 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001942
Romain Guy8e633c52010-03-04 12:51:36 -08001943 void startActivityForResultSafely(Intent intent, int requestCode) {
1944 try {
1945 startActivityForResult(intent, requestCode);
1946 } catch (ActivityNotFoundException e) {
1947 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1948 } catch (SecurityException e) {
1949 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1950 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1951 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1952 "or use the exported attribute for this activity.", e);
1953 }
1954 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955
Adam Cohena9cf38f2011-05-02 15:36:58 -07001956 private void handleFolderClick(FolderIcon folderIcon) {
1957 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001958 Folder openFolder = mWorkspace.getFolderForTag(info);
1959
1960 // If the folder info reports that the associated folder is open, then verify that
1961 // it is actually opened. There have been a few instances where this gets out of sync.
1962 if (info.opened && openFolder == null) {
1963 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1964 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1965 info.opened = false;
1966 }
1967
Adam Cohena9cf38f2011-05-02 15:36:58 -07001968 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 // Close any open folder
1970 closeFolder();
1971 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001972 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 } else {
1974 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 int folderScreen;
1976 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001977 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 // .. and close it
1979 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001980 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 // Close any folder open on the current screen
1982 closeFolder();
1983 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001984 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 }
1986 }
1987 }
1988 }
1989
Adam Cohen2801caf2011-05-13 20:57:39 -07001990 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001991 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001992 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1993 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1994 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1995
Adam Cohenc51934b2011-07-26 21:07:43 -07001996 FolderInfo info = (FolderInfo) fi.getTag();
1997 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1998 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001999 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2000 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002001 }
2002
Adam Cohen2801caf2011-05-13 20:57:39 -07002003 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2004 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2005 oa.start();
2006 }
2007
2008 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002009 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002010 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2011 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2012 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2013
Adam Cohenc51934b2011-07-26 21:07:43 -07002014 FolderInfo info = (FolderInfo) fi.getTag();
2015 CellLayout cl = null;
2016 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2017 cl = (CellLayout) fi.getParent().getParent();
2018 }
2019
2020 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07002021 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2022 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002023 oa.addListener(new AnimatorListenerAdapter() {
2024 @Override
2025 public void onAnimationEnd(Animator animation) {
2026 if (layout != null) {
2027 layout.clearFolderLeaveBehind();
2028 }
2029 }
2030 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002031 oa.start();
2032 }
2033
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002035 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 * is animated relative to the specified View. If the View is null, no animation
2037 * is played.
2038 *
2039 * @param folderInfo The FolderInfo describing the folder to open.
2040 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002041 public void openFolder(FolderIcon folderIcon) {
2042 Folder folder = folderIcon.mFolder;
2043 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044
Adam Cohen2801caf2011-05-13 20:57:39 -07002045 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002046 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047
Adam Cohen4554ee12011-08-03 16:13:21 -07002048 // Just verify that the folder hasn't already been added to the DragLayer.
2049 // There was a one-off crash where the folder had a parent already.
2050 if (folder.getParent() == null) {
2051 mDragLayer.addView(folder);
2052 mDragController.addDropTarget((DropTarget) folder);
2053 } else {
2054 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2055 folder.getParent() + ").");
2056 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002057 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002058 }
2059
2060 public void closeFolder() {
2061 Folder folder = mWorkspace.getOpenFolder();
2062 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002063 if (folder.isEditingName()) {
2064 folder.dismissEditingName();
2065 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002066 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002067
2068 // Dismiss the folder cling
2069 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002070 }
2071 }
2072
2073 void closeFolder(Folder folder) {
2074 folder.getInfo().opened = false;
2075
2076 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2077 if (parent != null) {
2078 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2079 shrinkAndFadeInFolderIcon(fi);
2080 }
2081 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 }
2083
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002085
Patrick Dubroye708c522011-03-01 16:03:43 -08002086 if (mState != State.WORKSPACE) {
2087 return false;
2088 }
2089
Joe Onorato9c1289c2009-08-17 11:03:03 -04002090 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 return false;
2092 }
2093
2094 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002095 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 }
2097
Michael Jurka0280c3b2010-09-17 15:00:07 -07002098 resetAddInfo();
2099 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002101 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 return true;
2103 }
2104
Winson Chung3d503fb2011-07-13 17:25:49 -07002105 // The hotseat touch handling does not go through Workspace, and we always allow long press
2106 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002107 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002108 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2109 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002110 if (itemUnderLongClick == null) {
2111 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002112 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2113 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002114 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002116 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002118 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 }
2120 }
2121 }
2122 return true;
2123 }
2124
Winson Chung3d503fb2011-07-13 17:25:49 -07002125 boolean isHotseatLayout(View layout) {
2126 return mHotseat != null && layout != null &&
2127 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2128 }
2129 Hotseat getHotseat() {
2130 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002131 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002132 SearchDropTargetBar getSearchBar() {
2133 return mSearchDropTargetBar;
2134 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002135
Winson Chung3d503fb2011-07-13 17:25:49 -07002136 /**
2137 * Returns the CellLayout of the specified container at the specified screen.
2138 */
2139 CellLayout getCellLayout(long container, int screen) {
2140 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2141 if (mHotseat != null) {
2142 return mHotseat.getLayout();
2143 } else {
2144 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002145 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002146 } else {
2147 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002148 }
2149 }
2150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002151 Workspace getWorkspace() {
2152 return mWorkspace;
2153 }
2154
Daniel Sandler843e8602010-06-07 14:59:01 -04002155 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2156 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002157 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002158 }
2159
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002160 public boolean isAllAppsButtonRank(int rank) {
2161 return mHotseat.isAllAppsButtonRank(rank);
2162 }
2163
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002164 // AllAppsView.Watcher
2165 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002166 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002167 mWorkspace.setVisibility(View.GONE);
2168 }
2169 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002170
2171 /**
2172 * Helper method for the cameraZoomIn/cameraZoomOut animations
2173 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002174 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002175 * @param scaleFactor The scale factor used for the zoom
2176 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002177 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002178 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002179 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002180 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002181
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002182 void updateWallpaperVisibility(boolean visible) {
2183 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2184 int curflags = getWindow().getAttributes().flags
2185 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2186 if (wpflags != curflags) {
2187 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2188 }
2189 }
2190
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002191 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2192 if (v instanceof LauncherTransitionable) {
2193 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2194 }
2195 }
2196
Michael Jurkabed61d22012-02-14 22:51:29 -08002197 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2198 if (v instanceof LauncherTransitionable) {
2199 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2200 }
Winson Chung70442722012-02-10 15:43:22 -08002201
2202 // Update the workspace transition step as well
2203 dispatchOnLauncherTransitionStep(v, 0f);
2204 }
2205
2206 private void dispatchOnLauncherTransitionStep(View v, float t) {
2207 if (v instanceof LauncherTransitionable) {
2208 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2209 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002210 }
2211
2212 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2213 if (v instanceof LauncherTransitionable) {
2214 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2215 }
Winson Chung70442722012-02-10 15:43:22 -08002216
2217 // Update the workspace transition step as well
2218 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002219 }
2220
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002221 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002222 * Things to test when changing the following seven functions.
2223 * - Home from workspace
2224 * - from center screen
2225 * - from other screens
2226 * - Home from all apps
2227 * - from center screen
2228 * - from other screens
2229 * - Back from all apps
2230 * - from center screen
2231 * - from other screens
2232 * - Launch app from workspace and quit
2233 * - with back
2234 * - with home
2235 * - Launch app from all apps and quit
2236 * - with back
2237 * - with home
2238 * - Go to a screen that's not the default, then all
2239 * apps, and launch and app, and go back
2240 * - with back
2241 * -with home
2242 * - On workspace, long press power and go back
2243 * - with back
2244 * - with home
2245 * - On all apps, long press power and go back
2246 * - with back
2247 * - with home
2248 * - On workspace, power off
2249 * - On all apps, power off
2250 * - Launch an app and turn off the screen while in that app
2251 * - Go back with home key
2252 * - Go back with back key TODO: make this not go to workspace
2253 * - From all apps
2254 * - From workspace
2255 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2256 * - From all apps
2257 * - From the center workspace
2258 * - From another workspace
2259 */
2260
2261 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002262 * Zoom the camera out from the workspace to reveal 'toView'.
2263 * Assumes that the view to show is anchored at either the very top or very bottom
2264 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002265 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002266 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002267 if (mStateAnimation != null) {
2268 mStateAnimation.cancel();
2269 mStateAnimation = null;
2270 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002271 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002272
Winson Chungf0ea4d32011-06-06 14:27:16 -07002273 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2274 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2275 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002276 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002277 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002278 final int startDelay =
2279 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002280
Michael Jurkab3e22d92011-10-31 15:58:33 -07002281 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002282
Michael Jurkad74c9842011-07-10 12:44:21 -07002283 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002284 Animator workspaceAnim =
2285 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002286
2287 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002288 toView.setScaleX(scale);
2289 toView.setScaleY(scale);
2290 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2291 scaleAnim.
2292 scaleX(1f).scaleY(1f).
2293 setDuration(duration).
2294 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002295
Winson Chungf0ea4d32011-06-06 14:27:16 -07002296 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002297 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002298 final ObjectAnimator alphaAnim = ObjectAnimator
2299 .ofFloat(toView, "alpha", 0f, 1f)
2300 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002301 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002302 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2303 @Override
2304 public void onAnimationUpdate(ValueAnimator animation) {
2305 float t = (Float) animation.getAnimatedValue();
2306 dispatchOnLauncherTransitionStep(fromView, t);
2307 dispatchOnLauncherTransitionStep(toView, t);
2308 }
2309 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002310
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002311 // toView should appear right at the end of the workspace shrink
2312 // animation
2313 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002314 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002315 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002316
2317 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002318 boolean animationCancelled = false;
2319
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002320 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002321 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002322 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002323 // Prepare the position
2324 toView.setTranslationX(0.0f);
2325 toView.setTranslationY(0.0f);
2326 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002327 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002328 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002329 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002330 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002331 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2332 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002333
2334 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2335 // Hide the workspace scrollbar
2336 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002337 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002338 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002339 if (!animationCancelled) {
2340 updateWallpaperVisibility(false);
2341 }
2342 }
2343
Adam Cohencff6af82011-09-13 14:51:53 -07002344 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002345 public void onAnimationCancel(Animator animation) {
2346 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002347 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002348 });
2349
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002350 if (workspaceAnim != null) {
2351 mStateAnimation.play(workspaceAnim);
2352 }
Michael Jurka1899a362011-11-03 13:50:45 -07002353
2354 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002355 final ViewTreeObserver observer;
2356
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002357 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2358 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002359
2360 // If any of the objects being animated haven't been measured/laid out
2361 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002362 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002363 (mWorkspace.getMeasuredWidth() == 0) ||
2364 (toView.getMeasuredWidth() == 0)) {
2365 observer = mWorkspace.getViewTreeObserver();
2366 delayAnim = true;
2367 } else {
2368 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002369 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002370
2371 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002372 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002373 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2374 public void onGlobalLayout() {
2375 mWorkspace.post(new Runnable() {
2376 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002377 // Check that mStateAnimation hasn't changed while
2378 // we waited for a layout pass
2379 if (mStateAnimation == stateAnimation) {
2380 // Need to update pivots for zoom if layout changed
2381 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002382 dispatchOnLauncherTransitionStart(fromView, animated, false);
2383 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka49779a12012-01-16 04:10:08 -08002384 mStateAnimation.start();
2385 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002386 }
2387 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002388 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002389 }
2390 };
2391 observer.addOnGlobalLayoutListener(delayedStart);
2392 } else {
2393 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002394 dispatchOnLauncherTransitionStart(fromView, animated, false);
2395 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka1899a362011-11-03 13:50:45 -07002396 mStateAnimation.start();
2397 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002398 } else {
2399 toView.setTranslationX(0.0f);
2400 toView.setTranslationY(0.0f);
2401 toView.setScaleX(1.0f);
2402 toView.setScaleY(1.0f);
2403 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002404 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002405
Michael Jurkabed61d22012-02-14 22:51:29 -08002406 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2407 // Hide the workspace scrollbar
2408 mWorkspace.hideScrollingIndicator(true);
2409 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002410 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002411 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002412 dispatchOnLauncherTransitionStart(fromView, animated, false);
2413 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002414 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002415 dispatchOnLauncherTransitionStart(toView, animated, false);
2416 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002417 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002418 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002419 }
2420
2421 /**
2422 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002423 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002424 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002425 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002426 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2427 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002428
Michael Jurkab3e22d92011-10-31 15:58:33 -07002429 if (mStateAnimation != null) {
2430 mStateAnimation.cancel();
2431 mStateAnimation = null;
2432 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002433 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002434
Winson Chungf0ea4d32011-06-06 14:27:16 -07002435 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002436 final int fadeOutDuration =
2437 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002438 final float scaleFactor = (float)
2439 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2440 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002441 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002442 Animator workspaceAnim = null;
2443
2444 if (toState == State.WORKSPACE) {
2445 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2446 workspaceAnim = mWorkspace.getChangeStateAnimation(
2447 Workspace.State.NORMAL, animated, stagger);
2448 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2449 workspaceAnim = mWorkspace.getChangeStateAnimation(
2450 Workspace.State.SPRING_LOADED, animated);
2451 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002452
Michael Jurkab3e22d92011-10-31 15:58:33 -07002453 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002454 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002455 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002456 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002457 final LauncherViewPropertyAnimator scaleAnim =
2458 new LauncherViewPropertyAnimator(fromView);
2459 scaleAnim.
2460 scaleX(scaleFactor).scaleY(scaleFactor).
2461 setDuration(duration).
2462 setInterpolator(new Workspace.ZoomInInterpolator());
2463
2464 final ObjectAnimator alphaAnim = ObjectAnimator
2465 .ofFloat(fromView, "alpha", 1f, 0f)
2466 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002467 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002468 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2469 @Override
2470 public void onAnimationUpdate(ValueAnimator animation) {
2471 float t = 1f - (Float) animation.getAnimatedValue();
2472 dispatchOnLauncherTransitionStep(fromView, t);
2473 dispatchOnLauncherTransitionStep(toView, t);
2474 }
2475 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002476
Michael Jurkabed61d22012-02-14 22:51:29 -08002477 mStateAnimation = new AnimatorSet();
2478
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002479 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2480 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002481
2482 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002483 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002484 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002485 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002486 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002487 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2488 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002489 if (mWorkspace != null) {
2490 mWorkspace.hideScrollingIndicator(false);
2491 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002492 if (onCompleteRunnable != null) {
2493 onCompleteRunnable.run();
2494 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002495 }
2496 });
2497
Winson Chungf0ea4d32011-06-06 14:27:16 -07002498 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002499 if (workspaceAnim != null) {
2500 mStateAnimation.play(workspaceAnim);
2501 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002502 dispatchOnLauncherTransitionStart(fromView, animated, true);
2503 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002504 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002505 } else {
2506 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002507 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002508 dispatchOnLauncherTransitionStart(fromView, animated, true);
2509 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002510 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002511 dispatchOnLauncherTransitionStart(toView, animated, true);
2512 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002513 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002514 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002515 }
2516
Michael Jurkae326f182011-11-21 14:05:46 -08002517 @Override
2518 public void onTrimMemory(int level) {
2519 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002520 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002521 mAppsCustomizeTabHost.onTrimMemory();
2522 }
2523 }
2524
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002525 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002526 showWorkspace(animated, null);
2527 }
2528
2529 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002530 if (mState != State.WORKSPACE) {
2531 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002532 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002533
2534 // Show the search bar and hotseat
2535 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002536 // We only need to animate in the dock divider if we're going from spring loaded mode
2537 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002538
2539 // Set focus to the AppsCustomize button
2540 if (mAllAppsButton != null) {
2541 mAllAppsButton.requestFocus();
2542 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002543 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002544
Michael Jurkab737ee62011-11-15 15:57:22 -08002545 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002546
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002547 // Change the state *after* we've called all the transition code
2548 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002549
Winson Chung5fb63472011-02-02 17:03:37 -08002550 // Resume the auto-advance of widgets
2551 mUserPresent = true;
2552 updateRunning();
2553
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002554 // send an accessibility event to announce the context change
2555 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002556 }
2557
Michael Jurkab3e22d92011-10-31 15:58:33 -07002558 void showAllApps(boolean animated) {
2559 if (mState != State.WORKSPACE) return;
2560
2561 showAppsCustomizeHelper(animated, false);
2562 mAppsCustomizeTabHost.requestFocus();
2563
2564 // Hide the search bar and hotseat
2565 mSearchDropTargetBar.hideSearchBar(animated);
2566
2567 // Change the state *after* we've called all the transition code
2568 mState = State.APPS_CUSTOMIZE;
2569
2570 // Pause the auto-advance of widgets until we are out of AllApps
2571 mUserPresent = false;
2572 updateRunning();
2573 closeFolder();
2574
2575 // Send an accessibility event to announce the context change
2576 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2577 }
2578
Adam Cohen7777d962011-08-18 18:58:38 -07002579 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002580 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002581 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002582 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002583 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002584 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002585 }
Adam Cohen7777d962011-08-18 18:58:38 -07002586
Adam Cohened66b2b2012-01-23 17:28:51 -08002587 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2588 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002589 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2590
Winson Chunge7a03942011-08-05 15:05:12 -07002591 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002592 @Override
2593 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002594 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002595 // Before we show workspace, hide all apps again because
2596 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2597 // clean up our state transition functions
2598 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002599 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002600 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002601 } else {
2602 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002603 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002604 }
2605 }, (extendedDelay ?
2606 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2607 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2608 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002609
Michael Jurkad3ef3062010-11-23 16:23:58 -08002610 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002611 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002612 final boolean animated = true;
2613 final boolean springLoaded = true;
2614 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002615 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002616 }
2617 // Otherwise, we are not in spring loaded mode, so don't do anything.
2618 }
2619
Michael Jurkab737ee62011-11-15 15:57:22 -08002620 void hideDockDivider() {
2621 if (mQsbDivider != null && mDockDivider != null) {
2622 mQsbDivider.setVisibility(View.INVISIBLE);
2623 mDockDivider.setVisibility(View.INVISIBLE);
2624 }
2625 }
2626
2627 void showDockDivider(boolean animated) {
2628 if (mQsbDivider != null && mDockDivider != null) {
2629 mQsbDivider.setVisibility(View.VISIBLE);
2630 mDockDivider.setVisibility(View.VISIBLE);
2631 if (mDividerAnimator != null) {
2632 mDividerAnimator.cancel();
2633 mQsbDivider.setAlpha(1f);
2634 mDockDivider.setAlpha(1f);
2635 mDividerAnimator = null;
2636 }
2637 if (animated) {
2638 mDividerAnimator = new AnimatorSet();
2639 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2640 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2641 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2642 mDividerAnimator.start();
2643 }
2644 }
2645 }
2646
Michael Jurkab3e22d92011-10-31 15:58:33 -07002647 void lockAllApps() {
2648 // TODO
2649 }
2650
2651 void unlockAllApps() {
2652 // TODO
2653 }
2654
Adam Cohenfc53cd22011-07-20 15:45:11 -07002655 public boolean isAllAppsCustomizeOpen() {
2656 return mState == State.APPS_CUSTOMIZE;
2657 }
2658
Winson Chungf0ea4d32011-06-06 14:27:16 -07002659 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002660 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002661 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002662 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002663 if (!LauncherApplication.isScreenLarge()) {
2664 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002665 if (mHotseat.getAlpha() != 1f) {
2666 int duration = mSearchDropTargetBar.getTransitionInDuration();
2667 mHotseat.animate().alpha(1f).setDuration(duration);
2668 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002669 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002670 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002671 }
2672 }
2673 }
2674
2675 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002676 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002677 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002678 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002679 if (!LauncherApplication.isScreenLarge()) {
2680 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002681 if (mHotseat.getAlpha() != 0f) {
2682 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2683 mHotseat.animate().alpha(0f).setDuration(duration);
2684 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002685 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002686 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002687 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002688 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002689 }
2690
Patrick Dubroy5f445422011-02-18 14:35:21 -08002691 /**
2692 * Add an item from all apps or customize onto the given workspace screen.
2693 * If layout is null, add to the current screen.
2694 */
2695 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002696 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002697 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002698 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002699 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002700
Winson Chungdff8ebb2011-09-08 17:25:31 -07002701 /** Maps the current orientation to an index for referencing orientation correct global icons */
2702 private int getCurrentOrientationIndexForGlobalIcons() {
2703 // default - 0, landscape - 1
2704 switch (getResources().getConfiguration().orientation) {
2705 case Configuration.ORIENTATION_LANDSCAPE:
2706 return 1;
2707 default:
2708 return 0;
2709 }
2710 }
2711
Michael Jurkaae65ee32012-04-30 11:45:54 -07002712 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002713 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002714 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002715 // Look for the toolbar icon specified in the activity meta-data
2716 Bundle metaData = packageManager.getActivityInfo(
2717 activityName, PackageManager.GET_META_DATA).metaData;
2718 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002719 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002720 if (iconResId != 0) {
2721 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002722 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002723 }
2724 }
2725 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002726 // This can happen if the activity defines an invalid drawable
2727 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2728 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002729 } catch (Resources.NotFoundException nfe) {
2730 // This can happen if the activity defines an invalid drawable
2731 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2732 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002733 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002734 return null;
2735 }
2736
2737 // if successful in getting icon, return it; otherwise, set button to use default drawable
2738 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002739 int buttonId, ComponentName activityName, int fallbackDrawableId,
2740 String toolbarResourceName) {
2741 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002742 Resources r = getResources();
2743 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2744 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002745
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002746 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002747 // If we were unable to find the icon via the meta-data, use a generic one
2748 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002749 toolbarIcon = r.getDrawable(fallbackDrawableId);
2750 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002751 if (button != null) {
2752 button.setCompoundDrawables(toolbarIcon, null, null, null);
2753 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002754 return null;
2755 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002756 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002757 if (button != null) {
2758 button.setCompoundDrawables(toolbarIcon, null, null, null);
2759 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002760 return toolbarIcon.getConstantState();
2761 }
2762 }
2763
2764 // if successful in getting icon, return it; otherwise, set button to use default drawable
2765 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002766 int buttonId, ComponentName activityName, int fallbackDrawableId,
2767 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002768 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002769 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002770
Michael Jurka19e0fc52011-07-22 18:00:21 -07002771 if (button != null) {
2772 // If we were unable to find the icon via the meta-data, use a
2773 // generic one
2774 if (toolbarIcon == null) {
2775 button.setImageResource(fallbackDrawableId);
2776 } else {
2777 button.setImageDrawable(toolbarIcon);
2778 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002779 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002780
2781 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2782
Michael Jurka0423dcf2010-10-05 14:56:18 -07002783 }
2784
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002785 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2786 TextView button = (TextView) findViewById(buttonId);
2787 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2788 }
2789
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002790 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002791 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002792 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002793 }
2794
Winson Chungbb185bd2011-11-21 12:31:42 -08002795 private void invalidatePressedFocusedStates(View container, View button) {
2796 if (container instanceof HolographicLinearLayout) {
2797 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2798 layout.invalidatePressedFocusedStates();
2799 } else if (button instanceof HolographicImageView) {
2800 HolographicImageView view = (HolographicImageView) button;
2801 view.invalidatePressedFocusedStates();
2802 }
2803 }
2804
Winson Chungc51db6a2011-10-05 11:44:49 -07002805 private boolean updateGlobalSearchIcon() {
2806 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002807 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2808 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002809 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002810 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002811 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002812
Winson Chung1cad91e2011-05-25 17:41:01 -07002813 final SearchManager searchManager =
2814 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2815 ComponentName activityName = searchManager.getGlobalSearchActivity();
2816 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002817 int coi = getCurrentOrientationIndexForGlobalIcons();
2818 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002819 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2820 TOOLBAR_SEARCH_ICON_METADATA_NAME);
2821 if (sGlobalSearchIcon[coi] == null) {
2822 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2823 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2824 TOOLBAR_ICON_METADATA_NAME);
2825 }
2826
Winson Chung649723c2011-07-06 20:41:23 -07002827 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002828 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2829 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002830 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002831 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002832 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002833 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002834 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002835 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2836 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2837 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002838 voiceButton.setVisibility(View.GONE);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002839 voiceButtonProxy.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002840 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002841 }
2842 }
2843
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002844 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002845 final View searchButtonContainer = findViewById(R.id.search_button_container);
2846 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002847 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002848 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002849 }
2850
Winson Chungc51db6a2011-10-05 11:44:49 -07002851 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002852 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002853 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002854 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002855 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002856
Winson Chungc51db6a2011-10-05 11:44:49 -07002857 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07002858 final SearchManager searchManager =
2859 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2860 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2861
2862 ComponentName activityName = null;
2863 if (globalSearchActivity != null) {
2864 // Check if the global search activity handles voice search
2865 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2866 intent.setPackage(globalSearchActivity.getPackageName());
2867 activityName = intent.resolveActivity(getPackageManager());
2868 }
2869
2870 if (activityName == null) {
2871 // Fallback: check if an activity other than the global search activity
2872 // resolves this
2873 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2874 activityName = intent.resolveActivity(getPackageManager());
2875 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002876 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002877 int coi = getCurrentOrientationIndexForGlobalIcons();
2878 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002879 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2880 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
2881 if (sVoiceSearchIcon[coi] == null) {
2882 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2883 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2884 TOOLBAR_ICON_METADATA_NAME);
2885 }
Winson Chung649723c2011-07-06 20:41:23 -07002886 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002887 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002888 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07002889 if (voiceButtonProxy != null) {
2890 voiceButtonProxy.setVisibility(View.VISIBLE);
2891 }
Winson Chungbb185bd2011-11-21 12:31:42 -08002892 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002893 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002894 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002895 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002896 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002897 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07002898 if (voiceButtonProxy != null) {
2899 voiceButtonProxy.setVisibility(View.GONE);
2900 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002901 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002902 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002903 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002904
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002905 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002906 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2907 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002908 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002909 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002910 }
2911
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002912 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002913 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002914 */
2915 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002916 final View marketButton = findViewById(R.id.market_button);
2917 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2918 // Find the app market activity by resolving an intent.
2919 // (If multiple app markets are installed, it will return the ResolverActivity.)
2920 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002921 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002922 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002923 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002924 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002925 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
2926 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07002927 marketButton.setVisibility(View.VISIBLE);
2928 } else {
2929 // We should hide and disable the view so that we don't try and restore the visibility
2930 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2931 marketButton.setVisibility(View.GONE);
2932 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002933 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002934 }
2935
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002936 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002937 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002938 }
2939
Michael Jurkad7c28052012-04-27 15:43:36 -07002940 @Override
2941 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2942 boolean result = super.dispatchPopulateAccessibilityEvent(event);
2943 final List<CharSequence> text = event.getText();
2944 text.clear();
2945 text.add(getString(R.string.home));
2946 return result;
2947 }
2948
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002949 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002950 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002951 */
2952 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2953 @Override
2954 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002955 closeSystemDialogs();
2956 }
2957 }
2958
2959 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002960 * Receives notifications whenever the appwidgets are reset.
2961 */
2962 private class AppWidgetResetObserver extends ContentObserver {
2963 public AppWidgetResetObserver() {
2964 super(new Handler());
2965 }
2966
2967 @Override
2968 public void onChange(boolean selfChange) {
2969 onAppWidgetReset();
2970 }
2971 }
2972
2973 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002974 * If the activity is currently paused, signal that we need to re-run the loader
2975 * in onResume.
2976 *
2977 * This needs to be called from incoming places where resources might have been loaded
2978 * while we are paused. That is becaues the Configuration might be wrong
2979 * when we're not running, and if it comes back to what it was when we
2980 * were paused, we are not restarted.
2981 *
2982 * Implementation of the method from LauncherModel.Callbacks.
2983 *
2984 * @return true if we are currently paused. The caller might be able to
2985 * skip some work in that case since we will come back again.
2986 */
2987 public boolean setLoadOnResume() {
2988 if (mPaused) {
2989 Log.i(TAG, "setLoadOnResume");
2990 mOnResumeNeedsLoad = true;
2991 return true;
2992 } else {
2993 return false;
2994 }
2995 }
2996
2997 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002998 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002999 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003000 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003001 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003002 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003003 } else {
3004 return SCREEN_COUNT / 2;
3005 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003006 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003007
Joe Onorato9c1289c2009-08-17 11:03:03 -04003008 /**
3009 * Refreshes the shortcuts shown on the workspace.
3010 *
3011 * Implementation of the method from LauncherModel.Callbacks.
3012 */
3013 public void startBinding() {
3014 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003015
Winson Chungf0c6ae02012-03-21 16:10:31 -07003016 mNewShortcutAnimatePage = -1;
3017 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003018 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003019 int count = workspace.getChildCount();
3020 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003021 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003022 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3023 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003024 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003025 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003026 if (mHotseat != null) {
3027 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028 }
3029 }
3030
3031 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003032 * Bind the items start-end from the list.
3033 *
3034 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003035 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003036 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003037 setLoadOnResume();
3038
Winson Chungf0c6ae02012-03-21 16:10:31 -07003039 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3040 Set<String> newApps = new HashSet<String>();
3041 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3042
3043 Workspace workspace = mWorkspace;
3044 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003045 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003046
3047 // Short circuit if we are loading dock items for a configuration which has no dock
3048 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3049 mHotseat == null) {
3050 continue;
3051 }
3052
Joe Onorato9c1289c2009-08-17 11:03:03 -04003053 switch (item.itemType) {
3054 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3055 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003056 ShortcutInfo info = (ShortcutInfo) item;
3057 String uri = info.intent.toUri(0).toString();
3058 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003059 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3060 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003061 if (newApps.contains(uri)) {
3062 newApps.remove(uri);
3063
3064 // Prepare the view to be animated up
3065 shortcut.setAlpha(0f);
3066 shortcut.setScaleX(0f);
3067 shortcut.setScaleY(0f);
3068 mNewShortcutAnimatePage = item.screen;
3069 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3070 mNewShortcutAnimateViews.add(shortcut);
3071 }
3072 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003073 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003074 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003075 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003076 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003077 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003078 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3079 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003080 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003083
Joe Onorato9c1289c2009-08-17 11:03:03 -04003084 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003085 }
3086
Joe Onorato9c1289c2009-08-17 11:03:03 -04003087 /**
3088 * Implementation of the method from LauncherModel.Callbacks.
3089 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003090 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003091 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003092 sFolders.clear();
3093 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003094 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003095
3096 /**
3097 * Add the views for a widget to the workspace.
3098 *
3099 * Implementation of the method from LauncherModel.Callbacks.
3100 */
3101 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003102 setLoadOnResume();
3103
Daniel Sandler843e8602010-06-07 14:59:01 -04003104 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3105 if (DEBUG_WIDGETS) {
3106 Log.d(TAG, "bindAppWidget: " + item);
3107 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003108 final Workspace workspace = mWorkspace;
3109
3110 final int appWidgetId = item.appWidgetId;
3111 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003112 if (DEBUG_WIDGETS) {
3113 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3114 }
3115
Joe Onorato9c1289c2009-08-17 11:03:03 -04003116 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3117
Joe Onorato9c1289c2009-08-17 11:03:03 -04003118 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3119 item.hostView.setTag(item);
3120
Winson Chung3d503fb2011-07-13 17:25:49 -07003121 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003122 item.cellY, item.spanX, item.spanY, false);
3123
Adam Cohended9f8d2010-11-03 13:25:16 -07003124 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3125
Joe Onorato9c1289c2009-08-17 11:03:03 -04003126 workspace.requestLayout();
3127
Winson Chungf51025f2012-05-03 15:21:11 -07003128 AppWidgetResizeFrame.updateWidgetSizeRanges(item.hostView,
3129 this, item.spanX, item.spanY);
3130
Daniel Sandler843e8602010-06-07 14:59:01 -04003131 if (DEBUG_WIDGETS) {
3132 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3133 + (SystemClock.uptimeMillis()-start) + "ms");
3134 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003135 }
3136
3137 /**
3138 * Callback saying that there aren't any more items to bind.
3139 *
3140 * Implementation of the method from LauncherModel.Callbacks.
3141 */
3142 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003143 setLoadOnResume();
3144
Joe Onorato9c1289c2009-08-17 11:03:03 -04003145 if (mSavedState != null) {
3146 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003147 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003148 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003149 mSavedState = null;
3150 }
3151
3152 if (mSavedInstanceState != null) {
3153 super.onRestoreInstanceState(mSavedInstanceState);
3154 mSavedInstanceState = null;
3155 }
3156
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003157 // If we received the result of any pending adds while the loader was running (e.g. the
3158 // widget configuration forced an orientation change), process them now.
3159 for (int i = 0; i < sPendingAddList.size(); i++) {
3160 completeAdd(sPendingAddList.get(i));
3161 }
3162 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003163
Winson Chungc51db6a2011-10-05 11:44:49 -07003164 // Update the market app icon as necessary (the other icons will be managed in response to
3165 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003166 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003167
Winson Chungf0c6ae02012-03-21 16:10:31 -07003168 // Animate up any icons as necessary
3169 if (mVisible || mWorkspaceLoading) {
3170 Runnable newAppsRunnable = new Runnable() {
3171 @Override
3172 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003173 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003174 }
3175 };
Winson Chunga2413752012-04-03 14:22:34 -07003176
3177 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3178 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3179 if (canRunNewAppsAnimation()) {
3180 // If the user has not interacted recently, then either snap to the new page to show
3181 // the new-apps animation or just run them if they are to appear on the current page
3182 if (willSnapPage) {
3183 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3184 } else {
3185 runNewAppsAnimation(false);
3186 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003187 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003188 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003189 // are on another page (or just normally if they are added to the current page)
3190 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003191 }
3192 }
3193
3194 mWorkspaceLoading = false;
3195 }
3196
Winson Chunga2413752012-04-03 14:22:34 -07003197 private boolean canRunNewAppsAnimation() {
3198 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3199 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3200 }
3201
Winson Chungf0c6ae02012-03-21 16:10:31 -07003202 /**
3203 * Runs a new animation that scales up icons that were added while Launcher was in the
3204 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003205 *
3206 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003207 */
Winson Chunga2413752012-04-03 14:22:34 -07003208 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003209 AnimatorSet anim = new AnimatorSet();
3210 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003211
3212 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003213 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3214 @Override
3215 public int compare(View a, View b) {
3216 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3217 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3218 int cellCountX = LauncherModel.getCellCountX();
3219 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3220 }
3221 });
Winson Chunga2413752012-04-03 14:22:34 -07003222
3223 // Animate each of the views in place (or show them immediately if requested)
3224 if (immediate) {
3225 for (View v : mNewShortcutAnimateViews) {
3226 v.setAlpha(1f);
3227 v.setScaleX(1f);
3228 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003229 }
Winson Chunga2413752012-04-03 14:22:34 -07003230 } else {
3231 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3232 View v = mNewShortcutAnimateViews.get(i);
3233 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3234 PropertyValuesHolder.ofFloat("alpha", 1f),
3235 PropertyValuesHolder.ofFloat("scaleX", 1f),
3236 PropertyValuesHolder.ofFloat("scaleY", 1f));
3237 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3238 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3239 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3240 bounceAnims.add(bounceAnim);
3241 }
3242 anim.playTogether(bounceAnims);
3243 anim.addListener(new AnimatorListenerAdapter() {
3244 @Override
3245 public void onAnimationEnd(Animator animation) {
3246 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3247 }
3248 });
3249 anim.start();
3250 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003251
3252 // Clean up
3253 mNewShortcutAnimatePage = -1;
3254 mNewShortcutAnimateViews.clear();
3255 new Thread("clearNewAppsThread") {
3256 public void run() {
3257 mSharedPrefs.edit()
3258 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3259 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3260 .commit();
3261 }
3262 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003263 }
3264
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003265 @Override
3266 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003267 boolean searchVisible = updateGlobalSearchIcon();
3268 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3269 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003270 }
3271
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003272 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003273 * Add the icons for all apps.
3274 *
3275 * Implementation of the method from LauncherModel.Callbacks.
3276 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003277 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3278 // Remove the progress bar entirely; we could also make it GONE
3279 // but better to remove it since we know it's not going to be used
3280 View progressBar = mAppsCustomizeTabHost.
3281 findViewById(R.id.apps_customize_progress_bar);
3282 if (progressBar != null) {
3283 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003284 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003285 // We just post the call to setApps so the user sees the progress bar
3286 // disappear-- otherwise, it just looks like the progress bar froze
3287 // which doesn't look great
3288 mAppsCustomizeTabHost.post(new Runnable() {
3289 public void run() {
3290 if (mAppsCustomizeContent != null) {
3291 mAppsCustomizeContent.setApps(apps);
3292 }
3293 }
3294 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003295 }
3296
3297 /**
3298 * A package was installed.
3299 *
3300 * Implementation of the method from LauncherModel.Callbacks.
3301 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003302 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003303 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003304
Winson Chung785d2eb2011-04-14 16:08:02 -07003305 if (mAppsCustomizeContent != null) {
3306 mAppsCustomizeContent.addApps(apps);
3307 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003308 }
3309
3310 /**
3311 * A package was updated.
3312 *
3313 * Implementation of the method from LauncherModel.Callbacks.
3314 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003315 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003316 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003317 if (mWorkspace != null) {
3318 mWorkspace.updateShortcuts(apps);
3319 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003320
Winson Chung785d2eb2011-04-14 16:08:02 -07003321 if (mAppsCustomizeContent != null) {
3322 mAppsCustomizeContent.updateApps(apps);
3323 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003324 }
3325
3326 /**
3327 * A package was uninstalled.
3328 *
3329 * Implementation of the method from LauncherModel.Callbacks.
3330 */
Joe Onorato36115782010-06-17 13:28:48 -04003331 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003332 if (permanent) {
3333 mWorkspace.removeItems(apps);
3334 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003335
Winson Chung785d2eb2011-04-14 16:08:02 -07003336 if (mAppsCustomizeContent != null) {
3337 mAppsCustomizeContent.removeApps(apps);
3338 }
Winson Chunga1820962011-10-03 16:31:06 -07003339
3340 // Notify the drag controller
3341 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003342 }
Joe Onoratobe386092009-11-17 17:32:16 -08003343
3344 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003345 * A number of packages were updated.
3346 */
3347 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003348 if (mAppsCustomizeContent != null) {
3349 mAppsCustomizeContent.onPackagesUpdated();
3350 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003351 }
3352
Winson Chung400438b2011-01-16 17:53:48 -08003353 private int mapConfigurationOriActivityInfoOri(int configOri) {
3354 final Display d = getWindowManager().getDefaultDisplay();
3355 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3356 switch (d.getRotation()) {
3357 case Surface.ROTATION_0:
3358 case Surface.ROTATION_180:
3359 // We are currently in the same basic orientation as the natural orientation
3360 naturalOri = configOri;
3361 break;
3362 case Surface.ROTATION_90:
3363 case Surface.ROTATION_270:
3364 // We are currently in the other basic orientation to the natural orientation
3365 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3366 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3367 break;
3368 }
3369
3370 int[] oriMap = {
3371 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3372 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3373 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3374 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3375 };
3376 // Since the map starts at portrait, we need to offset if this device's natural orientation
3377 // is landscape.
3378 int indexOffset = 0;
3379 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3380 indexOffset = 1;
3381 }
3382 return oriMap[(d.getRotation() + indexOffset) % 4];
3383 }
Adam Cohen446e9402011-09-15 18:21:21 -07003384
Winson Chung4b919f82012-05-01 10:44:08 -07003385 public boolean isRotationEnabled() {
3386 boolean forceEnableRotation = "true".equalsIgnoreCase(SystemProperties.get(
3387 FORCE_ENABLE_ROTATION_PROPERTY, "false"));
3388 boolean enableRotation = forceEnableRotation ||
3389 getResources().getBoolean(R.bool.allow_rotation);
3390 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003391 }
Winson Chung4b919f82012-05-01 10:44:08 -07003392 public void lockScreenOrientation() {
3393 if (isRotationEnabled()) {
3394 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3395 .getConfiguration().orientation));
3396 }
3397 }
3398 public void unlockScreenOrientation(boolean immediate) {
3399 if (isRotationEnabled()) {
3400 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003401 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003402 } else {
3403 mHandler.postDelayed(new Runnable() {
3404 public void run() {
3405 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3406 }
3407 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003408 }
Winson Chung4b919f82012-05-01 10:44:08 -07003409 }
Winson Chung400438b2011-01-16 17:53:48 -08003410 }
3411
Winson Chung82f55532011-08-09 14:14:23 -07003412 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003413 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003414 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003415 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003416
Winson Chung7d7541e2011-09-16 20:14:36 -07003417 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003418 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003419 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3420 Cling cling = (Cling) findViewById(clingId);
3421 if (cling != null) {
3422 cling.init(this, positionData);
3423 cling.setVisibility(View.VISIBLE);
3424 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3425 if (animate) {
3426 cling.buildLayer();
3427 cling.setAlpha(0f);
3428 cling.animate()
3429 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003430 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003431 .setDuration(SHOW_CLING_DURATION)
3432 .setStartDelay(delay)
3433 .start();
3434 } else {
3435 cling.setAlpha(1f);
3436 }
3437 }
3438 return cling;
3439 }
3440 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003441 if (cling != null) {
3442 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003443 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003444 anim.addListener(new AnimatorListenerAdapter() {
3445 public void onAnimationEnd(Animator animation) {
3446 cling.setVisibility(View.GONE);
3447 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003448 // We should update the shared preferences on a background thread
3449 new Thread("dismissClingThread") {
3450 public void run() {
3451 SharedPreferences.Editor editor = mSharedPrefs.edit();
3452 editor.putBoolean(flag, true);
3453 editor.commit();
3454 }
3455 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003456 };
3457 });
3458 anim.start();
3459 }
3460 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003461 private void removeCling(int id) {
3462 final View cling = findViewById(id);
3463 if (cling != null) {
3464 final ViewGroup parent = (ViewGroup) cling.getParent();
3465 parent.post(new Runnable() {
3466 @Override
3467 public void run() {
3468 parent.removeView(cling);
3469 }
3470 });
3471 }
3472 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003473 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003474 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003475 if (isClingsEnabled() &&
3476 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003477 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003478 } else {
3479 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003480 }
3481 }
3482 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003483 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003484 if (isClingsEnabled() &&
3485 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003486 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003487 } else {
3488 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003489 }
3490 }
3491 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003492 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003493 if (isClingsEnabled() &&
3494 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3495 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003496 } else {
3497 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003498 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003499 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003500 }
3501 public boolean isFolderClingVisible() {
3502 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003503 if (cling != null) {
3504 return cling.getVisibility() == View.VISIBLE;
3505 }
3506 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003507 }
Winson Chung82f55532011-08-09 14:14:23 -07003508 public void dismissWorkspaceCling(View v) {
3509 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003510 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003511 }
3512 public void dismissAllAppsCling(View v) {
3513 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003514 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3515 }
3516 public void dismissFolderCling(View v) {
3517 Cling cling = (Cling) findViewById(R.id.folder_cling);
3518 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003519 }
3520
Winson Chung80baf5a2010-08-09 16:03:15 -07003521 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003522 * Prints out out state for debugging.
3523 */
3524 public void dumpState() {
3525 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003526 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003527 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3528 Log.d(TAG, "mRestoring=" + mRestoring);
3529 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3530 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003531 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003532 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003533
Winson Chung785d2eb2011-04-14 16:08:02 -07003534 if (mAppsCustomizeContent != null) {
3535 mAppsCustomizeContent.dumpState();
3536 }
Joe Onoratobe386092009-11-17 17:32:16 -08003537 Log.d(TAG, "END launcher2 dump state");
3538 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003539
3540 @Override
3541 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3542 super.dump(prefix, fd, writer, args);
3543 writer.println(" ");
3544 writer.println("Debug logs: ");
3545 for (int i = 0; i < sDumpLogs.size(); i++) {
3546 writer.println(" " + sDumpLogs.get(i));
3547 }
3548 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003549}
Michael Jurka2763be32011-02-24 11:19:57 -08003550
Michael Jurkaabded662011-03-04 12:06:57 -08003551interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003552 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003553 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003554 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003555 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003556 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003557}