blob: fb27d02e826a1060591c49a20b75a6776647a051 [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;
108import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110/**
111 * Default launcher application.
112 */
Michael Jurka946ad472010-07-09 18:05:18 -0700113public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700114 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
115 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800116 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700117 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400120 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700121 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700122
Winson Chung70d72102011-08-12 11:24:35 -0700123 private static final int MENU_GROUP_WALLPAPER = 1;
124 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
125 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700126 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
127 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
129 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700130 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 private static final int REQUEST_PICK_APPLICATION = 6;
132 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700133 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700134 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Michael Jurka8b805b12012-04-18 14:23:14 -0700136 private static final int REQUEST_BIND_APPWIDGET = 11;
137
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
139
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800140 static final int SCREEN_COUNT = 5;
141 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Romain Guy98d01652009-06-30 16:21:04 -0700143 private static final String PREFERENCES = "launcher.preferences";
Adam Cohen23a4d302011-12-01 17:26:44 -0800144 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
146 // Type: int
147 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700148 // Type: int
149 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700151 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
152 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
154 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700155 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700157 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 // Type: boolean
159 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
160 // Type: long
161 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
162
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700163 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
164
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700165 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700166 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700167 private State mState = State.WORKSPACE;
168 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800169 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700170
Joe Onorato9c1289c2009-08-17 11:03:03 -0400171 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700172 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
173 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700174 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700175 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800178 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Winson Chunga2413752012-04-03 14:22:34 -0700180 // How long to wait before the new-shortcut animation automatically pans the workspace
181 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
182
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800183 private final BroadcastReceiver mCloseSystemDialogsReceiver
184 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800185 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private LayoutInflater mInflater;
188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800190 private View mQsbDivider;
191 private View mDockDivider;
192 private DragLayer mDragLayer;
193 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700194
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700195 private AppWidgetManager mAppWidgetManager;
196 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700197
Michael Jurkac9d95c52011-08-29 14:03:34 -0700198 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700199 private int[] mTmpAddItemCellCoordinates = new int[2];
200
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private FolderInfo mFolderInfo;
202
Winson Chung3d503fb2011-07-13 17:25:49 -0700203 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800204 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700205
Winson Chungc51db6a2011-10-05 11:44:49 -0700206 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700207 private AppsCustomizeTabHost mAppsCustomizeTabHost;
208 private AppsCustomizePagedView mAppsCustomizeContent;
209 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 private Bundle mSavedState;
212
213 private SpannableStringBuilder mDefaultKeySsb = null;
214
Joe Onorato9c1289c2009-08-17 11:03:03 -0400215 private boolean mWorkspaceLoading = true;
216
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800217 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 private boolean mRestoring;
219 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700220 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221
222 private Bundle mSavedInstanceState;
223
Joe Onorato9c1289c2009-08-17 11:03:03 -0400224 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800225 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800226 private boolean mUserPresent = true;
227 private boolean mVisible = false;
228 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700230 private static LocaleConfiguration sLocaleConfiguration = null;
231
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700232 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700233
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700234 private Intent mAppMarketIntent = null;
235
Adam Cohended9f8d2010-11-03 13:25:16 -0700236 // Related to the auto-advancing of widgets
237 private final int ADVANCE_MSG = 1;
238 private final int mAdvanceInterval = 20000;
239 private final int mAdvanceStagger = 250;
240 private long mAutoAdvanceSentTime;
241 private long mAutoAdvanceTimeLeft = -1;
242 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
243 new HashMap<View, AppWidgetProviderInfo>();
244
Winson Chung400438b2011-01-16 17:53:48 -0800245 // Determines how long to wait after a rotation before restoring the screen orientation to
246 // match the sensor state.
247 private final int mRestoreScreenOrientationDelay = 500;
248
Michael Jurka4ef207b2010-11-29 17:05:45 -0800249 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700250 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
251 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
252 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800253
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700254 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Michael Jurka8b805b12012-04-18 14:23:14 -0700255 PendingAddWidgetInfo mWidgetBeingBoundOrConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700256
Winson Chung46353de2012-02-16 14:05:10 -0800257 // We only want to get the SharedPreferences once since it does an FS stat each time we get
258 // it from the context.
259 private SharedPreferences mSharedPrefs;
260
Winson Chungf0c6ae02012-03-21 16:10:31 -0700261 // Holds the page that we need to animate to, and the icon views that we need to animate up
262 // when we scroll to that page on resume.
263 private int mNewShortcutAnimatePage = -1;
264 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700265
Michael Jurkaddd62e92011-02-16 17:49:14 -0800266 private BubbleTextView mWaitingForResume;
267
Michael Jurkac1f5d262011-09-30 19:32:27 -0700268 private Runnable mBuildLayersRunnable = new Runnable() {
269 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700270 if (mWorkspace != null) {
271 mWorkspace.buildPageHardwareLayers();
272 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700273 }
274 };
275
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800276 private static ArrayList<PendingAddArguments> sPendingAddList
277 = new ArrayList<PendingAddArguments>();
278
279 private static class PendingAddArguments {
280 int requestCode;
281 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700282 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800283 int screen;
284 int cellX;
285 int cellY;
286 }
287
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 @Override
289 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700290 if (DEBUG_STRICT_MODE) {
291 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
292 .detectDiskReads()
293 .detectDiskWrites()
294 .detectNetwork() // or .detectAll() for all detectable problems
295 .penaltyLog()
296 .build());
297 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
298 .detectLeakedSqlLiteObjects()
299 .detectLeakedClosableObjects()
300 .penaltyLog()
301 .penaltyDeath()
302 .build());
303 }
304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800306 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700307 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
308 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800309 mModel = app.setLauncher(this);
310 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400311 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700313
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700314 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700315 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
316 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700317
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200319 android.os.Debug.startMethodTracing(
320 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 }
322
323 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800324 setContentView(R.layout.launcher);
325 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700326 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800328 registerContentObservers();
329
Joe Onorato7c312c12009-08-13 21:36:53 -0700330 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700331
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 mSavedState = savedInstanceState;
333 restoreState(mSavedState);
334
Winson Chunga12a2502010-12-20 14:41:35 -0800335 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700336 if (mAppsCustomizeContent != null) {
337 mAppsCustomizeContent.onPackagesUpdated();
338 }
Winson Chunga12a2502010-12-20 14:41:35 -0800339
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 if (PROFILE_STARTUP) {
341 android.os.Debug.stopMethodTracing();
342 }
343
344 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700345 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 }
347
Michael Jurkac57b7a82011-08-09 22:02:20 -0700348 if (!mModel.isAllAppsLoaded()) {
349 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
350 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
351 }
352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 // For handling default keys
354 mDefaultKeySsb = new SpannableStringBuilder();
355 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800356
357 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
358 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800359
Winson Chungc51db6a2011-10-05 11:44:49 -0700360 boolean searchVisible = false;
361 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800362 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700363 int coi = getCurrentOrientationIndexForGlobalIcons();
364 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
365 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700366 updateAppMarketIcon();
367 searchVisible = updateGlobalSearchIcon();
368 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700369 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700370 if (sGlobalSearchIcon[coi] != null) {
371 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700372 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700373 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700374 if (sVoiceSearchIcon[coi] != null) {
375 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700376 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700377 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700378 if (sAppMarketIcon[coi] != null) {
379 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800380 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700381 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700382
Jim Miller14091b12012-04-24 19:27:54 -0700383 final String forceEnableRotation =
Adam Cohen23a4d302011-12-01 17:26:44 -0800384 SystemProperties.get(FORCE_ENABLE_ROTATION_PROPERTY, "false");
385
Adam Cohencb3f98e2012-04-24 14:24:35 -0700386 boolean enableRotation = getResources().getBoolean(R.bool.allow_rotation);
387
Adam Cohen446e9402011-09-15 18:21:21 -0700388 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohencb3f98e2012-04-24 14:24:35 -0700389 if (enableRotation || "true".equalsIgnoreCase(forceEnableRotation)) {
Adam Cohen446e9402011-09-15 18:21:21 -0700390 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
391 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 }
Romain Guycbb89e42009-06-08 15:52:54 -0700393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700395 if (sLocaleConfiguration == null) {
396 new AsyncTask<Void, Void, LocaleConfiguration>() {
397 @Override
398 protected LocaleConfiguration doInBackground(Void... unused) {
399 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
400 readConfiguration(Launcher.this, localeConfiguration);
401 return localeConfiguration;
402 }
403
404 @Override
405 protected void onPostExecute(LocaleConfiguration result) {
406 sLocaleConfiguration = result;
407 checkForLocaleChange(); // recursive, but now with a locale configuration
408 }
409 }.execute();
410 return;
411 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700412
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 final Configuration configuration = getResources().getConfiguration();
414
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700415 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 final String locale = configuration.locale.toString();
417
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700418 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 final int mcc = configuration.mcc;
420
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700421 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 final int mnc = configuration.mnc;
423
Romain Guy84f296c2009-11-04 15:00:44 -0800424 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425
Romain Guy84f296c2009-11-04 15:00:44 -0800426 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700427 sLocaleConfiguration.locale = locale;
428 sLocaleConfiguration.mcc = mcc;
429 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700430
Joe Onorato0589f0f2010-02-08 13:44:00 -0800431 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700432
433 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
434 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700435 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700436 public void run() {
437 writeConfiguration(Launcher.this, localeConfiguration);
438 }
439 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700440 }
441 }
442
443 private static class LocaleConfiguration {
444 public String locale;
445 public int mcc = -1;
446 public int mnc = -1;
447 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700448
Romain Guy98d01652009-06-30 16:21:04 -0700449 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
450 DataInputStream in = null;
451 try {
452 in = new DataInputStream(context.openFileInput(PREFERENCES));
453 configuration.locale = in.readUTF();
454 configuration.mcc = in.readInt();
455 configuration.mnc = in.readInt();
456 } catch (FileNotFoundException e) {
457 // Ignore
458 } catch (IOException e) {
459 // Ignore
460 } finally {
461 if (in != null) {
462 try {
463 in.close();
464 } catch (IOException e) {
465 // Ignore
466 }
467 }
468 }
469 }
470
471 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
472 DataOutputStream out = null;
473 try {
474 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
475 out.writeUTF(configuration.locale);
476 out.writeInt(configuration.mcc);
477 out.writeInt(configuration.mnc);
478 out.flush();
479 } catch (FileNotFoundException e) {
480 // Ignore
481 } catch (IOException e) {
482 //noinspection ResultOfMethodCallIgnored
483 context.getFileStreamPath(PREFERENCES).delete();
484 } finally {
485 if (out != null) {
486 try {
487 out.close();
488 } catch (IOException e) {
489 // Ignore
490 }
491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 }
493 }
494
Adam Cohen716b51e2011-06-30 12:09:54 -0700495 public DragLayer getDragLayer() {
496 return mDragLayer;
497 }
498
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 static int getScreen() {
500 synchronized (sLock) {
501 return sScreen;
502 }
503 }
504
505 static void setScreen(int screen) {
506 synchronized (sLock) {
507 sScreen = screen;
508 }
509 }
510
Winson Chung557d6ed2011-07-08 15:34:52 -0700511 /**
512 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
513 * a configuration step, this allows the proper animations to run after other transitions.
514 */
515 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700516 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800517 switch (args.requestCode) {
518 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700519 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
520 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800521 break;
522 case REQUEST_PICK_SHORTCUT:
523 processShortcut(args.intent);
524 break;
525 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700526 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
527 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700528 result = true;
529 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800530 case REQUEST_PICK_APPWIDGET:
531 addAppWidgetFromPick(args.intent);
532 break;
533 case REQUEST_CREATE_APPWIDGET:
534 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800535 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700536 result = true;
537 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800538 case REQUEST_PICK_WALLPAPER:
539 // We just wanted the activity result here so we can clear mWaitingForResult
540 break;
541 }
Michael Jurka27614d22012-04-02 06:40:22 -0700542 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
543 // if you turned the screen off and then back while in All Apps, Launcher would not
544 // return to the workspace. Clearing mAddInfo.container here fixes this issue
545 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700546 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800547 }
548
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700550 protected void onActivityResult(
551 final int requestCode, final int resultCode, final Intent data) {
552 if (requestCode == REQUEST_BIND_APPWIDGET) {
553 int appWidgetId = data != null ?
554 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
555 if (resultCode == RESULT_CANCELED) {
556 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
557 } else if (resultCode == RESULT_OK) {
558 addAppWidgetImpl(appWidgetId, mWidgetBeingBoundOrConfigured);
559 }
560 return;
561 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700562 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800563 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
564 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700565 mWaitingForResult = false;
566
Adam Cohened66b2b2012-01-23 17:28:51 -0800567 // We have special handling for widgets
568 if (isWidgetDrop) {
569 int appWidgetId = data != null ?
570 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700571 if (appWidgetId < 0) {
572 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
573 "widget configuration activity.");
574 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
575 } else {
576 completeTwoStageWidgetDrop(resultCode, appWidgetId);
577 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800578 return;
579 }
580
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 // The pattern used here is that a user PICKs a specific application,
582 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700583
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
585 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700586 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800587 final PendingAddArguments args = new PendingAddArguments();
588 args.requestCode = requestCode;
589 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700590 args.container = mPendingAddInfo.container;
591 args.screen = mPendingAddInfo.screen;
592 args.cellX = mPendingAddInfo.cellX;
593 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800594 if (isWorkspaceLocked()) {
595 sPendingAddList.add(args);
596 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700597 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800600 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700601 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800602 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
603 null);
604 }
605
606 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700607 CellLayout cellLayout =
608 (CellLayout) mWorkspace.getChildAt(mWidgetBeingBoundOrConfigured.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800609 Runnable onCompleteRunnable = null;
610 int animationType = 0;
611
612 if (resultCode == RESULT_OK) {
613 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
614 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Michael Jurka8b805b12012-04-18 14:23:14 -0700615 mWidgetBeingBoundOrConfigured.info);
616 mWidgetBeingBoundOrConfigured.boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800617 onCompleteRunnable = new Runnable() {
618 @Override
619 public void run() {
620 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
621 mPendingAddInfo.screen, layout, null);
622 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
623 null);
624 }
625 };
626 } else if (resultCode == RESULT_CANCELED) {
627 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
628 onCompleteRunnable = new Runnable() {
629 @Override
630 public void run() {
631 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
632 null);
633 }
634 };
635 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700636 mWorkspace.animateWidgetDrop(mWidgetBeingBoundOrConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800637 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Michael Jurka8b805b12012-04-18 14:23:14 -0700638 animationType, mWidgetBeingBoundOrConfigured.boundWidget, true);
639 mWidgetBeingBoundOrConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 }
641
642 @Override
643 protected void onResume() {
644 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700645
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800646 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700647 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700649 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700651 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 }
Winson Chunge4e50662012-01-23 14:45:13 -0800653
654 // Reset the pressed state of icons that were locked in the press state while activities
655 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800656 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800657 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800658 mWaitingForResume.setStayPressed(false);
659 }
Winson Chunge4e50662012-01-23 14:45:13 -0800660 if (mAppsCustomizeContent != null) {
661 // Resets the previous all apps icon press state
662 mAppsCustomizeContent.resetDrawableState();
663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 }
665
666 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700667 protected void onPause() {
668 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700669 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800670 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700671 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700672 }
Romain Guycbb89e42009-06-08 15:52:54 -0700673
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700674 @Override
675 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400676 // Flag the loader to stop early before switching
677 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700678 if (mAppsCustomizeContent != null) {
679 mAppsCustomizeContent.surrender();
680 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800681 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700682 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700683
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800684 // We can't hide the IME if it was forced open. So don't bother
685 /*
686 @Override
687 public void onWindowFocusChanged(boolean hasFocus) {
688 super.onWindowFocusChanged(hasFocus);
689
690 if (hasFocus) {
691 final InputMethodManager inputManager = (InputMethodManager)
692 getSystemService(Context.INPUT_METHOD_SERVICE);
693 WindowManager.LayoutParams lp = getWindow().getAttributes();
694 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
695 android.os.Handler()) {
696 protected void onReceiveResult(int resultCode, Bundle resultData) {
697 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
698 }
699 });
700 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
701 }
702 }
703 */
704
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 private boolean acceptFilter() {
706 final InputMethodManager inputManager = (InputMethodManager)
707 getSystemService(Context.INPUT_METHOD_SERVICE);
708 return !inputManager.isFullscreenMode();
709 }
710
711 @Override
712 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700713 final int uniChar = event.getUnicodeChar();
714 final boolean handled = super.onKeyDown(keyCode, event);
715 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
716 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
718 keyCode, event);
719 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700720 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700721 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700722 // showSearchDialog()
723 // If there are multiple keystrokes before the search dialog takes focus,
724 // onSearchRequested() will be called for every keystroke,
725 // but it is idempotent, so it's fine.
726 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 }
728 }
729
Joe Onorato8a9625e2010-01-28 15:55:35 -0800730 // Eat the long press event so the keyboard doesn't come up.
731 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
732 return true;
733 }
734
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 return handled;
736 }
737
Karl Rosaen138a0412009-04-23 19:00:21 -0700738 private String getTypedText() {
739 return mDefaultKeySsb.toString();
740 }
741
742 private void clearTypedText() {
743 mDefaultKeySsb.clear();
744 mDefaultKeySsb.clearSpans();
745 Selection.setSelection(mDefaultKeySsb, 0);
746 }
747
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700749 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
750 * State
751 */
752 private static State intToState(int stateOrdinal) {
753 State state = State.WORKSPACE;
754 final State[] stateValues = State.values();
755 for (int i = 0; i < stateValues.length; i++) {
756 if (stateValues[i].ordinal() == stateOrdinal) {
757 state = stateValues[i];
758 break;
759 }
760 }
761 return state;
762 }
763
764 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 * Restores the previous state, if it exists.
766 *
767 * @param savedState The previous state.
768 */
769 private void restoreState(Bundle savedState) {
770 if (savedState == null) {
771 return;
772 }
773
Michael Jurka883f55b2010-10-21 15:47:14 -0700774 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700775 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800776 showAllApps(false);
777 }
778
Winson Chungf0c6ae02012-03-21 16:10:31 -0700779 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700781 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 }
783
Winson Chung3d503fb2011-07-13 17:25:49 -0700784 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
785 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700786
Winson Chung3d503fb2011-07-13 17:25:49 -0700787 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
788 mPendingAddInfo.container = pendingAddContainer;
789 mPendingAddInfo.screen = pendingAddScreen;
790 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
791 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 mRestoring = true;
793 }
794
795 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
796 if (renameFolder) {
797 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700798 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 mRestoring = true;
800 }
Winson Chunga12a2502010-12-20 14:41:35 -0800801
Winson Chung785d2eb2011-04-14 16:08:02 -0700802
803 // Restore the AppsCustomize tab
804 if (mAppsCustomizeTabHost != null) {
805 String curTab = savedState.getString("apps_customize_currentTab");
806 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700807 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700808 mAppsCustomizeContent.setContentType(
809 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
810 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700811 mAppsCustomizeContent.loadAssociatedPages(
812 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700813 }
814
Winson Chung5afbf7b2011-07-25 11:53:08 -0700815 int currentIndex = savedState.getInt("apps_customize_currentIndex");
816 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 }
819
820 /**
821 * Finds all the views we need and configure them properly.
822 */
823 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700824 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400825
Winson Chung4c98d922011-05-31 16:50:48 -0700826 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
827 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800828 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
829 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830
Winson Chung4c98d922011-05-31 16:50:48 -0700831 // Setup the drag layer
832 mDragLayer.setup(this, dragController);
833
Winson Chung3d503fb2011-07-13 17:25:49 -0700834 // Setup the hotseat
835 mHotseat = (Hotseat) findViewById(R.id.hotseat);
836 if (mHotseat != null) {
837 mHotseat.setup(this);
838 }
839
Winson Chung4c98d922011-05-31 16:50:48 -0700840 // Setup the workspace
841 mWorkspace.setHapticFeedbackEnabled(false);
842 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700843 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700844 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700845
Winson Chungf0ea4d32011-06-06 14:27:16 -0700846 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700847 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700848
Winson Chungf0ea4d32011-06-06 14:27:16 -0700849 // Setup AppsCustomize
850 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
851 findViewById(R.id.apps_customize_pane);
852 mAppsCustomizeContent = (AppsCustomizePagedView)
853 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700854 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700855 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400856
Michael Jurka5130e402011-10-13 04:55:35 -0700857 // Get the all apps button
858 mAllAppsButton = findViewById(R.id.all_apps_button);
859 if (mAllAppsButton != null) {
860 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
861 @Override
862 public boolean onTouch(View v, MotionEvent event) {
863 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
864 onTouchDownAllAppsButton(v);
865 }
866 return false;
867 }
868 });
869 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700870 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700871 dragController.setDragScoller(mWorkspace);
872 dragController.setScrollView(mDragLayer);
873 dragController.setMoveTarget(mWorkspace);
874 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700875 if (mSearchDropTargetBar != null) {
876 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800877 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 }
879
880 /**
881 * Creates a view representing a shortcut.
882 *
883 * @param info The data structure describing the shortcut.
884 *
885 * @return A View inflated from R.layout.application.
886 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800887 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700889 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 }
891
892 /**
893 * Creates a view representing a shortcut inflated from the specified resource.
894 *
895 * @param layoutResId The id of the XML layout used to create the shortcut.
896 * @param parent The group the shortcut belongs to.
897 * @param info The data structure describing the shortcut.
898 *
899 * @return A View inflated from layoutResId.
900 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800901 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800902 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
903 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 return favorite;
906 }
907
908 /**
909 * Add an application shortcut to the workspace.
910 *
911 * @param data The intent describing the application.
912 * @param cellInfo The position on screen where to create the shortcut.
913 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700914 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700915 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700916 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700917
Adam Cohenfbba09b2011-07-18 21:43:05 -0700918 // First we check if we already know the exact location where we want to add this item.
919 if (cellX >= 0 && cellY >= 0) {
920 cellXY[0] = cellX;
921 cellXY[1] = cellY;
922 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700923 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700924 return;
925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800927 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800928
Romain Guy73b979d2009-06-09 12:57:21 -0700929 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800930 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800932 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700933 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700934 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800935 } else {
936 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 }
938 }
Romain Guycbb89e42009-06-08 15:52:54 -0700939
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 /**
941 * Add a shortcut to the workspace.
942 *
943 * @param data The intent describing the shortcut.
944 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700946 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
947 int cellY) {
948 int[] cellXY = mTmpAddItemCellCoordinates;
949 int[] touchXY = mPendingAddInfo.dropPos;
950 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700951
Michael Jurkad3ef3062010-11-23 16:23:58 -0800952 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700953
Adam Cohen558baaf2011-08-15 15:22:57 -0700954 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800955 if (info == null) {
956 return;
957 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700958 final View view = createShortcut(info);
959
Adam Cohenfbba09b2011-07-18 21:43:05 -0700960 // First we check if we already know the exact location where we want to add this item.
961 if (cellX >= 0 && cellY >= 0) {
962 cellXY[0] = cellX;
963 cellXY[1] = cellY;
964 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700965
966 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800967 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700968 true, null,null)) {
969 return;
970 }
971 DragObject dragObject = new DragObject();
972 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -0800973 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
974 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -0700975 return;
976 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700977 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800978 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700979 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800980 foundCellSpan = (result != null);
981 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700982 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800983 }
984
985 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -0700986 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700987 return;
988 }
989
Adam Cohen558baaf2011-08-15 15:22:57 -0700990 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991
992 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700993 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
994 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 }
996 }
997
Winson Chunga5c96362012-04-12 14:04:41 -0700998 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800999 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001000 // We want to account for the extra amount of padding that we are adding to the widget
1001 // to ensure that it gets the full amount of space that it has requested
1002 int requiredWidth = minWidth + padding.left + padding.right;
1003 int requiredHeight = minHeight + padding.top + padding.bottom;
1004 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
1005 }
1006
Winson Chunga5c96362012-04-12 14:04:41 -07001007 int[] getSpanForWidget(AppWidgetProviderInfo info) {
1008 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001009 }
1010
Winson Chunga5c96362012-04-12 14:04:41 -07001011 int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1012 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001013 }
1014
Winson Chunga5c96362012-04-12 14:04:41 -07001015 int[] getSpanForWidget(PendingAddWidgetInfo info) {
1016 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001017 }
1018
Winson Chunga5c96362012-04-12 14:04:41 -07001019 int[] getMinSpanForWidget(PendingAddWidgetInfo info) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001020 return getSpanForWidget(info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001021 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001022 }
1023
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001025 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001027 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001028 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001030 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1031 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1032 if (appWidgetInfo == null) {
1033 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1034 }
Romain Guycbb89e42009-06-08 15:52:54 -07001035
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001036 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001037 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001038
Winson Chunga5c96362012-04-12 14:04:41 -07001039 int[] minSpanXY = getMinSpanForWidget(appWidgetInfo);
1040 int[] spanXY = getSpanForWidget(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001041
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001043 // We have saved the position to which the widget was dragged-- this really only matters
1044 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001045 int[] cellXY = mTmpAddItemCellCoordinates;
1046 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001047 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001048 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001049 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1050 cellXY[0] = mPendingAddInfo.cellX;
1051 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001052 spanXY[0] = mPendingAddInfo.spanX;
1053 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001054 foundCellSpan = true;
1055 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001056 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001057 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001058 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1059 spanXY[1], cellXY, finalSpan);
1060 spanXY[0] = finalSpan[0];
1061 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001062 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001063 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001064 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001065 }
1066
Michael Jurka0280c3b2010-09-17 15:00:07 -07001067 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001068 if (appWidgetId != -1) {
1069 // Deleting an app widget ID is a void call but writes to disk before returning
1070 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001071 new Thread("deleteAppWidgetId") {
1072 public void run() {
1073 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1074 }
1075 }.start();
1076 }
Winson Chung93eef082012-03-23 15:59:27 -07001077 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001078 return;
1079 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001081 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -07001082 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
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);
Winson Chung3d503fb2011-07-13 17:25:49 -07001103 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001104 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001105
1106 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001108 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 }
Romain Guycbb89e42009-06-08 15:52:54 -07001110
Adam Cohended9f8d2010-11-03 13:25:16 -07001111 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1112 @Override
1113 public void onReceive(Context context, Intent intent) {
1114 final String action = intent.getAction();
1115 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1116 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001117 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001118 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001119
Winson Chungc100e8e2011-08-09 16:02:43 -07001120 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001121 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001122 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1123 mAppsCustomizeTabHost.reset();
1124 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001125 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001126 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1127 mUserPresent = true;
1128 updateRunning();
1129 }
1130 }
1131 };
1132
1133 @Override
1134 public void onAttachedToWindow() {
1135 super.onAttachedToWindow();
1136
1137 // Listen for broadcasts related to user-presence
1138 final IntentFilter filter = new IntentFilter();
1139 filter.addAction(Intent.ACTION_SCREEN_OFF);
1140 filter.addAction(Intent.ACTION_USER_PRESENT);
1141 registerReceiver(mReceiver, filter);
1142
Adam Cohend113e0c2010-11-11 10:48:05 -08001143 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001144 mVisible = true;
1145 }
1146
1147 @Override
1148 public void onDetachedFromWindow() {
1149 super.onDetachedFromWindow();
1150 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001151 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001152
Adam Cohend113e0c2010-11-11 10:48:05 -08001153 if (mAttached) {
1154 unregisterReceiver(mReceiver);
1155 mAttached = false;
1156 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001157 updateRunning();
1158 }
1159
1160 public void onWindowVisibilityChanged(int visibility) {
1161 mVisible = visibility == View.VISIBLE;
1162 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001163 // The following code used to be in onResume, but it turns out onResume is called when
1164 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1165 // is a more appropriate event to handle
1166 if (mVisible) {
1167 mAppsCustomizeTabHost.onWindowVisible();
1168 if (!mWorkspaceLoading) {
1169 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001170 // We want to let Launcher draw itself at least once before we force it to build
1171 // layers on all the workspace pages, so that transitioning to Launcher from other
1172 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1173 // a true draw so we wait until the second preDraw call to be safe
1174 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001175 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001176 // We delay the layer building a bit in order to give
1177 // other message processing a time to run. In particular
1178 // this avoids a delay in hiding the IME if it was
1179 // currently shown, because doing that may involve
1180 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001181 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurka6ee21d22012-02-21 18:20:27 -08001182 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001183 return true;
1184 }
1185 });
1186 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001187 // When Launcher comes back to foreground, a different Activity might be responsible for
1188 // the app market intent, so refresh the icon
1189 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001190 clearTypedText();
1191 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001192 }
1193
1194 private void sendAdvanceMessage(long delay) {
1195 mHandler.removeMessages(ADVANCE_MSG);
1196 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1197 mHandler.sendMessageDelayed(msg, delay);
1198 mAutoAdvanceSentTime = System.currentTimeMillis();
1199 }
1200
1201 private void updateRunning() {
1202 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1203 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1204 mAutoAdvanceRunning = autoAdvanceRunning;
1205 if (autoAdvanceRunning) {
1206 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1207 sendAdvanceMessage(delay);
1208 } else {
1209 if (!mWidgetsToAdvance.isEmpty()) {
1210 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1211 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1212 }
1213 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001214 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001215 }
1216 }
1217 }
1218
1219 private final Handler mHandler = new Handler() {
1220 @Override
1221 public void handleMessage(Message msg) {
1222 if (msg.what == ADVANCE_MSG) {
1223 int i = 0;
1224 for (View key: mWidgetsToAdvance.keySet()) {
1225 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1226 final int delay = mAdvanceStagger * i;
1227 if (v instanceof Advanceable) {
1228 postDelayed(new Runnable() {
1229 public void run() {
1230 ((Advanceable) v).advance();
1231 }
1232 }, delay);
1233 }
1234 i++;
1235 }
1236 sendAdvanceMessage(mAdvanceInterval);
1237 }
1238 }
1239 };
1240
1241 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001242 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001243 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1244 if (v instanceof Advanceable) {
1245 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001246 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001247 updateRunning();
1248 }
1249 }
1250
1251 void removeWidgetToAutoAdvance(View hostView) {
1252 if (mWidgetsToAdvance.containsKey(hostView)) {
1253 mWidgetsToAdvance.remove(hostView);
1254 updateRunning();
1255 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001256 }
1257
Joe Onorato9c1289c2009-08-17 11:03:03 -04001258 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001259 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001260 launcherInfo.hostView = null;
1261 }
1262
Winson Chung93eef082012-03-23 15:59:27 -07001263 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1264 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1265 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001266 }
1267
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001268 public LauncherAppWidgetHost getAppWidgetHost() {
1269 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 }
Romain Guycbb89e42009-06-08 15:52:54 -07001271
Winson Chunga9abd0e2010-10-27 17:18:37 -07001272 public LauncherModel getModel() {
1273 return mModel;
1274 }
1275
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001276 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001277 getWindow().closeAllPanels();
1278
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001279 // Whatever we were doing is hereby canceled.
1280 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001281 }
1282
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 @Override
1284 protected void onNewIntent(Intent intent) {
1285 super.onNewIntent(intent);
1286
1287 // Close the menu
1288 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001289 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001290 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001291
Joe Onorato14f122b2009-11-19 14:06:36 -08001292 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1293 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001294
Adam Cohenac56cff2011-09-28 20:45:37 -07001295 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001296 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001297 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001298 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1299 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001300 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001301 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001302
1303 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001304 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001305 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001306
Joe Onorato3a8820b2009-11-10 15:06:42 -08001307 final View v = getWindow().peekDecorView();
1308 if (v != null && v.getWindowToken() != null) {
1309 InputMethodManager imm = (InputMethodManager)getSystemService(
1310 INPUT_METHOD_SERVICE);
1311 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001313
Michael Jurka35aa14d2011-07-07 17:01:08 -07001314 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001315 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001316 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001317 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 }
1319 }
1320
1321 @Override
1322 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1323 // Do not call super here
1324 mSavedInstanceState = savedInstanceState;
1325 }
1326
1327 @Override
1328 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001329 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001330 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331
Michael Jurka883f55b2010-10-21 15:47:14 -07001332 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001333 // We close any open folder since it will not be re-opened, and we need to make sure
1334 // this state is reflected.
1335 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336
Winson Chung3d503fb2011-07-13 17:25:49 -07001337 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1338 mWaitingForResult) {
1339 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1340 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1341 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1342 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 }
1344
1345 if (mFolderInfo != null && mWaitingForResult) {
1346 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1347 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1348 }
Michael Jurka946ad472010-07-09 18:05:18 -07001349
Winson Chung785d2eb2011-04-14 16:08:02 -07001350 // Save the current AppsCustomize tab
1351 if (mAppsCustomizeTabHost != null) {
1352 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1353 if (currentTabTag != null) {
1354 outState.putString("apps_customize_currentTab", currentTabTag);
1355 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001356 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1357 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001358 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
1360
1361 @Override
1362 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001364
Winson Chunge7a03942011-08-05 15:05:12 -07001365 // Remove all pending runnables
1366 mHandler.removeMessages(ADVANCE_MSG);
1367 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001368 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001369
Winson Chungcd2b0142011-06-08 16:02:26 -07001370 // Stop callbacks from LauncherModel
1371 LauncherApplication app = ((LauncherApplication) getApplication());
1372 mModel.stopLoader();
1373 app.setLauncher(null);
1374
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001376 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001378 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001380 mAppWidgetHost = null;
1381
1382 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383
1384 TextKeyListener.getInstance().release();
1385
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386
Winson Chung3d503fb2011-07-13 17:25:49 -07001387 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001388
1389 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001390 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001391
1392 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1393 mWorkspace.removeAllViews();
1394 mWorkspace = null;
1395 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001396
1397 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 }
1399
Adam Cohena9cf38f2011-05-02 15:36:58 -07001400 public DragController getDragController() {
1401 return mDragController;
1402 }
1403
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 @Override
1405 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001406 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 super.startActivityForResult(intent, requestCode);
1408 }
1409
Winson Chung70d72102011-08-12 11:24:35 -07001410 /**
1411 * Indicates that we want global search for this activity by setting the globalSearch
1412 * argument for {@link #startSearch} to true.
1413 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001415 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001417
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001418 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001419
Karl Rosaen138a0412009-04-23 19:00:21 -07001420 if (initialQuery == null) {
1421 // Use any text typed in the launcher as the initial query
1422 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001423 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 if (appSearchData == null) {
1425 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001426 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001428 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001429
Karl Rosaen138a0412009-04-23 19:00:21 -07001430 final SearchManager searchManager =
1431 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001432 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001433 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 }
1435
Winson Chung70d72102011-08-12 11:24:35 -07001436 @Override
1437 public boolean onCreateOptionsMenu(Menu menu) {
1438 if (isWorkspaceLocked()) {
1439 return false;
1440 }
1441
1442 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001443
1444 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1445 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1446 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001447 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1448 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1449 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001450 String helpUrl = getString(R.string.help_url);
1451 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001452 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1453 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1454
Winson Chung70d72102011-08-12 11:24:35 -07001455 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1456 .setIcon(android.R.drawable.ic_menu_gallery)
1457 .setAlphabeticShortcut('W');
1458 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1459 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001460 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001461 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001462 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1463 .setIcon(android.R.drawable.ic_menu_preferences)
1464 .setIntent(settings)
1465 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001466 if (!helpUrl.isEmpty()) {
1467 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1468 .setIcon(android.R.drawable.ic_menu_help)
1469 .setIntent(help)
1470 .setAlphabeticShortcut('H');
1471 }
Winson Chung70d72102011-08-12 11:24:35 -07001472 return true;
1473 }
1474
1475 @Override
1476 public boolean onPrepareOptionsMenu(Menu menu) {
1477 super.onPrepareOptionsMenu(menu);
1478
1479 if (mAppsCustomizeTabHost.isTransitioning()) {
1480 return false;
1481 }
1482 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1483 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1484
1485 return true;
1486 }
1487
1488 @Override
1489 public boolean onOptionsItemSelected(MenuItem item) {
1490 switch (item.getItemId()) {
1491 case MENU_WALLPAPER_SETTINGS:
1492 startWallpaper();
1493 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001494 }
1495
1496 return super.onOptionsItemSelected(item);
1497 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001499 @Override
1500 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001501 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001502 // Use a custom animation for launching search
1503 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001504 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001505 }
1506
Joe Onorato9c1289c2009-08-17 11:03:03 -04001507 public boolean isWorkspaceLocked() {
1508 return mWorkspaceLoading || mWaitingForResult;
1509 }
1510
Michael Jurka0280c3b2010-09-17 15:00:07 -07001511 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001512 mPendingAddInfo.container = ItemInfo.NO_ID;
1513 mPendingAddInfo.screen = -1;
1514 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1515 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001516 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001517 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001518 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001519
Michael Jurkaaf442092010-06-10 17:01:57 -07001520 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001521 // TODO: catch bad widget exception when sent
1522 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001523 // TODO: Is this log message meaningful?
1524 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001525 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001526 }
1527
Adam Cohened66b2b2012-01-23 17:28:51 -08001528 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1529 final AppWidgetProviderInfo appWidget = info.info;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001530 if (appWidget.configure != null) {
1531 // Launch over to configure widget, if needed
1532 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1533 intent.setComponent(appWidget.configure);
1534 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001535 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001536 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001537 intent.putExtra(InstallWidgetReceiver.
1538 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001539
1540 final String mimeType = info.mimeType;
1541 final ClipData clipData = (ClipData) info.configurationData;
1542 final ClipDescription clipDesc = clipData.getDescription();
1543 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1544 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001545 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001546 final CharSequence stringData = item.getText();
1547 final Uri uriData = item.getUri();
1548 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001549 final String key = InstallWidgetReceiver.
1550 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001551 if (uriData != null) {
1552 intent.putExtra(key, uriData);
1553 } else if (intentData != null) {
1554 intent.putExtra(key, intentData);
1555 } else if (stringData != null) {
1556 intent.putExtra(key, stringData);
1557 }
1558 break;
1559 }
1560 }
1561 }
Winson Chung55cef262010-10-28 14:14:18 -07001562 }
Romain Guy8e633c52010-03-04 12:51:36 -08001563 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001564 mWidgetBeingBoundOrConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001566 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001567 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001568 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001569 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
1571 }
Romain Guycbb89e42009-06-08 15:52:54 -07001572
Adam Cohenfbba09b2011-07-18 21:43:05 -07001573 /**
1574 * Process a shortcut drop.
1575 *
1576 * @param componentName The name of the component
1577 * @param screen The screen where it should be added
1578 * @param cell The cell it should be added to, optional
1579 * @param position The location on the screen where it was dropped, optional
1580 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001581 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1582 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001583 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001584 mPendingAddInfo.container = container;
1585 mPendingAddInfo.screen = screen;
1586 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001587
1588 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001589 mPendingAddInfo.cellX = cell[0];
1590 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001591 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001592
1593 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1594 createShortcutIntent.setComponent(componentName);
1595 processShortcut(createShortcutIntent);
1596 }
1597
Adam Cohenfbba09b2011-07-18 21:43:05 -07001598 /**
1599 * Process a widget drop.
1600 *
1601 * @param info The PendingAppWidgetInfo of the widget being added.
1602 * @param screen The screen where it should be added
1603 * @param cell The cell it should be added to, optional
1604 * @param position The location on the screen where it was dropped, optional
1605 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001606 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001607 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001608 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001609 mPendingAddInfo.container = info.container = container;
1610 mPendingAddInfo.screen = info.screen = screen;
1611 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001612 mPendingAddInfo.minSpanX = info.minSpanX;
1613 mPendingAddInfo.minSpanY = info.minSpanY;
1614
Adam Cohenfbba09b2011-07-18 21:43:05 -07001615 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001616 mPendingAddInfo.cellX = cell[0];
1617 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001618 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001619 if (span != null) {
1620 mPendingAddInfo.spanX = span[0];
1621 mPendingAddInfo.spanY = span[1];
1622 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001623
Adam Cohened66b2b2012-01-23 17:28:51 -08001624 AppWidgetHostView hostView = info.boundWidget;
1625 int appWidgetId;
1626 if (hostView != null) {
1627 appWidgetId = hostView.getAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001628 addAppWidgetImpl(appWidgetId, info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001629 } else {
1630 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001631 mWidgetBeingBoundOrConfigured = info;
1632 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
1633 addAppWidgetImpl(appWidgetId, info);
1634 } else {
1635 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1636 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1637 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1638 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1639 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001640 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001641 }
1642
Joe Onoratodeb98af2010-02-19 14:59:39 -08001643 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001644 // Handle case where user selected "Applications"
1645 String applicationName = getResources().getString(R.string.group_applications);
1646 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001647
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001648 if (applicationName != null && applicationName.equals(shortcutName)) {
1649 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1650 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001651
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001652 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1653 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001654 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001655 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001656 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001657 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 }
1660
Winson Chung24ab2f12010-09-16 14:10:47 -07001661 void processWallpaper(Intent intent) {
1662 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1663 }
1664
Winson Chung3d503fb2011-07-13 17:25:49 -07001665 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1666 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001667 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 folderInfo.title = getText(R.string.folder_name);
1669
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001671 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1672 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001673 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674
1675 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001676 FolderIcon newFolder =
1677 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1678 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1679 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001680 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 }
Romain Guycbb89e42009-06-08 15:52:54 -07001682
Joe Onorato9c1289c2009-08-17 11:03:03 -04001683 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001684 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001685 }
1686
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001688 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001690 Intent chooser = Intent.createChooser(pickWallpaper,
1691 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001692 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1693 // Removed in Eclair MR1
1694// WallpaperManager wm = (WallpaperManager)
1695// getSystemService(Context.WALLPAPER_SERVICE);
1696// WallpaperInfo wi = wm.getWallpaperInfo();
1697// if (wi != null && wi.getSettingsActivity() != null) {
1698// LabeledIntent li = new LabeledIntent(getPackageName(),
1699// R.string.configure_wallpaper, 0);
1700// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1701// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1702// }
Mike Clerona0618e42009-10-22 13:55:21 -07001703 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 }
1705
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001706 /**
1707 * Registers various content observers. The current implementation registers
1708 * only a favorites observer to keep track of the favorites applications.
1709 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001710 private void registerContentObservers() {
1711 ContentResolver resolver = getContentResolver();
1712 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1713 true, mWidgetObserver);
1714 }
1715
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001716 @Override
1717 public boolean dispatchKeyEvent(KeyEvent event) {
1718 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1719 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001721 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001722 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001723 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001724 dumpState();
1725 return true;
1726 }
1727 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001728 }
1729 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1730 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001731 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001732 return true;
1733 }
1734 }
1735
1736 return super.dispatchKeyEvent(event);
1737 }
1738
Joe Onorato88ec0992009-11-19 13:16:06 -08001739 @Override
1740 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001741 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001742 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001743 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001744 Folder openFolder = mWorkspace.getOpenFolder();
1745 if (openFolder.isEditingName()) {
1746 openFolder.dismissEditingName();
1747 } else {
1748 closeFolder();
1749 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001750 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001751 mWorkspace.exitWidgetResizeMode();
1752
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001753 // Back button is a no-op here, but give at least some feedback for the button press
1754 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001755 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001756 }
1757
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001759 * Re-listen when widgets are reset.
1760 */
1761 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001762 if (mAppWidgetHost != null) {
1763 mAppWidgetHost.startListening();
1764 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001765 }
1766
1767 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001768 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1769 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001771 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001772 if (mModel != null) {
1773 mModel.unbindWorkspaceItems();
1774 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001776
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 /**
1778 * Launches the intent referred by the clicked shortcut.
1779 *
1780 * @param v The view representing the clicked shortcut.
1781 */
1782 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001783 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1784 // view has detached (it's possible for this to happen if the view is removed mid touch).
1785 if (v.getWindowToken() == null) {
1786 return;
1787 }
1788
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001789 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001790 return;
1791 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001792
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001794 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001796 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001797 int[] pos = new int[2];
1798 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001799 intent.setSourceBounds(new Rect(pos[0], pos[1],
1800 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001801
1802 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001803
1804 if (success && v instanceof BubbleTextView) {
1805 mWaitingForResume = (BubbleTextView) v;
1806 mWaitingForResume.setStayPressed(true);
1807 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001809 if (v instanceof FolderIcon) {
1810 FolderIcon fi = (FolderIcon) v;
1811 handleFolderClick(fi);
1812 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001813 } else if (v == mAllAppsButton) {
1814 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001815 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001816 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001817 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001818 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 }
1820 }
1821
Michael Jurka0e260592010-06-30 17:07:39 -07001822 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001823 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001824 // clicking anywhere on the workspace causes the customization drawer to slide down
1825 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001826 return false;
1827 }
1828
Michael Jurkaaf442092010-06-10 17:01:57 -07001829 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001830 * Event handler for the search button
1831 *
1832 * @param v The view that was clicked.
1833 */
1834 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001835 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1836
Amith Yamasania135ba82011-08-09 17:42:01 -07001837 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001838 }
1839
1840 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001841 * Event handler for the voice button
1842 *
1843 * @param v The view that was clicked.
1844 */
1845 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001846 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001847
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001848 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001849 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001850 startActivitySafely(null, intent, "onClickVoiceButton");
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001851 }
1852
1853 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001854 * Event handler for the "grid" button that appears on the home screen, which
1855 * enters all apps mode.
1856 *
1857 * @param v The view that was clicked.
1858 */
1859 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001860 showAllApps(true);
1861 }
1862
1863 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001864 // Provide the same haptic feedback that the system offers for virtual keys.
1865 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001866 }
1867
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001868 public void onClickAppMarketButton(View v) {
1869 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001870 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001871 } else {
1872 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001873 }
1874 }
1875
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001876 void startApplicationDetailsActivity(ComponentName componentName) {
1877 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001878 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1879 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001880 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001881 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001882 }
1883
Patrick Dubroy5539af72010-09-07 15:22:01 -07001884 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1885 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1886 // System applications cannot be installed. For now, show a toast explaining that.
1887 // We may give them the option of disabling apps this way.
1888 int messageId = R.string.uninstall_system_app_text;
1889 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1890 } else {
1891 String packageName = appInfo.componentName.getPackageName();
1892 String className = appInfo.componentName.getClassName();
1893 Intent intent = new Intent(
1894 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001895 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1896 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001897 startActivity(intent);
1898 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001899 }
1900
Winson Chungc7450e32012-04-17 17:34:08 -07001901 boolean startActivitySafely(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001903
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 try {
Winson Chungc7450e32012-04-17 17:34:08 -07001905 if (v != null) {
1906 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1907 v.getMeasuredWidth(), v.getMeasuredHeight());
1908
1909 startActivity(intent, opts.toBundle());
1910 } else {
1911 startActivity(intent);
1912 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001913 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 } catch (ActivityNotFoundException e) {
1915 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001916 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 } catch (SecurityException e) {
1918 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001919 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001921 "or use the exported attribute for this activity. "
1922 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001924 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001926
Romain Guy8e633c52010-03-04 12:51:36 -08001927 void startActivityForResultSafely(Intent intent, int requestCode) {
1928 try {
1929 startActivityForResult(intent, requestCode);
1930 } catch (ActivityNotFoundException e) {
1931 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1932 } catch (SecurityException e) {
1933 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1934 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1935 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1936 "or use the exported attribute for this activity.", e);
1937 }
1938 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939
Adam Cohena9cf38f2011-05-02 15:36:58 -07001940 private void handleFolderClick(FolderIcon folderIcon) {
1941 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001942 Folder openFolder = mWorkspace.getFolderForTag(info);
1943
1944 // If the folder info reports that the associated folder is open, then verify that
1945 // it is actually opened. There have been a few instances where this gets out of sync.
1946 if (info.opened && openFolder == null) {
1947 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1948 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1949 info.opened = false;
1950 }
1951
Adam Cohena9cf38f2011-05-02 15:36:58 -07001952 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 // Close any open folder
1954 closeFolder();
1955 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001956 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 } else {
1958 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 int folderScreen;
1960 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001961 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 // .. and close it
1963 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001964 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 // Close any folder open on the current screen
1966 closeFolder();
1967 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001968 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 }
1970 }
1971 }
1972 }
1973
Adam Cohen2801caf2011-05-13 20:57:39 -07001974 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001975 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001976 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1977 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1978 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1979
Adam Cohenc51934b2011-07-26 21:07:43 -07001980 FolderInfo info = (FolderInfo) fi.getTag();
1981 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1982 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001983 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1984 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001985 }
1986
Adam Cohen2801caf2011-05-13 20:57:39 -07001987 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1988 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1989 oa.start();
1990 }
1991
1992 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001993 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001994 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1995 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1996 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1997
Adam Cohenc51934b2011-07-26 21:07:43 -07001998 FolderInfo info = (FolderInfo) fi.getTag();
1999 CellLayout cl = null;
2000 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2001 cl = (CellLayout) fi.getParent().getParent();
2002 }
2003
2004 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07002005 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2006 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002007 oa.addListener(new AnimatorListenerAdapter() {
2008 @Override
2009 public void onAnimationEnd(Animator animation) {
2010 if (layout != null) {
2011 layout.clearFolderLeaveBehind();
2012 }
2013 }
2014 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002015 oa.start();
2016 }
2017
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002019 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 * is animated relative to the specified View. If the View is null, no animation
2021 * is played.
2022 *
2023 * @param folderInfo The FolderInfo describing the folder to open.
2024 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002025 public void openFolder(FolderIcon folderIcon) {
2026 Folder folder = folderIcon.mFolder;
2027 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028
Adam Cohen2801caf2011-05-13 20:57:39 -07002029 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002030 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031
Adam Cohen4554ee12011-08-03 16:13:21 -07002032 // Just verify that the folder hasn't already been added to the DragLayer.
2033 // There was a one-off crash where the folder had a parent already.
2034 if (folder.getParent() == null) {
2035 mDragLayer.addView(folder);
2036 mDragController.addDropTarget((DropTarget) folder);
2037 } else {
2038 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2039 folder.getParent() + ").");
2040 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002041 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002042 }
2043
2044 public void closeFolder() {
2045 Folder folder = mWorkspace.getOpenFolder();
2046 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002047 if (folder.isEditingName()) {
2048 folder.dismissEditingName();
2049 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002050 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002051
2052 // Dismiss the folder cling
2053 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002054 }
2055 }
2056
2057 void closeFolder(Folder folder) {
2058 folder.getInfo().opened = false;
2059
2060 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2061 if (parent != null) {
2062 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2063 shrinkAndFadeInFolderIcon(fi);
2064 }
2065 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 }
2067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002069
Patrick Dubroye708c522011-03-01 16:03:43 -08002070 if (mState != State.WORKSPACE) {
2071 return false;
2072 }
2073
Joe Onorato9c1289c2009-08-17 11:03:03 -04002074 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 return false;
2076 }
2077
2078 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002079 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 }
2081
Michael Jurka0280c3b2010-09-17 15:00:07 -07002082 resetAddInfo();
2083 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002085 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 return true;
2087 }
2088
Winson Chung3d503fb2011-07-13 17:25:49 -07002089 // The hotseat touch handling does not go through Workspace, and we always allow long press
2090 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002091 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002092 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2093 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002094 if (itemUnderLongClick == null) {
2095 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002096 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2097 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002098 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002100 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002102 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 }
2104 }
2105 }
2106 return true;
2107 }
2108
Winson Chung3d503fb2011-07-13 17:25:49 -07002109 boolean isHotseatLayout(View layout) {
2110 return mHotseat != null && layout != null &&
2111 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2112 }
2113 Hotseat getHotseat() {
2114 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002115 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002116 SearchDropTargetBar getSearchBar() {
2117 return mSearchDropTargetBar;
2118 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002119
Winson Chung3d503fb2011-07-13 17:25:49 -07002120 /**
2121 * Returns the CellLayout of the specified container at the specified screen.
2122 */
2123 CellLayout getCellLayout(long container, int screen) {
2124 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2125 if (mHotseat != null) {
2126 return mHotseat.getLayout();
2127 } else {
2128 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002129 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002130 } else {
2131 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002132 }
2133 }
2134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 Workspace getWorkspace() {
2136 return mWorkspace;
2137 }
2138
Daniel Sandler843e8602010-06-07 14:59:01 -04002139 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2140 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002141 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002142 }
2143
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002144 public boolean isAllAppsButtonRank(int rank) {
2145 return mHotseat.isAllAppsButtonRank(rank);
2146 }
2147
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002148 // AllAppsView.Watcher
2149 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002150 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002151 mWorkspace.setVisibility(View.GONE);
2152 }
2153 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002154
2155 /**
2156 * Helper method for the cameraZoomIn/cameraZoomOut animations
2157 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002158 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002159 * @param scaleFactor The scale factor used for the zoom
2160 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002161 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002162 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002163 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002164 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002165
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002166 void updateWallpaperVisibility(boolean visible) {
2167 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2168 int curflags = getWindow().getAttributes().flags
2169 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2170 if (wpflags != curflags) {
2171 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2172 }
2173 }
2174
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002175 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2176 if (v instanceof LauncherTransitionable) {
2177 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2178 }
2179 }
2180
Michael Jurkabed61d22012-02-14 22:51:29 -08002181 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2182 if (v instanceof LauncherTransitionable) {
2183 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2184 }
Winson Chung70442722012-02-10 15:43:22 -08002185
2186 // Update the workspace transition step as well
2187 dispatchOnLauncherTransitionStep(v, 0f);
2188 }
2189
2190 private void dispatchOnLauncherTransitionStep(View v, float t) {
2191 if (v instanceof LauncherTransitionable) {
2192 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2193 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002194 }
2195
2196 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2197 if (v instanceof LauncherTransitionable) {
2198 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2199 }
Winson Chung70442722012-02-10 15:43:22 -08002200
2201 // Update the workspace transition step as well
2202 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002203 }
2204
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002205 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002206 * Things to test when changing the following seven functions.
2207 * - Home from workspace
2208 * - from center screen
2209 * - from other screens
2210 * - Home from all apps
2211 * - from center screen
2212 * - from other screens
2213 * - Back from all apps
2214 * - from center screen
2215 * - from other screens
2216 * - Launch app from workspace and quit
2217 * - with back
2218 * - with home
2219 * - Launch app from all apps and quit
2220 * - with back
2221 * - with home
2222 * - Go to a screen that's not the default, then all
2223 * apps, and launch and app, and go back
2224 * - with back
2225 * -with home
2226 * - On workspace, long press power and go back
2227 * - with back
2228 * - with home
2229 * - On all apps, long press power and go back
2230 * - with back
2231 * - with home
2232 * - On workspace, power off
2233 * - On all apps, power off
2234 * - Launch an app and turn off the screen while in that app
2235 * - Go back with home key
2236 * - Go back with back key TODO: make this not go to workspace
2237 * - From all apps
2238 * - From workspace
2239 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2240 * - From all apps
2241 * - From the center workspace
2242 * - From another workspace
2243 */
2244
2245 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002246 * Zoom the camera out from the workspace to reveal 'toView'.
2247 * Assumes that the view to show is anchored at either the very top or very bottom
2248 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002249 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002250 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002251 if (mStateAnimation != null) {
2252 mStateAnimation.cancel();
2253 mStateAnimation = null;
2254 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002255 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002256
Winson Chungf0ea4d32011-06-06 14:27:16 -07002257 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2258 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2259 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002260 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002261 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002262 final int startDelay =
2263 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002264
Michael Jurkab3e22d92011-10-31 15:58:33 -07002265 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002266
Michael Jurkad74c9842011-07-10 12:44:21 -07002267 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002268 Animator workspaceAnim =
2269 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002270
2271 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002272 toView.setScaleX(scale);
2273 toView.setScaleY(scale);
2274 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2275 scaleAnim.
2276 scaleX(1f).scaleY(1f).
2277 setDuration(duration).
2278 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002279
Winson Chungf0ea4d32011-06-06 14:27:16 -07002280 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002281 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002282 final ObjectAnimator alphaAnim = ObjectAnimator
2283 .ofFloat(toView, "alpha", 0f, 1f)
2284 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002285 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002286 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2287 @Override
2288 public void onAnimationUpdate(ValueAnimator animation) {
2289 float t = (Float) animation.getAnimatedValue();
2290 dispatchOnLauncherTransitionStep(fromView, t);
2291 dispatchOnLauncherTransitionStep(toView, t);
2292 }
2293 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002294
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002295 // toView should appear right at the end of the workspace shrink
2296 // animation
2297 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002298 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002299 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002300
2301 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002302 boolean animationCancelled = false;
2303
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002304 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002305 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002306 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002307 // Prepare the position
2308 toView.setTranslationX(0.0f);
2309 toView.setTranslationY(0.0f);
2310 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002311 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002312 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002313 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002314 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002315 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2316 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002317
2318 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2319 // Hide the workspace scrollbar
2320 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002321 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002322 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002323 if (!animationCancelled) {
2324 updateWallpaperVisibility(false);
2325 }
2326 }
2327
Adam Cohencff6af82011-09-13 14:51:53 -07002328 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002329 public void onAnimationCancel(Animator animation) {
2330 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002331 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002332 });
2333
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002334 if (workspaceAnim != null) {
2335 mStateAnimation.play(workspaceAnim);
2336 }
Michael Jurka1899a362011-11-03 13:50:45 -07002337
2338 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002339 final ViewTreeObserver observer;
2340
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002341 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2342 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002343
2344 // If any of the objects being animated haven't been measured/laid out
2345 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002346 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002347 (mWorkspace.getMeasuredWidth() == 0) ||
2348 (toView.getMeasuredWidth() == 0)) {
2349 observer = mWorkspace.getViewTreeObserver();
2350 delayAnim = true;
2351 } else {
2352 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002353 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002354
2355 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002356 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002357 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2358 public void onGlobalLayout() {
2359 mWorkspace.post(new Runnable() {
2360 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002361 // Check that mStateAnimation hasn't changed while
2362 // we waited for a layout pass
2363 if (mStateAnimation == stateAnimation) {
2364 // Need to update pivots for zoom if layout changed
2365 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002366 dispatchOnLauncherTransitionStart(fromView, animated, false);
2367 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka49779a12012-01-16 04:10:08 -08002368 mStateAnimation.start();
2369 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002370 }
2371 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002372 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002373 }
2374 };
2375 observer.addOnGlobalLayoutListener(delayedStart);
2376 } else {
2377 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002378 dispatchOnLauncherTransitionStart(fromView, animated, false);
2379 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka1899a362011-11-03 13:50:45 -07002380 mStateAnimation.start();
2381 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002382 } else {
2383 toView.setTranslationX(0.0f);
2384 toView.setTranslationY(0.0f);
2385 toView.setScaleX(1.0f);
2386 toView.setScaleY(1.0f);
2387 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002388 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002389
Michael Jurkabed61d22012-02-14 22:51:29 -08002390 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2391 // Hide the workspace scrollbar
2392 mWorkspace.hideScrollingIndicator(true);
2393 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002394 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002395 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002396 dispatchOnLauncherTransitionStart(fromView, animated, false);
2397 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002398 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002399 dispatchOnLauncherTransitionStart(toView, animated, false);
2400 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002401 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002402 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002403 }
2404
2405 /**
2406 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002407 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002408 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002409 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002410 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2411 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002412
Michael Jurkab3e22d92011-10-31 15:58:33 -07002413 if (mStateAnimation != null) {
2414 mStateAnimation.cancel();
2415 mStateAnimation = null;
2416 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002417 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002418
Winson Chungf0ea4d32011-06-06 14:27:16 -07002419 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002420 final int fadeOutDuration =
2421 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002422 final float scaleFactor = (float)
2423 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2424 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002425 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002426 Animator workspaceAnim = null;
2427
2428 if (toState == State.WORKSPACE) {
2429 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2430 workspaceAnim = mWorkspace.getChangeStateAnimation(
2431 Workspace.State.NORMAL, animated, stagger);
2432 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2433 workspaceAnim = mWorkspace.getChangeStateAnimation(
2434 Workspace.State.SPRING_LOADED, animated);
2435 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002436
Michael Jurkab3e22d92011-10-31 15:58:33 -07002437 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002438 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002439 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002440 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002441 final LauncherViewPropertyAnimator scaleAnim =
2442 new LauncherViewPropertyAnimator(fromView);
2443 scaleAnim.
2444 scaleX(scaleFactor).scaleY(scaleFactor).
2445 setDuration(duration).
2446 setInterpolator(new Workspace.ZoomInInterpolator());
2447
2448 final ObjectAnimator alphaAnim = ObjectAnimator
2449 .ofFloat(fromView, "alpha", 1f, 0f)
2450 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002451 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002452 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2453 @Override
2454 public void onAnimationUpdate(ValueAnimator animation) {
2455 float t = 1f - (Float) animation.getAnimatedValue();
2456 dispatchOnLauncherTransitionStep(fromView, t);
2457 dispatchOnLauncherTransitionStep(toView, t);
2458 }
2459 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002460
Michael Jurkabed61d22012-02-14 22:51:29 -08002461 mStateAnimation = new AnimatorSet();
2462
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002463 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2464 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002465
2466 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002467 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002468 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002469 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002470 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002471 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2472 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002473 if (mWorkspace != null) {
2474 mWorkspace.hideScrollingIndicator(false);
2475 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002476 if (onCompleteRunnable != null) {
2477 onCompleteRunnable.run();
2478 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002479 }
2480 });
2481
Winson Chungf0ea4d32011-06-06 14:27:16 -07002482 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002483 if (workspaceAnim != null) {
2484 mStateAnimation.play(workspaceAnim);
2485 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002486 dispatchOnLauncherTransitionStart(fromView, animated, true);
2487 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002488 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002489 } else {
2490 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002491 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002492 dispatchOnLauncherTransitionStart(fromView, animated, true);
2493 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002494 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002495 dispatchOnLauncherTransitionStart(toView, animated, true);
2496 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002497 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002498 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002499 }
2500
Michael Jurkae326f182011-11-21 14:05:46 -08002501 @Override
2502 public void onTrimMemory(int level) {
2503 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002504 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002505 mAppsCustomizeTabHost.onTrimMemory();
2506 }
2507 }
2508
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002509 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002510 showWorkspace(animated, null);
2511 }
2512
2513 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002514 if (mState != State.WORKSPACE) {
2515 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002516 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002517
2518 // Show the search bar and hotseat
2519 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002520 // We only need to animate in the dock divider if we're going from spring loaded mode
2521 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002522
2523 // Set focus to the AppsCustomize button
2524 if (mAllAppsButton != null) {
2525 mAllAppsButton.requestFocus();
2526 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002527 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002528
Michael Jurkab737ee62011-11-15 15:57:22 -08002529 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002530
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002531 // Change the state *after* we've called all the transition code
2532 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002533
Winson Chung5fb63472011-02-02 17:03:37 -08002534 // Resume the auto-advance of widgets
2535 mUserPresent = true;
2536 updateRunning();
2537
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002538 // send an accessibility event to announce the context change
2539 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002540 }
2541
Michael Jurkab3e22d92011-10-31 15:58:33 -07002542 void showAllApps(boolean animated) {
2543 if (mState != State.WORKSPACE) return;
2544
2545 showAppsCustomizeHelper(animated, false);
2546 mAppsCustomizeTabHost.requestFocus();
2547
2548 // Hide the search bar and hotseat
2549 mSearchDropTargetBar.hideSearchBar(animated);
2550
2551 // Change the state *after* we've called all the transition code
2552 mState = State.APPS_CUSTOMIZE;
2553
2554 // Pause the auto-advance of widgets until we are out of AllApps
2555 mUserPresent = false;
2556 updateRunning();
2557 closeFolder();
2558
2559 // Send an accessibility event to announce the context change
2560 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2561 }
2562
Adam Cohen7777d962011-08-18 18:58:38 -07002563 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002564 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002565 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002566 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002567 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002568 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002569 }
Adam Cohen7777d962011-08-18 18:58:38 -07002570
Adam Cohened66b2b2012-01-23 17:28:51 -08002571 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2572 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002573 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2574
Winson Chunge7a03942011-08-05 15:05:12 -07002575 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002576 @Override
2577 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002578 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002579 // Before we show workspace, hide all apps again because
2580 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2581 // clean up our state transition functions
2582 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002583 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002584 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002585 } else {
2586 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002587 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002588 }
2589 }, (extendedDelay ?
2590 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2591 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2592 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002593
Michael Jurkad3ef3062010-11-23 16:23:58 -08002594 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002595 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002596 final boolean animated = true;
2597 final boolean springLoaded = true;
2598 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002599 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002600 }
2601 // Otherwise, we are not in spring loaded mode, so don't do anything.
2602 }
2603
Michael Jurkab737ee62011-11-15 15:57:22 -08002604 void hideDockDivider() {
2605 if (mQsbDivider != null && mDockDivider != null) {
2606 mQsbDivider.setVisibility(View.INVISIBLE);
2607 mDockDivider.setVisibility(View.INVISIBLE);
2608 }
2609 }
2610
2611 void showDockDivider(boolean animated) {
2612 if (mQsbDivider != null && mDockDivider != null) {
2613 mQsbDivider.setVisibility(View.VISIBLE);
2614 mDockDivider.setVisibility(View.VISIBLE);
2615 if (mDividerAnimator != null) {
2616 mDividerAnimator.cancel();
2617 mQsbDivider.setAlpha(1f);
2618 mDockDivider.setAlpha(1f);
2619 mDividerAnimator = null;
2620 }
2621 if (animated) {
2622 mDividerAnimator = new AnimatorSet();
2623 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2624 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2625 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2626 mDividerAnimator.start();
2627 }
2628 }
2629 }
2630
Michael Jurkab3e22d92011-10-31 15:58:33 -07002631 void lockAllApps() {
2632 // TODO
2633 }
2634
2635 void unlockAllApps() {
2636 // TODO
2637 }
2638
Adam Cohenfc53cd22011-07-20 15:45:11 -07002639 public boolean isAllAppsCustomizeOpen() {
2640 return mState == State.APPS_CUSTOMIZE;
2641 }
2642
Winson Chungf0ea4d32011-06-06 14:27:16 -07002643 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002644 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002645 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002646 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002647 if (!LauncherApplication.isScreenLarge()) {
2648 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002649 if (mHotseat.getAlpha() != 1f) {
2650 int duration = mSearchDropTargetBar.getTransitionInDuration();
2651 mHotseat.animate().alpha(1f).setDuration(duration);
2652 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002653 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002654 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002655 }
2656 }
2657 }
2658
2659 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002660 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002661 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002662 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002663 if (!LauncherApplication.isScreenLarge()) {
2664 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002665 if (mHotseat.getAlpha() != 0f) {
2666 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2667 mHotseat.animate().alpha(0f).setDuration(duration);
2668 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002669 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002670 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002671 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002672 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002673 }
2674
Patrick Dubroy5f445422011-02-18 14:35:21 -08002675 /**
2676 * Add an item from all apps or customize onto the given workspace screen.
2677 * If layout is null, add to the current screen.
2678 */
2679 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002680 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002681 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002682 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002683 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002684
Winson Chungdff8ebb2011-09-08 17:25:31 -07002685 /** Maps the current orientation to an index for referencing orientation correct global icons */
2686 private int getCurrentOrientationIndexForGlobalIcons() {
2687 // default - 0, landscape - 1
2688 switch (getResources().getConfiguration().orientation) {
2689 case Configuration.ORIENTATION_LANDSCAPE:
2690 return 1;
2691 default:
2692 return 0;
2693 }
2694 }
2695
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002696 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002697 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002698 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002699 // Look for the toolbar icon specified in the activity meta-data
2700 Bundle metaData = packageManager.getActivityInfo(
2701 activityName, PackageManager.GET_META_DATA).metaData;
2702 if (metaData != null) {
2703 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2704 if (iconResId != 0) {
2705 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002706 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002707 }
2708 }
2709 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002710 // This can happen if the activity defines an invalid drawable
2711 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2712 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002713 } catch (Resources.NotFoundException nfe) {
2714 // This can happen if the activity defines an invalid drawable
2715 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2716 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002717 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002718 return null;
2719 }
2720
2721 // if successful in getting icon, return it; otherwise, set button to use default drawable
2722 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2723 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002724 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002725 Resources r = getResources();
2726 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2727 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002728
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002729 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002730 // If we were unable to find the icon via the meta-data, use a generic one
2731 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002732 toolbarIcon = r.getDrawable(fallbackDrawableId);
2733 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002734 if (button != null) {
2735 button.setCompoundDrawables(toolbarIcon, null, null, null);
2736 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002737 return null;
2738 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002739 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 toolbarIcon.getConstantState();
2744 }
2745 }
2746
2747 // if successful in getting icon, return it; otherwise, set button to use default drawable
2748 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2749 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2750 ImageView button = (ImageView) findViewById(buttonId);
2751 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2752
Michael Jurka19e0fc52011-07-22 18:00:21 -07002753 if (button != null) {
2754 // If we were unable to find the icon via the meta-data, use a
2755 // generic one
2756 if (toolbarIcon == null) {
2757 button.setImageResource(fallbackDrawableId);
2758 } else {
2759 button.setImageDrawable(toolbarIcon);
2760 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002761 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002762
2763 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2764
Michael Jurka0423dcf2010-10-05 14:56:18 -07002765 }
2766
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002767 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2768 TextView button = (TextView) findViewById(buttonId);
2769 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2770 }
2771
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002772 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002773 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002774 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002775 }
2776
Winson Chungbb185bd2011-11-21 12:31:42 -08002777 private void invalidatePressedFocusedStates(View container, View button) {
2778 if (container instanceof HolographicLinearLayout) {
2779 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2780 layout.invalidatePressedFocusedStates();
2781 } else if (button instanceof HolographicImageView) {
2782 HolographicImageView view = (HolographicImageView) button;
2783 view.invalidatePressedFocusedStates();
2784 }
2785 }
2786
Winson Chungc51db6a2011-10-05 11:44:49 -07002787 private boolean updateGlobalSearchIcon() {
2788 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002789 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2790 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002791 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002792 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002793 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002794
Winson Chung1cad91e2011-05-25 17:41:01 -07002795 final SearchManager searchManager =
2796 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2797 ComponentName activityName = searchManager.getGlobalSearchActivity();
2798 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002799 int coi = getCurrentOrientationIndexForGlobalIcons();
2800 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002801 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002802 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002803 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2804 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002805 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002806 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002807 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002808 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002809 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002810 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2811 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2812 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002813 voiceButton.setVisibility(View.GONE);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002814 voiceButtonProxy.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002815 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002816 }
2817 }
2818
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002819 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002820 final View searchButtonContainer = findViewById(R.id.search_button_container);
2821 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002822 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002823 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002824 }
2825
Winson Chungc51db6a2011-10-05 11:44:49 -07002826 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002827 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002828 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002829 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002830 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002831
Winson Chungc51db6a2011-10-05 11:44:49 -07002832 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002833 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2834 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002835 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002836 int coi = getCurrentOrientationIndexForGlobalIcons();
2837 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002838 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002839 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002840 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002841 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07002842 if (voiceButtonProxy != null) {
2843 voiceButtonProxy.setVisibility(View.VISIBLE);
2844 }
Winson Chungbb185bd2011-11-21 12:31:42 -08002845 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002846 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002847 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002848 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002849 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002850 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07002851 if (voiceButtonProxy != null) {
2852 voiceButtonProxy.setVisibility(View.GONE);
2853 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002854 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002855 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002856 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002857
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002858 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002859 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2860 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002861 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002862 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002863 }
2864
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002865 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002866 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002867 */
2868 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002869 final View marketButton = findViewById(R.id.market_button);
2870 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2871 // Find the app market activity by resolving an intent.
2872 // (If multiple app markets are installed, it will return the ResolverActivity.)
2873 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002874 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002875 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002876 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002877 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002878 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002879 marketButton.setVisibility(View.VISIBLE);
2880 } else {
2881 // We should hide and disable the view so that we don't try and restore the visibility
2882 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2883 marketButton.setVisibility(View.GONE);
2884 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002885 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002886 }
2887
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002888 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002889 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002890 }
2891
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002892 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002893 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002894 */
2895 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2896 @Override
2897 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002898 closeSystemDialogs();
2899 }
2900 }
2901
2902 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002903 * Receives notifications whenever the appwidgets are reset.
2904 */
2905 private class AppWidgetResetObserver extends ContentObserver {
2906 public AppWidgetResetObserver() {
2907 super(new Handler());
2908 }
2909
2910 @Override
2911 public void onChange(boolean selfChange) {
2912 onAppWidgetReset();
2913 }
2914 }
2915
2916 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002917 * If the activity is currently paused, signal that we need to re-run the loader
2918 * in onResume.
2919 *
2920 * This needs to be called from incoming places where resources might have been loaded
2921 * while we are paused. That is becaues the Configuration might be wrong
2922 * when we're not running, and if it comes back to what it was when we
2923 * were paused, we are not restarted.
2924 *
2925 * Implementation of the method from LauncherModel.Callbacks.
2926 *
2927 * @return true if we are currently paused. The caller might be able to
2928 * skip some work in that case since we will come back again.
2929 */
2930 public boolean setLoadOnResume() {
2931 if (mPaused) {
2932 Log.i(TAG, "setLoadOnResume");
2933 mOnResumeNeedsLoad = true;
2934 return true;
2935 } else {
2936 return false;
2937 }
2938 }
2939
2940 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002941 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002942 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002943 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002944 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002945 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002946 } else {
2947 return SCREEN_COUNT / 2;
2948 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002949 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002950
Joe Onorato9c1289c2009-08-17 11:03:03 -04002951 /**
2952 * Refreshes the shortcuts shown on the workspace.
2953 *
2954 * Implementation of the method from LauncherModel.Callbacks.
2955 */
2956 public void startBinding() {
2957 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002958
Winson Chungf0c6ae02012-03-21 16:10:31 -07002959 mNewShortcutAnimatePage = -1;
2960 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07002961 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002962 int count = workspace.getChildCount();
2963 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002964 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002965 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2966 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002967 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08002968 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07002969 if (mHotseat != null) {
2970 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 }
2972 }
2973
2974 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002975 * Bind the items start-end from the list.
2976 *
2977 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002978 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002979 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002980 setLoadOnResume();
2981
Winson Chungf0c6ae02012-03-21 16:10:31 -07002982 // Get the list of added shortcuts and intersect them with the set of shortcuts here
2983 Set<String> newApps = new HashSet<String>();
2984 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
2985
2986 Workspace workspace = mWorkspace;
2987 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002988 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002989
2990 // Short circuit if we are loading dock items for a configuration which has no dock
2991 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2992 mHotseat == null) {
2993 continue;
2994 }
2995
Joe Onorato9c1289c2009-08-17 11:03:03 -04002996 switch (item.itemType) {
2997 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2998 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07002999 ShortcutInfo info = (ShortcutInfo) item;
3000 String uri = info.intent.toUri(0).toString();
3001 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003002 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3003 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003004 if (newApps.contains(uri)) {
3005 newApps.remove(uri);
3006
3007 // Prepare the view to be animated up
3008 shortcut.setAlpha(0f);
3009 shortcut.setScaleX(0f);
3010 shortcut.setScaleY(0f);
3011 mNewShortcutAnimatePage = item.screen;
3012 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3013 mNewShortcutAnimateViews.add(shortcut);
3014 }
3015 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003016 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003017 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003018 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003019 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003020 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003021 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3022 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003023 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003025 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003026
Joe Onorato9c1289c2009-08-17 11:03:03 -04003027 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028 }
3029
Joe Onorato9c1289c2009-08-17 11:03:03 -04003030 /**
3031 * Implementation of the method from LauncherModel.Callbacks.
3032 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003033 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003034 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003035 sFolders.clear();
3036 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003037 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003038
3039 /**
3040 * Add the views for a widget to the workspace.
3041 *
3042 * Implementation of the method from LauncherModel.Callbacks.
3043 */
3044 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003045 setLoadOnResume();
3046
Daniel Sandler843e8602010-06-07 14:59:01 -04003047 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3048 if (DEBUG_WIDGETS) {
3049 Log.d(TAG, "bindAppWidget: " + item);
3050 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003051 final Workspace workspace = mWorkspace;
3052
3053 final int appWidgetId = item.appWidgetId;
3054 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003055 if (DEBUG_WIDGETS) {
3056 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3057 }
3058
Joe Onorato9c1289c2009-08-17 11:03:03 -04003059 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3060
Joe Onorato9c1289c2009-08-17 11:03:03 -04003061 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3062 item.hostView.setTag(item);
3063
Winson Chung3d503fb2011-07-13 17:25:49 -07003064 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003065 item.cellY, item.spanX, item.spanY, false);
3066
Adam Cohended9f8d2010-11-03 13:25:16 -07003067 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3068
Joe Onorato9c1289c2009-08-17 11:03:03 -04003069 workspace.requestLayout();
3070
Daniel Sandler843e8602010-06-07 14:59:01 -04003071 if (DEBUG_WIDGETS) {
3072 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3073 + (SystemClock.uptimeMillis()-start) + "ms");
3074 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003075 }
3076
3077 /**
3078 * Callback saying that there aren't any more items to bind.
3079 *
3080 * Implementation of the method from LauncherModel.Callbacks.
3081 */
3082 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003083 setLoadOnResume();
3084
Joe Onorato9c1289c2009-08-17 11:03:03 -04003085 if (mSavedState != null) {
3086 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003087 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003088 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003089 mSavedState = null;
3090 }
3091
3092 if (mSavedInstanceState != null) {
3093 super.onRestoreInstanceState(mSavedInstanceState);
3094 mSavedInstanceState = null;
3095 }
3096
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003097 // If we received the result of any pending adds while the loader was running (e.g. the
3098 // widget configuration forced an orientation change), process them now.
3099 for (int i = 0; i < sPendingAddList.size(); i++) {
3100 completeAdd(sPendingAddList.get(i));
3101 }
3102 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003103
Winson Chungc51db6a2011-10-05 11:44:49 -07003104 // Update the market app icon as necessary (the other icons will be managed in response to
3105 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003106 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003107
Winson Chungf0c6ae02012-03-21 16:10:31 -07003108 // Animate up any icons as necessary
3109 if (mVisible || mWorkspaceLoading) {
3110 Runnable newAppsRunnable = new Runnable() {
3111 @Override
3112 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003113 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003114 }
3115 };
Winson Chunga2413752012-04-03 14:22:34 -07003116
3117 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3118 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3119 if (canRunNewAppsAnimation()) {
3120 // If the user has not interacted recently, then either snap to the new page to show
3121 // the new-apps animation or just run them if they are to appear on the current page
3122 if (willSnapPage) {
3123 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3124 } else {
3125 runNewAppsAnimation(false);
3126 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003127 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003128 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003129 // are on another page (or just normally if they are added to the current page)
3130 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003131 }
3132 }
3133
3134 mWorkspaceLoading = false;
3135 }
3136
Winson Chunga2413752012-04-03 14:22:34 -07003137 private boolean canRunNewAppsAnimation() {
3138 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3139 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3140 }
3141
Winson Chungf0c6ae02012-03-21 16:10:31 -07003142 /**
3143 * Runs a new animation that scales up icons that were added while Launcher was in the
3144 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003145 *
3146 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003147 */
Winson Chunga2413752012-04-03 14:22:34 -07003148 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003149 AnimatorSet anim = new AnimatorSet();
3150 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003151
3152 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003153 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3154 @Override
3155 public int compare(View a, View b) {
3156 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3157 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3158 int cellCountX = LauncherModel.getCellCountX();
3159 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3160 }
3161 });
Winson Chunga2413752012-04-03 14:22:34 -07003162
3163 // Animate each of the views in place (or show them immediately if requested)
3164 if (immediate) {
3165 for (View v : mNewShortcutAnimateViews) {
3166 v.setAlpha(1f);
3167 v.setScaleX(1f);
3168 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003169 }
Winson Chunga2413752012-04-03 14:22:34 -07003170 } else {
3171 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3172 View v = mNewShortcutAnimateViews.get(i);
3173 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3174 PropertyValuesHolder.ofFloat("alpha", 1f),
3175 PropertyValuesHolder.ofFloat("scaleX", 1f),
3176 PropertyValuesHolder.ofFloat("scaleY", 1f));
3177 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3178 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3179 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3180 bounceAnims.add(bounceAnim);
3181 }
3182 anim.playTogether(bounceAnims);
3183 anim.addListener(new AnimatorListenerAdapter() {
3184 @Override
3185 public void onAnimationEnd(Animator animation) {
3186 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3187 }
3188 });
3189 anim.start();
3190 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003191
3192 // Clean up
3193 mNewShortcutAnimatePage = -1;
3194 mNewShortcutAnimateViews.clear();
3195 new Thread("clearNewAppsThread") {
3196 public void run() {
3197 mSharedPrefs.edit()
3198 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3199 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3200 .commit();
3201 }
3202 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003203 }
3204
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003205 @Override
3206 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003207 boolean searchVisible = updateGlobalSearchIcon();
3208 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3209 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003210 }
3211
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003212 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003213 * Add the icons for all apps.
3214 *
3215 * Implementation of the method from LauncherModel.Callbacks.
3216 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003217 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3218 // Remove the progress bar entirely; we could also make it GONE
3219 // but better to remove it since we know it's not going to be used
3220 View progressBar = mAppsCustomizeTabHost.
3221 findViewById(R.id.apps_customize_progress_bar);
3222 if (progressBar != null) {
3223 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003224 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003225 // We just post the call to setApps so the user sees the progress bar
3226 // disappear-- otherwise, it just looks like the progress bar froze
3227 // which doesn't look great
3228 mAppsCustomizeTabHost.post(new Runnable() {
3229 public void run() {
3230 if (mAppsCustomizeContent != null) {
3231 mAppsCustomizeContent.setApps(apps);
3232 }
3233 }
3234 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003235 }
3236
3237 /**
3238 * A package was installed.
3239 *
3240 * Implementation of the method from LauncherModel.Callbacks.
3241 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003242 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003243 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003244
Winson Chung785d2eb2011-04-14 16:08:02 -07003245 if (mAppsCustomizeContent != null) {
3246 mAppsCustomizeContent.addApps(apps);
3247 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003248 }
3249
3250 /**
3251 * A package was updated.
3252 *
3253 * Implementation of the method from LauncherModel.Callbacks.
3254 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003255 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003256 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003257 if (mWorkspace != null) {
3258 mWorkspace.updateShortcuts(apps);
3259 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003260
Winson Chung785d2eb2011-04-14 16:08:02 -07003261 if (mAppsCustomizeContent != null) {
3262 mAppsCustomizeContent.updateApps(apps);
3263 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003264 }
3265
3266 /**
3267 * A package was uninstalled.
3268 *
3269 * Implementation of the method from LauncherModel.Callbacks.
3270 */
Joe Onorato36115782010-06-17 13:28:48 -04003271 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003272 if (permanent) {
3273 mWorkspace.removeItems(apps);
3274 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003275
Winson Chung785d2eb2011-04-14 16:08:02 -07003276 if (mAppsCustomizeContent != null) {
3277 mAppsCustomizeContent.removeApps(apps);
3278 }
Winson Chunga1820962011-10-03 16:31:06 -07003279
3280 // Notify the drag controller
3281 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003282 }
Joe Onoratobe386092009-11-17 17:32:16 -08003283
3284 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003285 * A number of packages were updated.
3286 */
3287 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003288 if (mAppsCustomizeContent != null) {
3289 mAppsCustomizeContent.onPackagesUpdated();
3290 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003291 }
3292
Winson Chung400438b2011-01-16 17:53:48 -08003293 private int mapConfigurationOriActivityInfoOri(int configOri) {
3294 final Display d = getWindowManager().getDefaultDisplay();
3295 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3296 switch (d.getRotation()) {
3297 case Surface.ROTATION_0:
3298 case Surface.ROTATION_180:
3299 // We are currently in the same basic orientation as the natural orientation
3300 naturalOri = configOri;
3301 break;
3302 case Surface.ROTATION_90:
3303 case Surface.ROTATION_270:
3304 // We are currently in the other basic orientation to the natural orientation
3305 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3306 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3307 break;
3308 }
3309
3310 int[] oriMap = {
3311 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3312 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3313 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3314 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3315 };
3316 // Since the map starts at portrait, we need to offset if this device's natural orientation
3317 // is landscape.
3318 int indexOffset = 0;
3319 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3320 indexOffset = 1;
3321 }
3322 return oriMap[(d.getRotation() + indexOffset) % 4];
3323 }
Adam Cohen446e9402011-09-15 18:21:21 -07003324
Winson Chung641d71d2012-04-26 15:58:01 -07003325 public void lockScreenOrientation() {
3326 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3327 .getConfiguration().orientation));
Winson Chung400438b2011-01-16 17:53:48 -08003328 }
Winson Chung641d71d2012-04-26 15:58:01 -07003329 public void unlockScreenOrientation() {
3330 mHandler.postDelayed(new Runnable() {
3331 public void run() {
3332 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3333 }
3334 }, mRestoreScreenOrientationDelay);
Winson Chung400438b2011-01-16 17:53:48 -08003335 }
3336
Winson Chung82f55532011-08-09 14:14:23 -07003337 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003338 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003339 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003340 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003341
Winson Chung7d7541e2011-09-16 20:14:36 -07003342 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003343 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003344 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3345 Cling cling = (Cling) findViewById(clingId);
3346 if (cling != null) {
3347 cling.init(this, positionData);
3348 cling.setVisibility(View.VISIBLE);
3349 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3350 if (animate) {
3351 cling.buildLayer();
3352 cling.setAlpha(0f);
3353 cling.animate()
3354 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003355 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003356 .setDuration(SHOW_CLING_DURATION)
3357 .setStartDelay(delay)
3358 .start();
3359 } else {
3360 cling.setAlpha(1f);
3361 }
3362 }
3363 return cling;
3364 }
3365 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003366 if (cling != null) {
3367 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003368 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003369 anim.addListener(new AnimatorListenerAdapter() {
3370 public void onAnimationEnd(Animator animation) {
3371 cling.setVisibility(View.GONE);
3372 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003373 // We should update the shared preferences on a background thread
3374 new Thread("dismissClingThread") {
3375 public void run() {
3376 SharedPreferences.Editor editor = mSharedPrefs.edit();
3377 editor.putBoolean(flag, true);
3378 editor.commit();
3379 }
3380 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003381 };
3382 });
3383 anim.start();
3384 }
3385 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003386 private void removeCling(int id) {
3387 final View cling = findViewById(id);
3388 if (cling != null) {
3389 final ViewGroup parent = (ViewGroup) cling.getParent();
3390 parent.post(new Runnable() {
3391 @Override
3392 public void run() {
3393 parent.removeView(cling);
3394 }
3395 });
3396 }
3397 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003398 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003399 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003400 if (isClingsEnabled() &&
3401 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003402 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003403 } else {
3404 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003405 }
3406 }
3407 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003408 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003409 if (isClingsEnabled() &&
3410 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003411 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003412 } else {
3413 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003414 }
3415 }
3416 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003417 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003418 if (isClingsEnabled() &&
3419 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3420 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003421 } else {
3422 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003423 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003424 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003425 }
3426 public boolean isFolderClingVisible() {
3427 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003428 if (cling != null) {
3429 return cling.getVisibility() == View.VISIBLE;
3430 }
3431 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003432 }
Winson Chung82f55532011-08-09 14:14:23 -07003433 public void dismissWorkspaceCling(View v) {
3434 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003435 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003436 }
3437 public void dismissAllAppsCling(View v) {
3438 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003439 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3440 }
3441 public void dismissFolderCling(View v) {
3442 Cling cling = (Cling) findViewById(R.id.folder_cling);
3443 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003444 }
3445
Winson Chung80baf5a2010-08-09 16:03:15 -07003446 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003447 * Prints out out state for debugging.
3448 */
3449 public void dumpState() {
3450 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003451 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003452 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3453 Log.d(TAG, "mRestoring=" + mRestoring);
3454 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3455 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003456 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003457 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003458
Winson Chung785d2eb2011-04-14 16:08:02 -07003459 if (mAppsCustomizeContent != null) {
3460 mAppsCustomizeContent.dumpState();
3461 }
Joe Onoratobe386092009-11-17 17:32:16 -08003462 Log.d(TAG, "END launcher2 dump state");
3463 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003464
3465 @Override
3466 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3467 super.dump(prefix, fd, writer, args);
3468 writer.println(" ");
3469 writer.println("Debug logs: ");
3470 for (int i = 0; i < sDumpLogs.size(); i++) {
3471 writer.println(" " + sDumpLogs.get(i));
3472 }
3473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003474}
Michael Jurka2763be32011-02-24 11:19:57 -08003475
Michael Jurkaabded662011-03-04 12:06:57 -08003476interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003477 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003478 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003479 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003480 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003481 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003482}