blob: 74ec6f3b5ece4fb236576019d816fae0e236d2b4 [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
Winson Chung36a62fe2012-05-06 18:04:42 -0700499 boolean isDraggingEnabled() {
500 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
501 // that is subsequently removed from the workspace in startBinding().
502 return !mModel.isLoadingWorkspace();
503 }
504
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 static int getScreen() {
506 synchronized (sLock) {
507 return sScreen;
508 }
509 }
510
511 static void setScreen(int screen) {
512 synchronized (sLock) {
513 sScreen = screen;
514 }
515 }
516
Winson Chung557d6ed2011-07-08 15:34:52 -0700517 /**
518 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
519 * a configuration step, this allows the proper animations to run after other transitions.
520 */
521 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700522 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800523 switch (args.requestCode) {
524 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700525 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
526 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800527 break;
528 case REQUEST_PICK_SHORTCUT:
529 processShortcut(args.intent);
530 break;
531 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700532 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
533 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700534 result = true;
535 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800536 case REQUEST_PICK_APPWIDGET:
537 addAppWidgetFromPick(args.intent);
538 break;
539 case REQUEST_CREATE_APPWIDGET:
540 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800541 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700542 result = true;
543 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800544 case REQUEST_PICK_WALLPAPER:
545 // We just wanted the activity result here so we can clear mWaitingForResult
546 break;
547 }
Michael Jurka27614d22012-04-02 06:40:22 -0700548 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
549 // if you turned the screen off and then back while in All Apps, Launcher would not
550 // return to the workspace. Clearing mAddInfo.container here fixes this issue
551 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700552 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800553 }
554
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700556 protected void onActivityResult(
557 final int requestCode, final int resultCode, final Intent data) {
558 if (requestCode == REQUEST_BIND_APPWIDGET) {
559 int appWidgetId = data != null ?
560 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
561 if (resultCode == RESULT_CANCELED) {
562 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
563 } else if (resultCode == RESULT_OK) {
564 addAppWidgetImpl(appWidgetId, mWidgetBeingBoundOrConfigured);
565 }
566 return;
567 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700568 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800569 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
570 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700571 mWaitingForResult = false;
572
Adam Cohened66b2b2012-01-23 17:28:51 -0800573 // We have special handling for widgets
574 if (isWidgetDrop) {
575 int appWidgetId = data != null ?
576 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700577 if (appWidgetId < 0) {
578 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
579 "widget configuration activity.");
580 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
581 } else {
582 completeTwoStageWidgetDrop(resultCode, appWidgetId);
583 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800584 return;
585 }
586
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 // The pattern used here is that a user PICKs a specific application,
588 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700589
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
591 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700592 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800593 final PendingAddArguments args = new PendingAddArguments();
594 args.requestCode = requestCode;
595 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700596 args.container = mPendingAddInfo.container;
597 args.screen = mPendingAddInfo.screen;
598 args.cellX = mPendingAddInfo.cellX;
599 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800600 if (isWorkspaceLocked()) {
601 sPendingAddList.add(args);
602 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700603 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800604 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800606 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700607 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800608 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
609 null);
610 }
611
612 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700613 CellLayout cellLayout =
614 (CellLayout) mWorkspace.getChildAt(mWidgetBeingBoundOrConfigured.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800615 Runnable onCompleteRunnable = null;
616 int animationType = 0;
617
618 if (resultCode == RESULT_OK) {
619 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
620 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Michael Jurka8b805b12012-04-18 14:23:14 -0700621 mWidgetBeingBoundOrConfigured.info);
622 mWidgetBeingBoundOrConfigured.boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800623 onCompleteRunnable = new Runnable() {
624 @Override
625 public void run() {
626 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
627 mPendingAddInfo.screen, layout, null);
628 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
629 null);
630 }
631 };
632 } else if (resultCode == RESULT_CANCELED) {
633 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
634 onCompleteRunnable = new Runnable() {
635 @Override
636 public void run() {
637 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
638 null);
639 }
640 };
641 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700642 mWorkspace.animateWidgetDrop(mWidgetBeingBoundOrConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800643 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Michael Jurka8b805b12012-04-18 14:23:14 -0700644 animationType, mWidgetBeingBoundOrConfigured.boundWidget, true);
645 mWidgetBeingBoundOrConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 }
647
648 @Override
649 protected void onResume() {
650 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700651
Winson Chungde0fb8f2012-05-08 14:37:08 -0700652 // Process any items that were added while Launcher was away
653 InstallShortcutReceiver.flushInstallQueue(this);
654
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800655 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700656 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700658 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400659 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700660 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 }
Winson Chunge4e50662012-01-23 14:45:13 -0800662
663 // Reset the pressed state of icons that were locked in the press state while activities
664 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800665 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800666 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800667 mWaitingForResume.setStayPressed(false);
668 }
Winson Chunge4e50662012-01-23 14:45:13 -0800669 if (mAppsCustomizeContent != null) {
670 // Resets the previous all apps icon press state
671 mAppsCustomizeContent.resetDrawableState();
672 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 }
674
675 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700676 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700677 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
678 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
679 // when Launcher resumes and we are still in AllApps.
680 updateWallpaperVisibility(true);
681
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700682 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700683 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800684 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700685 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700686 }
Romain Guycbb89e42009-06-08 15:52:54 -0700687
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700688 @Override
689 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400690 // Flag the loader to stop early before switching
691 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700692 if (mAppsCustomizeContent != null) {
693 mAppsCustomizeContent.surrender();
694 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800695 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700696 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800698 // We can't hide the IME if it was forced open. So don't bother
699 /*
700 @Override
701 public void onWindowFocusChanged(boolean hasFocus) {
702 super.onWindowFocusChanged(hasFocus);
703
704 if (hasFocus) {
705 final InputMethodManager inputManager = (InputMethodManager)
706 getSystemService(Context.INPUT_METHOD_SERVICE);
707 WindowManager.LayoutParams lp = getWindow().getAttributes();
708 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
709 android.os.Handler()) {
710 protected void onReceiveResult(int resultCode, Bundle resultData) {
711 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
712 }
713 });
714 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
715 }
716 }
717 */
718
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 private boolean acceptFilter() {
720 final InputMethodManager inputManager = (InputMethodManager)
721 getSystemService(Context.INPUT_METHOD_SERVICE);
722 return !inputManager.isFullscreenMode();
723 }
724
725 @Override
726 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700727 final int uniChar = event.getUnicodeChar();
728 final boolean handled = super.onKeyDown(keyCode, event);
729 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
730 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
732 keyCode, event);
733 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700734 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700735 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700736 // showSearchDialog()
737 // If there are multiple keystrokes before the search dialog takes focus,
738 // onSearchRequested() will be called for every keystroke,
739 // but it is idempotent, so it's fine.
740 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 }
742 }
743
Joe Onorato8a9625e2010-01-28 15:55:35 -0800744 // Eat the long press event so the keyboard doesn't come up.
745 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
746 return true;
747 }
748
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 return handled;
750 }
751
Karl Rosaen138a0412009-04-23 19:00:21 -0700752 private String getTypedText() {
753 return mDefaultKeySsb.toString();
754 }
755
756 private void clearTypedText() {
757 mDefaultKeySsb.clear();
758 mDefaultKeySsb.clearSpans();
759 Selection.setSelection(mDefaultKeySsb, 0);
760 }
761
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700763 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
764 * State
765 */
766 private static State intToState(int stateOrdinal) {
767 State state = State.WORKSPACE;
768 final State[] stateValues = State.values();
769 for (int i = 0; i < stateValues.length; i++) {
770 if (stateValues[i].ordinal() == stateOrdinal) {
771 state = stateValues[i];
772 break;
773 }
774 }
775 return state;
776 }
777
778 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 * Restores the previous state, if it exists.
780 *
781 * @param savedState The previous state.
782 */
783 private void restoreState(Bundle savedState) {
784 if (savedState == null) {
785 return;
786 }
787
Michael Jurka883f55b2010-10-21 15:47:14 -0700788 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700789 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800790 showAllApps(false);
791 }
792
Winson Chungf0c6ae02012-03-21 16:10:31 -0700793 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700795 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 }
797
Winson Chung3d503fb2011-07-13 17:25:49 -0700798 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
799 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700800
Winson Chung3d503fb2011-07-13 17:25:49 -0700801 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
802 mPendingAddInfo.container = pendingAddContainer;
803 mPendingAddInfo.screen = pendingAddScreen;
804 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
805 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 mRestoring = true;
807 }
808
809 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
810 if (renameFolder) {
811 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700812 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 mRestoring = true;
814 }
Winson Chunga12a2502010-12-20 14:41:35 -0800815
Winson Chung785d2eb2011-04-14 16:08:02 -0700816
817 // Restore the AppsCustomize tab
818 if (mAppsCustomizeTabHost != null) {
819 String curTab = savedState.getString("apps_customize_currentTab");
820 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700821 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700822 mAppsCustomizeContent.setContentType(
823 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
824 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700825 mAppsCustomizeContent.loadAssociatedPages(
826 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700827 }
828
Winson Chung5afbf7b2011-07-25 11:53:08 -0700829 int currentIndex = savedState.getInt("apps_customize_currentIndex");
830 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 }
833
834 /**
835 * Finds all the views we need and configure them properly.
836 */
837 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700838 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400839
Winson Chung4c98d922011-05-31 16:50:48 -0700840 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
841 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800842 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
843 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844
Winson Chung4c98d922011-05-31 16:50:48 -0700845 // Setup the drag layer
846 mDragLayer.setup(this, dragController);
847
Winson Chung3d503fb2011-07-13 17:25:49 -0700848 // Setup the hotseat
849 mHotseat = (Hotseat) findViewById(R.id.hotseat);
850 if (mHotseat != null) {
851 mHotseat.setup(this);
852 }
853
Winson Chung4c98d922011-05-31 16:50:48 -0700854 // Setup the workspace
855 mWorkspace.setHapticFeedbackEnabled(false);
856 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700857 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700858 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700859
Winson Chungf0ea4d32011-06-06 14:27:16 -0700860 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700861 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700862
Winson Chungf0ea4d32011-06-06 14:27:16 -0700863 // Setup AppsCustomize
864 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
865 findViewById(R.id.apps_customize_pane);
866 mAppsCustomizeContent = (AppsCustomizePagedView)
867 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700868 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700869 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400870
Michael Jurka5130e402011-10-13 04:55:35 -0700871 // Get the all apps button
872 mAllAppsButton = findViewById(R.id.all_apps_button);
873 if (mAllAppsButton != null) {
874 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
875 @Override
876 public boolean onTouch(View v, MotionEvent event) {
877 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
878 onTouchDownAllAppsButton(v);
879 }
880 return false;
881 }
882 });
883 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700884 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700885 dragController.setDragScoller(mWorkspace);
886 dragController.setScrollView(mDragLayer);
887 dragController.setMoveTarget(mWorkspace);
888 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700889 if (mSearchDropTargetBar != null) {
890 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800891 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 }
893
894 /**
895 * Creates a view representing a shortcut.
896 *
897 * @param info The data structure describing the shortcut.
898 *
899 * @return A View inflated from R.layout.application.
900 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800901 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700903 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
905
906 /**
907 * Creates a view representing a shortcut inflated from the specified resource.
908 *
909 * @param layoutResId The id of the XML layout used to create the shortcut.
910 * @param parent The group the shortcut belongs to.
911 * @param info The data structure describing the shortcut.
912 *
913 * @return A View inflated from layoutResId.
914 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800915 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800916 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
917 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 return favorite;
920 }
921
922 /**
923 * Add an application shortcut to the workspace.
924 *
925 * @param data The intent describing the application.
926 * @param cellInfo The position on screen where to create the shortcut.
927 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700928 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700929 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700930 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700931
Adam Cohenfbba09b2011-07-18 21:43:05 -0700932 // First we check if we already know the exact location where we want to add this item.
933 if (cellX >= 0 && cellY >= 0) {
934 cellXY[0] = cellX;
935 cellXY[1] = cellY;
936 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700937 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700938 return;
939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800941 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800942
Romain Guy73b979d2009-06-09 12:57:21 -0700943 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800944 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800946 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700947 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700948 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800949 } else {
950 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 }
952 }
Romain Guycbb89e42009-06-08 15:52:54 -0700953
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 /**
955 * Add a shortcut to the workspace.
956 *
957 * @param data The intent describing the shortcut.
958 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700960 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
961 int cellY) {
962 int[] cellXY = mTmpAddItemCellCoordinates;
963 int[] touchXY = mPendingAddInfo.dropPos;
964 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700965
Michael Jurkad3ef3062010-11-23 16:23:58 -0800966 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700967
Adam Cohen558baaf2011-08-15 15:22:57 -0700968 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800969 if (info == null) {
970 return;
971 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700972 final View view = createShortcut(info);
973
Adam Cohenfbba09b2011-07-18 21:43:05 -0700974 // First we check if we already know the exact location where we want to add this item.
975 if (cellX >= 0 && cellY >= 0) {
976 cellXY[0] = cellX;
977 cellXY[1] = cellY;
978 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700979
980 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800981 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700982 true, null,null)) {
983 return;
984 }
985 DragObject dragObject = new DragObject();
986 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -0800987 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
988 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -0700989 return;
990 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700991 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800992 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700993 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800994 foundCellSpan = (result != null);
995 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700996 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800997 }
998
999 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001000 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001001 return;
1002 }
1003
Adam Cohen558baaf2011-08-15 15:22:57 -07001004 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005
1006 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001007 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1008 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 }
1010 }
1011
Adam Cohen2f093b62012-04-30 18:59:53 -07001012 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1013 int minHeight) {
1014 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001015 // We want to account for the extra amount of padding that we are adding to the widget
1016 // to ensure that it gets the full amount of space that it has requested
1017 int requiredWidth = minWidth + padding.left + padding.right;
1018 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001019 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001020 }
1021
Adam Cohen2f093b62012-04-30 18:59:53 -07001022 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1023 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001024 }
1025
Adam Cohen2f093b62012-04-30 18:59:53 -07001026 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1027 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001028 }
1029
Adam Cohen2f093b62012-04-30 18:59:53 -07001030 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1031 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001032 }
1033
Adam Cohen2f093b62012-04-30 18:59:53 -07001034 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1035 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001036 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001037 }
1038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001040 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001042 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001043 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001045 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1046 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1047 if (appWidgetInfo == null) {
1048 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1049 }
Romain Guycbb89e42009-06-08 15:52:54 -07001050
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001051 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001052 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001053
Adam Cohen2f093b62012-04-30 18:59:53 -07001054 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1055 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001058 // We have saved the position to which the widget was dragged-- this really only matters
1059 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001060 int[] cellXY = mTmpAddItemCellCoordinates;
1061 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001062 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001063 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001064 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1065 cellXY[0] = mPendingAddInfo.cellX;
1066 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001067 spanXY[0] = mPendingAddInfo.spanX;
1068 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001069 foundCellSpan = true;
1070 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001071 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001072 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001073 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1074 spanXY[1], cellXY, finalSpan);
1075 spanXY[0] = finalSpan[0];
1076 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001077 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001078 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001079 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001080 }
1081
Michael Jurka0280c3b2010-09-17 15:00:07 -07001082 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001083 if (appWidgetId != -1) {
1084 // Deleting an app widget ID is a void call but writes to disk before returning
1085 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001086 new Thread("deleteAppWidgetId") {
1087 public void run() {
1088 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1089 }
1090 }.start();
1091 }
Winson Chung93eef082012-03-23 15:59:27 -07001092 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001093 return;
1094 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001096 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001097 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1098 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001099 launcherInfo.spanX = spanXY[0];
1100 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001101 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1102 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001103
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001105 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106
1107 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001108 if (hostView == null) {
1109 // Perform actual inflation because we're live
1110 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1111 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1112 } else {
1113 // The AppWidgetHostView has already been inflated and instantiated
1114 launcherInfo.hostView = hostView;
1115 }
Romain Guycbb89e42009-06-08 15:52:54 -07001116
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001118 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohena897f392012-04-27 18:12:05 -07001119 AppWidgetResizeFrame.updateWidgetSizeRanges(launcherInfo.hostView,
1120 this, launcherInfo.spanX, launcherInfo.spanY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001121 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001122 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001123
1124 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001126 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 }
Romain Guycbb89e42009-06-08 15:52:54 -07001128
Adam Cohended9f8d2010-11-03 13:25:16 -07001129 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1130 @Override
1131 public void onReceive(Context context, Intent intent) {
1132 final String action = intent.getAction();
1133 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1134 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001135 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001136 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001137
Winson Chungc100e8e2011-08-09 16:02:43 -07001138 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001139 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001140 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1141 mAppsCustomizeTabHost.reset();
1142 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001143 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001144 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1145 mUserPresent = true;
1146 updateRunning();
1147 }
1148 }
1149 };
1150
1151 @Override
1152 public void onAttachedToWindow() {
1153 super.onAttachedToWindow();
1154
1155 // Listen for broadcasts related to user-presence
1156 final IntentFilter filter = new IntentFilter();
1157 filter.addAction(Intent.ACTION_SCREEN_OFF);
1158 filter.addAction(Intent.ACTION_USER_PRESENT);
1159 registerReceiver(mReceiver, filter);
1160
Adam Cohend113e0c2010-11-11 10:48:05 -08001161 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001162 mVisible = true;
1163 }
1164
1165 @Override
1166 public void onDetachedFromWindow() {
1167 super.onDetachedFromWindow();
1168 mVisible = false;
1169
Adam Cohend113e0c2010-11-11 10:48:05 -08001170 if (mAttached) {
1171 unregisterReceiver(mReceiver);
1172 mAttached = false;
1173 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001174 updateRunning();
1175 }
1176
1177 public void onWindowVisibilityChanged(int visibility) {
1178 mVisible = visibility == View.VISIBLE;
1179 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001180 // The following code used to be in onResume, but it turns out onResume is called when
1181 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1182 // is a more appropriate event to handle
1183 if (mVisible) {
1184 mAppsCustomizeTabHost.onWindowVisible();
1185 if (!mWorkspaceLoading) {
1186 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001187 // We want to let Launcher draw itself at least once before we force it to build
1188 // layers on all the workspace pages, so that transitioning to Launcher from other
1189 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1190 // a true draw so we wait until the second preDraw call to be safe
1191 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001192 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001193 // We delay the layer building a bit in order to give
1194 // other message processing a time to run. In particular
1195 // this avoids a delay in hiding the IME if it was
1196 // currently shown, because doing that may involve
1197 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001198 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001199
1200 // We had to enable the wallpaper visibility when launching apps from all
1201 // apps (so that the transitions would be the same as when launching from
1202 // workspace) so take this time to see if we need to re-disable the
1203 // wallpaper visibility to ensure performance.
1204 mWorkspace.post(new Runnable() {
1205 @Override
1206 public void run() {
Winson Chung18f41f82012-05-09 13:28:10 -07001207 disableWallpaperIfInAllApps();
Winson Chung31ce0732012-05-06 13:36:43 -07001208 }
1209 });
1210
Michael Jurka6ee21d22012-02-21 18:20:27 -08001211 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001212 return true;
1213 }
1214 });
1215 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001216 // When Launcher comes back to foreground, a different Activity might be responsible for
1217 // the app market intent, so refresh the icon
1218 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001219 clearTypedText();
1220 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001221 }
1222
1223 private void sendAdvanceMessage(long delay) {
1224 mHandler.removeMessages(ADVANCE_MSG);
1225 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1226 mHandler.sendMessageDelayed(msg, delay);
1227 mAutoAdvanceSentTime = System.currentTimeMillis();
1228 }
1229
1230 private void updateRunning() {
1231 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1232 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1233 mAutoAdvanceRunning = autoAdvanceRunning;
1234 if (autoAdvanceRunning) {
1235 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1236 sendAdvanceMessage(delay);
1237 } else {
1238 if (!mWidgetsToAdvance.isEmpty()) {
1239 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1240 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1241 }
1242 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001243 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001244 }
1245 }
1246 }
1247
1248 private final Handler mHandler = new Handler() {
1249 @Override
1250 public void handleMessage(Message msg) {
1251 if (msg.what == ADVANCE_MSG) {
1252 int i = 0;
1253 for (View key: mWidgetsToAdvance.keySet()) {
1254 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1255 final int delay = mAdvanceStagger * i;
1256 if (v instanceof Advanceable) {
1257 postDelayed(new Runnable() {
1258 public void run() {
1259 ((Advanceable) v).advance();
1260 }
1261 }, delay);
1262 }
1263 i++;
1264 }
1265 sendAdvanceMessage(mAdvanceInterval);
1266 }
1267 }
1268 };
1269
1270 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001271 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001272 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1273 if (v instanceof Advanceable) {
1274 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001275 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001276 updateRunning();
1277 }
1278 }
1279
1280 void removeWidgetToAutoAdvance(View hostView) {
1281 if (mWidgetsToAdvance.containsKey(hostView)) {
1282 mWidgetsToAdvance.remove(hostView);
1283 updateRunning();
1284 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001285 }
1286
Joe Onorato9c1289c2009-08-17 11:03:03 -04001287 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001288 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001289 launcherInfo.hostView = null;
1290 }
1291
Winson Chung93eef082012-03-23 15:59:27 -07001292 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1293 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1294 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001295 }
1296
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001297 public LauncherAppWidgetHost getAppWidgetHost() {
1298 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 }
Romain Guycbb89e42009-06-08 15:52:54 -07001300
Winson Chunga9abd0e2010-10-27 17:18:37 -07001301 public LauncherModel getModel() {
1302 return mModel;
1303 }
1304
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001305 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001306 getWindow().closeAllPanels();
1307
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001308 // Whatever we were doing is hereby canceled.
1309 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001310 }
1311
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 @Override
1313 protected void onNewIntent(Intent intent) {
1314 super.onNewIntent(intent);
1315
1316 // Close the menu
1317 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001318 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001319 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001320
Joe Onorato14f122b2009-11-19 14:06:36 -08001321 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1322 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001323
Adam Cohenac56cff2011-09-28 20:45:37 -07001324 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001325 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001326 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001327 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1328 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001329 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001330 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001331
1332 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001333 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001334 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001335
Joe Onorato3a8820b2009-11-10 15:06:42 -08001336 final View v = getWindow().peekDecorView();
1337 if (v != null && v.getWindowToken() != null) {
1338 InputMethodManager imm = (InputMethodManager)getSystemService(
1339 INPUT_METHOD_SERVICE);
1340 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001342
Michael Jurka35aa14d2011-07-07 17:01:08 -07001343 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001344 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001345 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001346 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 }
1348 }
1349
1350 @Override
1351 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1352 // Do not call super here
1353 mSavedInstanceState = savedInstanceState;
1354 }
1355
1356 @Override
1357 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001358 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001359 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360
Michael Jurka883f55b2010-10-21 15:47:14 -07001361 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001362 // We close any open folder since it will not be re-opened, and we need to make sure
1363 // this state is reflected.
1364 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365
Winson Chung3d503fb2011-07-13 17:25:49 -07001366 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1367 mWaitingForResult) {
1368 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1369 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1370 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1371 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 }
1373
1374 if (mFolderInfo != null && mWaitingForResult) {
1375 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1376 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1377 }
Michael Jurka946ad472010-07-09 18:05:18 -07001378
Winson Chung785d2eb2011-04-14 16:08:02 -07001379 // Save the current AppsCustomize tab
1380 if (mAppsCustomizeTabHost != null) {
1381 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1382 if (currentTabTag != null) {
1383 outState.putString("apps_customize_currentTab", currentTabTag);
1384 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001385 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1386 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001387 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 }
1389
1390 @Override
1391 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001393
Winson Chunge7a03942011-08-05 15:05:12 -07001394 // Remove all pending runnables
1395 mHandler.removeMessages(ADVANCE_MSG);
1396 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001397 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001398
Winson Chungcd2b0142011-06-08 16:02:26 -07001399 // Stop callbacks from LauncherModel
1400 LauncherApplication app = ((LauncherApplication) getApplication());
1401 mModel.stopLoader();
1402 app.setLauncher(null);
1403
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001405 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001407 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001409 mAppWidgetHost = null;
1410
1411 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412
1413 TextKeyListener.getInstance().release();
1414
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415
Winson Chung3d503fb2011-07-13 17:25:49 -07001416 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001417
1418 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001419 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001420
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001421 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001422 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1423 mWorkspace.removeAllViews();
1424 mWorkspace = null;
1425 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001426
1427 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 }
1429
Adam Cohena9cf38f2011-05-02 15:36:58 -07001430 public DragController getDragController() {
1431 return mDragController;
1432 }
1433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 @Override
1435 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001436 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 super.startActivityForResult(intent, requestCode);
1438 }
1439
Winson Chung70d72102011-08-12 11:24:35 -07001440 /**
1441 * Indicates that we want global search for this activity by setting the globalSearch
1442 * argument for {@link #startSearch} to true.
1443 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001445 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001447
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001448 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001449
Karl Rosaen138a0412009-04-23 19:00:21 -07001450 if (initialQuery == null) {
1451 // Use any text typed in the launcher as the initial query
1452 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001453 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 if (appSearchData == null) {
1455 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001456 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001458 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001459
Karl Rosaen138a0412009-04-23 19:00:21 -07001460 final SearchManager searchManager =
1461 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001462 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001463 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 }
1465
Winson Chung70d72102011-08-12 11:24:35 -07001466 @Override
1467 public boolean onCreateOptionsMenu(Menu menu) {
1468 if (isWorkspaceLocked()) {
1469 return false;
1470 }
1471
1472 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001473
1474 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1475 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1476 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001477 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1478 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1479 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001480 String helpUrl = getString(R.string.help_url);
1481 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001482 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1483 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1484
Winson Chung70d72102011-08-12 11:24:35 -07001485 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1486 .setIcon(android.R.drawable.ic_menu_gallery)
1487 .setAlphabeticShortcut('W');
1488 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1489 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001490 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001491 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001492 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1493 .setIcon(android.R.drawable.ic_menu_preferences)
1494 .setIntent(settings)
1495 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001496 if (!helpUrl.isEmpty()) {
1497 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1498 .setIcon(android.R.drawable.ic_menu_help)
1499 .setIntent(help)
1500 .setAlphabeticShortcut('H');
1501 }
Winson Chung70d72102011-08-12 11:24:35 -07001502 return true;
1503 }
1504
1505 @Override
1506 public boolean onPrepareOptionsMenu(Menu menu) {
1507 super.onPrepareOptionsMenu(menu);
1508
1509 if (mAppsCustomizeTabHost.isTransitioning()) {
1510 return false;
1511 }
1512 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1513 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1514
1515 return true;
1516 }
1517
1518 @Override
1519 public boolean onOptionsItemSelected(MenuItem item) {
1520 switch (item.getItemId()) {
1521 case MENU_WALLPAPER_SETTINGS:
1522 startWallpaper();
1523 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001524 }
1525
1526 return super.onOptionsItemSelected(item);
1527 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001529 @Override
1530 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001531 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001532 // Use a custom animation for launching search
1533 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001534 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001535 }
1536
Joe Onorato9c1289c2009-08-17 11:03:03 -04001537 public boolean isWorkspaceLocked() {
1538 return mWorkspaceLoading || mWaitingForResult;
1539 }
1540
Michael Jurka0280c3b2010-09-17 15:00:07 -07001541 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001542 mPendingAddInfo.container = ItemInfo.NO_ID;
1543 mPendingAddInfo.screen = -1;
1544 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1545 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001546 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001547 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001548 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001549
Michael Jurkaaf442092010-06-10 17:01:57 -07001550 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001551 // TODO: catch bad widget exception when sent
1552 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001553 // TODO: Is this log message meaningful?
1554 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001555 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001556 }
1557
Adam Cohened66b2b2012-01-23 17:28:51 -08001558 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1559 final AppWidgetProviderInfo appWidget = info.info;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001560 if (appWidget.configure != null) {
1561 // Launch over to configure widget, if needed
1562 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1563 intent.setComponent(appWidget.configure);
1564 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001565 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001566 mWidgetBeingBoundOrConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001568 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001569 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001570 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001571 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 }
1573 }
Romain Guycbb89e42009-06-08 15:52:54 -07001574
Adam Cohenfbba09b2011-07-18 21:43:05 -07001575 /**
1576 * Process a shortcut drop.
1577 *
1578 * @param componentName The name of the component
1579 * @param screen The screen where it should be added
1580 * @param cell The cell it should be added to, optional
1581 * @param position The location on the screen where it was dropped, optional
1582 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001583 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1584 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001585 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001586 mPendingAddInfo.container = container;
1587 mPendingAddInfo.screen = screen;
1588 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001589
1590 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001591 mPendingAddInfo.cellX = cell[0];
1592 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001593 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001594
1595 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1596 createShortcutIntent.setComponent(componentName);
1597 processShortcut(createShortcutIntent);
1598 }
1599
Adam Cohenfbba09b2011-07-18 21:43:05 -07001600 /**
1601 * Process a widget drop.
1602 *
1603 * @param info The PendingAppWidgetInfo of the widget being added.
1604 * @param screen The screen where it should be added
1605 * @param cell The cell it should be added to, optional
1606 * @param position The location on the screen where it was dropped, optional
1607 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001608 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001609 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001610 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001611 mPendingAddInfo.container = info.container = container;
1612 mPendingAddInfo.screen = info.screen = screen;
1613 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001614 mPendingAddInfo.minSpanX = info.minSpanX;
1615 mPendingAddInfo.minSpanY = info.minSpanY;
1616
Adam Cohenfbba09b2011-07-18 21:43:05 -07001617 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001618 mPendingAddInfo.cellX = cell[0];
1619 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001620 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001621 if (span != null) {
1622 mPendingAddInfo.spanX = span[0];
1623 mPendingAddInfo.spanY = span[1];
1624 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001625
Adam Cohened66b2b2012-01-23 17:28:51 -08001626 AppWidgetHostView hostView = info.boundWidget;
1627 int appWidgetId;
1628 if (hostView != null) {
1629 appWidgetId = hostView.getAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001630 addAppWidgetImpl(appWidgetId, info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001631 } else {
1632 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001633 mWidgetBeingBoundOrConfigured = info;
1634 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
1635 addAppWidgetImpl(appWidgetId, info);
1636 } else {
1637 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1638 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1639 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1640 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1641 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001642 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001643 }
1644
Joe Onoratodeb98af2010-02-19 14:59:39 -08001645 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001646 // Handle case where user selected "Applications"
1647 String applicationName = getResources().getString(R.string.group_applications);
1648 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001649
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001650 if (applicationName != null && applicationName.equals(shortcutName)) {
1651 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1652 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001653
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001654 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1655 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001656 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001657 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001658 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001659 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001660 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 }
1662
Winson Chung24ab2f12010-09-16 14:10:47 -07001663 void processWallpaper(Intent intent) {
1664 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1665 }
1666
Winson Chung3d503fb2011-07-13 17:25:49 -07001667 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1668 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001669 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 folderInfo.title = getText(R.string.folder_name);
1671
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001673 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1674 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001675 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676
1677 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001678 FolderIcon newFolder =
1679 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1680 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1681 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001682 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 }
Romain Guycbb89e42009-06-08 15:52:54 -07001684
Joe Onorato9c1289c2009-08-17 11:03:03 -04001685 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001686 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001687 }
1688
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001690 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001692 Intent chooser = Intent.createChooser(pickWallpaper,
1693 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001694 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1695 // Removed in Eclair MR1
1696// WallpaperManager wm = (WallpaperManager)
1697// getSystemService(Context.WALLPAPER_SERVICE);
1698// WallpaperInfo wi = wm.getWallpaperInfo();
1699// if (wi != null && wi.getSettingsActivity() != null) {
1700// LabeledIntent li = new LabeledIntent(getPackageName(),
1701// R.string.configure_wallpaper, 0);
1702// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1703// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1704// }
Mike Clerona0618e42009-10-22 13:55:21 -07001705 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 }
1707
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001708 /**
1709 * Registers various content observers. The current implementation registers
1710 * only a favorites observer to keep track of the favorites applications.
1711 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001712 private void registerContentObservers() {
1713 ContentResolver resolver = getContentResolver();
1714 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1715 true, mWidgetObserver);
1716 }
1717
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 @Override
1719 public boolean dispatchKeyEvent(KeyEvent event) {
1720 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1721 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001723 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001724 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001725 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001726 dumpState();
1727 return true;
1728 }
1729 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001730 }
1731 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1732 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001733 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 return true;
1735 }
1736 }
1737
1738 return super.dispatchKeyEvent(event);
1739 }
1740
Joe Onorato88ec0992009-11-19 13:16:06 -08001741 @Override
1742 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001743 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001744 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001745 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001746 Folder openFolder = mWorkspace.getOpenFolder();
1747 if (openFolder.isEditingName()) {
1748 openFolder.dismissEditingName();
1749 } else {
1750 closeFolder();
1751 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001752 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001753 mWorkspace.exitWidgetResizeMode();
1754
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001755 // Back button is a no-op here, but give at least some feedback for the button press
1756 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001757 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001758 }
1759
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001761 * Re-listen when widgets are reset.
1762 */
1763 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001764 if (mAppWidgetHost != null) {
1765 mAppWidgetHost.startListening();
1766 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001767 }
1768
1769 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001770 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1771 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001773 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001774 if (mModel != null) {
1775 mModel.unbindWorkspaceItems();
1776 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001778
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 /**
1780 * Launches the intent referred by the clicked shortcut.
1781 *
1782 * @param v The view representing the clicked shortcut.
1783 */
1784 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001785 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1786 // view has detached (it's possible for this to happen if the view is removed mid touch).
1787 if (v.getWindowToken() == null) {
1788 return;
1789 }
1790
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001791 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001792 return;
1793 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001794
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001796 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001798 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001799 int[] pos = new int[2];
1800 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001801 intent.setSourceBounds(new Rect(pos[0], pos[1],
1802 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001803
1804 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001805
1806 if (success && v instanceof BubbleTextView) {
1807 mWaitingForResume = (BubbleTextView) v;
1808 mWaitingForResume.setStayPressed(true);
1809 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001811 if (v instanceof FolderIcon) {
1812 FolderIcon fi = (FolderIcon) v;
1813 handleFolderClick(fi);
1814 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001815 } else if (v == mAllAppsButton) {
1816 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001817 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001818 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001819 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001820 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 }
1822 }
1823
Michael Jurka0e260592010-06-30 17:07:39 -07001824 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001825 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001826 // clicking anywhere on the workspace causes the customization drawer to slide down
1827 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001828 return false;
1829 }
1830
Michael Jurkaaf442092010-06-10 17:01:57 -07001831 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001832 * Event handler for the search button
1833 *
1834 * @param v The view that was clicked.
1835 */
1836 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001837 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1838
Amith Yamasania135ba82011-08-09 17:42:01 -07001839 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001840 }
1841
1842 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001843 * Event handler for the voice button
1844 *
1845 * @param v The view that was clicked.
1846 */
1847 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001848 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001849
Michael Jurkaae65ee32012-04-30 11:45:54 -07001850 try {
1851 final SearchManager searchManager =
1852 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1853 ComponentName activityName = searchManager.getGlobalSearchActivity();
1854 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1855 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1856 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1857 if (activityName != null) {
1858 intent.setPackage(activityName.getPackageName());
1859 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001860 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07001861 } catch (ActivityNotFoundException e) {
1862 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1863 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1864 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1865 startActivitySafely(null, intent, "onClickVoiceButton");
1866 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001867 }
1868
1869 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001870 * Event handler for the "grid" button that appears on the home screen, which
1871 * enters all apps mode.
1872 *
1873 * @param v The view that was clicked.
1874 */
1875 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001876 showAllApps(true);
1877 }
1878
1879 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001880 // Provide the same haptic feedback that the system offers for virtual keys.
1881 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001882 }
1883
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001884 public void onClickAppMarketButton(View v) {
1885 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001886 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001887 } else {
1888 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001889 }
1890 }
1891
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001892 void startApplicationDetailsActivity(ComponentName componentName) {
1893 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001894 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1895 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001896 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001897 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001898 }
1899
Patrick Dubroy5539af72010-09-07 15:22:01 -07001900 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1901 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1902 // System applications cannot be installed. For now, show a toast explaining that.
1903 // We may give them the option of disabling apps this way.
1904 int messageId = R.string.uninstall_system_app_text;
1905 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1906 } else {
1907 String packageName = appInfo.componentName.getPackageName();
1908 String className = appInfo.componentName.getClassName();
1909 Intent intent = new Intent(
1910 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001911 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1912 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001913 startActivity(intent);
1914 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001915 }
1916
Michael Jurka86a720e2012-05-09 11:23:23 -07001917 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001919
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 try {
Winson Chung2672ff92012-05-04 16:22:30 -07001921 // Only launch using the new animation if the shortcut has not opted out (this is a
1922 // private contract between launcher and may be ignored in the future).
1923 boolean useLaunchAnimation = (v != null) &&
1924 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
1925 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07001926 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1927 v.getMeasuredWidth(), v.getMeasuredHeight());
1928
1929 startActivity(intent, opts.toBundle());
1930 } else {
1931 startActivity(intent);
1932 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001933 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 } catch (SecurityException e) {
1935 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001936 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001938 "or use the exported attribute for this activity. "
1939 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001941 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001943
Michael Jurka86a720e2012-05-09 11:23:23 -07001944 boolean startActivitySafely(View v, Intent intent, Object tag) {
1945 boolean success = false;
1946 try {
1947 success = startActivity(v, intent, tag);
1948 } catch (ActivityNotFoundException e) {
1949 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1950 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
1951 }
1952 return success;
1953 }
1954
Romain Guy8e633c52010-03-04 12:51:36 -08001955 void startActivityForResultSafely(Intent intent, int requestCode) {
1956 try {
1957 startActivityForResult(intent, requestCode);
1958 } catch (ActivityNotFoundException e) {
1959 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1960 } catch (SecurityException e) {
1961 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1962 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1963 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1964 "or use the exported attribute for this activity.", e);
1965 }
1966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967
Adam Cohena9cf38f2011-05-02 15:36:58 -07001968 private void handleFolderClick(FolderIcon folderIcon) {
1969 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001970 Folder openFolder = mWorkspace.getFolderForTag(info);
1971
1972 // If the folder info reports that the associated folder is open, then verify that
1973 // it is actually opened. There have been a few instances where this gets out of sync.
1974 if (info.opened && openFolder == null) {
1975 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1976 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1977 info.opened = false;
1978 }
1979
Adam Cohena9cf38f2011-05-02 15:36:58 -07001980 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 // Close any open folder
1982 closeFolder();
1983 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001984 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 } else {
1986 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 int folderScreen;
1988 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001989 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 // .. and close it
1991 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001992 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 // Close any folder open on the current screen
1994 closeFolder();
1995 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001996 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 }
1998 }
1999 }
2000 }
2001
Adam Cohen2801caf2011-05-13 20:57:39 -07002002 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002003 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002004 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2005 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2006 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2007
Adam Cohenc51934b2011-07-26 21:07:43 -07002008 FolderInfo info = (FolderInfo) fi.getTag();
2009 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2010 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002011 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2012 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002013 }
2014
Adam Cohen2801caf2011-05-13 20:57:39 -07002015 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2016 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2017 oa.start();
2018 }
2019
2020 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002021 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002022 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2023 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2024 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2025
Adam Cohenc51934b2011-07-26 21:07:43 -07002026 FolderInfo info = (FolderInfo) fi.getTag();
2027 CellLayout cl = null;
2028 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2029 cl = (CellLayout) fi.getParent().getParent();
2030 }
2031
2032 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07002033 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2034 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002035 oa.addListener(new AnimatorListenerAdapter() {
2036 @Override
2037 public void onAnimationEnd(Animator animation) {
2038 if (layout != null) {
2039 layout.clearFolderLeaveBehind();
2040 }
2041 }
2042 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002043 oa.start();
2044 }
2045
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002047 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 * is animated relative to the specified View. If the View is null, no animation
2049 * is played.
2050 *
2051 * @param folderInfo The FolderInfo describing the folder to open.
2052 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002053 public void openFolder(FolderIcon folderIcon) {
2054 Folder folder = folderIcon.mFolder;
2055 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056
Adam Cohen2801caf2011-05-13 20:57:39 -07002057 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002058 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059
Adam Cohen4554ee12011-08-03 16:13:21 -07002060 // Just verify that the folder hasn't already been added to the DragLayer.
2061 // There was a one-off crash where the folder had a parent already.
2062 if (folder.getParent() == null) {
2063 mDragLayer.addView(folder);
2064 mDragController.addDropTarget((DropTarget) folder);
2065 } else {
2066 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2067 folder.getParent() + ").");
2068 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002069 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002070 }
2071
2072 public void closeFolder() {
2073 Folder folder = mWorkspace.getOpenFolder();
2074 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002075 if (folder.isEditingName()) {
2076 folder.dismissEditingName();
2077 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002078 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002079
2080 // Dismiss the folder cling
2081 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002082 }
2083 }
2084
2085 void closeFolder(Folder folder) {
2086 folder.getInfo().opened = false;
2087
2088 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2089 if (parent != null) {
2090 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2091 shrinkAndFadeInFolderIcon(fi);
2092 }
2093 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 }
2095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002097 if (!isDraggingEnabled()) return false;
2098 if (isWorkspaceLocked()) return false;
2099 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100
2101 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002102 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 }
2104
Michael Jurka0280c3b2010-09-17 15:00:07 -07002105 resetAddInfo();
2106 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002108 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 return true;
2110 }
2111
Winson Chung3d503fb2011-07-13 17:25:49 -07002112 // The hotseat touch handling does not go through Workspace, and we always allow long press
2113 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002114 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002115 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2116 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002117 if (itemUnderLongClick == null) {
2118 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002119 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2120 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002121 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002123 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002125 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 }
2127 }
2128 }
2129 return true;
2130 }
2131
Winson Chung3d503fb2011-07-13 17:25:49 -07002132 boolean isHotseatLayout(View layout) {
2133 return mHotseat != null && layout != null &&
2134 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2135 }
2136 Hotseat getHotseat() {
2137 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002138 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002139 SearchDropTargetBar getSearchBar() {
2140 return mSearchDropTargetBar;
2141 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002142
Winson Chung3d503fb2011-07-13 17:25:49 -07002143 /**
2144 * Returns the CellLayout of the specified container at the specified screen.
2145 */
2146 CellLayout getCellLayout(long container, int screen) {
2147 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2148 if (mHotseat != null) {
2149 return mHotseat.getLayout();
2150 } else {
2151 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002152 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002153 } else {
2154 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002155 }
2156 }
2157
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 Workspace getWorkspace() {
2159 return mWorkspace;
2160 }
2161
Daniel Sandler843e8602010-06-07 14:59:01 -04002162 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2163 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002164 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002165 }
2166
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002167 public boolean isAllAppsButtonRank(int rank) {
2168 return mHotseat.isAllAppsButtonRank(rank);
2169 }
2170
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002171 // AllAppsView.Watcher
2172 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002173 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002174 mWorkspace.setVisibility(View.GONE);
2175 }
2176 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002177
2178 /**
2179 * Helper method for the cameraZoomIn/cameraZoomOut animations
2180 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002181 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002182 * @param scaleFactor The scale factor used for the zoom
2183 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002184 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002185 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002186 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002187 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002188
Winson Chung18f41f82012-05-09 13:28:10 -07002189 void disableWallpaperIfInAllApps() {
2190 // Only disable it if we are in all apps
2191 if (mState == State.APPS_CUSTOMIZE) {
2192 if (mAppsCustomizeTabHost != null &&
2193 !mAppsCustomizeTabHost.isTransitioning()) {
2194 updateWallpaperVisibility(false);
2195 }
2196 }
2197 }
2198
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002199 void updateWallpaperVisibility(boolean visible) {
2200 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2201 int curflags = getWindow().getAttributes().flags
2202 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2203 if (wpflags != curflags) {
2204 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2205 }
2206 }
2207
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002208 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2209 if (v instanceof LauncherTransitionable) {
2210 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2211 }
2212 }
2213
Michael Jurkabed61d22012-02-14 22:51:29 -08002214 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2215 if (v instanceof LauncherTransitionable) {
2216 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2217 }
Winson Chung70442722012-02-10 15:43:22 -08002218
2219 // Update the workspace transition step as well
2220 dispatchOnLauncherTransitionStep(v, 0f);
2221 }
2222
2223 private void dispatchOnLauncherTransitionStep(View v, float t) {
2224 if (v instanceof LauncherTransitionable) {
2225 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2226 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002227 }
2228
2229 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2230 if (v instanceof LauncherTransitionable) {
2231 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2232 }
Winson Chung70442722012-02-10 15:43:22 -08002233
2234 // Update the workspace transition step as well
2235 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002236 }
2237
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002238 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002239 * Things to test when changing the following seven functions.
2240 * - Home from workspace
2241 * - from center screen
2242 * - from other screens
2243 * - Home from all apps
2244 * - from center screen
2245 * - from other screens
2246 * - Back from all apps
2247 * - from center screen
2248 * - from other screens
2249 * - Launch app from workspace and quit
2250 * - with back
2251 * - with home
2252 * - Launch app from all apps and quit
2253 * - with back
2254 * - with home
2255 * - Go to a screen that's not the default, then all
2256 * apps, and launch and app, and go back
2257 * - with back
2258 * -with home
2259 * - On workspace, long press power and go back
2260 * - with back
2261 * - with home
2262 * - On all apps, long press power and go back
2263 * - with back
2264 * - with home
2265 * - On workspace, power off
2266 * - On all apps, power off
2267 * - Launch an app and turn off the screen while in that app
2268 * - Go back with home key
2269 * - Go back with back key TODO: make this not go to workspace
2270 * - From all apps
2271 * - From workspace
2272 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2273 * - From all apps
2274 * - From the center workspace
2275 * - From another workspace
2276 */
2277
2278 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002279 * Zoom the camera out from the workspace to reveal 'toView'.
2280 * Assumes that the view to show is anchored at either the very top or very bottom
2281 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002282 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002283 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002284 if (mStateAnimation != null) {
2285 mStateAnimation.cancel();
2286 mStateAnimation = null;
2287 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002288 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002289
Winson Chungf0ea4d32011-06-06 14:27:16 -07002290 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2291 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2292 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002293 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002294 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002295 final int startDelay =
2296 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002297
Michael Jurkab3e22d92011-10-31 15:58:33 -07002298 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002299
Michael Jurkad74c9842011-07-10 12:44:21 -07002300 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002301 Animator workspaceAnim =
2302 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002303
2304 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002305 toView.setScaleX(scale);
2306 toView.setScaleY(scale);
2307 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2308 scaleAnim.
2309 scaleX(1f).scaleY(1f).
2310 setDuration(duration).
2311 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002312
Winson Chungf0ea4d32011-06-06 14:27:16 -07002313 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002314 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002315 final ObjectAnimator alphaAnim = ObjectAnimator
2316 .ofFloat(toView, "alpha", 0f, 1f)
2317 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002318 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002319 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2320 @Override
2321 public void onAnimationUpdate(ValueAnimator animation) {
2322 float t = (Float) animation.getAnimatedValue();
2323 dispatchOnLauncherTransitionStep(fromView, t);
2324 dispatchOnLauncherTransitionStep(toView, t);
2325 }
2326 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002327
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002328 // toView should appear right at the end of the workspace shrink
2329 // animation
2330 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002331 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002332 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002333
2334 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002335 boolean animationCancelled = false;
2336
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002337 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002338 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002339 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002340 // Prepare the position
2341 toView.setTranslationX(0.0f);
2342 toView.setTranslationY(0.0f);
2343 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002344 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002345 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002346 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002347 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002348 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2349 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002350
2351 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2352 // Hide the workspace scrollbar
2353 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002354 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002355 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002356 if (!animationCancelled) {
2357 updateWallpaperVisibility(false);
2358 }
2359 }
2360
Adam Cohencff6af82011-09-13 14:51:53 -07002361 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002362 public void onAnimationCancel(Animator animation) {
2363 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002364 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002365 });
2366
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002367 if (workspaceAnim != null) {
2368 mStateAnimation.play(workspaceAnim);
2369 }
Michael Jurka1899a362011-11-03 13:50:45 -07002370
2371 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002372 final ViewTreeObserver observer;
2373
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002374 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2375 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002376
2377 // If any of the objects being animated haven't been measured/laid out
2378 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002379 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002380 (mWorkspace.getMeasuredWidth() == 0) ||
2381 (toView.getMeasuredWidth() == 0)) {
2382 observer = mWorkspace.getViewTreeObserver();
2383 delayAnim = true;
2384 } else {
2385 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002386 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002387
2388 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002389 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002390 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2391 public void onGlobalLayout() {
2392 mWorkspace.post(new Runnable() {
2393 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002394 // Check that mStateAnimation hasn't changed while
2395 // we waited for a layout pass
2396 if (mStateAnimation == stateAnimation) {
2397 // Need to update pivots for zoom if layout changed
2398 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002399 dispatchOnLauncherTransitionStart(fromView, animated, false);
2400 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka49779a12012-01-16 04:10:08 -08002401 mStateAnimation.start();
2402 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002403 }
2404 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002405 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002406 }
2407 };
2408 observer.addOnGlobalLayoutListener(delayedStart);
2409 } else {
2410 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002411 dispatchOnLauncherTransitionStart(fromView, animated, false);
2412 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka1899a362011-11-03 13:50:45 -07002413 mStateAnimation.start();
2414 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002415 } else {
2416 toView.setTranslationX(0.0f);
2417 toView.setTranslationY(0.0f);
2418 toView.setScaleX(1.0f);
2419 toView.setScaleY(1.0f);
2420 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002421 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002422
Michael Jurkabed61d22012-02-14 22:51:29 -08002423 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2424 // Hide the workspace scrollbar
2425 mWorkspace.hideScrollingIndicator(true);
2426 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002427 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002428 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002429 dispatchOnLauncherTransitionStart(fromView, animated, false);
2430 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002431 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002432 dispatchOnLauncherTransitionStart(toView, animated, false);
2433 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002434 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002435 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002436 }
2437
2438 /**
2439 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002440 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002441 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002442 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002443 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2444 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002445
Michael Jurkab3e22d92011-10-31 15:58:33 -07002446 if (mStateAnimation != null) {
2447 mStateAnimation.cancel();
2448 mStateAnimation = null;
2449 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002450 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002451
Winson Chungf0ea4d32011-06-06 14:27:16 -07002452 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002453 final int fadeOutDuration =
2454 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002455 final float scaleFactor = (float)
2456 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2457 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002458 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002459 Animator workspaceAnim = null;
2460
2461 if (toState == State.WORKSPACE) {
2462 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2463 workspaceAnim = mWorkspace.getChangeStateAnimation(
2464 Workspace.State.NORMAL, animated, stagger);
2465 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2466 workspaceAnim = mWorkspace.getChangeStateAnimation(
2467 Workspace.State.SPRING_LOADED, animated);
2468 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002469
Michael Jurkab3e22d92011-10-31 15:58:33 -07002470 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002471 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002472 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002473 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002474 final LauncherViewPropertyAnimator scaleAnim =
2475 new LauncherViewPropertyAnimator(fromView);
2476 scaleAnim.
2477 scaleX(scaleFactor).scaleY(scaleFactor).
2478 setDuration(duration).
2479 setInterpolator(new Workspace.ZoomInInterpolator());
2480
2481 final ObjectAnimator alphaAnim = ObjectAnimator
2482 .ofFloat(fromView, "alpha", 1f, 0f)
2483 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002484 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002485 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2486 @Override
2487 public void onAnimationUpdate(ValueAnimator animation) {
2488 float t = 1f - (Float) animation.getAnimatedValue();
2489 dispatchOnLauncherTransitionStep(fromView, t);
2490 dispatchOnLauncherTransitionStep(toView, t);
2491 }
2492 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002493
Michael Jurkabed61d22012-02-14 22:51:29 -08002494 mStateAnimation = new AnimatorSet();
2495
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002496 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2497 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002498
2499 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002500 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002501 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002502 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002503 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002504 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2505 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002506 if (mWorkspace != null) {
2507 mWorkspace.hideScrollingIndicator(false);
2508 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002509 if (onCompleteRunnable != null) {
2510 onCompleteRunnable.run();
2511 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002512 }
2513 });
2514
Winson Chungf0ea4d32011-06-06 14:27:16 -07002515 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002516 if (workspaceAnim != null) {
2517 mStateAnimation.play(workspaceAnim);
2518 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002519 dispatchOnLauncherTransitionStart(fromView, animated, true);
2520 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002521 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002522 } else {
2523 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002524 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002525 dispatchOnLauncherTransitionStart(fromView, animated, true);
2526 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002527 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002528 dispatchOnLauncherTransitionStart(toView, animated, true);
2529 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002530 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002531 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002532 }
2533
Michael Jurkae326f182011-11-21 14:05:46 -08002534 @Override
2535 public void onTrimMemory(int level) {
2536 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002537 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002538 mAppsCustomizeTabHost.onTrimMemory();
2539 }
2540 }
2541
Winson Chung18f41f82012-05-09 13:28:10 -07002542 @Override
2543 public void onWindowFocusChanged(boolean hasFocus) {
2544 if (!hasFocus) {
2545 // When another window occludes launcher (like the notification shade, or recents),
2546 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2547 updateWallpaperVisibility(true);
2548 } else {
2549 // When launcher has focus again, disable the wallpaper if we are in AllApps
2550 disableWallpaperIfInAllApps();
2551 }
2552 }
2553
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002554 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002555 showWorkspace(animated, null);
2556 }
2557
2558 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002559 if (mState != State.WORKSPACE) {
2560 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002561 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002562
2563 // Show the search bar and hotseat
2564 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002565 // We only need to animate in the dock divider if we're going from spring loaded mode
2566 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002567
2568 // Set focus to the AppsCustomize button
2569 if (mAllAppsButton != null) {
2570 mAllAppsButton.requestFocus();
2571 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002572 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002573
Michael Jurkab737ee62011-11-15 15:57:22 -08002574 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002575
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002576 // Change the state *after* we've called all the transition code
2577 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002578
Winson Chung5fb63472011-02-02 17:03:37 -08002579 // Resume the auto-advance of widgets
2580 mUserPresent = true;
2581 updateRunning();
2582
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002583 // send an accessibility event to announce the context change
2584 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002585 }
2586
Michael Jurkab3e22d92011-10-31 15:58:33 -07002587 void showAllApps(boolean animated) {
2588 if (mState != State.WORKSPACE) return;
2589
2590 showAppsCustomizeHelper(animated, false);
2591 mAppsCustomizeTabHost.requestFocus();
2592
2593 // Hide the search bar and hotseat
2594 mSearchDropTargetBar.hideSearchBar(animated);
2595
2596 // Change the state *after* we've called all the transition code
2597 mState = State.APPS_CUSTOMIZE;
2598
2599 // Pause the auto-advance of widgets until we are out of AllApps
2600 mUserPresent = false;
2601 updateRunning();
2602 closeFolder();
2603
2604 // Send an accessibility event to announce the context change
2605 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2606 }
2607
Adam Cohen7777d962011-08-18 18:58:38 -07002608 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002609 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002610 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002611 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002612 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002613 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002614 }
Adam Cohen7777d962011-08-18 18:58:38 -07002615
Adam Cohened66b2b2012-01-23 17:28:51 -08002616 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2617 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002618 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2619
Winson Chunge7a03942011-08-05 15:05:12 -07002620 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002621 @Override
2622 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002623 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002624 // Before we show workspace, hide all apps again because
2625 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2626 // clean up our state transition functions
2627 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002628 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002629 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002630 } else {
2631 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002632 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002633 }
2634 }, (extendedDelay ?
2635 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2636 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2637 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002638
Michael Jurkad3ef3062010-11-23 16:23:58 -08002639 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002640 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002641 final boolean animated = true;
2642 final boolean springLoaded = true;
2643 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002644 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002645 }
2646 // Otherwise, we are not in spring loaded mode, so don't do anything.
2647 }
2648
Michael Jurkab737ee62011-11-15 15:57:22 -08002649 void hideDockDivider() {
2650 if (mQsbDivider != null && mDockDivider != null) {
2651 mQsbDivider.setVisibility(View.INVISIBLE);
2652 mDockDivider.setVisibility(View.INVISIBLE);
2653 }
2654 }
2655
2656 void showDockDivider(boolean animated) {
2657 if (mQsbDivider != null && mDockDivider != null) {
2658 mQsbDivider.setVisibility(View.VISIBLE);
2659 mDockDivider.setVisibility(View.VISIBLE);
2660 if (mDividerAnimator != null) {
2661 mDividerAnimator.cancel();
2662 mQsbDivider.setAlpha(1f);
2663 mDockDivider.setAlpha(1f);
2664 mDividerAnimator = null;
2665 }
2666 if (animated) {
2667 mDividerAnimator = new AnimatorSet();
2668 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2669 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2670 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2671 mDividerAnimator.start();
2672 }
2673 }
2674 }
2675
Michael Jurkab3e22d92011-10-31 15:58:33 -07002676 void lockAllApps() {
2677 // TODO
2678 }
2679
2680 void unlockAllApps() {
2681 // TODO
2682 }
2683
Adam Cohenfc53cd22011-07-20 15:45:11 -07002684 public boolean isAllAppsCustomizeOpen() {
2685 return mState == State.APPS_CUSTOMIZE;
2686 }
2687
Winson Chungf0ea4d32011-06-06 14:27:16 -07002688 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002689 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002690 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002691 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002692 if (!LauncherApplication.isScreenLarge()) {
2693 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002694 if (mHotseat.getAlpha() != 1f) {
2695 int duration = mSearchDropTargetBar.getTransitionInDuration();
2696 mHotseat.animate().alpha(1f).setDuration(duration);
2697 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002698 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002699 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002700 }
2701 }
2702 }
2703
2704 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002705 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002706 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002707 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002708 if (!LauncherApplication.isScreenLarge()) {
2709 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002710 if (mHotseat.getAlpha() != 0f) {
2711 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2712 mHotseat.animate().alpha(0f).setDuration(duration);
2713 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002714 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002715 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002716 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002717 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002718 }
2719
Patrick Dubroy5f445422011-02-18 14:35:21 -08002720 /**
2721 * Add an item from all apps or customize onto the given workspace screen.
2722 * If layout is null, add to the current screen.
2723 */
2724 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002725 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002726 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002727 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002728 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002729
Winson Chungdff8ebb2011-09-08 17:25:31 -07002730 /** Maps the current orientation to an index for referencing orientation correct global icons */
2731 private int getCurrentOrientationIndexForGlobalIcons() {
2732 // default - 0, landscape - 1
2733 switch (getResources().getConfiguration().orientation) {
2734 case Configuration.ORIENTATION_LANDSCAPE:
2735 return 1;
2736 default:
2737 return 0;
2738 }
2739 }
2740
Michael Jurkaae65ee32012-04-30 11:45:54 -07002741 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002742 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002743 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002744 // Look for the toolbar icon specified in the activity meta-data
2745 Bundle metaData = packageManager.getActivityInfo(
2746 activityName, PackageManager.GET_META_DATA).metaData;
2747 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002748 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002749 if (iconResId != 0) {
2750 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002751 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002752 }
2753 }
2754 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002755 // This can happen if the activity defines an invalid drawable
2756 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2757 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002758 } catch (Resources.NotFoundException nfe) {
2759 // This can happen if the activity defines an invalid drawable
2760 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2761 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002762 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002763 return null;
2764 }
2765
2766 // if successful in getting icon, return it; otherwise, set button to use default drawable
2767 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002768 int buttonId, ComponentName activityName, int fallbackDrawableId,
2769 String toolbarResourceName) {
2770 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002771 Resources r = getResources();
2772 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2773 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002774
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002775 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002776 // If we were unable to find the icon via the meta-data, use a generic one
2777 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002778 toolbarIcon = r.getDrawable(fallbackDrawableId);
2779 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002780 if (button != null) {
2781 button.setCompoundDrawables(toolbarIcon, null, null, null);
2782 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002783 return null;
2784 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002785 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002786 if (button != null) {
2787 button.setCompoundDrawables(toolbarIcon, null, null, null);
2788 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002789 return toolbarIcon.getConstantState();
2790 }
2791 }
2792
2793 // if successful in getting icon, return it; otherwise, set button to use default drawable
2794 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002795 int buttonId, ComponentName activityName, int fallbackDrawableId,
2796 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002797 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002798 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002799
Michael Jurka19e0fc52011-07-22 18:00:21 -07002800 if (button != null) {
2801 // If we were unable to find the icon via the meta-data, use a
2802 // generic one
2803 if (toolbarIcon == null) {
2804 button.setImageResource(fallbackDrawableId);
2805 } else {
2806 button.setImageDrawable(toolbarIcon);
2807 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002808 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002809
2810 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2811
Michael Jurka0423dcf2010-10-05 14:56:18 -07002812 }
2813
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002814 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2815 TextView button = (TextView) findViewById(buttonId);
2816 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2817 }
2818
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002819 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002820 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002821 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002822 }
2823
Winson Chungbb185bd2011-11-21 12:31:42 -08002824 private void invalidatePressedFocusedStates(View container, View button) {
2825 if (container instanceof HolographicLinearLayout) {
2826 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2827 layout.invalidatePressedFocusedStates();
2828 } else if (button instanceof HolographicImageView) {
2829 HolographicImageView view = (HolographicImageView) button;
2830 view.invalidatePressedFocusedStates();
2831 }
2832 }
2833
Winson Chungc51db6a2011-10-05 11:44:49 -07002834 private boolean updateGlobalSearchIcon() {
2835 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002836 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2837 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002838 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002839 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002840 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002841
Winson Chung1cad91e2011-05-25 17:41:01 -07002842 final SearchManager searchManager =
2843 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2844 ComponentName activityName = searchManager.getGlobalSearchActivity();
2845 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002846 int coi = getCurrentOrientationIndexForGlobalIcons();
2847 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002848 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2849 TOOLBAR_SEARCH_ICON_METADATA_NAME);
2850 if (sGlobalSearchIcon[coi] == null) {
2851 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2852 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2853 TOOLBAR_ICON_METADATA_NAME);
2854 }
2855
Winson Chung649723c2011-07-06 20:41:23 -07002856 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002857 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2858 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002859 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002860 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002861 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002862 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002863 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002864 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2865 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2866 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002867 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07002868 if (voiceButtonProxy != null) {
2869 voiceButtonProxy.setVisibility(View.GONE);
2870 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002871 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002872 }
2873 }
2874
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002875 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002876 final View searchButtonContainer = findViewById(R.id.search_button_container);
2877 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002878 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002879 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002880 }
2881
Winson Chungc51db6a2011-10-05 11:44:49 -07002882 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002883 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002884 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002885 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002886 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002887
Winson Chungc51db6a2011-10-05 11:44:49 -07002888 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07002889 final SearchManager searchManager =
2890 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2891 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2892
2893 ComponentName activityName = null;
2894 if (globalSearchActivity != null) {
2895 // Check if the global search activity handles voice search
2896 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2897 intent.setPackage(globalSearchActivity.getPackageName());
2898 activityName = intent.resolveActivity(getPackageManager());
2899 }
2900
2901 if (activityName == null) {
2902 // Fallback: check if an activity other than the global search activity
2903 // resolves this
2904 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2905 activityName = intent.resolveActivity(getPackageManager());
2906 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002907 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002908 int coi = getCurrentOrientationIndexForGlobalIcons();
2909 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002910 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2911 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
2912 if (sVoiceSearchIcon[coi] == null) {
2913 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2914 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2915 TOOLBAR_ICON_METADATA_NAME);
2916 }
Winson Chung649723c2011-07-06 20:41:23 -07002917 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002918 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002919 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07002920 if (voiceButtonProxy != null) {
2921 voiceButtonProxy.setVisibility(View.VISIBLE);
2922 }
Winson Chungbb185bd2011-11-21 12:31:42 -08002923 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002924 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002925 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002926 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002927 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002928 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07002929 if (voiceButtonProxy != null) {
2930 voiceButtonProxy.setVisibility(View.GONE);
2931 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002932 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002933 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002934 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002935
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002936 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002937 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2938 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002939 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002940 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002941 }
2942
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002943 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002944 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002945 */
2946 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002947 final View marketButton = findViewById(R.id.market_button);
2948 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2949 // Find the app market activity by resolving an intent.
2950 // (If multiple app markets are installed, it will return the ResolverActivity.)
2951 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002952 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002953 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002954 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002955 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002956 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
2957 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07002958 marketButton.setVisibility(View.VISIBLE);
2959 } else {
2960 // We should hide and disable the view so that we don't try and restore the visibility
2961 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2962 marketButton.setVisibility(View.GONE);
2963 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002964 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002965 }
2966
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002967 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002968 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002969 }
2970
Michael Jurkad7c28052012-04-27 15:43:36 -07002971 @Override
2972 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2973 boolean result = super.dispatchPopulateAccessibilityEvent(event);
2974 final List<CharSequence> text = event.getText();
2975 text.clear();
2976 text.add(getString(R.string.home));
2977 return result;
2978 }
2979
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002980 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002981 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002982 */
2983 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2984 @Override
2985 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002986 closeSystemDialogs();
2987 }
2988 }
2989
2990 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002991 * Receives notifications whenever the appwidgets are reset.
2992 */
2993 private class AppWidgetResetObserver extends ContentObserver {
2994 public AppWidgetResetObserver() {
2995 super(new Handler());
2996 }
2997
2998 @Override
2999 public void onChange(boolean selfChange) {
3000 onAppWidgetReset();
3001 }
3002 }
3003
3004 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003005 * If the activity is currently paused, signal that we need to re-run the loader
3006 * in onResume.
3007 *
3008 * This needs to be called from incoming places where resources might have been loaded
3009 * while we are paused. That is becaues the Configuration might be wrong
3010 * when we're not running, and if it comes back to what it was when we
3011 * were paused, we are not restarted.
3012 *
3013 * Implementation of the method from LauncherModel.Callbacks.
3014 *
3015 * @return true if we are currently paused. The caller might be able to
3016 * skip some work in that case since we will come back again.
3017 */
3018 public boolean setLoadOnResume() {
3019 if (mPaused) {
3020 Log.i(TAG, "setLoadOnResume");
3021 mOnResumeNeedsLoad = true;
3022 return true;
3023 } else {
3024 return false;
3025 }
3026 }
3027
3028 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003029 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003030 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003031 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003032 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003033 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003034 } else {
3035 return SCREEN_COUNT / 2;
3036 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003037 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003038
Joe Onorato9c1289c2009-08-17 11:03:03 -04003039 /**
3040 * Refreshes the shortcuts shown on the workspace.
3041 *
3042 * Implementation of the method from LauncherModel.Callbacks.
3043 */
3044 public void startBinding() {
3045 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003046
Winson Chungf0c6ae02012-03-21 16:10:31 -07003047 mNewShortcutAnimatePage = -1;
3048 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003049 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003050 int count = workspace.getChildCount();
3051 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003052 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003053 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3054 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003055 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003056 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003057 if (mHotseat != null) {
3058 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003059 }
3060 }
3061
3062 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003063 * Bind the items start-end from the list.
3064 *
3065 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003066 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003067 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003068 setLoadOnResume();
3069
Winson Chungf0c6ae02012-03-21 16:10:31 -07003070 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3071 Set<String> newApps = new HashSet<String>();
3072 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3073
3074 Workspace workspace = mWorkspace;
3075 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003076 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003077
3078 // Short circuit if we are loading dock items for a configuration which has no dock
3079 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3080 mHotseat == null) {
3081 continue;
3082 }
3083
Joe Onorato9c1289c2009-08-17 11:03:03 -04003084 switch (item.itemType) {
3085 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3086 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003087 ShortcutInfo info = (ShortcutInfo) item;
3088 String uri = info.intent.toUri(0).toString();
3089 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003090 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3091 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003092 if (newApps.contains(uri)) {
3093 newApps.remove(uri);
3094
3095 // Prepare the view to be animated up
3096 shortcut.setAlpha(0f);
3097 shortcut.setScaleX(0f);
3098 shortcut.setScaleY(0f);
3099 mNewShortcutAnimatePage = item.screen;
3100 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3101 mNewShortcutAnimateViews.add(shortcut);
3102 }
3103 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003104 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003105 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003106 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003107 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003108 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003109 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3110 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003111 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003112 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003113 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003114
Joe Onorato9c1289c2009-08-17 11:03:03 -04003115 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003116 }
3117
Joe Onorato9c1289c2009-08-17 11:03:03 -04003118 /**
3119 * Implementation of the method from LauncherModel.Callbacks.
3120 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003121 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003122 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003123 sFolders.clear();
3124 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003125 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003126
3127 /**
3128 * Add the views for a widget to the workspace.
3129 *
3130 * Implementation of the method from LauncherModel.Callbacks.
3131 */
3132 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003133 setLoadOnResume();
3134
Daniel Sandler843e8602010-06-07 14:59:01 -04003135 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3136 if (DEBUG_WIDGETS) {
3137 Log.d(TAG, "bindAppWidget: " + item);
3138 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003139 final Workspace workspace = mWorkspace;
3140
3141 final int appWidgetId = item.appWidgetId;
3142 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003143 if (DEBUG_WIDGETS) {
3144 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3145 }
3146
Joe Onorato9c1289c2009-08-17 11:03:03 -04003147 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3148
Joe Onorato9c1289c2009-08-17 11:03:03 -04003149 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3150 item.hostView.setTag(item);
3151
Winson Chung3d503fb2011-07-13 17:25:49 -07003152 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003153 item.cellY, item.spanX, item.spanY, false);
3154
Adam Cohended9f8d2010-11-03 13:25:16 -07003155 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3156
Joe Onorato9c1289c2009-08-17 11:03:03 -04003157 workspace.requestLayout();
3158
Winson Chungf51025f2012-05-03 15:21:11 -07003159 AppWidgetResizeFrame.updateWidgetSizeRanges(item.hostView,
3160 this, item.spanX, item.spanY);
3161
Daniel Sandler843e8602010-06-07 14:59:01 -04003162 if (DEBUG_WIDGETS) {
3163 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3164 + (SystemClock.uptimeMillis()-start) + "ms");
3165 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003166 }
3167
3168 /**
3169 * Callback saying that there aren't any more items to bind.
3170 *
3171 * Implementation of the method from LauncherModel.Callbacks.
3172 */
3173 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003174 setLoadOnResume();
3175
Joe Onorato9c1289c2009-08-17 11:03:03 -04003176 if (mSavedState != null) {
3177 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003178 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003179 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003180 mSavedState = null;
3181 }
3182
3183 if (mSavedInstanceState != null) {
3184 super.onRestoreInstanceState(mSavedInstanceState);
3185 mSavedInstanceState = null;
3186 }
3187
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003188 // If we received the result of any pending adds while the loader was running (e.g. the
3189 // widget configuration forced an orientation change), process them now.
3190 for (int i = 0; i < sPendingAddList.size(); i++) {
3191 completeAdd(sPendingAddList.get(i));
3192 }
3193 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003194
Winson Chungc51db6a2011-10-05 11:44:49 -07003195 // Update the market app icon as necessary (the other icons will be managed in response to
3196 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003197 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003198
Winson Chungf0c6ae02012-03-21 16:10:31 -07003199 // Animate up any icons as necessary
3200 if (mVisible || mWorkspaceLoading) {
3201 Runnable newAppsRunnable = new Runnable() {
3202 @Override
3203 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003204 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003205 }
3206 };
Winson Chunga2413752012-04-03 14:22:34 -07003207
3208 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3209 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3210 if (canRunNewAppsAnimation()) {
3211 // If the user has not interacted recently, then either snap to the new page to show
3212 // the new-apps animation or just run them if they are to appear on the current page
3213 if (willSnapPage) {
3214 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3215 } else {
3216 runNewAppsAnimation(false);
3217 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003218 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003219 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003220 // are on another page (or just normally if they are added to the current page)
3221 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003222 }
3223 }
3224
3225 mWorkspaceLoading = false;
3226 }
3227
Winson Chunga2413752012-04-03 14:22:34 -07003228 private boolean canRunNewAppsAnimation() {
3229 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3230 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3231 }
3232
Winson Chungf0c6ae02012-03-21 16:10:31 -07003233 /**
3234 * Runs a new animation that scales up icons that were added while Launcher was in the
3235 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003236 *
3237 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003238 */
Winson Chunga2413752012-04-03 14:22:34 -07003239 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003240 AnimatorSet anim = new AnimatorSet();
3241 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003242
3243 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003244 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3245 @Override
3246 public int compare(View a, View b) {
3247 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3248 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3249 int cellCountX = LauncherModel.getCellCountX();
3250 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3251 }
3252 });
Winson Chunga2413752012-04-03 14:22:34 -07003253
3254 // Animate each of the views in place (or show them immediately if requested)
3255 if (immediate) {
3256 for (View v : mNewShortcutAnimateViews) {
3257 v.setAlpha(1f);
3258 v.setScaleX(1f);
3259 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003260 }
Winson Chunga2413752012-04-03 14:22:34 -07003261 } else {
3262 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3263 View v = mNewShortcutAnimateViews.get(i);
3264 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3265 PropertyValuesHolder.ofFloat("alpha", 1f),
3266 PropertyValuesHolder.ofFloat("scaleX", 1f),
3267 PropertyValuesHolder.ofFloat("scaleY", 1f));
3268 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3269 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3270 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3271 bounceAnims.add(bounceAnim);
3272 }
3273 anim.playTogether(bounceAnims);
3274 anim.addListener(new AnimatorListenerAdapter() {
3275 @Override
3276 public void onAnimationEnd(Animator animation) {
3277 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3278 }
3279 });
3280 anim.start();
3281 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003282
3283 // Clean up
3284 mNewShortcutAnimatePage = -1;
3285 mNewShortcutAnimateViews.clear();
3286 new Thread("clearNewAppsThread") {
3287 public void run() {
3288 mSharedPrefs.edit()
3289 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3290 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3291 .commit();
3292 }
3293 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003294 }
3295
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003296 @Override
3297 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003298 boolean searchVisible = updateGlobalSearchIcon();
3299 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3300 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003301 }
3302
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003303 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003304 * Add the icons for all apps.
3305 *
3306 * Implementation of the method from LauncherModel.Callbacks.
3307 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003308 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3309 // Remove the progress bar entirely; we could also make it GONE
3310 // but better to remove it since we know it's not going to be used
3311 View progressBar = mAppsCustomizeTabHost.
3312 findViewById(R.id.apps_customize_progress_bar);
3313 if (progressBar != null) {
3314 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003315 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003316 // We just post the call to setApps so the user sees the progress bar
3317 // disappear-- otherwise, it just looks like the progress bar froze
3318 // which doesn't look great
3319 mAppsCustomizeTabHost.post(new Runnable() {
3320 public void run() {
3321 if (mAppsCustomizeContent != null) {
3322 mAppsCustomizeContent.setApps(apps);
3323 }
3324 }
3325 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003326 }
3327
3328 /**
3329 * A package was installed.
3330 *
3331 * Implementation of the method from LauncherModel.Callbacks.
3332 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003333 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003334 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003335
Winson Chung785d2eb2011-04-14 16:08:02 -07003336 if (mAppsCustomizeContent != null) {
3337 mAppsCustomizeContent.addApps(apps);
3338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003339 }
3340
3341 /**
3342 * A package was updated.
3343 *
3344 * Implementation of the method from LauncherModel.Callbacks.
3345 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003346 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003347 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003348 if (mWorkspace != null) {
3349 mWorkspace.updateShortcuts(apps);
3350 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003351
Winson Chung785d2eb2011-04-14 16:08:02 -07003352 if (mAppsCustomizeContent != null) {
3353 mAppsCustomizeContent.updateApps(apps);
3354 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003355 }
3356
3357 /**
3358 * A package was uninstalled.
3359 *
3360 * Implementation of the method from LauncherModel.Callbacks.
3361 */
Joe Onorato36115782010-06-17 13:28:48 -04003362 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003363 if (permanent) {
3364 mWorkspace.removeItems(apps);
3365 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003366
Winson Chung785d2eb2011-04-14 16:08:02 -07003367 if (mAppsCustomizeContent != null) {
3368 mAppsCustomizeContent.removeApps(apps);
3369 }
Winson Chunga1820962011-10-03 16:31:06 -07003370
3371 // Notify the drag controller
3372 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003373 }
Joe Onoratobe386092009-11-17 17:32:16 -08003374
3375 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003376 * A number of packages were updated.
3377 */
3378 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003379 if (mAppsCustomizeContent != null) {
3380 mAppsCustomizeContent.onPackagesUpdated();
3381 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003382 }
3383
Winson Chung400438b2011-01-16 17:53:48 -08003384 private int mapConfigurationOriActivityInfoOri(int configOri) {
3385 final Display d = getWindowManager().getDefaultDisplay();
3386 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3387 switch (d.getRotation()) {
3388 case Surface.ROTATION_0:
3389 case Surface.ROTATION_180:
3390 // We are currently in the same basic orientation as the natural orientation
3391 naturalOri = configOri;
3392 break;
3393 case Surface.ROTATION_90:
3394 case Surface.ROTATION_270:
3395 // We are currently in the other basic orientation to the natural orientation
3396 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3397 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3398 break;
3399 }
3400
3401 int[] oriMap = {
3402 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3403 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3404 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3405 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3406 };
3407 // Since the map starts at portrait, we need to offset if this device's natural orientation
3408 // is landscape.
3409 int indexOffset = 0;
3410 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3411 indexOffset = 1;
3412 }
3413 return oriMap[(d.getRotation() + indexOffset) % 4];
3414 }
Adam Cohen446e9402011-09-15 18:21:21 -07003415
Winson Chung4b919f82012-05-01 10:44:08 -07003416 public boolean isRotationEnabled() {
3417 boolean forceEnableRotation = "true".equalsIgnoreCase(SystemProperties.get(
3418 FORCE_ENABLE_ROTATION_PROPERTY, "false"));
3419 boolean enableRotation = forceEnableRotation ||
3420 getResources().getBoolean(R.bool.allow_rotation);
3421 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003422 }
Winson Chung4b919f82012-05-01 10:44:08 -07003423 public void lockScreenOrientation() {
3424 if (isRotationEnabled()) {
3425 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3426 .getConfiguration().orientation));
3427 }
3428 }
3429 public void unlockScreenOrientation(boolean immediate) {
3430 if (isRotationEnabled()) {
3431 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003432 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003433 } else {
3434 mHandler.postDelayed(new Runnable() {
3435 public void run() {
3436 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3437 }
3438 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003439 }
Winson Chung4b919f82012-05-01 10:44:08 -07003440 }
Winson Chung400438b2011-01-16 17:53:48 -08003441 }
3442
Winson Chung82f55532011-08-09 14:14:23 -07003443 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003444 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003445 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003446 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003447
Winson Chung7d7541e2011-09-16 20:14:36 -07003448 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003449 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003450 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3451 Cling cling = (Cling) findViewById(clingId);
3452 if (cling != null) {
3453 cling.init(this, positionData);
3454 cling.setVisibility(View.VISIBLE);
3455 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3456 if (animate) {
3457 cling.buildLayer();
3458 cling.setAlpha(0f);
3459 cling.animate()
3460 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003461 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003462 .setDuration(SHOW_CLING_DURATION)
3463 .setStartDelay(delay)
3464 .start();
3465 } else {
3466 cling.setAlpha(1f);
3467 }
3468 }
3469 return cling;
3470 }
3471 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003472 if (cling != null) {
3473 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003474 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003475 anim.addListener(new AnimatorListenerAdapter() {
3476 public void onAnimationEnd(Animator animation) {
3477 cling.setVisibility(View.GONE);
3478 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003479 // We should update the shared preferences on a background thread
3480 new Thread("dismissClingThread") {
3481 public void run() {
3482 SharedPreferences.Editor editor = mSharedPrefs.edit();
3483 editor.putBoolean(flag, true);
3484 editor.commit();
3485 }
3486 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003487 };
3488 });
3489 anim.start();
3490 }
3491 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003492 private void removeCling(int id) {
3493 final View cling = findViewById(id);
3494 if (cling != null) {
3495 final ViewGroup parent = (ViewGroup) cling.getParent();
3496 parent.post(new Runnable() {
3497 @Override
3498 public void run() {
3499 parent.removeView(cling);
3500 }
3501 });
3502 }
3503 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003504 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003505 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003506 if (isClingsEnabled() &&
3507 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003508 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003509 } else {
3510 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003511 }
3512 }
3513 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003514 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003515 if (isClingsEnabled() &&
3516 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003517 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003518 } else {
3519 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003520 }
3521 }
3522 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003523 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003524 if (isClingsEnabled() &&
3525 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3526 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003527 } else {
3528 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003529 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003530 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003531 }
3532 public boolean isFolderClingVisible() {
3533 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003534 if (cling != null) {
3535 return cling.getVisibility() == View.VISIBLE;
3536 }
3537 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003538 }
Winson Chung82f55532011-08-09 14:14:23 -07003539 public void dismissWorkspaceCling(View v) {
3540 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003541 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003542 }
3543 public void dismissAllAppsCling(View v) {
3544 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003545 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3546 }
3547 public void dismissFolderCling(View v) {
3548 Cling cling = (Cling) findViewById(R.id.folder_cling);
3549 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003550 }
3551
Winson Chung80baf5a2010-08-09 16:03:15 -07003552 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003553 * Prints out out state for debugging.
3554 */
3555 public void dumpState() {
3556 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003557 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003558 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3559 Log.d(TAG, "mRestoring=" + mRestoring);
3560 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3561 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003562 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003563 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003564
Winson Chung785d2eb2011-04-14 16:08:02 -07003565 if (mAppsCustomizeContent != null) {
3566 mAppsCustomizeContent.dumpState();
3567 }
Joe Onoratobe386092009-11-17 17:32:16 -08003568 Log.d(TAG, "END launcher2 dump state");
3569 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003570
3571 @Override
3572 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3573 super.dump(prefix, fd, writer, args);
3574 writer.println(" ");
3575 writer.println("Debug logs: ");
3576 for (int i = 0; i < sDumpLogs.size(); i++) {
3577 writer.println(" " + sDumpLogs.get(i));
3578 }
3579 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003580}
Michael Jurka2763be32011-02-24 11:19:57 -08003581
Michael Jurkaabded662011-03-04 12:06:57 -08003582interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003583 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003584 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003585 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003586 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003587 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003588}