blob: ce855a1214c598bdc9ab5b8a908022022dee4479 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070041import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070042import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070045import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070047import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080048import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070049import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070050import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040051import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070052import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020054import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080055import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070056import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070057import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040058import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080059import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070060import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080061import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.text.Selection;
63import android.text.SpannableStringBuilder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070065import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080066import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080067import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.view.KeyEvent;
69import android.view.LayoutInflater;
70import android.view.Menu;
71import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070072import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080073import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080075import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070076import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080077import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080078import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070079import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080080import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070081import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070082import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080083import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070085import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070086import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070087import android.widget.TextView;
88import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070089
Adam Cohendf2cc412011-04-27 16:56:57 -070090import com.android.common.Search;
91import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070092import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080093
Adam Cohenc0dcf592011-06-01 15:30:43 -070094import java.io.DataInputStream;
95import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070096import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070097import java.io.FileNotFoundException;
98import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070099import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700101import java.util.Collection;
102import java.util.Collections;
103import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700104import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700105import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700106import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109/**
110 * Default launcher application.
111 */
Michael Jurka946ad472010-07-09 18:05:18 -0700112public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700113 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
114 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800115 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700116 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
Joe Onorato9c1289c2009-08-17 11:03:03 -0400118 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400119 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700120 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700121
Winson Chung70d72102011-08-12 11:24:35 -0700122 private static final int MENU_GROUP_WALLPAPER = 1;
123 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
124 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700125 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
126 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
128 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700129 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 private static final int REQUEST_PICK_APPLICATION = 6;
131 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700132 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700133 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Michael Jurka8b805b12012-04-18 14:23:14 -0700135 private static final int REQUEST_BIND_APPWIDGET = 11;
136
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
138
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800139 static final int SCREEN_COUNT = 5;
140 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Romain Guy98d01652009-06-30 16:21:04 -0700142 private static final String PREFERENCES = "launcher.preferences";
Adam Cohen23a4d302011-12-01 17:26:44 -0800143 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Winson Chung2672ff92012-05-04 16:22:30 -0700145 // The Intent extra that defines whether to ignore the launch animation
146 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
147 "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
148
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 // Type: int
150 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700151 // Type: int
152 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700154 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
155 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
157 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700158 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 // Type: boolean
162 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
163 // Type: long
164 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
165
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700166 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700167 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
168 "com.android.launcher.toolbar_search_icon";
169 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
170 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700171
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700172 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700173 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700174 private State mState = State.WORKSPACE;
175 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800176 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700177
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700179 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
180 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700181 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700182 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800185 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Winson Chunga2413752012-04-03 14:22:34 -0700187 // How long to wait before the new-shortcut animation automatically pans the workspace
188 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
189
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800190 private final BroadcastReceiver mCloseSystemDialogsReceiver
191 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800192 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private LayoutInflater mInflater;
195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800197 private View mQsbDivider;
198 private View mDockDivider;
199 private DragLayer mDragLayer;
200 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700201
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700202 private AppWidgetManager mAppWidgetManager;
203 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700204
Michael Jurkac9d95c52011-08-29 14:03:34 -0700205 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700206 private int[] mTmpAddItemCellCoordinates = new int[2];
207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private FolderInfo mFolderInfo;
209
Winson Chung3d503fb2011-07-13 17:25:49 -0700210 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800211 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700212
Winson Chungc51db6a2011-10-05 11:44:49 -0700213 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700214 private AppsCustomizeTabHost mAppsCustomizeTabHost;
215 private AppsCustomizePagedView mAppsCustomizeContent;
216 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 private Bundle mSavedState;
219
220 private SpannableStringBuilder mDefaultKeySsb = null;
221
Joe Onorato9c1289c2009-08-17 11:03:03 -0400222 private boolean mWorkspaceLoading = true;
223
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800224 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 private boolean mRestoring;
226 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700227 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228
229 private Bundle mSavedInstanceState;
230
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800232 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800233 private boolean mUserPresent = true;
234 private boolean mVisible = false;
235 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700237 private static LocaleConfiguration sLocaleConfiguration = null;
238
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700239 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700240
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700241 private Intent mAppMarketIntent = null;
242
Adam Cohended9f8d2010-11-03 13:25:16 -0700243 // Related to the auto-advancing of widgets
244 private final int ADVANCE_MSG = 1;
245 private final int mAdvanceInterval = 20000;
246 private final int mAdvanceStagger = 250;
247 private long mAutoAdvanceSentTime;
248 private long mAutoAdvanceTimeLeft = -1;
249 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
250 new HashMap<View, AppWidgetProviderInfo>();
251
Winson Chung400438b2011-01-16 17:53:48 -0800252 // Determines how long to wait after a rotation before restoring the screen orientation to
253 // match the sensor state.
254 private final int mRestoreScreenOrientationDelay = 500;
255
Michael Jurka4ef207b2010-11-29 17:05:45 -0800256 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700257 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
258 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
259 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800260
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700261 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Michael Jurka8b805b12012-04-18 14:23:14 -0700262 PendingAddWidgetInfo mWidgetBeingBoundOrConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700263
Winson Chung46353de2012-02-16 14:05:10 -0800264 // We only want to get the SharedPreferences once since it does an FS stat each time we get
265 // it from the context.
266 private SharedPreferences mSharedPrefs;
267
Winson Chungf0c6ae02012-03-21 16:10:31 -0700268 // Holds the page that we need to animate to, and the icon views that we need to animate up
269 // when we scroll to that page on resume.
270 private int mNewShortcutAnimatePage = -1;
271 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700272
Michael Jurkaddd62e92011-02-16 17:49:14 -0800273 private BubbleTextView mWaitingForResume;
274
Michael Jurkac1f5d262011-09-30 19:32:27 -0700275 private Runnable mBuildLayersRunnable = new Runnable() {
276 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700277 if (mWorkspace != null) {
278 mWorkspace.buildPageHardwareLayers();
279 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700280 }
281 };
282
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800283 private static ArrayList<PendingAddArguments> sPendingAddList
284 = new ArrayList<PendingAddArguments>();
285
286 private static class PendingAddArguments {
287 int requestCode;
288 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700289 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800290 int screen;
291 int cellX;
292 int cellY;
293 }
294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 @Override
296 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700297 if (DEBUG_STRICT_MODE) {
298 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
299 .detectDiskReads()
300 .detectDiskWrites()
301 .detectNetwork() // or .detectAll() for all detectable problems
302 .penaltyLog()
303 .build());
304 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
305 .detectLeakedSqlLiteObjects()
306 .detectLeakedClosableObjects()
307 .penaltyLog()
308 .penaltyDeath()
309 .build());
310 }
311
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800313 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700314 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
315 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800316 mModel = app.setLauncher(this);
317 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400318 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700320
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700321 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700322 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
323 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700324
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200326 android.os.Debug.startMethodTracing(
327 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 }
329
330 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 setContentView(R.layout.launcher);
332 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700333 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800335 registerContentObservers();
336
Joe Onorato7c312c12009-08-13 21:36:53 -0700337 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700338
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 mSavedState = savedInstanceState;
340 restoreState(mSavedState);
341
Winson Chunga12a2502010-12-20 14:41:35 -0800342 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700343 if (mAppsCustomizeContent != null) {
344 mAppsCustomizeContent.onPackagesUpdated();
345 }
Winson Chunga12a2502010-12-20 14:41:35 -0800346
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 if (PROFILE_STARTUP) {
348 android.os.Debug.stopMethodTracing();
349 }
350
351 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700352 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 }
354
Michael Jurkac57b7a82011-08-09 22:02:20 -0700355 if (!mModel.isAllAppsLoaded()) {
356 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
357 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
358 }
359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 // For handling default keys
361 mDefaultKeySsb = new SpannableStringBuilder();
362 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800363
364 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
365 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800366
Winson Chungc51db6a2011-10-05 11:44:49 -0700367 boolean searchVisible = false;
368 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800369 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700370 int coi = getCurrentOrientationIndexForGlobalIcons();
371 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
372 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700373 updateAppMarketIcon();
374 searchVisible = updateGlobalSearchIcon();
375 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700376 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700377 if (sGlobalSearchIcon[coi] != null) {
378 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700379 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700380 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700381 if (sVoiceSearchIcon[coi] != null) {
382 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700383 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700384 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700385 if (sAppMarketIcon[coi] != null) {
386 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800387 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700388 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700389
390 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Winson Chung4b919f82012-05-01 10:44:08 -0700391 unlockScreenOrientation(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 }
Romain Guycbb89e42009-06-08 15:52:54 -0700393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700395 if (sLocaleConfiguration == null) {
396 new AsyncTask<Void, Void, LocaleConfiguration>() {
397 @Override
398 protected LocaleConfiguration doInBackground(Void... unused) {
399 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
400 readConfiguration(Launcher.this, localeConfiguration);
401 return localeConfiguration;
402 }
403
404 @Override
405 protected void onPostExecute(LocaleConfiguration result) {
406 sLocaleConfiguration = result;
407 checkForLocaleChange(); // recursive, but now with a locale configuration
408 }
409 }.execute();
410 return;
411 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700412
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 final Configuration configuration = getResources().getConfiguration();
414
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700415 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 final String locale = configuration.locale.toString();
417
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700418 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 final int mcc = configuration.mcc;
420
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700421 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 final int mnc = configuration.mnc;
423
Romain Guy84f296c2009-11-04 15:00:44 -0800424 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425
Romain Guy84f296c2009-11-04 15:00:44 -0800426 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700427 sLocaleConfiguration.locale = locale;
428 sLocaleConfiguration.mcc = mcc;
429 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700430
Joe Onorato0589f0f2010-02-08 13:44:00 -0800431 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700432
433 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
434 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700435 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700436 public void run() {
437 writeConfiguration(Launcher.this, localeConfiguration);
438 }
439 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700440 }
441 }
442
443 private static class LocaleConfiguration {
444 public String locale;
445 public int mcc = -1;
446 public int mnc = -1;
447 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700448
Romain Guy98d01652009-06-30 16:21:04 -0700449 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
450 DataInputStream in = null;
451 try {
452 in = new DataInputStream(context.openFileInput(PREFERENCES));
453 configuration.locale = in.readUTF();
454 configuration.mcc = in.readInt();
455 configuration.mnc = in.readInt();
456 } catch (FileNotFoundException e) {
457 // Ignore
458 } catch (IOException e) {
459 // Ignore
460 } finally {
461 if (in != null) {
462 try {
463 in.close();
464 } catch (IOException e) {
465 // Ignore
466 }
467 }
468 }
469 }
470
471 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
472 DataOutputStream out = null;
473 try {
474 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
475 out.writeUTF(configuration.locale);
476 out.writeInt(configuration.mcc);
477 out.writeInt(configuration.mnc);
478 out.flush();
479 } catch (FileNotFoundException e) {
480 // Ignore
481 } catch (IOException e) {
482 //noinspection ResultOfMethodCallIgnored
483 context.getFileStreamPath(PREFERENCES).delete();
484 } finally {
485 if (out != null) {
486 try {
487 out.close();
488 } catch (IOException e) {
489 // Ignore
490 }
491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 }
493 }
494
Adam Cohen716b51e2011-06-30 12:09:54 -0700495 public DragLayer getDragLayer() {
496 return mDragLayer;
497 }
498
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 static int getScreen() {
500 synchronized (sLock) {
501 return sScreen;
502 }
503 }
504
505 static void setScreen(int screen) {
506 synchronized (sLock) {
507 sScreen = screen;
508 }
509 }
510
Winson Chung557d6ed2011-07-08 15:34:52 -0700511 /**
512 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
513 * a configuration step, this allows the proper animations to run after other transitions.
514 */
515 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700516 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800517 switch (args.requestCode) {
518 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700519 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
520 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800521 break;
522 case REQUEST_PICK_SHORTCUT:
523 processShortcut(args.intent);
524 break;
525 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700526 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
527 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700528 result = true;
529 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800530 case REQUEST_PICK_APPWIDGET:
531 addAppWidgetFromPick(args.intent);
532 break;
533 case REQUEST_CREATE_APPWIDGET:
534 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800535 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700536 result = true;
537 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800538 case REQUEST_PICK_WALLPAPER:
539 // We just wanted the activity result here so we can clear mWaitingForResult
540 break;
541 }
Michael Jurka27614d22012-04-02 06:40:22 -0700542 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
543 // if you turned the screen off and then back while in All Apps, Launcher would not
544 // return to the workspace. Clearing mAddInfo.container here fixes this issue
545 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700546 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800547 }
548
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700550 protected void onActivityResult(
551 final int requestCode, final int resultCode, final Intent data) {
552 if (requestCode == REQUEST_BIND_APPWIDGET) {
553 int appWidgetId = data != null ?
554 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
555 if (resultCode == RESULT_CANCELED) {
556 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
557 } else if (resultCode == RESULT_OK) {
558 addAppWidgetImpl(appWidgetId, mWidgetBeingBoundOrConfigured);
559 }
560 return;
561 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700562 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800563 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
564 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700565 mWaitingForResult = false;
566
Adam Cohened66b2b2012-01-23 17:28:51 -0800567 // We have special handling for widgets
568 if (isWidgetDrop) {
569 int appWidgetId = data != null ?
570 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700571 if (appWidgetId < 0) {
572 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
573 "widget configuration activity.");
574 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
575 } else {
576 completeTwoStageWidgetDrop(resultCode, appWidgetId);
577 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800578 return;
579 }
580
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 // The pattern used here is that a user PICKs a specific application,
582 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700583
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
585 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700586 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800587 final PendingAddArguments args = new PendingAddArguments();
588 args.requestCode = requestCode;
589 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700590 args.container = mPendingAddInfo.container;
591 args.screen = mPendingAddInfo.screen;
592 args.cellX = mPendingAddInfo.cellX;
593 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800594 if (isWorkspaceLocked()) {
595 sPendingAddList.add(args);
596 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700597 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800600 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700601 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800602 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
603 null);
604 }
605
606 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700607 CellLayout cellLayout =
608 (CellLayout) mWorkspace.getChildAt(mWidgetBeingBoundOrConfigured.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800609 Runnable onCompleteRunnable = null;
610 int animationType = 0;
611
612 if (resultCode == RESULT_OK) {
613 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
614 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Michael Jurka8b805b12012-04-18 14:23:14 -0700615 mWidgetBeingBoundOrConfigured.info);
616 mWidgetBeingBoundOrConfigured.boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800617 onCompleteRunnable = new Runnable() {
618 @Override
619 public void run() {
620 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
621 mPendingAddInfo.screen, layout, null);
622 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
623 null);
624 }
625 };
626 } else if (resultCode == RESULT_CANCELED) {
627 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
628 onCompleteRunnable = new Runnable() {
629 @Override
630 public void run() {
631 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
632 null);
633 }
634 };
635 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700636 mWorkspace.animateWidgetDrop(mWidgetBeingBoundOrConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800637 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Michael Jurka8b805b12012-04-18 14:23:14 -0700638 animationType, mWidgetBeingBoundOrConfigured.boundWidget, true);
639 mWidgetBeingBoundOrConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 }
641
642 @Override
643 protected void onResume() {
644 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700645
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800646 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700647 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700649 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700651 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 }
Winson Chunge4e50662012-01-23 14:45:13 -0800653
654 // Reset the pressed state of icons that were locked in the press state while activities
655 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800656 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800657 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800658 mWaitingForResume.setStayPressed(false);
659 }
Winson Chunge4e50662012-01-23 14:45:13 -0800660 if (mAppsCustomizeContent != null) {
661 // Resets the previous all apps icon press state
662 mAppsCustomizeContent.resetDrawableState();
663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 }
665
666 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700667 protected void onPause() {
668 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700669 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800670 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700671 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700672 }
Romain Guycbb89e42009-06-08 15:52:54 -0700673
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700674 @Override
675 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400676 // Flag the loader to stop early before switching
677 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700678 if (mAppsCustomizeContent != null) {
679 mAppsCustomizeContent.surrender();
680 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800681 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700682 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700683
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800684 // We can't hide the IME if it was forced open. So don't bother
685 /*
686 @Override
687 public void onWindowFocusChanged(boolean hasFocus) {
688 super.onWindowFocusChanged(hasFocus);
689
690 if (hasFocus) {
691 final InputMethodManager inputManager = (InputMethodManager)
692 getSystemService(Context.INPUT_METHOD_SERVICE);
693 WindowManager.LayoutParams lp = getWindow().getAttributes();
694 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
695 android.os.Handler()) {
696 protected void onReceiveResult(int resultCode, Bundle resultData) {
697 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
698 }
699 });
700 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
701 }
702 }
703 */
704
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 private boolean acceptFilter() {
706 final InputMethodManager inputManager = (InputMethodManager)
707 getSystemService(Context.INPUT_METHOD_SERVICE);
708 return !inputManager.isFullscreenMode();
709 }
710
711 @Override
712 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700713 final int uniChar = event.getUnicodeChar();
714 final boolean handled = super.onKeyDown(keyCode, event);
715 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
716 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
718 keyCode, event);
719 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700720 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700721 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700722 // showSearchDialog()
723 // If there are multiple keystrokes before the search dialog takes focus,
724 // onSearchRequested() will be called for every keystroke,
725 // but it is idempotent, so it's fine.
726 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 }
728 }
729
Joe Onorato8a9625e2010-01-28 15:55:35 -0800730 // Eat the long press event so the keyboard doesn't come up.
731 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
732 return true;
733 }
734
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 return handled;
736 }
737
Karl Rosaen138a0412009-04-23 19:00:21 -0700738 private String getTypedText() {
739 return mDefaultKeySsb.toString();
740 }
741
742 private void clearTypedText() {
743 mDefaultKeySsb.clear();
744 mDefaultKeySsb.clearSpans();
745 Selection.setSelection(mDefaultKeySsb, 0);
746 }
747
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700749 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
750 * State
751 */
752 private static State intToState(int stateOrdinal) {
753 State state = State.WORKSPACE;
754 final State[] stateValues = State.values();
755 for (int i = 0; i < stateValues.length; i++) {
756 if (stateValues[i].ordinal() == stateOrdinal) {
757 state = stateValues[i];
758 break;
759 }
760 }
761 return state;
762 }
763
764 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 * Restores the previous state, if it exists.
766 *
767 * @param savedState The previous state.
768 */
769 private void restoreState(Bundle savedState) {
770 if (savedState == null) {
771 return;
772 }
773
Michael Jurka883f55b2010-10-21 15:47:14 -0700774 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700775 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800776 showAllApps(false);
777 }
778
Winson Chungf0c6ae02012-03-21 16:10:31 -0700779 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700781 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 }
783
Winson Chung3d503fb2011-07-13 17:25:49 -0700784 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
785 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700786
Winson Chung3d503fb2011-07-13 17:25:49 -0700787 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
788 mPendingAddInfo.container = pendingAddContainer;
789 mPendingAddInfo.screen = pendingAddScreen;
790 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
791 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 mRestoring = true;
793 }
794
795 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
796 if (renameFolder) {
797 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700798 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 mRestoring = true;
800 }
Winson Chunga12a2502010-12-20 14:41:35 -0800801
Winson Chung785d2eb2011-04-14 16:08:02 -0700802
803 // Restore the AppsCustomize tab
804 if (mAppsCustomizeTabHost != null) {
805 String curTab = savedState.getString("apps_customize_currentTab");
806 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700807 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700808 mAppsCustomizeContent.setContentType(
809 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
810 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700811 mAppsCustomizeContent.loadAssociatedPages(
812 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700813 }
814
Winson Chung5afbf7b2011-07-25 11:53:08 -0700815 int currentIndex = savedState.getInt("apps_customize_currentIndex");
816 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 }
819
820 /**
821 * Finds all the views we need and configure them properly.
822 */
823 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700824 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400825
Winson Chung4c98d922011-05-31 16:50:48 -0700826 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
827 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800828 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
829 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830
Winson Chung4c98d922011-05-31 16:50:48 -0700831 // Setup the drag layer
832 mDragLayer.setup(this, dragController);
833
Winson Chung3d503fb2011-07-13 17:25:49 -0700834 // Setup the hotseat
835 mHotseat = (Hotseat) findViewById(R.id.hotseat);
836 if (mHotseat != null) {
837 mHotseat.setup(this);
838 }
839
Winson Chung4c98d922011-05-31 16:50:48 -0700840 // Setup the workspace
841 mWorkspace.setHapticFeedbackEnabled(false);
842 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700843 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700844 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700845
Winson Chungf0ea4d32011-06-06 14:27:16 -0700846 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700847 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700848
Winson Chungf0ea4d32011-06-06 14:27:16 -0700849 // Setup AppsCustomize
850 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
851 findViewById(R.id.apps_customize_pane);
852 mAppsCustomizeContent = (AppsCustomizePagedView)
853 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700854 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700855 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400856
Michael Jurka5130e402011-10-13 04:55:35 -0700857 // Get the all apps button
858 mAllAppsButton = findViewById(R.id.all_apps_button);
859 if (mAllAppsButton != null) {
860 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
861 @Override
862 public boolean onTouch(View v, MotionEvent event) {
863 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
864 onTouchDownAllAppsButton(v);
865 }
866 return false;
867 }
868 });
869 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700870 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700871 dragController.setDragScoller(mWorkspace);
872 dragController.setScrollView(mDragLayer);
873 dragController.setMoveTarget(mWorkspace);
874 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700875 if (mSearchDropTargetBar != null) {
876 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800877 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 }
879
880 /**
881 * Creates a view representing a shortcut.
882 *
883 * @param info The data structure describing the shortcut.
884 *
885 * @return A View inflated from R.layout.application.
886 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800887 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700889 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 }
891
892 /**
893 * Creates a view representing a shortcut inflated from the specified resource.
894 *
895 * @param layoutResId The id of the XML layout used to create the shortcut.
896 * @param parent The group the shortcut belongs to.
897 * @param info The data structure describing the shortcut.
898 *
899 * @return A View inflated from layoutResId.
900 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800901 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800902 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
903 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 return favorite;
906 }
907
908 /**
909 * Add an application shortcut to the workspace.
910 *
911 * @param data The intent describing the application.
912 * @param cellInfo The position on screen where to create the shortcut.
913 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700914 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700915 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700916 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700917
Adam Cohenfbba09b2011-07-18 21:43:05 -0700918 // First we check if we already know the exact location where we want to add this item.
919 if (cellX >= 0 && cellY >= 0) {
920 cellXY[0] = cellX;
921 cellXY[1] = cellY;
922 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700923 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700924 return;
925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800927 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800928
Romain Guy73b979d2009-06-09 12:57:21 -0700929 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800930 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800932 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700933 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700934 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800935 } else {
936 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 }
938 }
Romain Guycbb89e42009-06-08 15:52:54 -0700939
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 /**
941 * Add a shortcut to the workspace.
942 *
943 * @param data The intent describing the shortcut.
944 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700946 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
947 int cellY) {
948 int[] cellXY = mTmpAddItemCellCoordinates;
949 int[] touchXY = mPendingAddInfo.dropPos;
950 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700951
Michael Jurkad3ef3062010-11-23 16:23:58 -0800952 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700953
Adam Cohen558baaf2011-08-15 15:22:57 -0700954 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800955 if (info == null) {
956 return;
957 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700958 final View view = createShortcut(info);
959
Adam Cohenfbba09b2011-07-18 21:43:05 -0700960 // First we check if we already know the exact location where we want to add this item.
961 if (cellX >= 0 && cellY >= 0) {
962 cellXY[0] = cellX;
963 cellXY[1] = cellY;
964 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700965
966 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800967 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700968 true, null,null)) {
969 return;
970 }
971 DragObject dragObject = new DragObject();
972 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -0800973 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
974 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -0700975 return;
976 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700977 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800978 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700979 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800980 foundCellSpan = (result != null);
981 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700982 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800983 }
984
985 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -0700986 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700987 return;
988 }
989
Adam Cohen558baaf2011-08-15 15:22:57 -0700990 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991
992 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700993 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
994 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 }
996 }
997
Adam Cohen2f093b62012-04-30 18:59:53 -0700998 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
999 int minHeight) {
1000 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001001 // We want to account for the extra amount of padding that we are adding to the widget
1002 // to ensure that it gets the full amount of space that it has requested
1003 int requiredWidth = minWidth + padding.left + padding.right;
1004 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001005 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001006 }
1007
Adam Cohen2f093b62012-04-30 18:59:53 -07001008 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1009 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001010 }
1011
Adam Cohen2f093b62012-04-30 18:59:53 -07001012 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1013 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001014 }
1015
Adam Cohen2f093b62012-04-30 18:59:53 -07001016 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1017 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001018 }
1019
Adam Cohen2f093b62012-04-30 18:59:53 -07001020 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1021 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001022 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001023 }
1024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001026 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001028 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001029 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001031 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1032 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1033 if (appWidgetInfo == null) {
1034 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1035 }
Romain Guycbb89e42009-06-08 15:52:54 -07001036
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001037 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001038 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001039
Adam Cohen2f093b62012-04-30 18:59:53 -07001040 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1041 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001044 // We have saved the position to which the widget was dragged-- this really only matters
1045 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001046 int[] cellXY = mTmpAddItemCellCoordinates;
1047 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001048 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001049 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001050 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1051 cellXY[0] = mPendingAddInfo.cellX;
1052 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001053 spanXY[0] = mPendingAddInfo.spanX;
1054 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001055 foundCellSpan = true;
1056 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001057 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001058 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001059 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1060 spanXY[1], cellXY, finalSpan);
1061 spanXY[0] = finalSpan[0];
1062 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001063 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001064 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001065 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001066 }
1067
Michael Jurka0280c3b2010-09-17 15:00:07 -07001068 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001069 if (appWidgetId != -1) {
1070 // Deleting an app widget ID is a void call but writes to disk before returning
1071 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001072 new Thread("deleteAppWidgetId") {
1073 public void run() {
1074 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1075 }
1076 }.start();
1077 }
Winson Chung93eef082012-03-23 15:59:27 -07001078 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001079 return;
1080 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001082 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001083 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1084 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001085 launcherInfo.spanX = spanXY[0];
1086 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001087 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1088 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001091 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092
1093 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001094 if (hostView == null) {
1095 // Perform actual inflation because we're live
1096 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1097 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1098 } else {
1099 // The AppWidgetHostView has already been inflated and instantiated
1100 launcherInfo.hostView = hostView;
1101 }
Romain Guycbb89e42009-06-08 15:52:54 -07001102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001104 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohena897f392012-04-27 18:12:05 -07001105 AppWidgetResizeFrame.updateWidgetSizeRanges(launcherInfo.hostView,
1106 this, launcherInfo.spanX, launcherInfo.spanY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001107 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001108 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001109
1110 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001112 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 }
Romain Guycbb89e42009-06-08 15:52:54 -07001114
Adam Cohended9f8d2010-11-03 13:25:16 -07001115 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1116 @Override
1117 public void onReceive(Context context, Intent intent) {
1118 final String action = intent.getAction();
1119 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1120 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001121 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001122 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001123
Winson Chungc100e8e2011-08-09 16:02:43 -07001124 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001125 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001126 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1127 mAppsCustomizeTabHost.reset();
1128 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001129 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001130 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1131 mUserPresent = true;
1132 updateRunning();
1133 }
1134 }
1135 };
1136
1137 @Override
1138 public void onAttachedToWindow() {
1139 super.onAttachedToWindow();
1140
1141 // Listen for broadcasts related to user-presence
1142 final IntentFilter filter = new IntentFilter();
1143 filter.addAction(Intent.ACTION_SCREEN_OFF);
1144 filter.addAction(Intent.ACTION_USER_PRESENT);
1145 registerReceiver(mReceiver, filter);
1146
Adam Cohend113e0c2010-11-11 10:48:05 -08001147 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001148 mVisible = true;
1149 }
1150
1151 @Override
1152 public void onDetachedFromWindow() {
1153 super.onDetachedFromWindow();
1154 mVisible = false;
1155
Adam Cohend113e0c2010-11-11 10:48:05 -08001156 if (mAttached) {
1157 unregisterReceiver(mReceiver);
1158 mAttached = false;
1159 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001160 updateRunning();
1161 }
1162
1163 public void onWindowVisibilityChanged(int visibility) {
1164 mVisible = visibility == View.VISIBLE;
1165 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001166 // The following code used to be in onResume, but it turns out onResume is called when
1167 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1168 // is a more appropriate event to handle
1169 if (mVisible) {
1170 mAppsCustomizeTabHost.onWindowVisible();
1171 if (!mWorkspaceLoading) {
1172 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001173 // We want to let Launcher draw itself at least once before we force it to build
1174 // layers on all the workspace pages, so that transitioning to Launcher from other
1175 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1176 // a true draw so we wait until the second preDraw call to be safe
1177 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001178 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001179 // We delay the layer building a bit in order to give
1180 // other message processing a time to run. In particular
1181 // this avoids a delay in hiding the IME if it was
1182 // currently shown, because doing that may involve
1183 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001184 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurka6ee21d22012-02-21 18:20:27 -08001185 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001186 return true;
1187 }
1188 });
1189 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001190 // When Launcher comes back to foreground, a different Activity might be responsible for
1191 // the app market intent, so refresh the icon
1192 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001193 clearTypedText();
1194 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001195 }
1196
1197 private void sendAdvanceMessage(long delay) {
1198 mHandler.removeMessages(ADVANCE_MSG);
1199 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1200 mHandler.sendMessageDelayed(msg, delay);
1201 mAutoAdvanceSentTime = System.currentTimeMillis();
1202 }
1203
1204 private void updateRunning() {
1205 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1206 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1207 mAutoAdvanceRunning = autoAdvanceRunning;
1208 if (autoAdvanceRunning) {
1209 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1210 sendAdvanceMessage(delay);
1211 } else {
1212 if (!mWidgetsToAdvance.isEmpty()) {
1213 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1214 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1215 }
1216 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001217 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001218 }
1219 }
1220 }
1221
1222 private final Handler mHandler = new Handler() {
1223 @Override
1224 public void handleMessage(Message msg) {
1225 if (msg.what == ADVANCE_MSG) {
1226 int i = 0;
1227 for (View key: mWidgetsToAdvance.keySet()) {
1228 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1229 final int delay = mAdvanceStagger * i;
1230 if (v instanceof Advanceable) {
1231 postDelayed(new Runnable() {
1232 public void run() {
1233 ((Advanceable) v).advance();
1234 }
1235 }, delay);
1236 }
1237 i++;
1238 }
1239 sendAdvanceMessage(mAdvanceInterval);
1240 }
1241 }
1242 };
1243
1244 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001245 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001246 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1247 if (v instanceof Advanceable) {
1248 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001249 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001250 updateRunning();
1251 }
1252 }
1253
1254 void removeWidgetToAutoAdvance(View hostView) {
1255 if (mWidgetsToAdvance.containsKey(hostView)) {
1256 mWidgetsToAdvance.remove(hostView);
1257 updateRunning();
1258 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001259 }
1260
Joe Onorato9c1289c2009-08-17 11:03:03 -04001261 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001262 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001263 launcherInfo.hostView = null;
1264 }
1265
Winson Chung93eef082012-03-23 15:59:27 -07001266 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1267 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1268 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001269 }
1270
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001271 public LauncherAppWidgetHost getAppWidgetHost() {
1272 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 }
Romain Guycbb89e42009-06-08 15:52:54 -07001274
Winson Chunga9abd0e2010-10-27 17:18:37 -07001275 public LauncherModel getModel() {
1276 return mModel;
1277 }
1278
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001279 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001280 getWindow().closeAllPanels();
1281
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001282 // Whatever we were doing is hereby canceled.
1283 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001284 }
1285
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 @Override
1287 protected void onNewIntent(Intent intent) {
1288 super.onNewIntent(intent);
1289
1290 // Close the menu
1291 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001292 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001293 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001294
Joe Onorato14f122b2009-11-19 14:06:36 -08001295 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1296 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001297
Adam Cohenac56cff2011-09-28 20:45:37 -07001298 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001299 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001300 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001301 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1302 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001303 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001304 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001305
1306 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001307 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001308 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001309
Joe Onorato3a8820b2009-11-10 15:06:42 -08001310 final View v = getWindow().peekDecorView();
1311 if (v != null && v.getWindowToken() != null) {
1312 InputMethodManager imm = (InputMethodManager)getSystemService(
1313 INPUT_METHOD_SERVICE);
1314 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001316
Michael Jurka35aa14d2011-07-07 17:01:08 -07001317 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001318 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001319 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
1322 }
1323
1324 @Override
1325 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1326 // Do not call super here
1327 mSavedInstanceState = savedInstanceState;
1328 }
1329
1330 @Override
1331 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001332 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001333 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334
Michael Jurka883f55b2010-10-21 15:47:14 -07001335 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001336 // We close any open folder since it will not be re-opened, and we need to make sure
1337 // this state is reflected.
1338 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339
Winson Chung3d503fb2011-07-13 17:25:49 -07001340 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1341 mWaitingForResult) {
1342 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1343 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1344 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1345 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
1347
1348 if (mFolderInfo != null && mWaitingForResult) {
1349 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1350 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1351 }
Michael Jurka946ad472010-07-09 18:05:18 -07001352
Winson Chung785d2eb2011-04-14 16:08:02 -07001353 // Save the current AppsCustomize tab
1354 if (mAppsCustomizeTabHost != null) {
1355 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1356 if (currentTabTag != null) {
1357 outState.putString("apps_customize_currentTab", currentTabTag);
1358 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001359 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1360 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001361 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 }
1363
1364 @Override
1365 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001367
Winson Chunge7a03942011-08-05 15:05:12 -07001368 // Remove all pending runnables
1369 mHandler.removeMessages(ADVANCE_MSG);
1370 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001371 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001372
Winson Chungcd2b0142011-06-08 16:02:26 -07001373 // Stop callbacks from LauncherModel
1374 LauncherApplication app = ((LauncherApplication) getApplication());
1375 mModel.stopLoader();
1376 app.setLauncher(null);
1377
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001379 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001381 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001383 mAppWidgetHost = null;
1384
1385 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386
1387 TextKeyListener.getInstance().release();
1388
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389
Winson Chung3d503fb2011-07-13 17:25:49 -07001390 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001391
1392 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001393 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001394
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001395 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001396 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1397 mWorkspace.removeAllViews();
1398 mWorkspace = null;
1399 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001400
1401 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 }
1403
Adam Cohena9cf38f2011-05-02 15:36:58 -07001404 public DragController getDragController() {
1405 return mDragController;
1406 }
1407
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 @Override
1409 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001410 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 super.startActivityForResult(intent, requestCode);
1412 }
1413
Winson Chung70d72102011-08-12 11:24:35 -07001414 /**
1415 * Indicates that we want global search for this activity by setting the globalSearch
1416 * argument for {@link #startSearch} to true.
1417 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001419 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001421
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001422 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001423
Karl Rosaen138a0412009-04-23 19:00:21 -07001424 if (initialQuery == null) {
1425 // Use any text typed in the launcher as the initial query
1426 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001427 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 if (appSearchData == null) {
1429 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001430 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001432 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001433
Karl Rosaen138a0412009-04-23 19:00:21 -07001434 final SearchManager searchManager =
1435 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001436 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001437 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 }
1439
Winson Chung70d72102011-08-12 11:24:35 -07001440 @Override
1441 public boolean onCreateOptionsMenu(Menu menu) {
1442 if (isWorkspaceLocked()) {
1443 return false;
1444 }
1445
1446 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001447
1448 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1449 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1450 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001451 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1452 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1453 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001454 String helpUrl = getString(R.string.help_url);
1455 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001456 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1457 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1458
Winson Chung70d72102011-08-12 11:24:35 -07001459 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1460 .setIcon(android.R.drawable.ic_menu_gallery)
1461 .setAlphabeticShortcut('W');
1462 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1463 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001464 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001465 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001466 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1467 .setIcon(android.R.drawable.ic_menu_preferences)
1468 .setIntent(settings)
1469 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001470 if (!helpUrl.isEmpty()) {
1471 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1472 .setIcon(android.R.drawable.ic_menu_help)
1473 .setIntent(help)
1474 .setAlphabeticShortcut('H');
1475 }
Winson Chung70d72102011-08-12 11:24:35 -07001476 return true;
1477 }
1478
1479 @Override
1480 public boolean onPrepareOptionsMenu(Menu menu) {
1481 super.onPrepareOptionsMenu(menu);
1482
1483 if (mAppsCustomizeTabHost.isTransitioning()) {
1484 return false;
1485 }
1486 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1487 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1488
1489 return true;
1490 }
1491
1492 @Override
1493 public boolean onOptionsItemSelected(MenuItem item) {
1494 switch (item.getItemId()) {
1495 case MENU_WALLPAPER_SETTINGS:
1496 startWallpaper();
1497 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001498 }
1499
1500 return super.onOptionsItemSelected(item);
1501 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001503 @Override
1504 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001505 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001506 // Use a custom animation for launching search
1507 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001508 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001509 }
1510
Joe Onorato9c1289c2009-08-17 11:03:03 -04001511 public boolean isWorkspaceLocked() {
1512 return mWorkspaceLoading || mWaitingForResult;
1513 }
1514
Michael Jurka0280c3b2010-09-17 15:00:07 -07001515 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001516 mPendingAddInfo.container = ItemInfo.NO_ID;
1517 mPendingAddInfo.screen = -1;
1518 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1519 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001520 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001521 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001522 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001523
Michael Jurkaaf442092010-06-10 17:01:57 -07001524 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001525 // TODO: catch bad widget exception when sent
1526 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001527 // TODO: Is this log message meaningful?
1528 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001529 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001530 }
1531
Adam Cohened66b2b2012-01-23 17:28:51 -08001532 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1533 final AppWidgetProviderInfo appWidget = info.info;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001534 if (appWidget.configure != null) {
1535 // Launch over to configure widget, if needed
1536 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1537 intent.setComponent(appWidget.configure);
1538 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001539 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001540 mWidgetBeingBoundOrConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001542 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001543 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001544 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001545 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 }
1547 }
Romain Guycbb89e42009-06-08 15:52:54 -07001548
Adam Cohenfbba09b2011-07-18 21:43:05 -07001549 /**
1550 * Process a shortcut drop.
1551 *
1552 * @param componentName The name of the component
1553 * @param screen The screen where it should be added
1554 * @param cell The cell it should be added to, optional
1555 * @param position The location on the screen where it was dropped, optional
1556 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001557 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1558 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001559 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001560 mPendingAddInfo.container = container;
1561 mPendingAddInfo.screen = screen;
1562 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001563
1564 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001565 mPendingAddInfo.cellX = cell[0];
1566 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001567 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001568
1569 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1570 createShortcutIntent.setComponent(componentName);
1571 processShortcut(createShortcutIntent);
1572 }
1573
Adam Cohenfbba09b2011-07-18 21:43:05 -07001574 /**
1575 * Process a widget drop.
1576 *
1577 * @param info The PendingAppWidgetInfo of the widget being added.
1578 * @param screen The screen where it should be added
1579 * @param cell The cell it should be added to, optional
1580 * @param position The location on the screen where it was dropped, optional
1581 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001582 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001583 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001584 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001585 mPendingAddInfo.container = info.container = container;
1586 mPendingAddInfo.screen = info.screen = screen;
1587 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001588 mPendingAddInfo.minSpanX = info.minSpanX;
1589 mPendingAddInfo.minSpanY = info.minSpanY;
1590
Adam Cohenfbba09b2011-07-18 21:43:05 -07001591 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001592 mPendingAddInfo.cellX = cell[0];
1593 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001594 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001595 if (span != null) {
1596 mPendingAddInfo.spanX = span[0];
1597 mPendingAddInfo.spanY = span[1];
1598 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001599
Adam Cohened66b2b2012-01-23 17:28:51 -08001600 AppWidgetHostView hostView = info.boundWidget;
1601 int appWidgetId;
1602 if (hostView != null) {
1603 appWidgetId = hostView.getAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001604 addAppWidgetImpl(appWidgetId, info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001605 } else {
1606 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001607 mWidgetBeingBoundOrConfigured = info;
1608 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
1609 addAppWidgetImpl(appWidgetId, info);
1610 } else {
1611 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1612 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1613 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1614 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1615 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001616 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001617 }
1618
Joe Onoratodeb98af2010-02-19 14:59:39 -08001619 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001620 // Handle case where user selected "Applications"
1621 String applicationName = getResources().getString(R.string.group_applications);
1622 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001623
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001624 if (applicationName != null && applicationName.equals(shortcutName)) {
1625 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1626 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001627
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001628 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1629 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001630 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001631 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001632 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001633 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001634 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 }
1636
Winson Chung24ab2f12010-09-16 14:10:47 -07001637 void processWallpaper(Intent intent) {
1638 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1639 }
1640
Winson Chung3d503fb2011-07-13 17:25:49 -07001641 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1642 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001643 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 folderInfo.title = getText(R.string.folder_name);
1645
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001647 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1648 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001649 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650
1651 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001652 FolderIcon newFolder =
1653 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1654 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1655 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001656 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 }
Romain Guycbb89e42009-06-08 15:52:54 -07001658
Joe Onorato9c1289c2009-08-17 11:03:03 -04001659 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001660 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001661 }
1662
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001664 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001666 Intent chooser = Intent.createChooser(pickWallpaper,
1667 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001668 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1669 // Removed in Eclair MR1
1670// WallpaperManager wm = (WallpaperManager)
1671// getSystemService(Context.WALLPAPER_SERVICE);
1672// WallpaperInfo wi = wm.getWallpaperInfo();
1673// if (wi != null && wi.getSettingsActivity() != null) {
1674// LabeledIntent li = new LabeledIntent(getPackageName(),
1675// R.string.configure_wallpaper, 0);
1676// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1677// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1678// }
Mike Clerona0618e42009-10-22 13:55:21 -07001679 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 }
1681
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001682 /**
1683 * Registers various content observers. The current implementation registers
1684 * only a favorites observer to keep track of the favorites applications.
1685 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001686 private void registerContentObservers() {
1687 ContentResolver resolver = getContentResolver();
1688 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1689 true, mWidgetObserver);
1690 }
1691
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 @Override
1693 public boolean dispatchKeyEvent(KeyEvent event) {
1694 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1695 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001697 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001698 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001699 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001700 dumpState();
1701 return true;
1702 }
1703 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001704 }
1705 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1706 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001707 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 return true;
1709 }
1710 }
1711
1712 return super.dispatchKeyEvent(event);
1713 }
1714
Joe Onorato88ec0992009-11-19 13:16:06 -08001715 @Override
1716 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001717 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001718 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001719 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001720 Folder openFolder = mWorkspace.getOpenFolder();
1721 if (openFolder.isEditingName()) {
1722 openFolder.dismissEditingName();
1723 } else {
1724 closeFolder();
1725 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001726 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001727 mWorkspace.exitWidgetResizeMode();
1728
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001729 // Back button is a no-op here, but give at least some feedback for the button press
1730 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001731 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001732 }
1733
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001735 * Re-listen when widgets are reset.
1736 */
1737 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001738 if (mAppWidgetHost != null) {
1739 mAppWidgetHost.startListening();
1740 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001741 }
1742
1743 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001744 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1745 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001747 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001748 if (mModel != null) {
1749 mModel.unbindWorkspaceItems();
1750 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001752
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 /**
1754 * Launches the intent referred by the clicked shortcut.
1755 *
1756 * @param v The view representing the clicked shortcut.
1757 */
1758 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001759 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1760 // view has detached (it's possible for this to happen if the view is removed mid touch).
1761 if (v.getWindowToken() == null) {
1762 return;
1763 }
1764
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001765 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001766 return;
1767 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001768
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001770 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001772 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001773 int[] pos = new int[2];
1774 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001775 intent.setSourceBounds(new Rect(pos[0], pos[1],
1776 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001777
1778 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001779
1780 if (success && v instanceof BubbleTextView) {
1781 mWaitingForResume = (BubbleTextView) v;
1782 mWaitingForResume.setStayPressed(true);
1783 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001785 if (v instanceof FolderIcon) {
1786 FolderIcon fi = (FolderIcon) v;
1787 handleFolderClick(fi);
1788 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001789 } else if (v == mAllAppsButton) {
1790 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001791 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001792 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001793 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001794 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 }
1796 }
1797
Michael Jurka0e260592010-06-30 17:07:39 -07001798 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001799 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001800 // clicking anywhere on the workspace causes the customization drawer to slide down
1801 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001802 return false;
1803 }
1804
Michael Jurkaaf442092010-06-10 17:01:57 -07001805 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001806 * Event handler for the search button
1807 *
1808 * @param v The view that was clicked.
1809 */
1810 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001811 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1812
Amith Yamasania135ba82011-08-09 17:42:01 -07001813 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001814 }
1815
1816 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001817 * Event handler for the voice button
1818 *
1819 * @param v The view that was clicked.
1820 */
1821 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001822 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001823
Michael Jurkaae65ee32012-04-30 11:45:54 -07001824 try {
1825 final SearchManager searchManager =
1826 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1827 ComponentName activityName = searchManager.getGlobalSearchActivity();
1828 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1829 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1830 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1831 if (activityName != null) {
1832 intent.setPackage(activityName.getPackageName());
1833 }
1834 startActivitySafely(null, intent, "onClickVoiceButton");
1835 } catch (ActivityNotFoundException e) {
1836 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1837 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1838 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1839 startActivitySafely(null, intent, "onClickVoiceButton");
1840 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001841 }
1842
1843 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001844 * Event handler for the "grid" button that appears on the home screen, which
1845 * enters all apps mode.
1846 *
1847 * @param v The view that was clicked.
1848 */
1849 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001850 showAllApps(true);
1851 }
1852
1853 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001854 // Provide the same haptic feedback that the system offers for virtual keys.
1855 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001856 }
1857
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001858 public void onClickAppMarketButton(View v) {
1859 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001860 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001861 } else {
1862 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001863 }
1864 }
1865
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001866 void startApplicationDetailsActivity(ComponentName componentName) {
1867 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001868 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1869 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001870 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001871 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001872 }
1873
Patrick Dubroy5539af72010-09-07 15:22:01 -07001874 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1875 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1876 // System applications cannot be installed. For now, show a toast explaining that.
1877 // We may give them the option of disabling apps this way.
1878 int messageId = R.string.uninstall_system_app_text;
1879 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1880 } else {
1881 String packageName = appInfo.componentName.getPackageName();
1882 String className = appInfo.componentName.getClassName();
1883 Intent intent = new Intent(
1884 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001885 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1886 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001887 startActivity(intent);
1888 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001889 }
1890
Winson Chungc7450e32012-04-17 17:34:08 -07001891 boolean startActivitySafely(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001893
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 try {
Winson Chung2672ff92012-05-04 16:22:30 -07001895 // Only launch using the new animation if the shortcut has not opted out (this is a
1896 // private contract between launcher and may be ignored in the future).
1897 boolean useLaunchAnimation = (v != null) &&
1898 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
1899 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07001900 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1901 v.getMeasuredWidth(), v.getMeasuredHeight());
1902
1903 startActivity(intent, opts.toBundle());
1904 } else {
1905 startActivity(intent);
1906 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001907 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 } catch (ActivityNotFoundException e) {
1909 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001910 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 } catch (SecurityException e) {
1912 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001913 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001915 "or use the exported attribute for this activity. "
1916 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001918 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001920
Romain Guy8e633c52010-03-04 12:51:36 -08001921 void startActivityForResultSafely(Intent intent, int requestCode) {
1922 try {
1923 startActivityForResult(intent, requestCode);
1924 } catch (ActivityNotFoundException e) {
1925 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1926 } catch (SecurityException e) {
1927 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1928 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1929 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1930 "or use the exported attribute for this activity.", e);
1931 }
1932 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933
Adam Cohena9cf38f2011-05-02 15:36:58 -07001934 private void handleFolderClick(FolderIcon folderIcon) {
1935 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001936 Folder openFolder = mWorkspace.getFolderForTag(info);
1937
1938 // If the folder info reports that the associated folder is open, then verify that
1939 // it is actually opened. There have been a few instances where this gets out of sync.
1940 if (info.opened && openFolder == null) {
1941 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1942 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1943 info.opened = false;
1944 }
1945
Adam Cohena9cf38f2011-05-02 15:36:58 -07001946 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 // Close any open folder
1948 closeFolder();
1949 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001950 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 } else {
1952 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 int folderScreen;
1954 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001955 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 // .. and close it
1957 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001958 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 // Close any folder open on the current screen
1960 closeFolder();
1961 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001962 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 }
1964 }
1965 }
1966 }
1967
Adam Cohen2801caf2011-05-13 20:57:39 -07001968 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001969 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001970 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1971 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1972 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1973
Adam Cohenc51934b2011-07-26 21:07:43 -07001974 FolderInfo info = (FolderInfo) fi.getTag();
1975 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1976 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001977 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1978 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001979 }
1980
Adam Cohen2801caf2011-05-13 20:57:39 -07001981 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1982 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1983 oa.start();
1984 }
1985
1986 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001987 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001988 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1989 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1990 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1991
Adam Cohenc51934b2011-07-26 21:07:43 -07001992 FolderInfo info = (FolderInfo) fi.getTag();
1993 CellLayout cl = null;
1994 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1995 cl = (CellLayout) fi.getParent().getParent();
1996 }
1997
1998 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001999 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2000 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002001 oa.addListener(new AnimatorListenerAdapter() {
2002 @Override
2003 public void onAnimationEnd(Animator animation) {
2004 if (layout != null) {
2005 layout.clearFolderLeaveBehind();
2006 }
2007 }
2008 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002009 oa.start();
2010 }
2011
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002013 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 * is animated relative to the specified View. If the View is null, no animation
2015 * is played.
2016 *
2017 * @param folderInfo The FolderInfo describing the folder to open.
2018 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002019 public void openFolder(FolderIcon folderIcon) {
2020 Folder folder = folderIcon.mFolder;
2021 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022
Adam Cohen2801caf2011-05-13 20:57:39 -07002023 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002024 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025
Adam Cohen4554ee12011-08-03 16:13:21 -07002026 // Just verify that the folder hasn't already been added to the DragLayer.
2027 // There was a one-off crash where the folder had a parent already.
2028 if (folder.getParent() == null) {
2029 mDragLayer.addView(folder);
2030 mDragController.addDropTarget((DropTarget) folder);
2031 } else {
2032 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2033 folder.getParent() + ").");
2034 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002035 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002036 }
2037
2038 public void closeFolder() {
2039 Folder folder = mWorkspace.getOpenFolder();
2040 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002041 if (folder.isEditingName()) {
2042 folder.dismissEditingName();
2043 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002044 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002045
2046 // Dismiss the folder cling
2047 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002048 }
2049 }
2050
2051 void closeFolder(Folder folder) {
2052 folder.getInfo().opened = false;
2053
2054 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2055 if (parent != null) {
2056 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2057 shrinkAndFadeInFolderIcon(fi);
2058 }
2059 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 }
2061
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002063
Patrick Dubroye708c522011-03-01 16:03:43 -08002064 if (mState != State.WORKSPACE) {
2065 return false;
2066 }
2067
Joe Onorato9c1289c2009-08-17 11:03:03 -04002068 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 return false;
2070 }
2071
2072 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002073 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 }
2075
Michael Jurka0280c3b2010-09-17 15:00:07 -07002076 resetAddInfo();
2077 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002079 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 return true;
2081 }
2082
Winson Chung3d503fb2011-07-13 17:25:49 -07002083 // The hotseat touch handling does not go through Workspace, and we always allow long press
2084 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002085 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002086 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2087 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002088 if (itemUnderLongClick == null) {
2089 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002090 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2091 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002092 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002094 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002096 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 }
2098 }
2099 }
2100 return true;
2101 }
2102
Winson Chung3d503fb2011-07-13 17:25:49 -07002103 boolean isHotseatLayout(View layout) {
2104 return mHotseat != null && layout != null &&
2105 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2106 }
2107 Hotseat getHotseat() {
2108 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002109 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002110 SearchDropTargetBar getSearchBar() {
2111 return mSearchDropTargetBar;
2112 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002113
Winson Chung3d503fb2011-07-13 17:25:49 -07002114 /**
2115 * Returns the CellLayout of the specified container at the specified screen.
2116 */
2117 CellLayout getCellLayout(long container, int screen) {
2118 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2119 if (mHotseat != null) {
2120 return mHotseat.getLayout();
2121 } else {
2122 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002123 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002124 } else {
2125 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002126 }
2127 }
2128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 Workspace getWorkspace() {
2130 return mWorkspace;
2131 }
2132
Daniel Sandler843e8602010-06-07 14:59:01 -04002133 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2134 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002135 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002136 }
2137
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002138 public boolean isAllAppsButtonRank(int rank) {
2139 return mHotseat.isAllAppsButtonRank(rank);
2140 }
2141
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002142 // AllAppsView.Watcher
2143 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002144 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002145 mWorkspace.setVisibility(View.GONE);
2146 }
2147 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002148
2149 /**
2150 * Helper method for the cameraZoomIn/cameraZoomOut animations
2151 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002152 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002153 * @param scaleFactor The scale factor used for the zoom
2154 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002155 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002156 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002157 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002158 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002159
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002160 void updateWallpaperVisibility(boolean visible) {
2161 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2162 int curflags = getWindow().getAttributes().flags
2163 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2164 if (wpflags != curflags) {
2165 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2166 }
2167 }
2168
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002169 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2170 if (v instanceof LauncherTransitionable) {
2171 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2172 }
2173 }
2174
Michael Jurkabed61d22012-02-14 22:51:29 -08002175 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2176 if (v instanceof LauncherTransitionable) {
2177 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2178 }
Winson Chung70442722012-02-10 15:43:22 -08002179
2180 // Update the workspace transition step as well
2181 dispatchOnLauncherTransitionStep(v, 0f);
2182 }
2183
2184 private void dispatchOnLauncherTransitionStep(View v, float t) {
2185 if (v instanceof LauncherTransitionable) {
2186 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2187 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002188 }
2189
2190 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2191 if (v instanceof LauncherTransitionable) {
2192 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2193 }
Winson Chung70442722012-02-10 15:43:22 -08002194
2195 // Update the workspace transition step as well
2196 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002197 }
2198
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002199 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002200 * Things to test when changing the following seven functions.
2201 * - Home from workspace
2202 * - from center screen
2203 * - from other screens
2204 * - Home from all apps
2205 * - from center screen
2206 * - from other screens
2207 * - Back from all apps
2208 * - from center screen
2209 * - from other screens
2210 * - Launch app from workspace and quit
2211 * - with back
2212 * - with home
2213 * - Launch app from all apps and quit
2214 * - with back
2215 * - with home
2216 * - Go to a screen that's not the default, then all
2217 * apps, and launch and app, and go back
2218 * - with back
2219 * -with home
2220 * - On workspace, long press power and go back
2221 * - with back
2222 * - with home
2223 * - On all apps, long press power and go back
2224 * - with back
2225 * - with home
2226 * - On workspace, power off
2227 * - On all apps, power off
2228 * - Launch an app and turn off the screen while in that app
2229 * - Go back with home key
2230 * - Go back with back key TODO: make this not go to workspace
2231 * - From all apps
2232 * - From workspace
2233 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2234 * - From all apps
2235 * - From the center workspace
2236 * - From another workspace
2237 */
2238
2239 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002240 * Zoom the camera out from the workspace to reveal 'toView'.
2241 * Assumes that the view to show is anchored at either the very top or very bottom
2242 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002243 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002244 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002245 if (mStateAnimation != null) {
2246 mStateAnimation.cancel();
2247 mStateAnimation = null;
2248 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002249 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002250
Winson Chungf0ea4d32011-06-06 14:27:16 -07002251 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2252 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2253 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002254 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002255 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002256 final int startDelay =
2257 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002258
Michael Jurkab3e22d92011-10-31 15:58:33 -07002259 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002260
Michael Jurkad74c9842011-07-10 12:44:21 -07002261 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002262 Animator workspaceAnim =
2263 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002264
2265 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002266 toView.setScaleX(scale);
2267 toView.setScaleY(scale);
2268 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2269 scaleAnim.
2270 scaleX(1f).scaleY(1f).
2271 setDuration(duration).
2272 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002273
Winson Chungf0ea4d32011-06-06 14:27:16 -07002274 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002275 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002276 final ObjectAnimator alphaAnim = ObjectAnimator
2277 .ofFloat(toView, "alpha", 0f, 1f)
2278 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002279 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002280 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2281 @Override
2282 public void onAnimationUpdate(ValueAnimator animation) {
2283 float t = (Float) animation.getAnimatedValue();
2284 dispatchOnLauncherTransitionStep(fromView, t);
2285 dispatchOnLauncherTransitionStep(toView, t);
2286 }
2287 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002288
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002289 // toView should appear right at the end of the workspace shrink
2290 // animation
2291 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002292 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002293 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002294
2295 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002296 boolean animationCancelled = false;
2297
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002298 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002299 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002300 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002301 // Prepare the position
2302 toView.setTranslationX(0.0f);
2303 toView.setTranslationY(0.0f);
2304 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002305 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002306 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002307 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002308 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002309 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2310 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002311
2312 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2313 // Hide the workspace scrollbar
2314 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002315 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002316 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002317 if (!animationCancelled) {
2318 updateWallpaperVisibility(false);
2319 }
2320 }
2321
Adam Cohencff6af82011-09-13 14:51:53 -07002322 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002323 public void onAnimationCancel(Animator animation) {
2324 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002325 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002326 });
2327
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002328 if (workspaceAnim != null) {
2329 mStateAnimation.play(workspaceAnim);
2330 }
Michael Jurka1899a362011-11-03 13:50:45 -07002331
2332 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002333 final ViewTreeObserver observer;
2334
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002335 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2336 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002337
2338 // If any of the objects being animated haven't been measured/laid out
2339 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002340 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002341 (mWorkspace.getMeasuredWidth() == 0) ||
2342 (toView.getMeasuredWidth() == 0)) {
2343 observer = mWorkspace.getViewTreeObserver();
2344 delayAnim = true;
2345 } else {
2346 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002347 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002348
2349 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002350 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002351 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2352 public void onGlobalLayout() {
2353 mWorkspace.post(new Runnable() {
2354 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002355 // Check that mStateAnimation hasn't changed while
2356 // we waited for a layout pass
2357 if (mStateAnimation == stateAnimation) {
2358 // Need to update pivots for zoom if layout changed
2359 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002360 dispatchOnLauncherTransitionStart(fromView, animated, false);
2361 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka49779a12012-01-16 04:10:08 -08002362 mStateAnimation.start();
2363 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002364 }
2365 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002366 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002367 }
2368 };
2369 observer.addOnGlobalLayoutListener(delayedStart);
2370 } else {
2371 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002372 dispatchOnLauncherTransitionStart(fromView, animated, false);
2373 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka1899a362011-11-03 13:50:45 -07002374 mStateAnimation.start();
2375 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002376 } else {
2377 toView.setTranslationX(0.0f);
2378 toView.setTranslationY(0.0f);
2379 toView.setScaleX(1.0f);
2380 toView.setScaleY(1.0f);
2381 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002382 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002383
Michael Jurkabed61d22012-02-14 22:51:29 -08002384 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2385 // Hide the workspace scrollbar
2386 mWorkspace.hideScrollingIndicator(true);
2387 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002388 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002389 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002390 dispatchOnLauncherTransitionStart(fromView, animated, false);
2391 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002392 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002393 dispatchOnLauncherTransitionStart(toView, animated, false);
2394 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002395 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002396 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002397 }
2398
2399 /**
2400 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002401 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002402 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002403 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002404 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2405 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002406
Michael Jurkab3e22d92011-10-31 15:58:33 -07002407 if (mStateAnimation != null) {
2408 mStateAnimation.cancel();
2409 mStateAnimation = null;
2410 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002411 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002412
Winson Chungf0ea4d32011-06-06 14:27:16 -07002413 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002414 final int fadeOutDuration =
2415 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002416 final float scaleFactor = (float)
2417 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2418 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002419 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002420 Animator workspaceAnim = null;
2421
2422 if (toState == State.WORKSPACE) {
2423 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2424 workspaceAnim = mWorkspace.getChangeStateAnimation(
2425 Workspace.State.NORMAL, animated, stagger);
2426 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2427 workspaceAnim = mWorkspace.getChangeStateAnimation(
2428 Workspace.State.SPRING_LOADED, animated);
2429 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002430
Michael Jurkab3e22d92011-10-31 15:58:33 -07002431 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002432 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002433 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002434 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002435 final LauncherViewPropertyAnimator scaleAnim =
2436 new LauncherViewPropertyAnimator(fromView);
2437 scaleAnim.
2438 scaleX(scaleFactor).scaleY(scaleFactor).
2439 setDuration(duration).
2440 setInterpolator(new Workspace.ZoomInInterpolator());
2441
2442 final ObjectAnimator alphaAnim = ObjectAnimator
2443 .ofFloat(fromView, "alpha", 1f, 0f)
2444 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002445 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002446 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2447 @Override
2448 public void onAnimationUpdate(ValueAnimator animation) {
2449 float t = 1f - (Float) animation.getAnimatedValue();
2450 dispatchOnLauncherTransitionStep(fromView, t);
2451 dispatchOnLauncherTransitionStep(toView, t);
2452 }
2453 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002454
Michael Jurkabed61d22012-02-14 22:51:29 -08002455 mStateAnimation = new AnimatorSet();
2456
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002457 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2458 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002459
2460 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002461 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002462 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002463 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002464 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002465 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2466 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002467 if (mWorkspace != null) {
2468 mWorkspace.hideScrollingIndicator(false);
2469 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002470 if (onCompleteRunnable != null) {
2471 onCompleteRunnable.run();
2472 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002473 }
2474 });
2475
Winson Chungf0ea4d32011-06-06 14:27:16 -07002476 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002477 if (workspaceAnim != null) {
2478 mStateAnimation.play(workspaceAnim);
2479 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002480 dispatchOnLauncherTransitionStart(fromView, animated, true);
2481 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002482 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002483 } else {
2484 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002485 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002486 dispatchOnLauncherTransitionStart(fromView, animated, true);
2487 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002488 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002489 dispatchOnLauncherTransitionStart(toView, animated, true);
2490 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002491 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002492 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002493 }
2494
Michael Jurkae326f182011-11-21 14:05:46 -08002495 @Override
2496 public void onTrimMemory(int level) {
2497 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002498 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002499 mAppsCustomizeTabHost.onTrimMemory();
2500 }
2501 }
2502
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002503 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002504 showWorkspace(animated, null);
2505 }
2506
2507 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002508 if (mState != State.WORKSPACE) {
2509 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002510 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002511
2512 // Show the search bar and hotseat
2513 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002514 // We only need to animate in the dock divider if we're going from spring loaded mode
2515 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002516
2517 // Set focus to the AppsCustomize button
2518 if (mAllAppsButton != null) {
2519 mAllAppsButton.requestFocus();
2520 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002521 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002522
Michael Jurkab737ee62011-11-15 15:57:22 -08002523 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002524
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002525 // Change the state *after* we've called all the transition code
2526 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002527
Winson Chung5fb63472011-02-02 17:03:37 -08002528 // Resume the auto-advance of widgets
2529 mUserPresent = true;
2530 updateRunning();
2531
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002532 // send an accessibility event to announce the context change
2533 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002534 }
2535
Michael Jurkab3e22d92011-10-31 15:58:33 -07002536 void showAllApps(boolean animated) {
2537 if (mState != State.WORKSPACE) return;
2538
2539 showAppsCustomizeHelper(animated, false);
2540 mAppsCustomizeTabHost.requestFocus();
2541
2542 // Hide the search bar and hotseat
2543 mSearchDropTargetBar.hideSearchBar(animated);
2544
2545 // Change the state *after* we've called all the transition code
2546 mState = State.APPS_CUSTOMIZE;
2547
2548 // Pause the auto-advance of widgets until we are out of AllApps
2549 mUserPresent = false;
2550 updateRunning();
2551 closeFolder();
2552
2553 // Send an accessibility event to announce the context change
2554 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2555 }
2556
Adam Cohen7777d962011-08-18 18:58:38 -07002557 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002558 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002559 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002560 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002561 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002562 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002563 }
Adam Cohen7777d962011-08-18 18:58:38 -07002564
Adam Cohened66b2b2012-01-23 17:28:51 -08002565 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2566 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002567 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2568
Winson Chunge7a03942011-08-05 15:05:12 -07002569 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002570 @Override
2571 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002572 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002573 // Before we show workspace, hide all apps again because
2574 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2575 // clean up our state transition functions
2576 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002577 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002578 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002579 } else {
2580 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002581 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002582 }
2583 }, (extendedDelay ?
2584 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2585 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2586 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002587
Michael Jurkad3ef3062010-11-23 16:23:58 -08002588 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002589 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002590 final boolean animated = true;
2591 final boolean springLoaded = true;
2592 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002593 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002594 }
2595 // Otherwise, we are not in spring loaded mode, so don't do anything.
2596 }
2597
Michael Jurkab737ee62011-11-15 15:57:22 -08002598 void hideDockDivider() {
2599 if (mQsbDivider != null && mDockDivider != null) {
2600 mQsbDivider.setVisibility(View.INVISIBLE);
2601 mDockDivider.setVisibility(View.INVISIBLE);
2602 }
2603 }
2604
2605 void showDockDivider(boolean animated) {
2606 if (mQsbDivider != null && mDockDivider != null) {
2607 mQsbDivider.setVisibility(View.VISIBLE);
2608 mDockDivider.setVisibility(View.VISIBLE);
2609 if (mDividerAnimator != null) {
2610 mDividerAnimator.cancel();
2611 mQsbDivider.setAlpha(1f);
2612 mDockDivider.setAlpha(1f);
2613 mDividerAnimator = null;
2614 }
2615 if (animated) {
2616 mDividerAnimator = new AnimatorSet();
2617 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2618 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2619 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2620 mDividerAnimator.start();
2621 }
2622 }
2623 }
2624
Michael Jurkab3e22d92011-10-31 15:58:33 -07002625 void lockAllApps() {
2626 // TODO
2627 }
2628
2629 void unlockAllApps() {
2630 // TODO
2631 }
2632
Adam Cohenfc53cd22011-07-20 15:45:11 -07002633 public boolean isAllAppsCustomizeOpen() {
2634 return mState == State.APPS_CUSTOMIZE;
2635 }
2636
Winson Chungf0ea4d32011-06-06 14:27:16 -07002637 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002638 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002639 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002640 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002641 if (!LauncherApplication.isScreenLarge()) {
2642 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002643 if (mHotseat.getAlpha() != 1f) {
2644 int duration = mSearchDropTargetBar.getTransitionInDuration();
2645 mHotseat.animate().alpha(1f).setDuration(duration);
2646 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002647 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002648 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002649 }
2650 }
2651 }
2652
2653 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002654 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002655 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002656 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002657 if (!LauncherApplication.isScreenLarge()) {
2658 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002659 if (mHotseat.getAlpha() != 0f) {
2660 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2661 mHotseat.animate().alpha(0f).setDuration(duration);
2662 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002663 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002664 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002665 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002666 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002667 }
2668
Patrick Dubroy5f445422011-02-18 14:35:21 -08002669 /**
2670 * Add an item from all apps or customize onto the given workspace screen.
2671 * If layout is null, add to the current screen.
2672 */
2673 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002674 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002675 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002676 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002677 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002678
Winson Chungdff8ebb2011-09-08 17:25:31 -07002679 /** Maps the current orientation to an index for referencing orientation correct global icons */
2680 private int getCurrentOrientationIndexForGlobalIcons() {
2681 // default - 0, landscape - 1
2682 switch (getResources().getConfiguration().orientation) {
2683 case Configuration.ORIENTATION_LANDSCAPE:
2684 return 1;
2685 default:
2686 return 0;
2687 }
2688 }
2689
Michael Jurkaae65ee32012-04-30 11:45:54 -07002690 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002691 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002692 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002693 // Look for the toolbar icon specified in the activity meta-data
2694 Bundle metaData = packageManager.getActivityInfo(
2695 activityName, PackageManager.GET_META_DATA).metaData;
2696 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002697 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002698 if (iconResId != 0) {
2699 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002700 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002701 }
2702 }
2703 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002704 // This can happen if the activity defines an invalid drawable
2705 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2706 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002707 } catch (Resources.NotFoundException nfe) {
2708 // This can happen if the activity defines an invalid drawable
2709 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2710 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002711 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002712 return null;
2713 }
2714
2715 // if successful in getting icon, return it; otherwise, set button to use default drawable
2716 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002717 int buttonId, ComponentName activityName, int fallbackDrawableId,
2718 String toolbarResourceName) {
2719 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002720 Resources r = getResources();
2721 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2722 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002723
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002724 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002725 // If we were unable to find the icon via the meta-data, use a generic one
2726 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002727 toolbarIcon = r.getDrawable(fallbackDrawableId);
2728 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002729 if (button != null) {
2730 button.setCompoundDrawables(toolbarIcon, null, null, null);
2731 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002732 return null;
2733 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002734 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002735 if (button != null) {
2736 button.setCompoundDrawables(toolbarIcon, null, null, null);
2737 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002738 return toolbarIcon.getConstantState();
2739 }
2740 }
2741
2742 // if successful in getting icon, return it; otherwise, set button to use default drawable
2743 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002744 int buttonId, ComponentName activityName, int fallbackDrawableId,
2745 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002746 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002747 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002748
Michael Jurka19e0fc52011-07-22 18:00:21 -07002749 if (button != null) {
2750 // If we were unable to find the icon via the meta-data, use a
2751 // generic one
2752 if (toolbarIcon == null) {
2753 button.setImageResource(fallbackDrawableId);
2754 } else {
2755 button.setImageDrawable(toolbarIcon);
2756 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002757 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002758
2759 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2760
Michael Jurka0423dcf2010-10-05 14:56:18 -07002761 }
2762
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002763 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2764 TextView button = (TextView) findViewById(buttonId);
2765 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2766 }
2767
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002768 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002769 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002770 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002771 }
2772
Winson Chungbb185bd2011-11-21 12:31:42 -08002773 private void invalidatePressedFocusedStates(View container, View button) {
2774 if (container instanceof HolographicLinearLayout) {
2775 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2776 layout.invalidatePressedFocusedStates();
2777 } else if (button instanceof HolographicImageView) {
2778 HolographicImageView view = (HolographicImageView) button;
2779 view.invalidatePressedFocusedStates();
2780 }
2781 }
2782
Winson Chungc51db6a2011-10-05 11:44:49 -07002783 private boolean updateGlobalSearchIcon() {
2784 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002785 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2786 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002787 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002788 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002789 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002790
Winson Chung1cad91e2011-05-25 17:41:01 -07002791 final SearchManager searchManager =
2792 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2793 ComponentName activityName = searchManager.getGlobalSearchActivity();
2794 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002795 int coi = getCurrentOrientationIndexForGlobalIcons();
2796 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002797 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2798 TOOLBAR_SEARCH_ICON_METADATA_NAME);
2799 if (sGlobalSearchIcon[coi] == null) {
2800 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2801 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2802 TOOLBAR_ICON_METADATA_NAME);
2803 }
2804
Winson Chung649723c2011-07-06 20:41:23 -07002805 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002806 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2807 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002808 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002809 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002810 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002811 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002812 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002813 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2814 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2815 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002816 voiceButton.setVisibility(View.GONE);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002817 voiceButtonProxy.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002818 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002819 }
2820 }
2821
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002822 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002823 final View searchButtonContainer = findViewById(R.id.search_button_container);
2824 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002825 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002826 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002827 }
2828
Winson Chungc51db6a2011-10-05 11:44:49 -07002829 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002830 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002831 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002832 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002833 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002834
Winson Chungc51db6a2011-10-05 11:44:49 -07002835 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07002836 final SearchManager searchManager =
2837 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2838 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2839
2840 ComponentName activityName = null;
2841 if (globalSearchActivity != null) {
2842 // Check if the global search activity handles voice search
2843 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2844 intent.setPackage(globalSearchActivity.getPackageName());
2845 activityName = intent.resolveActivity(getPackageManager());
2846 }
2847
2848 if (activityName == null) {
2849 // Fallback: check if an activity other than the global search activity
2850 // resolves this
2851 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2852 activityName = intent.resolveActivity(getPackageManager());
2853 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002854 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002855 int coi = getCurrentOrientationIndexForGlobalIcons();
2856 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002857 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2858 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
2859 if (sVoiceSearchIcon[coi] == null) {
2860 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2861 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2862 TOOLBAR_ICON_METADATA_NAME);
2863 }
Winson Chung649723c2011-07-06 20:41:23 -07002864 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002865 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002866 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07002867 if (voiceButtonProxy != null) {
2868 voiceButtonProxy.setVisibility(View.VISIBLE);
2869 }
Winson Chungbb185bd2011-11-21 12:31:42 -08002870 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002871 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002872 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002873 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002874 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002875 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07002876 if (voiceButtonProxy != null) {
2877 voiceButtonProxy.setVisibility(View.GONE);
2878 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002879 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002880 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002881 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002882
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002883 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002884 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2885 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002886 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002887 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002888 }
2889
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002890 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002891 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002892 */
2893 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002894 final View marketButton = findViewById(R.id.market_button);
2895 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2896 // Find the app market activity by resolving an intent.
2897 // (If multiple app markets are installed, it will return the ResolverActivity.)
2898 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002899 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002900 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002901 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002902 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002903 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
2904 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07002905 marketButton.setVisibility(View.VISIBLE);
2906 } else {
2907 // We should hide and disable the view so that we don't try and restore the visibility
2908 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2909 marketButton.setVisibility(View.GONE);
2910 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002911 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002912 }
2913
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002914 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002915 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002916 }
2917
Michael Jurkad7c28052012-04-27 15:43:36 -07002918 @Override
2919 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2920 boolean result = super.dispatchPopulateAccessibilityEvent(event);
2921 final List<CharSequence> text = event.getText();
2922 text.clear();
2923 text.add(getString(R.string.home));
2924 return result;
2925 }
2926
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002927 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002928 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002929 */
2930 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2931 @Override
2932 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002933 closeSystemDialogs();
2934 }
2935 }
2936
2937 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002938 * Receives notifications whenever the appwidgets are reset.
2939 */
2940 private class AppWidgetResetObserver extends ContentObserver {
2941 public AppWidgetResetObserver() {
2942 super(new Handler());
2943 }
2944
2945 @Override
2946 public void onChange(boolean selfChange) {
2947 onAppWidgetReset();
2948 }
2949 }
2950
2951 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002952 * If the activity is currently paused, signal that we need to re-run the loader
2953 * in onResume.
2954 *
2955 * This needs to be called from incoming places where resources might have been loaded
2956 * while we are paused. That is becaues the Configuration might be wrong
2957 * when we're not running, and if it comes back to what it was when we
2958 * were paused, we are not restarted.
2959 *
2960 * Implementation of the method from LauncherModel.Callbacks.
2961 *
2962 * @return true if we are currently paused. The caller might be able to
2963 * skip some work in that case since we will come back again.
2964 */
2965 public boolean setLoadOnResume() {
2966 if (mPaused) {
2967 Log.i(TAG, "setLoadOnResume");
2968 mOnResumeNeedsLoad = true;
2969 return true;
2970 } else {
2971 return false;
2972 }
2973 }
2974
2975 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002976 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002978 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002979 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002980 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002981 } else {
2982 return SCREEN_COUNT / 2;
2983 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002984 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002985
Joe Onorato9c1289c2009-08-17 11:03:03 -04002986 /**
2987 * Refreshes the shortcuts shown on the workspace.
2988 *
2989 * Implementation of the method from LauncherModel.Callbacks.
2990 */
2991 public void startBinding() {
2992 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002993
Winson Chungf0c6ae02012-03-21 16:10:31 -07002994 mNewShortcutAnimatePage = -1;
2995 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07002996 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002997 int count = workspace.getChildCount();
2998 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002999 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003000 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3001 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003002 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003003 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003004 if (mHotseat != null) {
3005 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003006 }
3007 }
3008
3009 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003010 * Bind the items start-end from the list.
3011 *
3012 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003013 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003014 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003015 setLoadOnResume();
3016
Winson Chungf0c6ae02012-03-21 16:10:31 -07003017 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3018 Set<String> newApps = new HashSet<String>();
3019 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3020
3021 Workspace workspace = mWorkspace;
3022 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003023 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003024
3025 // Short circuit if we are loading dock items for a configuration which has no dock
3026 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3027 mHotseat == null) {
3028 continue;
3029 }
3030
Joe Onorato9c1289c2009-08-17 11:03:03 -04003031 switch (item.itemType) {
3032 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3033 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003034 ShortcutInfo info = (ShortcutInfo) item;
3035 String uri = info.intent.toUri(0).toString();
3036 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003037 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3038 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003039 if (newApps.contains(uri)) {
3040 newApps.remove(uri);
3041
3042 // Prepare the view to be animated up
3043 shortcut.setAlpha(0f);
3044 shortcut.setScaleX(0f);
3045 shortcut.setScaleY(0f);
3046 mNewShortcutAnimatePage = item.screen;
3047 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3048 mNewShortcutAnimateViews.add(shortcut);
3049 }
3050 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003051 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003052 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003053 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003054 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003055 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003056 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3057 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003058 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003059 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003060 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003061
Joe Onorato9c1289c2009-08-17 11:03:03 -04003062 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003063 }
3064
Joe Onorato9c1289c2009-08-17 11:03:03 -04003065 /**
3066 * Implementation of the method from LauncherModel.Callbacks.
3067 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003068 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003069 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003070 sFolders.clear();
3071 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003072 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003073
3074 /**
3075 * Add the views for a widget to the workspace.
3076 *
3077 * Implementation of the method from LauncherModel.Callbacks.
3078 */
3079 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003080 setLoadOnResume();
3081
Daniel Sandler843e8602010-06-07 14:59:01 -04003082 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3083 if (DEBUG_WIDGETS) {
3084 Log.d(TAG, "bindAppWidget: " + item);
3085 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003086 final Workspace workspace = mWorkspace;
3087
3088 final int appWidgetId = item.appWidgetId;
3089 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003090 if (DEBUG_WIDGETS) {
3091 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3092 }
3093
Joe Onorato9c1289c2009-08-17 11:03:03 -04003094 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3095
Joe Onorato9c1289c2009-08-17 11:03:03 -04003096 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3097 item.hostView.setTag(item);
3098
Winson Chung3d503fb2011-07-13 17:25:49 -07003099 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003100 item.cellY, item.spanX, item.spanY, false);
3101
Adam Cohended9f8d2010-11-03 13:25:16 -07003102 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3103
Joe Onorato9c1289c2009-08-17 11:03:03 -04003104 workspace.requestLayout();
3105
Winson Chungf51025f2012-05-03 15:21:11 -07003106 AppWidgetResizeFrame.updateWidgetSizeRanges(item.hostView,
3107 this, item.spanX, item.spanY);
3108
Daniel Sandler843e8602010-06-07 14:59:01 -04003109 if (DEBUG_WIDGETS) {
3110 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3111 + (SystemClock.uptimeMillis()-start) + "ms");
3112 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003113 }
3114
3115 /**
3116 * Callback saying that there aren't any more items to bind.
3117 *
3118 * Implementation of the method from LauncherModel.Callbacks.
3119 */
3120 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003121 setLoadOnResume();
3122
Joe Onorato9c1289c2009-08-17 11:03:03 -04003123 if (mSavedState != null) {
3124 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003125 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003126 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003127 mSavedState = null;
3128 }
3129
3130 if (mSavedInstanceState != null) {
3131 super.onRestoreInstanceState(mSavedInstanceState);
3132 mSavedInstanceState = null;
3133 }
3134
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003135 // If we received the result of any pending adds while the loader was running (e.g. the
3136 // widget configuration forced an orientation change), process them now.
3137 for (int i = 0; i < sPendingAddList.size(); i++) {
3138 completeAdd(sPendingAddList.get(i));
3139 }
3140 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003141
Winson Chungc51db6a2011-10-05 11:44:49 -07003142 // Update the market app icon as necessary (the other icons will be managed in response to
3143 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003144 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003145
Winson Chungf0c6ae02012-03-21 16:10:31 -07003146 // Animate up any icons as necessary
3147 if (mVisible || mWorkspaceLoading) {
3148 Runnable newAppsRunnable = new Runnable() {
3149 @Override
3150 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003151 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003152 }
3153 };
Winson Chunga2413752012-04-03 14:22:34 -07003154
3155 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3156 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3157 if (canRunNewAppsAnimation()) {
3158 // If the user has not interacted recently, then either snap to the new page to show
3159 // the new-apps animation or just run them if they are to appear on the current page
3160 if (willSnapPage) {
3161 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3162 } else {
3163 runNewAppsAnimation(false);
3164 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003165 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003166 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003167 // are on another page (or just normally if they are added to the current page)
3168 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003169 }
3170 }
3171
3172 mWorkspaceLoading = false;
3173 }
3174
Winson Chunga2413752012-04-03 14:22:34 -07003175 private boolean canRunNewAppsAnimation() {
3176 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3177 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3178 }
3179
Winson Chungf0c6ae02012-03-21 16:10:31 -07003180 /**
3181 * Runs a new animation that scales up icons that were added while Launcher was in the
3182 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003183 *
3184 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003185 */
Winson Chunga2413752012-04-03 14:22:34 -07003186 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003187 AnimatorSet anim = new AnimatorSet();
3188 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003189
3190 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003191 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3192 @Override
3193 public int compare(View a, View b) {
3194 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3195 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3196 int cellCountX = LauncherModel.getCellCountX();
3197 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3198 }
3199 });
Winson Chunga2413752012-04-03 14:22:34 -07003200
3201 // Animate each of the views in place (or show them immediately if requested)
3202 if (immediate) {
3203 for (View v : mNewShortcutAnimateViews) {
3204 v.setAlpha(1f);
3205 v.setScaleX(1f);
3206 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003207 }
Winson Chunga2413752012-04-03 14:22:34 -07003208 } else {
3209 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3210 View v = mNewShortcutAnimateViews.get(i);
3211 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3212 PropertyValuesHolder.ofFloat("alpha", 1f),
3213 PropertyValuesHolder.ofFloat("scaleX", 1f),
3214 PropertyValuesHolder.ofFloat("scaleY", 1f));
3215 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3216 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3217 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3218 bounceAnims.add(bounceAnim);
3219 }
3220 anim.playTogether(bounceAnims);
3221 anim.addListener(new AnimatorListenerAdapter() {
3222 @Override
3223 public void onAnimationEnd(Animator animation) {
3224 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3225 }
3226 });
3227 anim.start();
3228 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003229
3230 // Clean up
3231 mNewShortcutAnimatePage = -1;
3232 mNewShortcutAnimateViews.clear();
3233 new Thread("clearNewAppsThread") {
3234 public void run() {
3235 mSharedPrefs.edit()
3236 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3237 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3238 .commit();
3239 }
3240 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003241 }
3242
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003243 @Override
3244 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003245 boolean searchVisible = updateGlobalSearchIcon();
3246 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3247 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003248 }
3249
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003250 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003251 * Add the icons for all apps.
3252 *
3253 * Implementation of the method from LauncherModel.Callbacks.
3254 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003255 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3256 // Remove the progress bar entirely; we could also make it GONE
3257 // but better to remove it since we know it's not going to be used
3258 View progressBar = mAppsCustomizeTabHost.
3259 findViewById(R.id.apps_customize_progress_bar);
3260 if (progressBar != null) {
3261 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003262 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003263 // We just post the call to setApps so the user sees the progress bar
3264 // disappear-- otherwise, it just looks like the progress bar froze
3265 // which doesn't look great
3266 mAppsCustomizeTabHost.post(new Runnable() {
3267 public void run() {
3268 if (mAppsCustomizeContent != null) {
3269 mAppsCustomizeContent.setApps(apps);
3270 }
3271 }
3272 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003273 }
3274
3275 /**
3276 * A package was installed.
3277 *
3278 * Implementation of the method from LauncherModel.Callbacks.
3279 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003280 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003281 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003282
Winson Chung785d2eb2011-04-14 16:08:02 -07003283 if (mAppsCustomizeContent != null) {
3284 mAppsCustomizeContent.addApps(apps);
3285 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003286 }
3287
3288 /**
3289 * A package was updated.
3290 *
3291 * Implementation of the method from LauncherModel.Callbacks.
3292 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003293 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003294 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003295 if (mWorkspace != null) {
3296 mWorkspace.updateShortcuts(apps);
3297 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003298
Winson Chung785d2eb2011-04-14 16:08:02 -07003299 if (mAppsCustomizeContent != null) {
3300 mAppsCustomizeContent.updateApps(apps);
3301 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003302 }
3303
3304 /**
3305 * A package was uninstalled.
3306 *
3307 * Implementation of the method from LauncherModel.Callbacks.
3308 */
Joe Onorato36115782010-06-17 13:28:48 -04003309 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003310 if (permanent) {
3311 mWorkspace.removeItems(apps);
3312 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003313
Winson Chung785d2eb2011-04-14 16:08:02 -07003314 if (mAppsCustomizeContent != null) {
3315 mAppsCustomizeContent.removeApps(apps);
3316 }
Winson Chunga1820962011-10-03 16:31:06 -07003317
3318 // Notify the drag controller
3319 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003320 }
Joe Onoratobe386092009-11-17 17:32:16 -08003321
3322 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003323 * A number of packages were updated.
3324 */
3325 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003326 if (mAppsCustomizeContent != null) {
3327 mAppsCustomizeContent.onPackagesUpdated();
3328 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003329 }
3330
Winson Chung400438b2011-01-16 17:53:48 -08003331 private int mapConfigurationOriActivityInfoOri(int configOri) {
3332 final Display d = getWindowManager().getDefaultDisplay();
3333 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3334 switch (d.getRotation()) {
3335 case Surface.ROTATION_0:
3336 case Surface.ROTATION_180:
3337 // We are currently in the same basic orientation as the natural orientation
3338 naturalOri = configOri;
3339 break;
3340 case Surface.ROTATION_90:
3341 case Surface.ROTATION_270:
3342 // We are currently in the other basic orientation to the natural orientation
3343 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3344 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3345 break;
3346 }
3347
3348 int[] oriMap = {
3349 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3350 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3351 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3352 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3353 };
3354 // Since the map starts at portrait, we need to offset if this device's natural orientation
3355 // is landscape.
3356 int indexOffset = 0;
3357 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3358 indexOffset = 1;
3359 }
3360 return oriMap[(d.getRotation() + indexOffset) % 4];
3361 }
Adam Cohen446e9402011-09-15 18:21:21 -07003362
Winson Chung4b919f82012-05-01 10:44:08 -07003363 public boolean isRotationEnabled() {
3364 boolean forceEnableRotation = "true".equalsIgnoreCase(SystemProperties.get(
3365 FORCE_ENABLE_ROTATION_PROPERTY, "false"));
3366 boolean enableRotation = forceEnableRotation ||
3367 getResources().getBoolean(R.bool.allow_rotation);
3368 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003369 }
Winson Chung4b919f82012-05-01 10:44:08 -07003370 public void lockScreenOrientation() {
3371 if (isRotationEnabled()) {
3372 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3373 .getConfiguration().orientation));
3374 }
3375 }
3376 public void unlockScreenOrientation(boolean immediate) {
3377 if (isRotationEnabled()) {
3378 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003379 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003380 } else {
3381 mHandler.postDelayed(new Runnable() {
3382 public void run() {
3383 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3384 }
3385 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003386 }
Winson Chung4b919f82012-05-01 10:44:08 -07003387 }
Winson Chung400438b2011-01-16 17:53:48 -08003388 }
3389
Winson Chung82f55532011-08-09 14:14:23 -07003390 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003391 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003392 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003393 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003394
Winson Chung7d7541e2011-09-16 20:14:36 -07003395 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003396 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003397 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3398 Cling cling = (Cling) findViewById(clingId);
3399 if (cling != null) {
3400 cling.init(this, positionData);
3401 cling.setVisibility(View.VISIBLE);
3402 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3403 if (animate) {
3404 cling.buildLayer();
3405 cling.setAlpha(0f);
3406 cling.animate()
3407 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003408 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003409 .setDuration(SHOW_CLING_DURATION)
3410 .setStartDelay(delay)
3411 .start();
3412 } else {
3413 cling.setAlpha(1f);
3414 }
3415 }
3416 return cling;
3417 }
3418 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003419 if (cling != null) {
3420 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003421 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003422 anim.addListener(new AnimatorListenerAdapter() {
3423 public void onAnimationEnd(Animator animation) {
3424 cling.setVisibility(View.GONE);
3425 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003426 // We should update the shared preferences on a background thread
3427 new Thread("dismissClingThread") {
3428 public void run() {
3429 SharedPreferences.Editor editor = mSharedPrefs.edit();
3430 editor.putBoolean(flag, true);
3431 editor.commit();
3432 }
3433 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003434 };
3435 });
3436 anim.start();
3437 }
3438 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003439 private void removeCling(int id) {
3440 final View cling = findViewById(id);
3441 if (cling != null) {
3442 final ViewGroup parent = (ViewGroup) cling.getParent();
3443 parent.post(new Runnable() {
3444 @Override
3445 public void run() {
3446 parent.removeView(cling);
3447 }
3448 });
3449 }
3450 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003451 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003452 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003453 if (isClingsEnabled() &&
3454 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003455 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003456 } else {
3457 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003458 }
3459 }
3460 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003461 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003462 if (isClingsEnabled() &&
3463 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003464 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003465 } else {
3466 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003467 }
3468 }
3469 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003470 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003471 if (isClingsEnabled() &&
3472 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3473 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003474 } else {
3475 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003476 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003477 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003478 }
3479 public boolean isFolderClingVisible() {
3480 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003481 if (cling != null) {
3482 return cling.getVisibility() == View.VISIBLE;
3483 }
3484 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003485 }
Winson Chung82f55532011-08-09 14:14:23 -07003486 public void dismissWorkspaceCling(View v) {
3487 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003488 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003489 }
3490 public void dismissAllAppsCling(View v) {
3491 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003492 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3493 }
3494 public void dismissFolderCling(View v) {
3495 Cling cling = (Cling) findViewById(R.id.folder_cling);
3496 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003497 }
3498
Winson Chung80baf5a2010-08-09 16:03:15 -07003499 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003500 * Prints out out state for debugging.
3501 */
3502 public void dumpState() {
3503 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003504 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003505 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3506 Log.d(TAG, "mRestoring=" + mRestoring);
3507 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3508 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003509 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003510 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003511
Winson Chung785d2eb2011-04-14 16:08:02 -07003512 if (mAppsCustomizeContent != null) {
3513 mAppsCustomizeContent.dumpState();
3514 }
Joe Onoratobe386092009-11-17 17:32:16 -08003515 Log.d(TAG, "END launcher2 dump state");
3516 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003517
3518 @Override
3519 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3520 super.dump(prefix, fd, writer, args);
3521 writer.println(" ");
3522 writer.println("Debug logs: ");
3523 for (int i = 0; i < sDumpLogs.size(); i++) {
3524 writer.println(" " + sDumpLogs.get(i));
3525 }
3526 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003527}
Michael Jurka2763be32011-02-24 11:19:57 -08003528
Michael Jurkaabded662011-03-04 12:06:57 -08003529interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003530 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003531 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003532 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003533 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003534 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003535}