blob: ff8e78594816414e9c1e3434370c2a7f30e8fcd2 [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;
Winson Chung68846fd2010-10-29 11:00:27 -070036import android.content.ClipData;
37import android.content.ClipDescription;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040053import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070054import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020056import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080057import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070058import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070059import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040060import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080061import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070062import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080063import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.Selection;
65import android.text.SpannableStringBuilder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070067import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080068import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080069import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.KeyEvent;
71import android.view.LayoutInflater;
72import android.view.Menu;
73import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070074import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080075import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080077import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070078import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080079import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080080import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070081import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080082import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070083import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070084import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080085import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070087import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070088import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070089import android.widget.TextView;
90import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070091
Adam Cohendf2cc412011-04-27 16:56:57 -070092import com.android.common.Search;
93import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070094import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080095
Adam Cohenc0dcf592011-06-01 15:30:43 -070096import java.io.DataInputStream;
97import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070098import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.FileNotFoundException;
100import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700101import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700103import java.util.Collection;
104import java.util.Collections;
105import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700108import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700109import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111/**
112 * Default launcher application.
113 */
Michael Jurka946ad472010-07-09 18:05:18 -0700114public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700115 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
116 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800117 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700118 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Joe Onorato9c1289c2009-08-17 11:03:03 -0400120 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400121 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700122 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700123
Winson Chung70d72102011-08-12 11:24:35 -0700124 private static final int MENU_GROUP_WALLPAPER = 1;
125 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
126 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700127 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
128 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
130 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700131 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int REQUEST_PICK_APPLICATION = 6;
133 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700135 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Michael Jurka8b805b12012-04-18 14:23:14 -0700137 private static final int REQUEST_BIND_APPWIDGET = 11;
138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
140
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800141 static final int SCREEN_COUNT = 5;
142 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Romain Guy98d01652009-06-30 16:21:04 -0700144 private static final String PREFERENCES = "launcher.preferences";
Adam Cohen23a4d302011-12-01 17:26:44 -0800145 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
147 // Type: int
148 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700149 // Type: int
150 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700152 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
153 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
155 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700156 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700158 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 // Type: boolean
160 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
161 // Type: long
162 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
163
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700164 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700165 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
166 "com.android.launcher.toolbar_search_icon";
167 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
168 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700169
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700170 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700171 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700172 private State mState = State.WORKSPACE;
173 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800174 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700175
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700177 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
178 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700179 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700180 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800183 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Winson Chunga2413752012-04-03 14:22:34 -0700185 // How long to wait before the new-shortcut animation automatically pans the workspace
186 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
187
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800188 private final BroadcastReceiver mCloseSystemDialogsReceiver
189 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800190 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private LayoutInflater mInflater;
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800195 private View mQsbDivider;
196 private View mDockDivider;
197 private DragLayer mDragLayer;
198 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700199
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700200 private AppWidgetManager mAppWidgetManager;
201 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700202
Michael Jurkac9d95c52011-08-29 14:03:34 -0700203 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700204 private int[] mTmpAddItemCellCoordinates = new int[2];
205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private FolderInfo mFolderInfo;
207
Winson Chung3d503fb2011-07-13 17:25:49 -0700208 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800209 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700210
Winson Chungc51db6a2011-10-05 11:44:49 -0700211 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700212 private AppsCustomizeTabHost mAppsCustomizeTabHost;
213 private AppsCustomizePagedView mAppsCustomizeContent;
214 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private Bundle mSavedState;
217
218 private SpannableStringBuilder mDefaultKeySsb = null;
219
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220 private boolean mWorkspaceLoading = true;
221
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800222 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private boolean mRestoring;
224 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700225 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226
227 private Bundle mSavedInstanceState;
228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800230 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800231 private boolean mUserPresent = true;
232 private boolean mVisible = false;
233 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700235 private static LocaleConfiguration sLocaleConfiguration = null;
236
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700237 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700238
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700239 private Intent mAppMarketIntent = null;
240
Adam Cohended9f8d2010-11-03 13:25:16 -0700241 // Related to the auto-advancing of widgets
242 private final int ADVANCE_MSG = 1;
243 private final int mAdvanceInterval = 20000;
244 private final int mAdvanceStagger = 250;
245 private long mAutoAdvanceSentTime;
246 private long mAutoAdvanceTimeLeft = -1;
247 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
248 new HashMap<View, AppWidgetProviderInfo>();
249
Winson Chung400438b2011-01-16 17:53:48 -0800250 // Determines how long to wait after a rotation before restoring the screen orientation to
251 // match the sensor state.
252 private final int mRestoreScreenOrientationDelay = 500;
253
Michael Jurka4ef207b2010-11-29 17:05:45 -0800254 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700255 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
256 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
257 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800258
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700259 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Michael Jurka8b805b12012-04-18 14:23:14 -0700260 PendingAddWidgetInfo mWidgetBeingBoundOrConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700261
Winson Chung46353de2012-02-16 14:05:10 -0800262 // We only want to get the SharedPreferences once since it does an FS stat each time we get
263 // it from the context.
264 private SharedPreferences mSharedPrefs;
265
Winson Chungf0c6ae02012-03-21 16:10:31 -0700266 // Holds the page that we need to animate to, and the icon views that we need to animate up
267 // when we scroll to that page on resume.
268 private int mNewShortcutAnimatePage = -1;
269 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700270
Michael Jurkaddd62e92011-02-16 17:49:14 -0800271 private BubbleTextView mWaitingForResume;
272
Michael Jurkac1f5d262011-09-30 19:32:27 -0700273 private Runnable mBuildLayersRunnable = new Runnable() {
274 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700275 if (mWorkspace != null) {
276 mWorkspace.buildPageHardwareLayers();
277 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700278 }
279 };
280
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800281 private static ArrayList<PendingAddArguments> sPendingAddList
282 = new ArrayList<PendingAddArguments>();
283
284 private static class PendingAddArguments {
285 int requestCode;
286 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700287 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800288 int screen;
289 int cellX;
290 int cellY;
291 }
292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 @Override
294 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700295 if (DEBUG_STRICT_MODE) {
296 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
297 .detectDiskReads()
298 .detectDiskWrites()
299 .detectNetwork() // or .detectAll() for all detectable problems
300 .penaltyLog()
301 .build());
302 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
303 .detectLeakedSqlLiteObjects()
304 .detectLeakedClosableObjects()
305 .penaltyLog()
306 .penaltyDeath()
307 .build());
308 }
309
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800311 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700312 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
313 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800314 mModel = app.setLauncher(this);
315 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400316 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800317 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700318
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700319 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700320 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
321 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700322
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200324 android.os.Debug.startMethodTracing(
325 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 }
327
328 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 setContentView(R.layout.launcher);
330 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700331 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800333 registerContentObservers();
334
Joe Onorato7c312c12009-08-13 21:36:53 -0700335 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700336
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 mSavedState = savedInstanceState;
338 restoreState(mSavedState);
339
Winson Chunga12a2502010-12-20 14:41:35 -0800340 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700341 if (mAppsCustomizeContent != null) {
342 mAppsCustomizeContent.onPackagesUpdated();
343 }
Winson Chunga12a2502010-12-20 14:41:35 -0800344
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 if (PROFILE_STARTUP) {
346 android.os.Debug.stopMethodTracing();
347 }
348
349 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700350 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 }
352
Michael Jurkac57b7a82011-08-09 22:02:20 -0700353 if (!mModel.isAllAppsLoaded()) {
354 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
355 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
356 }
357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 // For handling default keys
359 mDefaultKeySsb = new SpannableStringBuilder();
360 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800361
362 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
363 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800364
Winson Chungc51db6a2011-10-05 11:44:49 -0700365 boolean searchVisible = false;
366 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800367 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700368 int coi = getCurrentOrientationIndexForGlobalIcons();
369 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
370 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700371 updateAppMarketIcon();
372 searchVisible = updateGlobalSearchIcon();
373 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700374 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700375 if (sGlobalSearchIcon[coi] != null) {
376 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700377 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700378 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700379 if (sVoiceSearchIcon[coi] != null) {
380 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700381 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700382 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700383 if (sAppMarketIcon[coi] != null) {
384 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800385 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700386 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700387
388 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Winson Chung4b919f82012-05-01 10:44:08 -0700389 unlockScreenOrientation(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 }
Romain Guycbb89e42009-06-08 15:52:54 -0700391
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700393 if (sLocaleConfiguration == null) {
394 new AsyncTask<Void, Void, LocaleConfiguration>() {
395 @Override
396 protected LocaleConfiguration doInBackground(Void... unused) {
397 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
398 readConfiguration(Launcher.this, localeConfiguration);
399 return localeConfiguration;
400 }
401
402 @Override
403 protected void onPostExecute(LocaleConfiguration result) {
404 sLocaleConfiguration = result;
405 checkForLocaleChange(); // recursive, but now with a locale configuration
406 }
407 }.execute();
408 return;
409 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700410
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 final Configuration configuration = getResources().getConfiguration();
412
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700413 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 final String locale = configuration.locale.toString();
415
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700416 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 final int mcc = configuration.mcc;
418
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700419 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 final int mnc = configuration.mnc;
421
Romain Guy84f296c2009-11-04 15:00:44 -0800422 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423
Romain Guy84f296c2009-11-04 15:00:44 -0800424 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700425 sLocaleConfiguration.locale = locale;
426 sLocaleConfiguration.mcc = mcc;
427 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700428
Joe Onorato0589f0f2010-02-08 13:44:00 -0800429 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700430
431 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
432 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700433 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700434 public void run() {
435 writeConfiguration(Launcher.this, localeConfiguration);
436 }
437 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700438 }
439 }
440
441 private static class LocaleConfiguration {
442 public String locale;
443 public int mcc = -1;
444 public int mnc = -1;
445 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700446
Romain Guy98d01652009-06-30 16:21:04 -0700447 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
448 DataInputStream in = null;
449 try {
450 in = new DataInputStream(context.openFileInput(PREFERENCES));
451 configuration.locale = in.readUTF();
452 configuration.mcc = in.readInt();
453 configuration.mnc = in.readInt();
454 } catch (FileNotFoundException e) {
455 // Ignore
456 } catch (IOException e) {
457 // Ignore
458 } finally {
459 if (in != null) {
460 try {
461 in.close();
462 } catch (IOException e) {
463 // Ignore
464 }
465 }
466 }
467 }
468
469 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
470 DataOutputStream out = null;
471 try {
472 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
473 out.writeUTF(configuration.locale);
474 out.writeInt(configuration.mcc);
475 out.writeInt(configuration.mnc);
476 out.flush();
477 } catch (FileNotFoundException e) {
478 // Ignore
479 } catch (IOException e) {
480 //noinspection ResultOfMethodCallIgnored
481 context.getFileStreamPath(PREFERENCES).delete();
482 } finally {
483 if (out != null) {
484 try {
485 out.close();
486 } catch (IOException e) {
487 // Ignore
488 }
489 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 }
491 }
492
Adam Cohen716b51e2011-06-30 12:09:54 -0700493 public DragLayer getDragLayer() {
494 return mDragLayer;
495 }
496
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800497 static int getScreen() {
498 synchronized (sLock) {
499 return sScreen;
500 }
501 }
502
503 static void setScreen(int screen) {
504 synchronized (sLock) {
505 sScreen = screen;
506 }
507 }
508
Winson Chung557d6ed2011-07-08 15:34:52 -0700509 /**
510 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
511 * a configuration step, this allows the proper animations to run after other transitions.
512 */
513 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700514 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800515 switch (args.requestCode) {
516 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700517 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
518 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800519 break;
520 case REQUEST_PICK_SHORTCUT:
521 processShortcut(args.intent);
522 break;
523 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700524 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
525 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700526 result = true;
527 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800528 case REQUEST_PICK_APPWIDGET:
529 addAppWidgetFromPick(args.intent);
530 break;
531 case REQUEST_CREATE_APPWIDGET:
532 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800533 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700534 result = true;
535 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800536 case REQUEST_PICK_WALLPAPER:
537 // We just wanted the activity result here so we can clear mWaitingForResult
538 break;
539 }
Michael Jurka27614d22012-04-02 06:40:22 -0700540 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
541 // if you turned the screen off and then back while in All Apps, Launcher would not
542 // return to the workspace. Clearing mAddInfo.container here fixes this issue
543 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700544 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800545 }
546
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700548 protected void onActivityResult(
549 final int requestCode, final int resultCode, final Intent data) {
550 if (requestCode == REQUEST_BIND_APPWIDGET) {
551 int appWidgetId = data != null ?
552 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
553 if (resultCode == RESULT_CANCELED) {
554 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
555 } else if (resultCode == RESULT_OK) {
556 addAppWidgetImpl(appWidgetId, mWidgetBeingBoundOrConfigured);
557 }
558 return;
559 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700560 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800561 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
562 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700563 mWaitingForResult = false;
564
Adam Cohened66b2b2012-01-23 17:28:51 -0800565 // We have special handling for widgets
566 if (isWidgetDrop) {
567 int appWidgetId = data != null ?
568 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700569 if (appWidgetId < 0) {
570 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
571 "widget configuration activity.");
572 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
573 } else {
574 completeTwoStageWidgetDrop(resultCode, appWidgetId);
575 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800576 return;
577 }
578
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 // The pattern used here is that a user PICKs a specific application,
580 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700581
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
583 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700584 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800585 final PendingAddArguments args = new PendingAddArguments();
586 args.requestCode = requestCode;
587 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700588 args.container = mPendingAddInfo.container;
589 args.screen = mPendingAddInfo.screen;
590 args.cellX = mPendingAddInfo.cellX;
591 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800592 if (isWorkspaceLocked()) {
593 sPendingAddList.add(args);
594 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700595 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800598 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700599 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800600 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
601 null);
602 }
603
604 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700605 CellLayout cellLayout =
606 (CellLayout) mWorkspace.getChildAt(mWidgetBeingBoundOrConfigured.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800607 Runnable onCompleteRunnable = null;
608 int animationType = 0;
609
610 if (resultCode == RESULT_OK) {
611 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
612 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Michael Jurka8b805b12012-04-18 14:23:14 -0700613 mWidgetBeingBoundOrConfigured.info);
614 mWidgetBeingBoundOrConfigured.boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800615 onCompleteRunnable = new Runnable() {
616 @Override
617 public void run() {
618 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
619 mPendingAddInfo.screen, layout, null);
620 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
621 null);
622 }
623 };
624 } else if (resultCode == RESULT_CANCELED) {
625 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
626 onCompleteRunnable = new Runnable() {
627 @Override
628 public void run() {
629 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
630 null);
631 }
632 };
633 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700634 mWorkspace.animateWidgetDrop(mWidgetBeingBoundOrConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800635 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Michael Jurka8b805b12012-04-18 14:23:14 -0700636 animationType, mWidgetBeingBoundOrConfigured.boundWidget, true);
637 mWidgetBeingBoundOrConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 }
639
640 @Override
641 protected void onResume() {
642 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700643
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800644 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700645 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400646 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700647 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700649 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
Winson Chunge4e50662012-01-23 14:45:13 -0800651
652 // Reset the pressed state of icons that were locked in the press state while activities
653 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800654 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800655 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800656 mWaitingForResume.setStayPressed(false);
657 }
Winson Chunge4e50662012-01-23 14:45:13 -0800658 if (mAppsCustomizeContent != null) {
659 // Resets the previous all apps icon press state
660 mAppsCustomizeContent.resetDrawableState();
661 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 }
663
664 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700665 protected void onPause() {
666 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700667 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800668 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700669 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700670 }
Romain Guycbb89e42009-06-08 15:52:54 -0700671
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700672 @Override
673 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400674 // Flag the loader to stop early before switching
675 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700676 if (mAppsCustomizeContent != null) {
677 mAppsCustomizeContent.surrender();
678 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800679 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700680 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700681
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800682 // We can't hide the IME if it was forced open. So don't bother
683 /*
684 @Override
685 public void onWindowFocusChanged(boolean hasFocus) {
686 super.onWindowFocusChanged(hasFocus);
687
688 if (hasFocus) {
689 final InputMethodManager inputManager = (InputMethodManager)
690 getSystemService(Context.INPUT_METHOD_SERVICE);
691 WindowManager.LayoutParams lp = getWindow().getAttributes();
692 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
693 android.os.Handler()) {
694 protected void onReceiveResult(int resultCode, Bundle resultData) {
695 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
696 }
697 });
698 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
699 }
700 }
701 */
702
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 private boolean acceptFilter() {
704 final InputMethodManager inputManager = (InputMethodManager)
705 getSystemService(Context.INPUT_METHOD_SERVICE);
706 return !inputManager.isFullscreenMode();
707 }
708
709 @Override
710 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700711 final int uniChar = event.getUnicodeChar();
712 final boolean handled = super.onKeyDown(keyCode, event);
713 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
714 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
716 keyCode, event);
717 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700718 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700719 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700720 // showSearchDialog()
721 // If there are multiple keystrokes before the search dialog takes focus,
722 // onSearchRequested() will be called for every keystroke,
723 // but it is idempotent, so it's fine.
724 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 }
726 }
727
Joe Onorato8a9625e2010-01-28 15:55:35 -0800728 // Eat the long press event so the keyboard doesn't come up.
729 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
730 return true;
731 }
732
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 return handled;
734 }
735
Karl Rosaen138a0412009-04-23 19:00:21 -0700736 private String getTypedText() {
737 return mDefaultKeySsb.toString();
738 }
739
740 private void clearTypedText() {
741 mDefaultKeySsb.clear();
742 mDefaultKeySsb.clearSpans();
743 Selection.setSelection(mDefaultKeySsb, 0);
744 }
745
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700747 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
748 * State
749 */
750 private static State intToState(int stateOrdinal) {
751 State state = State.WORKSPACE;
752 final State[] stateValues = State.values();
753 for (int i = 0; i < stateValues.length; i++) {
754 if (stateValues[i].ordinal() == stateOrdinal) {
755 state = stateValues[i];
756 break;
757 }
758 }
759 return state;
760 }
761
762 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 * Restores the previous state, if it exists.
764 *
765 * @param savedState The previous state.
766 */
767 private void restoreState(Bundle savedState) {
768 if (savedState == null) {
769 return;
770 }
771
Michael Jurka883f55b2010-10-21 15:47:14 -0700772 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700773 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800774 showAllApps(false);
775 }
776
Winson Chungf0c6ae02012-03-21 16:10:31 -0700777 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700779 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 }
781
Winson Chung3d503fb2011-07-13 17:25:49 -0700782 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
783 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700784
Winson Chung3d503fb2011-07-13 17:25:49 -0700785 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
786 mPendingAddInfo.container = pendingAddContainer;
787 mPendingAddInfo.screen = pendingAddScreen;
788 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
789 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 mRestoring = true;
791 }
792
793 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
794 if (renameFolder) {
795 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700796 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 mRestoring = true;
798 }
Winson Chunga12a2502010-12-20 14:41:35 -0800799
Winson Chung785d2eb2011-04-14 16:08:02 -0700800
801 // Restore the AppsCustomize tab
802 if (mAppsCustomizeTabHost != null) {
803 String curTab = savedState.getString("apps_customize_currentTab");
804 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700805 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700806 mAppsCustomizeContent.setContentType(
807 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
808 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700809 mAppsCustomizeContent.loadAssociatedPages(
810 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700811 }
812
Winson Chung5afbf7b2011-07-25 11:53:08 -0700813 int currentIndex = savedState.getInt("apps_customize_currentIndex");
814 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700815 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 }
817
818 /**
819 * Finds all the views we need and configure them properly.
820 */
821 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700822 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400823
Winson Chung4c98d922011-05-31 16:50:48 -0700824 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
825 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800826 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
827 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828
Winson Chung4c98d922011-05-31 16:50:48 -0700829 // Setup the drag layer
830 mDragLayer.setup(this, dragController);
831
Winson Chung3d503fb2011-07-13 17:25:49 -0700832 // Setup the hotseat
833 mHotseat = (Hotseat) findViewById(R.id.hotseat);
834 if (mHotseat != null) {
835 mHotseat.setup(this);
836 }
837
Winson Chung4c98d922011-05-31 16:50:48 -0700838 // Setup the workspace
839 mWorkspace.setHapticFeedbackEnabled(false);
840 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700841 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700842 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700843
Winson Chungf0ea4d32011-06-06 14:27:16 -0700844 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700845 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700846
Winson Chungf0ea4d32011-06-06 14:27:16 -0700847 // Setup AppsCustomize
848 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
849 findViewById(R.id.apps_customize_pane);
850 mAppsCustomizeContent = (AppsCustomizePagedView)
851 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700852 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700853 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400854
Michael Jurka5130e402011-10-13 04:55:35 -0700855 // Get the all apps button
856 mAllAppsButton = findViewById(R.id.all_apps_button);
857 if (mAllAppsButton != null) {
858 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
859 @Override
860 public boolean onTouch(View v, MotionEvent event) {
861 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
862 onTouchDownAllAppsButton(v);
863 }
864 return false;
865 }
866 });
867 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700868 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700869 dragController.setDragScoller(mWorkspace);
870 dragController.setScrollView(mDragLayer);
871 dragController.setMoveTarget(mWorkspace);
872 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700873 if (mSearchDropTargetBar != null) {
874 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800875 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 }
877
878 /**
879 * Creates a view representing a shortcut.
880 *
881 * @param info The data structure describing the shortcut.
882 *
883 * @return A View inflated from R.layout.application.
884 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800885 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700887 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 }
889
890 /**
891 * Creates a view representing a shortcut inflated from the specified resource.
892 *
893 * @param layoutResId The id of the XML layout used to create the shortcut.
894 * @param parent The group the shortcut belongs to.
895 * @param info The data structure describing the shortcut.
896 *
897 * @return A View inflated from layoutResId.
898 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800899 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800900 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
901 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 return favorite;
904 }
905
906 /**
907 * Add an application shortcut to the workspace.
908 *
909 * @param data The intent describing the application.
910 * @param cellInfo The position on screen where to create the shortcut.
911 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700912 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700913 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700914 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700915
Adam Cohenfbba09b2011-07-18 21:43:05 -0700916 // First we check if we already know the exact location where we want to add this item.
917 if (cellX >= 0 && cellY >= 0) {
918 cellXY[0] = cellX;
919 cellXY[1] = cellY;
920 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700921 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700922 return;
923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800925 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800926
Romain Guy73b979d2009-06-09 12:57:21 -0700927 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800928 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800930 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700931 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700932 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800933 } else {
934 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 }
936 }
Romain Guycbb89e42009-06-08 15:52:54 -0700937
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 /**
939 * Add a shortcut to the workspace.
940 *
941 * @param data The intent describing the shortcut.
942 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700944 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
945 int cellY) {
946 int[] cellXY = mTmpAddItemCellCoordinates;
947 int[] touchXY = mPendingAddInfo.dropPos;
948 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700949
Michael Jurkad3ef3062010-11-23 16:23:58 -0800950 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700951
Adam Cohen558baaf2011-08-15 15:22:57 -0700952 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800953 if (info == null) {
954 return;
955 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700956 final View view = createShortcut(info);
957
Adam Cohenfbba09b2011-07-18 21:43:05 -0700958 // First we check if we already know the exact location where we want to add this item.
959 if (cellX >= 0 && cellY >= 0) {
960 cellXY[0] = cellX;
961 cellXY[1] = cellY;
962 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700963
964 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800965 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700966 true, null,null)) {
967 return;
968 }
969 DragObject dragObject = new DragObject();
970 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -0800971 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
972 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -0700973 return;
974 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700975 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800976 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700977 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800978 foundCellSpan = (result != null);
979 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700980 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800981 }
982
983 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -0700984 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700985 return;
986 }
987
Adam Cohen558baaf2011-08-15 15:22:57 -0700988 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989
990 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700991 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
992 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 }
994 }
995
Adam Cohen2f093b62012-04-30 18:59:53 -0700996 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
997 int minHeight) {
998 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700999 // We want to account for the extra amount of padding that we are adding to the widget
1000 // to ensure that it gets the full amount of space that it has requested
1001 int requiredWidth = minWidth + padding.left + padding.right;
1002 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001003 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001004 }
1005
Adam Cohen2f093b62012-04-30 18:59:53 -07001006 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1007 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001008 }
1009
Adam Cohen2f093b62012-04-30 18:59:53 -07001010 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1011 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001012 }
1013
Adam Cohen2f093b62012-04-30 18:59:53 -07001014 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1015 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001016 }
1017
Adam Cohen2f093b62012-04-30 18:59:53 -07001018 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1019 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001020 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001021 }
1022
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001024 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001026 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001027 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001029 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1030 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1031 if (appWidgetInfo == null) {
1032 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1033 }
Romain Guycbb89e42009-06-08 15:52:54 -07001034
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001035 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001036 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001037
Adam Cohen2f093b62012-04-30 18:59:53 -07001038 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1039 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001042 // We have saved the position to which the widget was dragged-- this really only matters
1043 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001044 int[] cellXY = mTmpAddItemCellCoordinates;
1045 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001046 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001047 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001048 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1049 cellXY[0] = mPendingAddInfo.cellX;
1050 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001051 spanXY[0] = mPendingAddInfo.spanX;
1052 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001053 foundCellSpan = true;
1054 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001055 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001056 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001057 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1058 spanXY[1], cellXY, finalSpan);
1059 spanXY[0] = finalSpan[0];
1060 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001061 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001062 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001063 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001064 }
1065
Michael Jurka0280c3b2010-09-17 15:00:07 -07001066 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001067 if (appWidgetId != -1) {
1068 // Deleting an app widget ID is a void call but writes to disk before returning
1069 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001070 new Thread("deleteAppWidgetId") {
1071 public void run() {
1072 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1073 }
1074 }.start();
1075 }
Winson Chung93eef082012-03-23 15:59:27 -07001076 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001077 return;
1078 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001080 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001081 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1082 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001083 launcherInfo.spanX = spanXY[0];
1084 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001085 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1086 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001087
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001089 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090
1091 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001092 if (hostView == null) {
1093 // Perform actual inflation because we're live
1094 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1095 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1096 } else {
1097 // The AppWidgetHostView has already been inflated and instantiated
1098 launcherInfo.hostView = hostView;
1099 }
Romain Guycbb89e42009-06-08 15:52:54 -07001100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001102 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohena897f392012-04-27 18:12:05 -07001103 AppWidgetResizeFrame.updateWidgetSizeRanges(launcherInfo.hostView,
1104 this, launcherInfo.spanX, launcherInfo.spanY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001105 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001106 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001107
1108 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001110 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
Romain Guycbb89e42009-06-08 15:52:54 -07001112
Adam Cohended9f8d2010-11-03 13:25:16 -07001113 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1114 @Override
1115 public void onReceive(Context context, Intent intent) {
1116 final String action = intent.getAction();
1117 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1118 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001119 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001120 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001121
Winson Chungc100e8e2011-08-09 16:02:43 -07001122 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001123 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001124 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1125 mAppsCustomizeTabHost.reset();
1126 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001127 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001128 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1129 mUserPresent = true;
1130 updateRunning();
1131 }
1132 }
1133 };
1134
1135 @Override
1136 public void onAttachedToWindow() {
1137 super.onAttachedToWindow();
1138
1139 // Listen for broadcasts related to user-presence
1140 final IntentFilter filter = new IntentFilter();
1141 filter.addAction(Intent.ACTION_SCREEN_OFF);
1142 filter.addAction(Intent.ACTION_USER_PRESENT);
1143 registerReceiver(mReceiver, filter);
1144
Adam Cohend113e0c2010-11-11 10:48:05 -08001145 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001146 mVisible = true;
1147 }
1148
1149 @Override
1150 public void onDetachedFromWindow() {
1151 super.onDetachedFromWindow();
1152 mVisible = false;
1153
Adam Cohend113e0c2010-11-11 10:48:05 -08001154 if (mAttached) {
1155 unregisterReceiver(mReceiver);
1156 mAttached = false;
1157 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001158 updateRunning();
1159 }
1160
1161 public void onWindowVisibilityChanged(int visibility) {
1162 mVisible = visibility == View.VISIBLE;
1163 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001164 // The following code used to be in onResume, but it turns out onResume is called when
1165 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1166 // is a more appropriate event to handle
1167 if (mVisible) {
1168 mAppsCustomizeTabHost.onWindowVisible();
1169 if (!mWorkspaceLoading) {
1170 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001171 // We want to let Launcher draw itself at least once before we force it to build
1172 // layers on all the workspace pages, so that transitioning to Launcher from other
1173 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1174 // a true draw so we wait until the second preDraw call to be safe
1175 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001176 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001177 // We delay the layer building a bit in order to give
1178 // other message processing a time to run. In particular
1179 // this avoids a delay in hiding the IME if it was
1180 // currently shown, because doing that may involve
1181 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001182 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001183
1184 // We had to enable the wallpaper visibility when launching apps from all
1185 // apps (so that the transitions would be the same as when launching from
1186 // workspace) so take this time to see if we need to re-disable the
1187 // wallpaper visibility to ensure performance.
1188 mWorkspace.post(new Runnable() {
1189 @Override
1190 public void run() {
1191 if (mState == State.APPS_CUSTOMIZE) {
1192 if (mAppsCustomizeTabHost != null &&
1193 !mAppsCustomizeTabHost.isTransitioning()) {
1194 updateWallpaperVisibility(false);
1195 }
1196 }
1197 }
1198 });
1199
Michael Jurka6ee21d22012-02-21 18:20:27 -08001200 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001201 return true;
1202 }
1203 });
1204 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001205 // When Launcher comes back to foreground, a different Activity might be responsible for
1206 // the app market intent, so refresh the icon
1207 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001208 clearTypedText();
1209 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001210 }
1211
1212 private void sendAdvanceMessage(long delay) {
1213 mHandler.removeMessages(ADVANCE_MSG);
1214 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1215 mHandler.sendMessageDelayed(msg, delay);
1216 mAutoAdvanceSentTime = System.currentTimeMillis();
1217 }
1218
1219 private void updateRunning() {
1220 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1221 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1222 mAutoAdvanceRunning = autoAdvanceRunning;
1223 if (autoAdvanceRunning) {
1224 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1225 sendAdvanceMessage(delay);
1226 } else {
1227 if (!mWidgetsToAdvance.isEmpty()) {
1228 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1229 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1230 }
1231 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001232 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001233 }
1234 }
1235 }
1236
1237 private final Handler mHandler = new Handler() {
1238 @Override
1239 public void handleMessage(Message msg) {
1240 if (msg.what == ADVANCE_MSG) {
1241 int i = 0;
1242 for (View key: mWidgetsToAdvance.keySet()) {
1243 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1244 final int delay = mAdvanceStagger * i;
1245 if (v instanceof Advanceable) {
1246 postDelayed(new Runnable() {
1247 public void run() {
1248 ((Advanceable) v).advance();
1249 }
1250 }, delay);
1251 }
1252 i++;
1253 }
1254 sendAdvanceMessage(mAdvanceInterval);
1255 }
1256 }
1257 };
1258
1259 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001260 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001261 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1262 if (v instanceof Advanceable) {
1263 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001264 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001265 updateRunning();
1266 }
1267 }
1268
1269 void removeWidgetToAutoAdvance(View hostView) {
1270 if (mWidgetsToAdvance.containsKey(hostView)) {
1271 mWidgetsToAdvance.remove(hostView);
1272 updateRunning();
1273 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001274 }
1275
Joe Onorato9c1289c2009-08-17 11:03:03 -04001276 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001277 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001278 launcherInfo.hostView = null;
1279 }
1280
Winson Chung93eef082012-03-23 15:59:27 -07001281 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1282 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1283 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001284 }
1285
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001286 public LauncherAppWidgetHost getAppWidgetHost() {
1287 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 }
Romain Guycbb89e42009-06-08 15:52:54 -07001289
Winson Chunga9abd0e2010-10-27 17:18:37 -07001290 public LauncherModel getModel() {
1291 return mModel;
1292 }
1293
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001294 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001295 getWindow().closeAllPanels();
1296
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001297 // Whatever we were doing is hereby canceled.
1298 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001299 }
1300
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 @Override
1302 protected void onNewIntent(Intent intent) {
1303 super.onNewIntent(intent);
1304
1305 // Close the menu
1306 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001307 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001308 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001309
Joe Onorato14f122b2009-11-19 14:06:36 -08001310 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1311 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001312
Adam Cohenac56cff2011-09-28 20:45:37 -07001313 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001314 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001315 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001316 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1317 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001318 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001319 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001320
1321 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001322 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001323 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001324
Joe Onorato3a8820b2009-11-10 15:06:42 -08001325 final View v = getWindow().peekDecorView();
1326 if (v != null && v.getWindowToken() != null) {
1327 InputMethodManager imm = (InputMethodManager)getSystemService(
1328 INPUT_METHOD_SERVICE);
1329 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001331
Michael Jurka35aa14d2011-07-07 17:01:08 -07001332 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001333 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001334 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001335 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 }
1337 }
1338
1339 @Override
1340 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1341 // Do not call super here
1342 mSavedInstanceState = savedInstanceState;
1343 }
1344
1345 @Override
1346 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001347 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001348 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349
Michael Jurka883f55b2010-10-21 15:47:14 -07001350 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001351 // We close any open folder since it will not be re-opened, and we need to make sure
1352 // this state is reflected.
1353 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354
Winson Chung3d503fb2011-07-13 17:25:49 -07001355 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1356 mWaitingForResult) {
1357 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1358 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1359 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1360 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 }
1362
1363 if (mFolderInfo != null && mWaitingForResult) {
1364 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1365 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1366 }
Michael Jurka946ad472010-07-09 18:05:18 -07001367
Winson Chung785d2eb2011-04-14 16:08:02 -07001368 // Save the current AppsCustomize tab
1369 if (mAppsCustomizeTabHost != null) {
1370 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1371 if (currentTabTag != null) {
1372 outState.putString("apps_customize_currentTab", currentTabTag);
1373 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001374 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1375 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001376 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 }
1378
1379 @Override
1380 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001382
Winson Chunge7a03942011-08-05 15:05:12 -07001383 // Remove all pending runnables
1384 mHandler.removeMessages(ADVANCE_MSG);
1385 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001386 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001387
Winson Chungcd2b0142011-06-08 16:02:26 -07001388 // Stop callbacks from LauncherModel
1389 LauncherApplication app = ((LauncherApplication) getApplication());
1390 mModel.stopLoader();
1391 app.setLauncher(null);
1392
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001394 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001396 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001398 mAppWidgetHost = null;
1399
1400 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401
1402 TextKeyListener.getInstance().release();
1403
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404
Winson Chung3d503fb2011-07-13 17:25:49 -07001405 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001406
1407 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001408 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001409
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001410 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001411 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1412 mWorkspace.removeAllViews();
1413 mWorkspace = null;
1414 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001415
1416 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 }
1418
Adam Cohena9cf38f2011-05-02 15:36:58 -07001419 public DragController getDragController() {
1420 return mDragController;
1421 }
1422
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 @Override
1424 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001425 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 super.startActivityForResult(intent, requestCode);
1427 }
1428
Winson Chung70d72102011-08-12 11:24:35 -07001429 /**
1430 * Indicates that we want global search for this activity by setting the globalSearch
1431 * argument for {@link #startSearch} to true.
1432 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001434 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001436
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001437 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001438
Karl Rosaen138a0412009-04-23 19:00:21 -07001439 if (initialQuery == null) {
1440 // Use any text typed in the launcher as the initial query
1441 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001442 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 if (appSearchData == null) {
1444 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001445 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001447 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001448
Karl Rosaen138a0412009-04-23 19:00:21 -07001449 final SearchManager searchManager =
1450 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001451 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001452 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 }
1454
Winson Chung70d72102011-08-12 11:24:35 -07001455 @Override
1456 public boolean onCreateOptionsMenu(Menu menu) {
1457 if (isWorkspaceLocked()) {
1458 return false;
1459 }
1460
1461 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001462
1463 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1464 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1465 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001466 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1467 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1468 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001469 String helpUrl = getString(R.string.help_url);
1470 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001471 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1472 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1473
Winson Chung70d72102011-08-12 11:24:35 -07001474 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1475 .setIcon(android.R.drawable.ic_menu_gallery)
1476 .setAlphabeticShortcut('W');
1477 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1478 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001479 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001480 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001481 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1482 .setIcon(android.R.drawable.ic_menu_preferences)
1483 .setIntent(settings)
1484 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001485 if (!helpUrl.isEmpty()) {
1486 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1487 .setIcon(android.R.drawable.ic_menu_help)
1488 .setIntent(help)
1489 .setAlphabeticShortcut('H');
1490 }
Winson Chung70d72102011-08-12 11:24:35 -07001491 return true;
1492 }
1493
1494 @Override
1495 public boolean onPrepareOptionsMenu(Menu menu) {
1496 super.onPrepareOptionsMenu(menu);
1497
1498 if (mAppsCustomizeTabHost.isTransitioning()) {
1499 return false;
1500 }
1501 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1502 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1503
1504 return true;
1505 }
1506
1507 @Override
1508 public boolean onOptionsItemSelected(MenuItem item) {
1509 switch (item.getItemId()) {
1510 case MENU_WALLPAPER_SETTINGS:
1511 startWallpaper();
1512 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001513 }
1514
1515 return super.onOptionsItemSelected(item);
1516 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001518 @Override
1519 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001520 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001521 // Use a custom animation for launching search
1522 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001523 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001524 }
1525
Joe Onorato9c1289c2009-08-17 11:03:03 -04001526 public boolean isWorkspaceLocked() {
1527 return mWorkspaceLoading || mWaitingForResult;
1528 }
1529
Michael Jurka0280c3b2010-09-17 15:00:07 -07001530 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001531 mPendingAddInfo.container = ItemInfo.NO_ID;
1532 mPendingAddInfo.screen = -1;
1533 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1534 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001535 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001536 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001537 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001538
Michael Jurkaaf442092010-06-10 17:01:57 -07001539 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001540 // TODO: catch bad widget exception when sent
1541 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001542 // TODO: Is this log message meaningful?
1543 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001544 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001545 }
1546
Adam Cohened66b2b2012-01-23 17:28:51 -08001547 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1548 final AppWidgetProviderInfo appWidget = info.info;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001549 if (appWidget.configure != null) {
1550 // Launch over to configure widget, if needed
1551 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1552 intent.setComponent(appWidget.configure);
1553 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001554 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001555 mWidgetBeingBoundOrConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001557 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001558 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001559 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001560 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 }
1562 }
Romain Guycbb89e42009-06-08 15:52:54 -07001563
Adam Cohenfbba09b2011-07-18 21:43:05 -07001564 /**
1565 * Process a shortcut drop.
1566 *
1567 * @param componentName The name of the component
1568 * @param screen The screen where it should be added
1569 * @param cell The cell it should be added to, optional
1570 * @param position The location on the screen where it was dropped, optional
1571 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001572 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1573 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001574 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001575 mPendingAddInfo.container = container;
1576 mPendingAddInfo.screen = screen;
1577 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001578
1579 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001580 mPendingAddInfo.cellX = cell[0];
1581 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001582 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001583
1584 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1585 createShortcutIntent.setComponent(componentName);
1586 processShortcut(createShortcutIntent);
1587 }
1588
Adam Cohenfbba09b2011-07-18 21:43:05 -07001589 /**
1590 * Process a widget drop.
1591 *
1592 * @param info The PendingAppWidgetInfo of the widget being added.
1593 * @param screen The screen where it should be added
1594 * @param cell The cell it should be added to, optional
1595 * @param position The location on the screen where it was dropped, optional
1596 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001597 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001598 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001599 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001600 mPendingAddInfo.container = info.container = container;
1601 mPendingAddInfo.screen = info.screen = screen;
1602 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001603 mPendingAddInfo.minSpanX = info.minSpanX;
1604 mPendingAddInfo.minSpanY = info.minSpanY;
1605
Adam Cohenfbba09b2011-07-18 21:43:05 -07001606 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001607 mPendingAddInfo.cellX = cell[0];
1608 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001609 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001610 if (span != null) {
1611 mPendingAddInfo.spanX = span[0];
1612 mPendingAddInfo.spanY = span[1];
1613 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001614
Adam Cohened66b2b2012-01-23 17:28:51 -08001615 AppWidgetHostView hostView = info.boundWidget;
1616 int appWidgetId;
1617 if (hostView != null) {
1618 appWidgetId = hostView.getAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001619 addAppWidgetImpl(appWidgetId, info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001620 } else {
1621 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001622 mWidgetBeingBoundOrConfigured = info;
1623 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
1624 addAppWidgetImpl(appWidgetId, info);
1625 } else {
1626 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1627 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1628 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1629 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1630 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001631 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001632 }
1633
Joe Onoratodeb98af2010-02-19 14:59:39 -08001634 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001635 // Handle case where user selected "Applications"
1636 String applicationName = getResources().getString(R.string.group_applications);
1637 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001638
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001639 if (applicationName != null && applicationName.equals(shortcutName)) {
1640 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1641 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001642
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001643 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1644 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001645 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001646 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001647 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001648 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001649 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 }
1651
Winson Chung24ab2f12010-09-16 14:10:47 -07001652 void processWallpaper(Intent intent) {
1653 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1654 }
1655
Winson Chung3d503fb2011-07-13 17:25:49 -07001656 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1657 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001658 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 folderInfo.title = getText(R.string.folder_name);
1660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001662 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1663 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001664 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665
1666 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001667 FolderIcon newFolder =
1668 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1669 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1670 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001671 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 }
Romain Guycbb89e42009-06-08 15:52:54 -07001673
Joe Onorato9c1289c2009-08-17 11:03:03 -04001674 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001675 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001676 }
1677
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001679 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001681 Intent chooser = Intent.createChooser(pickWallpaper,
1682 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001683 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1684 // Removed in Eclair MR1
1685// WallpaperManager wm = (WallpaperManager)
1686// getSystemService(Context.WALLPAPER_SERVICE);
1687// WallpaperInfo wi = wm.getWallpaperInfo();
1688// if (wi != null && wi.getSettingsActivity() != null) {
1689// LabeledIntent li = new LabeledIntent(getPackageName(),
1690// R.string.configure_wallpaper, 0);
1691// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1692// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1693// }
Mike Clerona0618e42009-10-22 13:55:21 -07001694 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 }
1696
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001697 /**
1698 * Registers various content observers. The current implementation registers
1699 * only a favorites observer to keep track of the favorites applications.
1700 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001701 private void registerContentObservers() {
1702 ContentResolver resolver = getContentResolver();
1703 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1704 true, mWidgetObserver);
1705 }
1706
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 @Override
1708 public boolean dispatchKeyEvent(KeyEvent event) {
1709 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1710 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001712 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001713 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001714 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001715 dumpState();
1716 return true;
1717 }
1718 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001719 }
1720 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1721 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001722 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 return true;
1724 }
1725 }
1726
1727 return super.dispatchKeyEvent(event);
1728 }
1729
Joe Onorato88ec0992009-11-19 13:16:06 -08001730 @Override
1731 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001732 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001733 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001734 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001735 Folder openFolder = mWorkspace.getOpenFolder();
1736 if (openFolder.isEditingName()) {
1737 openFolder.dismissEditingName();
1738 } else {
1739 closeFolder();
1740 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001741 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001742 mWorkspace.exitWidgetResizeMode();
1743
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001744 // Back button is a no-op here, but give at least some feedback for the button press
1745 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001746 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001747 }
1748
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001750 * Re-listen when widgets are reset.
1751 */
1752 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001753 if (mAppWidgetHost != null) {
1754 mAppWidgetHost.startListening();
1755 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001756 }
1757
1758 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001759 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1760 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001762 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001763 if (mModel != null) {
1764 mModel.unbindWorkspaceItems();
1765 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001767
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 /**
1769 * Launches the intent referred by the clicked shortcut.
1770 *
1771 * @param v The view representing the clicked shortcut.
1772 */
1773 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001774 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1775 // view has detached (it's possible for this to happen if the view is removed mid touch).
1776 if (v.getWindowToken() == null) {
1777 return;
1778 }
1779
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001780 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001781 return;
1782 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001783
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001785 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001787 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001788 int[] pos = new int[2];
1789 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001790 intent.setSourceBounds(new Rect(pos[0], pos[1],
1791 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001792
1793 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001794
1795 if (success && v instanceof BubbleTextView) {
1796 mWaitingForResume = (BubbleTextView) v;
1797 mWaitingForResume.setStayPressed(true);
1798 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001800 if (v instanceof FolderIcon) {
1801 FolderIcon fi = (FolderIcon) v;
1802 handleFolderClick(fi);
1803 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001804 } else if (v == mAllAppsButton) {
1805 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001806 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001807 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001808 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001809 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 }
1811 }
1812
Michael Jurka0e260592010-06-30 17:07:39 -07001813 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001814 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001815 // clicking anywhere on the workspace causes the customization drawer to slide down
1816 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001817 return false;
1818 }
1819
Michael Jurkaaf442092010-06-10 17:01:57 -07001820 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001821 * Event handler for the search button
1822 *
1823 * @param v The view that was clicked.
1824 */
1825 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001826 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1827
Amith Yamasania135ba82011-08-09 17:42:01 -07001828 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001829 }
1830
1831 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001832 * Event handler for the voice button
1833 *
1834 * @param v The view that was clicked.
1835 */
1836 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001837 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001838
Michael Jurkaae65ee32012-04-30 11:45:54 -07001839 try {
1840 final SearchManager searchManager =
1841 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1842 ComponentName activityName = searchManager.getGlobalSearchActivity();
1843 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1844 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1845 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1846 if (activityName != null) {
1847 intent.setPackage(activityName.getPackageName());
1848 }
1849 startActivitySafely(null, intent, "onClickVoiceButton");
1850 } catch (ActivityNotFoundException e) {
1851 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1852 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1853 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1854 startActivitySafely(null, intent, "onClickVoiceButton");
1855 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001856 }
1857
1858 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001859 * Event handler for the "grid" button that appears on the home screen, which
1860 * enters all apps mode.
1861 *
1862 * @param v The view that was clicked.
1863 */
1864 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001865 showAllApps(true);
1866 }
1867
1868 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001869 // Provide the same haptic feedback that the system offers for virtual keys.
1870 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001871 }
1872
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001873 public void onClickAppMarketButton(View v) {
1874 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001875 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001876 } else {
1877 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001878 }
1879 }
1880
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001881 void startApplicationDetailsActivity(ComponentName componentName) {
1882 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001883 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1884 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001885 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001886 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001887 }
1888
Patrick Dubroy5539af72010-09-07 15:22:01 -07001889 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1890 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1891 // System applications cannot be installed. For now, show a toast explaining that.
1892 // We may give them the option of disabling apps this way.
1893 int messageId = R.string.uninstall_system_app_text;
1894 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1895 } else {
1896 String packageName = appInfo.componentName.getPackageName();
1897 String className = appInfo.componentName.getClassName();
1898 Intent intent = new Intent(
1899 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001900 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1901 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001902 startActivity(intent);
1903 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001904 }
1905
Winson Chungc7450e32012-04-17 17:34:08 -07001906 boolean startActivitySafely(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001908
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 try {
Winson Chungc7450e32012-04-17 17:34:08 -07001910 if (v != null) {
1911 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1912 v.getMeasuredWidth(), v.getMeasuredHeight());
1913
1914 startActivity(intent, opts.toBundle());
1915 } else {
1916 startActivity(intent);
1917 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001918 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 } catch (ActivityNotFoundException e) {
1920 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001921 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 } catch (SecurityException e) {
1923 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001924 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001926 "or use the exported attribute for this activity. "
1927 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001929 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001931
Romain Guy8e633c52010-03-04 12:51:36 -08001932 void startActivityForResultSafely(Intent intent, int requestCode) {
1933 try {
1934 startActivityForResult(intent, requestCode);
1935 } catch (ActivityNotFoundException e) {
1936 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1937 } catch (SecurityException e) {
1938 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1939 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1940 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1941 "or use the exported attribute for this activity.", e);
1942 }
1943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944
Adam Cohena9cf38f2011-05-02 15:36:58 -07001945 private void handleFolderClick(FolderIcon folderIcon) {
1946 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001947 Folder openFolder = mWorkspace.getFolderForTag(info);
1948
1949 // If the folder info reports that the associated folder is open, then verify that
1950 // it is actually opened. There have been a few instances where this gets out of sync.
1951 if (info.opened && openFolder == null) {
1952 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1953 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1954 info.opened = false;
1955 }
1956
Adam Cohena9cf38f2011-05-02 15:36:58 -07001957 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 // Close any open folder
1959 closeFolder();
1960 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001961 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 } else {
1963 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 int folderScreen;
1965 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001966 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 // .. and close it
1968 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001969 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 // Close any folder open on the current screen
1971 closeFolder();
1972 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001973 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 }
1975 }
1976 }
1977 }
1978
Adam Cohen2801caf2011-05-13 20:57:39 -07001979 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001980 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001981 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1982 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1983 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1984
Adam Cohenc51934b2011-07-26 21:07:43 -07001985 FolderInfo info = (FolderInfo) fi.getTag();
1986 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1987 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001988 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1989 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001990 }
1991
Adam Cohen2801caf2011-05-13 20:57:39 -07001992 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1993 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1994 oa.start();
1995 }
1996
1997 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001998 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001999 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2000 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2001 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2002
Adam Cohenc51934b2011-07-26 21:07:43 -07002003 FolderInfo info = (FolderInfo) fi.getTag();
2004 CellLayout cl = null;
2005 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2006 cl = (CellLayout) fi.getParent().getParent();
2007 }
2008
2009 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07002010 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2011 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002012 oa.addListener(new AnimatorListenerAdapter() {
2013 @Override
2014 public void onAnimationEnd(Animator animation) {
2015 if (layout != null) {
2016 layout.clearFolderLeaveBehind();
2017 }
2018 }
2019 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002020 oa.start();
2021 }
2022
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002024 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 * is animated relative to the specified View. If the View is null, no animation
2026 * is played.
2027 *
2028 * @param folderInfo The FolderInfo describing the folder to open.
2029 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002030 public void openFolder(FolderIcon folderIcon) {
2031 Folder folder = folderIcon.mFolder;
2032 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033
Adam Cohen2801caf2011-05-13 20:57:39 -07002034 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002035 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036
Adam Cohen4554ee12011-08-03 16:13:21 -07002037 // Just verify that the folder hasn't already been added to the DragLayer.
2038 // There was a one-off crash where the folder had a parent already.
2039 if (folder.getParent() == null) {
2040 mDragLayer.addView(folder);
2041 mDragController.addDropTarget((DropTarget) folder);
2042 } else {
2043 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2044 folder.getParent() + ").");
2045 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002046 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002047 }
2048
2049 public void closeFolder() {
2050 Folder folder = mWorkspace.getOpenFolder();
2051 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002052 if (folder.isEditingName()) {
2053 folder.dismissEditingName();
2054 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002055 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002056
2057 // Dismiss the folder cling
2058 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002059 }
2060 }
2061
2062 void closeFolder(Folder folder) {
2063 folder.getInfo().opened = false;
2064
2065 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2066 if (parent != null) {
2067 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2068 shrinkAndFadeInFolderIcon(fi);
2069 }
2070 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 }
2072
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002074
Patrick Dubroye708c522011-03-01 16:03:43 -08002075 if (mState != State.WORKSPACE) {
2076 return false;
2077 }
2078
Joe Onorato9c1289c2009-08-17 11:03:03 -04002079 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 return false;
2081 }
2082
2083 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002084 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 }
2086
Michael Jurka0280c3b2010-09-17 15:00:07 -07002087 resetAddInfo();
2088 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002090 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 return true;
2092 }
2093
Winson Chung3d503fb2011-07-13 17:25:49 -07002094 // The hotseat touch handling does not go through Workspace, and we always allow long press
2095 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002096 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002097 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2098 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002099 if (itemUnderLongClick == null) {
2100 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002101 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2102 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002103 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002105 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002107 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 }
2109 }
2110 }
2111 return true;
2112 }
2113
Winson Chung3d503fb2011-07-13 17:25:49 -07002114 boolean isHotseatLayout(View layout) {
2115 return mHotseat != null && layout != null &&
2116 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2117 }
2118 Hotseat getHotseat() {
2119 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002120 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002121 SearchDropTargetBar getSearchBar() {
2122 return mSearchDropTargetBar;
2123 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002124
Winson Chung3d503fb2011-07-13 17:25:49 -07002125 /**
2126 * Returns the CellLayout of the specified container at the specified screen.
2127 */
2128 CellLayout getCellLayout(long container, int screen) {
2129 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2130 if (mHotseat != null) {
2131 return mHotseat.getLayout();
2132 } else {
2133 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002134 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002135 } else {
2136 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002137 }
2138 }
2139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 Workspace getWorkspace() {
2141 return mWorkspace;
2142 }
2143
Daniel Sandler843e8602010-06-07 14:59:01 -04002144 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2145 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002146 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002147 }
2148
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002149 public boolean isAllAppsButtonRank(int rank) {
2150 return mHotseat.isAllAppsButtonRank(rank);
2151 }
2152
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002153 // AllAppsView.Watcher
2154 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002155 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002156 mWorkspace.setVisibility(View.GONE);
2157 }
2158 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002159
2160 /**
2161 * Helper method for the cameraZoomIn/cameraZoomOut animations
2162 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002163 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002164 * @param scaleFactor The scale factor used for the zoom
2165 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002166 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002167 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002168 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002169 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002170
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002171 void updateWallpaperVisibility(boolean visible) {
2172 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2173 int curflags = getWindow().getAttributes().flags
2174 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2175 if (wpflags != curflags) {
2176 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2177 }
2178 }
2179
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002180 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2181 if (v instanceof LauncherTransitionable) {
2182 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2183 }
2184 }
2185
Michael Jurkabed61d22012-02-14 22:51:29 -08002186 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2187 if (v instanceof LauncherTransitionable) {
2188 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2189 }
Winson Chung70442722012-02-10 15:43:22 -08002190
2191 // Update the workspace transition step as well
2192 dispatchOnLauncherTransitionStep(v, 0f);
2193 }
2194
2195 private void dispatchOnLauncherTransitionStep(View v, float t) {
2196 if (v instanceof LauncherTransitionable) {
2197 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2198 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002199 }
2200
2201 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2202 if (v instanceof LauncherTransitionable) {
2203 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2204 }
Winson Chung70442722012-02-10 15:43:22 -08002205
2206 // Update the workspace transition step as well
2207 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002208 }
2209
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002210 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002211 * Things to test when changing the following seven functions.
2212 * - Home from workspace
2213 * - from center screen
2214 * - from other screens
2215 * - Home from all apps
2216 * - from center screen
2217 * - from other screens
2218 * - Back from all apps
2219 * - from center screen
2220 * - from other screens
2221 * - Launch app from workspace and quit
2222 * - with back
2223 * - with home
2224 * - Launch app from all apps and quit
2225 * - with back
2226 * - with home
2227 * - Go to a screen that's not the default, then all
2228 * apps, and launch and app, and go back
2229 * - with back
2230 * -with home
2231 * - On workspace, long press power and go back
2232 * - with back
2233 * - with home
2234 * - On all apps, long press power and go back
2235 * - with back
2236 * - with home
2237 * - On workspace, power off
2238 * - On all apps, power off
2239 * - Launch an app and turn off the screen while in that app
2240 * - Go back with home key
2241 * - Go back with back key TODO: make this not go to workspace
2242 * - From all apps
2243 * - From workspace
2244 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2245 * - From all apps
2246 * - From the center workspace
2247 * - From another workspace
2248 */
2249
2250 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002251 * Zoom the camera out from the workspace to reveal 'toView'.
2252 * Assumes that the view to show is anchored at either the very top or very bottom
2253 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002254 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002255 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002256 if (mStateAnimation != null) {
2257 mStateAnimation.cancel();
2258 mStateAnimation = null;
2259 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002260 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002261
Winson Chungf0ea4d32011-06-06 14:27:16 -07002262 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2263 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2264 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002265 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002266 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002267 final int startDelay =
2268 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002269
Michael Jurkab3e22d92011-10-31 15:58:33 -07002270 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002271
Michael Jurkad74c9842011-07-10 12:44:21 -07002272 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002273 Animator workspaceAnim =
2274 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002275
2276 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002277 toView.setScaleX(scale);
2278 toView.setScaleY(scale);
2279 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2280 scaleAnim.
2281 scaleX(1f).scaleY(1f).
2282 setDuration(duration).
2283 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002284
Winson Chungf0ea4d32011-06-06 14:27:16 -07002285 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002286 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002287 final ObjectAnimator alphaAnim = ObjectAnimator
2288 .ofFloat(toView, "alpha", 0f, 1f)
2289 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002290 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002291 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2292 @Override
2293 public void onAnimationUpdate(ValueAnimator animation) {
2294 float t = (Float) animation.getAnimatedValue();
2295 dispatchOnLauncherTransitionStep(fromView, t);
2296 dispatchOnLauncherTransitionStep(toView, t);
2297 }
2298 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002299
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002300 // toView should appear right at the end of the workspace shrink
2301 // animation
2302 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002303 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002304 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002305
2306 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002307 boolean animationCancelled = false;
2308
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002309 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002310 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002311 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002312 // Prepare the position
2313 toView.setTranslationX(0.0f);
2314 toView.setTranslationY(0.0f);
2315 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002316 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002317 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002318 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002319 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002320 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2321 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002322
2323 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2324 // Hide the workspace scrollbar
2325 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002326 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002327 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002328 if (!animationCancelled) {
2329 updateWallpaperVisibility(false);
2330 }
2331 }
2332
Adam Cohencff6af82011-09-13 14:51:53 -07002333 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002334 public void onAnimationCancel(Animator animation) {
2335 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002336 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002337 });
2338
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002339 if (workspaceAnim != null) {
2340 mStateAnimation.play(workspaceAnim);
2341 }
Michael Jurka1899a362011-11-03 13:50:45 -07002342
2343 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002344 final ViewTreeObserver observer;
2345
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002346 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2347 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002348
2349 // If any of the objects being animated haven't been measured/laid out
2350 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002351 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002352 (mWorkspace.getMeasuredWidth() == 0) ||
2353 (toView.getMeasuredWidth() == 0)) {
2354 observer = mWorkspace.getViewTreeObserver();
2355 delayAnim = true;
2356 } else {
2357 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002358 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002359
2360 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002361 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002362 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2363 public void onGlobalLayout() {
2364 mWorkspace.post(new Runnable() {
2365 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002366 // Check that mStateAnimation hasn't changed while
2367 // we waited for a layout pass
2368 if (mStateAnimation == stateAnimation) {
2369 // Need to update pivots for zoom if layout changed
2370 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002371 dispatchOnLauncherTransitionStart(fromView, animated, false);
2372 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka49779a12012-01-16 04:10:08 -08002373 mStateAnimation.start();
2374 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002375 }
2376 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002377 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002378 }
2379 };
2380 observer.addOnGlobalLayoutListener(delayedStart);
2381 } else {
2382 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002383 dispatchOnLauncherTransitionStart(fromView, animated, false);
2384 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka1899a362011-11-03 13:50:45 -07002385 mStateAnimation.start();
2386 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002387 } else {
2388 toView.setTranslationX(0.0f);
2389 toView.setTranslationY(0.0f);
2390 toView.setScaleX(1.0f);
2391 toView.setScaleY(1.0f);
2392 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002393 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002394
Michael Jurkabed61d22012-02-14 22:51:29 -08002395 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2396 // Hide the workspace scrollbar
2397 mWorkspace.hideScrollingIndicator(true);
2398 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002399 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002400 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002401 dispatchOnLauncherTransitionStart(fromView, animated, false);
2402 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002403 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002404 dispatchOnLauncherTransitionStart(toView, animated, false);
2405 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002406 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002407 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002408 }
2409
2410 /**
2411 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002412 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002413 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002414 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002415 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2416 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002417
Michael Jurkab3e22d92011-10-31 15:58:33 -07002418 if (mStateAnimation != null) {
2419 mStateAnimation.cancel();
2420 mStateAnimation = null;
2421 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002422 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002423
Winson Chungf0ea4d32011-06-06 14:27:16 -07002424 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002425 final int fadeOutDuration =
2426 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002427 final float scaleFactor = (float)
2428 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2429 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002430 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002431 Animator workspaceAnim = null;
2432
2433 if (toState == State.WORKSPACE) {
2434 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2435 workspaceAnim = mWorkspace.getChangeStateAnimation(
2436 Workspace.State.NORMAL, animated, stagger);
2437 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2438 workspaceAnim = mWorkspace.getChangeStateAnimation(
2439 Workspace.State.SPRING_LOADED, animated);
2440 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002441
Michael Jurkab3e22d92011-10-31 15:58:33 -07002442 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002443 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002444 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002445 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002446 final LauncherViewPropertyAnimator scaleAnim =
2447 new LauncherViewPropertyAnimator(fromView);
2448 scaleAnim.
2449 scaleX(scaleFactor).scaleY(scaleFactor).
2450 setDuration(duration).
2451 setInterpolator(new Workspace.ZoomInInterpolator());
2452
2453 final ObjectAnimator alphaAnim = ObjectAnimator
2454 .ofFloat(fromView, "alpha", 1f, 0f)
2455 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002456 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002457 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2458 @Override
2459 public void onAnimationUpdate(ValueAnimator animation) {
2460 float t = 1f - (Float) animation.getAnimatedValue();
2461 dispatchOnLauncherTransitionStep(fromView, t);
2462 dispatchOnLauncherTransitionStep(toView, t);
2463 }
2464 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002465
Michael Jurkabed61d22012-02-14 22:51:29 -08002466 mStateAnimation = new AnimatorSet();
2467
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002468 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2469 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002470
2471 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002472 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002473 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002474 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002475 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002476 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2477 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002478 if (mWorkspace != null) {
2479 mWorkspace.hideScrollingIndicator(false);
2480 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002481 if (onCompleteRunnable != null) {
2482 onCompleteRunnable.run();
2483 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002484 }
2485 });
2486
Winson Chungf0ea4d32011-06-06 14:27:16 -07002487 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002488 if (workspaceAnim != null) {
2489 mStateAnimation.play(workspaceAnim);
2490 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002491 dispatchOnLauncherTransitionStart(fromView, animated, true);
2492 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002493 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002494 } else {
2495 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002496 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002497 dispatchOnLauncherTransitionStart(fromView, animated, true);
2498 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002499 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002500 dispatchOnLauncherTransitionStart(toView, animated, true);
2501 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002502 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002503 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002504 }
2505
Michael Jurkae326f182011-11-21 14:05:46 -08002506 @Override
2507 public void onTrimMemory(int level) {
2508 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002509 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002510 mAppsCustomizeTabHost.onTrimMemory();
2511 }
2512 }
2513
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002514 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002515 showWorkspace(animated, null);
2516 }
2517
2518 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002519 if (mState != State.WORKSPACE) {
2520 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002521 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002522
2523 // Show the search bar and hotseat
2524 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002525 // We only need to animate in the dock divider if we're going from spring loaded mode
2526 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002527
2528 // Set focus to the AppsCustomize button
2529 if (mAllAppsButton != null) {
2530 mAllAppsButton.requestFocus();
2531 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002532 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002533
Michael Jurkab737ee62011-11-15 15:57:22 -08002534 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002535
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002536 // Change the state *after* we've called all the transition code
2537 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002538
Winson Chung5fb63472011-02-02 17:03:37 -08002539 // Resume the auto-advance of widgets
2540 mUserPresent = true;
2541 updateRunning();
2542
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002543 // send an accessibility event to announce the context change
2544 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002545 }
2546
Michael Jurkab3e22d92011-10-31 15:58:33 -07002547 void showAllApps(boolean animated) {
2548 if (mState != State.WORKSPACE) return;
2549
2550 showAppsCustomizeHelper(animated, false);
2551 mAppsCustomizeTabHost.requestFocus();
2552
2553 // Hide the search bar and hotseat
2554 mSearchDropTargetBar.hideSearchBar(animated);
2555
2556 // Change the state *after* we've called all the transition code
2557 mState = State.APPS_CUSTOMIZE;
2558
2559 // Pause the auto-advance of widgets until we are out of AllApps
2560 mUserPresent = false;
2561 updateRunning();
2562 closeFolder();
2563
2564 // Send an accessibility event to announce the context change
2565 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2566 }
2567
Adam Cohen7777d962011-08-18 18:58:38 -07002568 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002569 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002570 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002571 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002572 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002573 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002574 }
Adam Cohen7777d962011-08-18 18:58:38 -07002575
Adam Cohened66b2b2012-01-23 17:28:51 -08002576 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2577 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002578 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2579
Winson Chunge7a03942011-08-05 15:05:12 -07002580 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002581 @Override
2582 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002583 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002584 // Before we show workspace, hide all apps again because
2585 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2586 // clean up our state transition functions
2587 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002588 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002589 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002590 } else {
2591 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002592 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002593 }
2594 }, (extendedDelay ?
2595 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2596 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2597 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002598
Michael Jurkad3ef3062010-11-23 16:23:58 -08002599 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002600 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002601 final boolean animated = true;
2602 final boolean springLoaded = true;
2603 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002604 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002605 }
2606 // Otherwise, we are not in spring loaded mode, so don't do anything.
2607 }
2608
Michael Jurkab737ee62011-11-15 15:57:22 -08002609 void hideDockDivider() {
2610 if (mQsbDivider != null && mDockDivider != null) {
2611 mQsbDivider.setVisibility(View.INVISIBLE);
2612 mDockDivider.setVisibility(View.INVISIBLE);
2613 }
2614 }
2615
2616 void showDockDivider(boolean animated) {
2617 if (mQsbDivider != null && mDockDivider != null) {
2618 mQsbDivider.setVisibility(View.VISIBLE);
2619 mDockDivider.setVisibility(View.VISIBLE);
2620 if (mDividerAnimator != null) {
2621 mDividerAnimator.cancel();
2622 mQsbDivider.setAlpha(1f);
2623 mDockDivider.setAlpha(1f);
2624 mDividerAnimator = null;
2625 }
2626 if (animated) {
2627 mDividerAnimator = new AnimatorSet();
2628 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2629 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2630 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2631 mDividerAnimator.start();
2632 }
2633 }
2634 }
2635
Michael Jurkab3e22d92011-10-31 15:58:33 -07002636 void lockAllApps() {
2637 // TODO
2638 }
2639
2640 void unlockAllApps() {
2641 // TODO
2642 }
2643
Adam Cohenfc53cd22011-07-20 15:45:11 -07002644 public boolean isAllAppsCustomizeOpen() {
2645 return mState == State.APPS_CUSTOMIZE;
2646 }
2647
Winson Chungf0ea4d32011-06-06 14:27:16 -07002648 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002649 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002650 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002651 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002652 if (!LauncherApplication.isScreenLarge()) {
2653 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002654 if (mHotseat.getAlpha() != 1f) {
2655 int duration = mSearchDropTargetBar.getTransitionInDuration();
2656 mHotseat.animate().alpha(1f).setDuration(duration);
2657 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002658 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002659 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002660 }
2661 }
2662 }
2663
2664 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002665 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002666 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002667 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002668 if (!LauncherApplication.isScreenLarge()) {
2669 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002670 if (mHotseat.getAlpha() != 0f) {
2671 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2672 mHotseat.animate().alpha(0f).setDuration(duration);
2673 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002674 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002675 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002676 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002677 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002678 }
2679
Patrick Dubroy5f445422011-02-18 14:35:21 -08002680 /**
2681 * Add an item from all apps or customize onto the given workspace screen.
2682 * If layout is null, add to the current screen.
2683 */
2684 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002685 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002686 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002687 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002688 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002689
Winson Chungdff8ebb2011-09-08 17:25:31 -07002690 /** Maps the current orientation to an index for referencing orientation correct global icons */
2691 private int getCurrentOrientationIndexForGlobalIcons() {
2692 // default - 0, landscape - 1
2693 switch (getResources().getConfiguration().orientation) {
2694 case Configuration.ORIENTATION_LANDSCAPE:
2695 return 1;
2696 default:
2697 return 0;
2698 }
2699 }
2700
Michael Jurkaae65ee32012-04-30 11:45:54 -07002701 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002702 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002703 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002704 // Look for the toolbar icon specified in the activity meta-data
2705 Bundle metaData = packageManager.getActivityInfo(
2706 activityName, PackageManager.GET_META_DATA).metaData;
2707 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002708 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002709 if (iconResId != 0) {
2710 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002711 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002712 }
2713 }
2714 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002715 // This can happen if the activity defines an invalid drawable
2716 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2717 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002718 } catch (Resources.NotFoundException nfe) {
2719 // This can happen if the activity defines an invalid drawable
2720 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2721 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002722 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002723 return null;
2724 }
2725
2726 // if successful in getting icon, return it; otherwise, set button to use default drawable
2727 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002728 int buttonId, ComponentName activityName, int fallbackDrawableId,
2729 String toolbarResourceName) {
2730 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002731 Resources r = getResources();
2732 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2733 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002734
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002735 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002736 // If we were unable to find the icon via the meta-data, use a generic one
2737 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002738 toolbarIcon = r.getDrawable(fallbackDrawableId);
2739 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002740 if (button != null) {
2741 button.setCompoundDrawables(toolbarIcon, null, null, null);
2742 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002743 return null;
2744 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002745 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002746 if (button != null) {
2747 button.setCompoundDrawables(toolbarIcon, null, null, null);
2748 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002749 return toolbarIcon.getConstantState();
2750 }
2751 }
2752
2753 // if successful in getting icon, return it; otherwise, set button to use default drawable
2754 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002755 int buttonId, ComponentName activityName, int fallbackDrawableId,
2756 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002757 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002758 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002759
Michael Jurka19e0fc52011-07-22 18:00:21 -07002760 if (button != null) {
2761 // If we were unable to find the icon via the meta-data, use a
2762 // generic one
2763 if (toolbarIcon == null) {
2764 button.setImageResource(fallbackDrawableId);
2765 } else {
2766 button.setImageDrawable(toolbarIcon);
2767 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002768 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002769
2770 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2771
Michael Jurka0423dcf2010-10-05 14:56:18 -07002772 }
2773
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002774 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2775 TextView button = (TextView) findViewById(buttonId);
2776 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2777 }
2778
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002779 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002780 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002781 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002782 }
2783
Winson Chungbb185bd2011-11-21 12:31:42 -08002784 private void invalidatePressedFocusedStates(View container, View button) {
2785 if (container instanceof HolographicLinearLayout) {
2786 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2787 layout.invalidatePressedFocusedStates();
2788 } else if (button instanceof HolographicImageView) {
2789 HolographicImageView view = (HolographicImageView) button;
2790 view.invalidatePressedFocusedStates();
2791 }
2792 }
2793
Winson Chungc51db6a2011-10-05 11:44:49 -07002794 private boolean updateGlobalSearchIcon() {
2795 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002796 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2797 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002798 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002799 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002800 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002801
Winson Chung1cad91e2011-05-25 17:41:01 -07002802 final SearchManager searchManager =
2803 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2804 ComponentName activityName = searchManager.getGlobalSearchActivity();
2805 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002806 int coi = getCurrentOrientationIndexForGlobalIcons();
2807 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002808 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2809 TOOLBAR_SEARCH_ICON_METADATA_NAME);
2810 if (sGlobalSearchIcon[coi] == null) {
2811 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2812 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2813 TOOLBAR_ICON_METADATA_NAME);
2814 }
2815
Winson Chung649723c2011-07-06 20:41:23 -07002816 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002817 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2818 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002819 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002820 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002821 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002822 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002823 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002824 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2825 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2826 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002827 voiceButton.setVisibility(View.GONE);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002828 voiceButtonProxy.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002829 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002830 }
2831 }
2832
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002833 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002834 final View searchButtonContainer = findViewById(R.id.search_button_container);
2835 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002836 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002837 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002838 }
2839
Winson Chungc51db6a2011-10-05 11:44:49 -07002840 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002841 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002842 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002843 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002844 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002845
Winson Chungc51db6a2011-10-05 11:44:49 -07002846 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07002847 final SearchManager searchManager =
2848 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2849 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2850
2851 ComponentName activityName = null;
2852 if (globalSearchActivity != null) {
2853 // Check if the global search activity handles voice search
2854 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2855 intent.setPackage(globalSearchActivity.getPackageName());
2856 activityName = intent.resolveActivity(getPackageManager());
2857 }
2858
2859 if (activityName == null) {
2860 // Fallback: check if an activity other than the global search activity
2861 // resolves this
2862 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2863 activityName = intent.resolveActivity(getPackageManager());
2864 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002865 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002866 int coi = getCurrentOrientationIndexForGlobalIcons();
2867 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002868 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2869 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
2870 if (sVoiceSearchIcon[coi] == null) {
2871 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2872 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2873 TOOLBAR_ICON_METADATA_NAME);
2874 }
Winson Chung649723c2011-07-06 20:41:23 -07002875 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002876 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002877 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07002878 if (voiceButtonProxy != null) {
2879 voiceButtonProxy.setVisibility(View.VISIBLE);
2880 }
Winson Chungbb185bd2011-11-21 12:31:42 -08002881 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002882 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002883 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002884 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002885 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002886 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07002887 if (voiceButtonProxy != null) {
2888 voiceButtonProxy.setVisibility(View.GONE);
2889 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002890 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002891 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002892 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002893
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002894 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002895 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2896 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002897 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002898 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002899 }
2900
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002901 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002902 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002903 */
2904 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002905 final View marketButton = findViewById(R.id.market_button);
2906 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2907 // Find the app market activity by resolving an intent.
2908 // (If multiple app markets are installed, it will return the ResolverActivity.)
2909 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002910 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002911 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002912 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002913 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002914 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
2915 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07002916 marketButton.setVisibility(View.VISIBLE);
2917 } else {
2918 // We should hide and disable the view so that we don't try and restore the visibility
2919 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2920 marketButton.setVisibility(View.GONE);
2921 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002922 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002923 }
2924
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002925 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002926 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002927 }
2928
Michael Jurkad7c28052012-04-27 15:43:36 -07002929 @Override
2930 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2931 boolean result = super.dispatchPopulateAccessibilityEvent(event);
2932 final List<CharSequence> text = event.getText();
2933 text.clear();
2934 text.add(getString(R.string.home));
2935 return result;
2936 }
2937
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002938 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002939 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002940 */
2941 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2942 @Override
2943 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002944 closeSystemDialogs();
2945 }
2946 }
2947
2948 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002949 * Receives notifications whenever the appwidgets are reset.
2950 */
2951 private class AppWidgetResetObserver extends ContentObserver {
2952 public AppWidgetResetObserver() {
2953 super(new Handler());
2954 }
2955
2956 @Override
2957 public void onChange(boolean selfChange) {
2958 onAppWidgetReset();
2959 }
2960 }
2961
2962 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002963 * If the activity is currently paused, signal that we need to re-run the loader
2964 * in onResume.
2965 *
2966 * This needs to be called from incoming places where resources might have been loaded
2967 * while we are paused. That is becaues the Configuration might be wrong
2968 * when we're not running, and if it comes back to what it was when we
2969 * were paused, we are not restarted.
2970 *
2971 * Implementation of the method from LauncherModel.Callbacks.
2972 *
2973 * @return true if we are currently paused. The caller might be able to
2974 * skip some work in that case since we will come back again.
2975 */
2976 public boolean setLoadOnResume() {
2977 if (mPaused) {
2978 Log.i(TAG, "setLoadOnResume");
2979 mOnResumeNeedsLoad = true;
2980 return true;
2981 } else {
2982 return false;
2983 }
2984 }
2985
2986 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002987 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002988 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002989 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002990 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002991 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002992 } else {
2993 return SCREEN_COUNT / 2;
2994 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002995 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002996
Joe Onorato9c1289c2009-08-17 11:03:03 -04002997 /**
2998 * Refreshes the shortcuts shown on the workspace.
2999 *
3000 * Implementation of the method from LauncherModel.Callbacks.
3001 */
3002 public void startBinding() {
3003 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003004
Winson Chungf0c6ae02012-03-21 16:10:31 -07003005 mNewShortcutAnimatePage = -1;
3006 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003007 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003008 int count = workspace.getChildCount();
3009 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003010 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003011 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3012 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003013 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003014 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003015 if (mHotseat != null) {
3016 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003017 }
3018 }
3019
3020 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003021 * Bind the items start-end from the list.
3022 *
3023 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003024 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003025 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003026 setLoadOnResume();
3027
Winson Chungf0c6ae02012-03-21 16:10:31 -07003028 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3029 Set<String> newApps = new HashSet<String>();
3030 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3031
3032 Workspace workspace = mWorkspace;
3033 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003034 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003035
3036 // Short circuit if we are loading dock items for a configuration which has no dock
3037 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3038 mHotseat == null) {
3039 continue;
3040 }
3041
Joe Onorato9c1289c2009-08-17 11:03:03 -04003042 switch (item.itemType) {
3043 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3044 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003045 ShortcutInfo info = (ShortcutInfo) item;
3046 String uri = info.intent.toUri(0).toString();
3047 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003048 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3049 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003050 if (newApps.contains(uri)) {
3051 newApps.remove(uri);
3052
3053 // Prepare the view to be animated up
3054 shortcut.setAlpha(0f);
3055 shortcut.setScaleX(0f);
3056 shortcut.setScaleY(0f);
3057 mNewShortcutAnimatePage = item.screen;
3058 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3059 mNewShortcutAnimateViews.add(shortcut);
3060 }
3061 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003062 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003063 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003064 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003065 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003066 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003067 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3068 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003069 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003070 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003071 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003072
Joe Onorato9c1289c2009-08-17 11:03:03 -04003073 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003074 }
3075
Joe Onorato9c1289c2009-08-17 11:03:03 -04003076 /**
3077 * Implementation of the method from LauncherModel.Callbacks.
3078 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003079 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003080 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003081 sFolders.clear();
3082 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003083 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003084
3085 /**
3086 * Add the views for a widget to the workspace.
3087 *
3088 * Implementation of the method from LauncherModel.Callbacks.
3089 */
3090 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003091 setLoadOnResume();
3092
Daniel Sandler843e8602010-06-07 14:59:01 -04003093 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3094 if (DEBUG_WIDGETS) {
3095 Log.d(TAG, "bindAppWidget: " + item);
3096 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003097 final Workspace workspace = mWorkspace;
3098
3099 final int appWidgetId = item.appWidgetId;
3100 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003101 if (DEBUG_WIDGETS) {
3102 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3103 }
3104
Joe Onorato9c1289c2009-08-17 11:03:03 -04003105 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3106
Joe Onorato9c1289c2009-08-17 11:03:03 -04003107 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3108 item.hostView.setTag(item);
3109
Winson Chung3d503fb2011-07-13 17:25:49 -07003110 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003111 item.cellY, item.spanX, item.spanY, false);
3112
Adam Cohended9f8d2010-11-03 13:25:16 -07003113 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3114
Joe Onorato9c1289c2009-08-17 11:03:03 -04003115 workspace.requestLayout();
3116
Winson Chungf51025f2012-05-03 15:21:11 -07003117 AppWidgetResizeFrame.updateWidgetSizeRanges(item.hostView,
3118 this, item.spanX, item.spanY);
3119
Daniel Sandler843e8602010-06-07 14:59:01 -04003120 if (DEBUG_WIDGETS) {
3121 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3122 + (SystemClock.uptimeMillis()-start) + "ms");
3123 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003124 }
3125
3126 /**
3127 * Callback saying that there aren't any more items to bind.
3128 *
3129 * Implementation of the method from LauncherModel.Callbacks.
3130 */
3131 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003132 setLoadOnResume();
3133
Joe Onorato9c1289c2009-08-17 11:03:03 -04003134 if (mSavedState != null) {
3135 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003136 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003137 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003138 mSavedState = null;
3139 }
3140
3141 if (mSavedInstanceState != null) {
3142 super.onRestoreInstanceState(mSavedInstanceState);
3143 mSavedInstanceState = null;
3144 }
3145
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003146 // If we received the result of any pending adds while the loader was running (e.g. the
3147 // widget configuration forced an orientation change), process them now.
3148 for (int i = 0; i < sPendingAddList.size(); i++) {
3149 completeAdd(sPendingAddList.get(i));
3150 }
3151 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003152
Winson Chungc51db6a2011-10-05 11:44:49 -07003153 // Update the market app icon as necessary (the other icons will be managed in response to
3154 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003155 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003156
Winson Chungf0c6ae02012-03-21 16:10:31 -07003157 // Animate up any icons as necessary
3158 if (mVisible || mWorkspaceLoading) {
3159 Runnable newAppsRunnable = new Runnable() {
3160 @Override
3161 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003162 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003163 }
3164 };
Winson Chunga2413752012-04-03 14:22:34 -07003165
3166 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3167 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3168 if (canRunNewAppsAnimation()) {
3169 // If the user has not interacted recently, then either snap to the new page to show
3170 // the new-apps animation or just run them if they are to appear on the current page
3171 if (willSnapPage) {
3172 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3173 } else {
3174 runNewAppsAnimation(false);
3175 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003176 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003177 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003178 // are on another page (or just normally if they are added to the current page)
3179 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003180 }
3181 }
3182
3183 mWorkspaceLoading = false;
3184 }
3185
Winson Chunga2413752012-04-03 14:22:34 -07003186 private boolean canRunNewAppsAnimation() {
3187 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3188 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3189 }
3190
Winson Chungf0c6ae02012-03-21 16:10:31 -07003191 /**
3192 * Runs a new animation that scales up icons that were added while Launcher was in the
3193 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003194 *
3195 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003196 */
Winson Chunga2413752012-04-03 14:22:34 -07003197 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003198 AnimatorSet anim = new AnimatorSet();
3199 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003200
3201 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003202 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3203 @Override
3204 public int compare(View a, View b) {
3205 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3206 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3207 int cellCountX = LauncherModel.getCellCountX();
3208 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3209 }
3210 });
Winson Chunga2413752012-04-03 14:22:34 -07003211
3212 // Animate each of the views in place (or show them immediately if requested)
3213 if (immediate) {
3214 for (View v : mNewShortcutAnimateViews) {
3215 v.setAlpha(1f);
3216 v.setScaleX(1f);
3217 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003218 }
Winson Chunga2413752012-04-03 14:22:34 -07003219 } else {
3220 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3221 View v = mNewShortcutAnimateViews.get(i);
3222 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3223 PropertyValuesHolder.ofFloat("alpha", 1f),
3224 PropertyValuesHolder.ofFloat("scaleX", 1f),
3225 PropertyValuesHolder.ofFloat("scaleY", 1f));
3226 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3227 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3228 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3229 bounceAnims.add(bounceAnim);
3230 }
3231 anim.playTogether(bounceAnims);
3232 anim.addListener(new AnimatorListenerAdapter() {
3233 @Override
3234 public void onAnimationEnd(Animator animation) {
3235 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3236 }
3237 });
3238 anim.start();
3239 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003240
3241 // Clean up
3242 mNewShortcutAnimatePage = -1;
3243 mNewShortcutAnimateViews.clear();
3244 new Thread("clearNewAppsThread") {
3245 public void run() {
3246 mSharedPrefs.edit()
3247 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3248 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3249 .commit();
3250 }
3251 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003252 }
3253
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003254 @Override
3255 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003256 boolean searchVisible = updateGlobalSearchIcon();
3257 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3258 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003259 }
3260
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003261 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003262 * Add the icons for all apps.
3263 *
3264 * Implementation of the method from LauncherModel.Callbacks.
3265 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003266 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3267 // Remove the progress bar entirely; we could also make it GONE
3268 // but better to remove it since we know it's not going to be used
3269 View progressBar = mAppsCustomizeTabHost.
3270 findViewById(R.id.apps_customize_progress_bar);
3271 if (progressBar != null) {
3272 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003273 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003274 // We just post the call to setApps so the user sees the progress bar
3275 // disappear-- otherwise, it just looks like the progress bar froze
3276 // which doesn't look great
3277 mAppsCustomizeTabHost.post(new Runnable() {
3278 public void run() {
3279 if (mAppsCustomizeContent != null) {
3280 mAppsCustomizeContent.setApps(apps);
3281 }
3282 }
3283 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003284 }
3285
3286 /**
3287 * A package was installed.
3288 *
3289 * Implementation of the method from LauncherModel.Callbacks.
3290 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003291 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003292 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003293
Winson Chung785d2eb2011-04-14 16:08:02 -07003294 if (mAppsCustomizeContent != null) {
3295 mAppsCustomizeContent.addApps(apps);
3296 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003297 }
3298
3299 /**
3300 * A package was updated.
3301 *
3302 * Implementation of the method from LauncherModel.Callbacks.
3303 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003304 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003305 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003306 if (mWorkspace != null) {
3307 mWorkspace.updateShortcuts(apps);
3308 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003309
Winson Chung785d2eb2011-04-14 16:08:02 -07003310 if (mAppsCustomizeContent != null) {
3311 mAppsCustomizeContent.updateApps(apps);
3312 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003313 }
3314
3315 /**
3316 * A package was uninstalled.
3317 *
3318 * Implementation of the method from LauncherModel.Callbacks.
3319 */
Joe Onorato36115782010-06-17 13:28:48 -04003320 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003321 if (permanent) {
3322 mWorkspace.removeItems(apps);
3323 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003324
Winson Chung785d2eb2011-04-14 16:08:02 -07003325 if (mAppsCustomizeContent != null) {
3326 mAppsCustomizeContent.removeApps(apps);
3327 }
Winson Chunga1820962011-10-03 16:31:06 -07003328
3329 // Notify the drag controller
3330 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003331 }
Joe Onoratobe386092009-11-17 17:32:16 -08003332
3333 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003334 * A number of packages were updated.
3335 */
3336 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003337 if (mAppsCustomizeContent != null) {
3338 mAppsCustomizeContent.onPackagesUpdated();
3339 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003340 }
3341
Winson Chung400438b2011-01-16 17:53:48 -08003342 private int mapConfigurationOriActivityInfoOri(int configOri) {
3343 final Display d = getWindowManager().getDefaultDisplay();
3344 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3345 switch (d.getRotation()) {
3346 case Surface.ROTATION_0:
3347 case Surface.ROTATION_180:
3348 // We are currently in the same basic orientation as the natural orientation
3349 naturalOri = configOri;
3350 break;
3351 case Surface.ROTATION_90:
3352 case Surface.ROTATION_270:
3353 // We are currently in the other basic orientation to the natural orientation
3354 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3355 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3356 break;
3357 }
3358
3359 int[] oriMap = {
3360 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3361 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3362 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3363 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3364 };
3365 // Since the map starts at portrait, we need to offset if this device's natural orientation
3366 // is landscape.
3367 int indexOffset = 0;
3368 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3369 indexOffset = 1;
3370 }
3371 return oriMap[(d.getRotation() + indexOffset) % 4];
3372 }
Adam Cohen446e9402011-09-15 18:21:21 -07003373
Winson Chung4b919f82012-05-01 10:44:08 -07003374 public boolean isRotationEnabled() {
3375 boolean forceEnableRotation = "true".equalsIgnoreCase(SystemProperties.get(
3376 FORCE_ENABLE_ROTATION_PROPERTY, "false"));
3377 boolean enableRotation = forceEnableRotation ||
3378 getResources().getBoolean(R.bool.allow_rotation);
3379 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003380 }
Winson Chung4b919f82012-05-01 10:44:08 -07003381 public void lockScreenOrientation() {
3382 if (isRotationEnabled()) {
3383 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3384 .getConfiguration().orientation));
3385 }
3386 }
3387 public void unlockScreenOrientation(boolean immediate) {
3388 if (isRotationEnabled()) {
3389 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003390 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003391 } else {
3392 mHandler.postDelayed(new Runnable() {
3393 public void run() {
3394 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3395 }
3396 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003397 }
Winson Chung4b919f82012-05-01 10:44:08 -07003398 }
Winson Chung400438b2011-01-16 17:53:48 -08003399 }
3400
Winson Chung82f55532011-08-09 14:14:23 -07003401 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003402 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003403 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003404 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003405
Winson Chung7d7541e2011-09-16 20:14:36 -07003406 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003407 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003408 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3409 Cling cling = (Cling) findViewById(clingId);
3410 if (cling != null) {
3411 cling.init(this, positionData);
3412 cling.setVisibility(View.VISIBLE);
3413 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3414 if (animate) {
3415 cling.buildLayer();
3416 cling.setAlpha(0f);
3417 cling.animate()
3418 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003419 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003420 .setDuration(SHOW_CLING_DURATION)
3421 .setStartDelay(delay)
3422 .start();
3423 } else {
3424 cling.setAlpha(1f);
3425 }
3426 }
3427 return cling;
3428 }
3429 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003430 if (cling != null) {
3431 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003432 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003433 anim.addListener(new AnimatorListenerAdapter() {
3434 public void onAnimationEnd(Animator animation) {
3435 cling.setVisibility(View.GONE);
3436 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003437 // We should update the shared preferences on a background thread
3438 new Thread("dismissClingThread") {
3439 public void run() {
3440 SharedPreferences.Editor editor = mSharedPrefs.edit();
3441 editor.putBoolean(flag, true);
3442 editor.commit();
3443 }
3444 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003445 };
3446 });
3447 anim.start();
3448 }
3449 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003450 private void removeCling(int id) {
3451 final View cling = findViewById(id);
3452 if (cling != null) {
3453 final ViewGroup parent = (ViewGroup) cling.getParent();
3454 parent.post(new Runnable() {
3455 @Override
3456 public void run() {
3457 parent.removeView(cling);
3458 }
3459 });
3460 }
3461 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003462 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003463 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003464 if (isClingsEnabled() &&
3465 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003466 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003467 } else {
3468 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003469 }
3470 }
3471 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003472 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003473 if (isClingsEnabled() &&
3474 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003475 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003476 } else {
3477 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003478 }
3479 }
3480 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003481 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003482 if (isClingsEnabled() &&
3483 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3484 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003485 } else {
3486 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003487 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003488 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003489 }
3490 public boolean isFolderClingVisible() {
3491 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003492 if (cling != null) {
3493 return cling.getVisibility() == View.VISIBLE;
3494 }
3495 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003496 }
Winson Chung82f55532011-08-09 14:14:23 -07003497 public void dismissWorkspaceCling(View v) {
3498 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003499 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003500 }
3501 public void dismissAllAppsCling(View v) {
3502 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003503 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3504 }
3505 public void dismissFolderCling(View v) {
3506 Cling cling = (Cling) findViewById(R.id.folder_cling);
3507 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003508 }
3509
Winson Chung80baf5a2010-08-09 16:03:15 -07003510 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003511 * Prints out out state for debugging.
3512 */
3513 public void dumpState() {
3514 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003515 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003516 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3517 Log.d(TAG, "mRestoring=" + mRestoring);
3518 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3519 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003520 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003521 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003522
Winson Chung785d2eb2011-04-14 16:08:02 -07003523 if (mAppsCustomizeContent != null) {
3524 mAppsCustomizeContent.dumpState();
3525 }
Joe Onoratobe386092009-11-17 17:32:16 -08003526 Log.d(TAG, "END launcher2 dump state");
3527 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003528
3529 @Override
3530 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3531 super.dump(prefix, fd, writer, args);
3532 writer.println(" ");
3533 writer.println("Debug logs: ");
3534 for (int i = 0; i < sDumpLogs.size(); i++) {
3535 writer.println(" " + sDumpLogs.get(i));
3536 }
3537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003538}
Michael Jurka2763be32011-02-24 11:19:57 -08003539
Michael Jurkaabded662011-03-04 12:06:57 -08003540interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003541 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003542 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003543 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003544 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003545 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003546}