blob: af58f79df234cc43c993571f4807e5f15453e52c [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
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
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;
Bjorn Bringert85f418d2013-09-06 12:50:05 +010048import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070050import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070054import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080067import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070072import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
86import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080087import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080089import android.view.accessibility.AccessibilityEvent;
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -070090import android.view.accessibility.AccessibilityManager;
Adam Cohencff6af82011-09-13 14:51:53 -070091import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080092import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Advanceable;
95import android.widget.FrameLayout;
96import android.widget.ImageView;
97import android.widget.TextView;
98import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070099
Daniel Sandler325dc232013-06-05 22:57:57 -0400100import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800101
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.DataInputStream;
103import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700104import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700107import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700109import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700112import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700115import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117/**
118 * Default launcher application.
119 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100120public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700121 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700122 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800123 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Daniel Sandlerf061f822013-06-27 13:59:36 -0400126 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400127 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700128 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400129 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700130 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700133 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 private static final int REQUEST_PICK_APPLICATION = 6;
135 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700137 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Michael Jurka8b805b12012-04-18 14:23:14 -0700139 private static final int REQUEST_BIND_APPWIDGET = 11;
140
Mathew Inwood876a8462013-06-14 14:12:41 +0100141 /**
142 * IntentStarter uses request codes starting with this. This must be greater than all activity
143 * request codes used internally.
144 */
145 protected static final int REQUEST_LAST = 100;
146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
148
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800149 static final int SCREEN_COUNT = 5;
150 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Romain Guy98d01652009-06-30 16:21:04 -0700152 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800153 // To turn on these properties, type
154 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
155 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
156 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Winson Chung2672ff92012-05-04 16:22:30 -0700158 // The Intent extra that defines whether to ignore the launch animation
159 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400160 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: int
163 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700164 // Type: int
165 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700167 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
168 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
170 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700171 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700173 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 // Type: boolean
175 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
176 // Type: long
177 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700178 // Type: int
179 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
180 // Type: int
181 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
182 // Type: parcelable
183 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100185 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700186 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100187 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700188 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100189 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700190
Adam Cohen39a06042013-07-19 14:30:12 -0700191 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700192 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700193
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700194 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700195 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700196 private State mState = State.WORKSPACE;
197 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700198
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700200 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
201 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chungaf40f202013-09-18 18:26:31 -0700202 private static final int SHOW_CLING_DURATION = 250;
203 private static final int DISMISS_CLING_DURATION = 200;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800206 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
Winson Chunga2413752012-04-03 14:22:34 -0700208 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700209 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
210 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700211 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700212
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800213 private final BroadcastReceiver mCloseSystemDialogsReceiver
214 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800215 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private LayoutInflater mInflater;
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700220 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800221 private DragLayer mDragLayer;
222 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700223 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700224
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700225 private AppWidgetManager mAppWidgetManager;
226 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Michael Jurkac9d95c52011-08-29 14:03:34 -0700228 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700229 private AppWidgetProviderInfo mPendingAddWidgetInfo;
230
Michael Jurka0280c3b2010-09-17 15:00:07 -0700231 private int[] mTmpAddItemCellCoordinates = new int[2];
232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 private FolderInfo mFolderInfo;
234
Winson Chung3d503fb2011-07-13 17:25:49 -0700235 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700236 private View mOverviewPanel;
237
Michael Jurka838a4ca2011-02-07 13:33:06 -0800238 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700239
Winson Chungc51db6a2011-10-05 11:44:49 -0700240 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700241 private AppsCustomizeTabHost mAppsCustomizeTabHost;
242 private AppsCustomizePagedView mAppsCustomizeContent;
243 private boolean mAutoAdvanceRunning = false;
Cristina Stancu476493b2013-08-07 17:20:14 +0100244 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700247 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
248 // scroll issues (because the workspace may not have been measured yet) and extra work.
249 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
250 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251
252 private SpannableStringBuilder mDefaultKeySsb = null;
253
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254 private boolean mWorkspaceLoading = true;
255
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800256 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private boolean mRestoring;
258 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700259 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
Michael Jurka1e2f4652013-07-08 18:03:46 -0700261 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700262 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
263
Winson Chung4a2afa32012-07-19 14:53:05 -0700264 // Keep track of whether the user has left launcher
265 private static boolean sPausedFromUserAction = false;
266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 private Bundle mSavedInstanceState;
268
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800270 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800271 private boolean mUserPresent = true;
272 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700273 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800274 private boolean mAttached = false;
Winson Chungfa545132013-09-26 17:45:32 -0700275 private static final boolean DISABLE_CLINGS = false;
Winson Chunge6eabff2013-09-24 11:01:23 -0700276 private static final boolean DISABLE_CUSTOM_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700278 private static LocaleConfiguration sLocaleConfiguration = null;
279
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700280 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700281
Adam Cohen61f560d2013-09-30 15:58:20 -0700282 private View.OnTouchListener mHapticFeedbackTouchListener;
283
Adam Cohended9f8d2010-11-03 13:25:16 -0700284 // Related to the auto-advancing of widgets
285 private final int ADVANCE_MSG = 1;
286 private final int mAdvanceInterval = 20000;
287 private final int mAdvanceStagger = 250;
288 private long mAutoAdvanceSentTime;
289 private long mAutoAdvanceTimeLeft = -1;
290 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
291 new HashMap<View, AppWidgetProviderInfo>();
292
Winson Chung400438b2011-01-16 17:53:48 -0800293 // Determines how long to wait after a rotation before restoring the screen orientation to
294 // match the sensor state.
295 private final int mRestoreScreenOrientationDelay = 500;
296
Michael Jurka4ef207b2010-11-29 17:05:45 -0800297 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700298 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
299 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
300 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800301
Winson Chungd64a6662013-09-30 11:06:59 -0700302 private Intent mAppMarketIntent = null;
303 private static final boolean DISABLE_MARKET_BUTTON = true;
304
Craig Mautner360310b2012-10-26 15:13:08 -0700305 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700306
Adam Cohen1462de32012-07-24 22:34:36 -0700307 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
308
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700309 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700310 static Date sDateStamp = new Date();
311 static DateFormat sDateFormat =
312 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
313 static long sRunStart = System.currentTimeMillis();
314 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700315
Winson Chung46353de2012-02-16 14:05:10 -0800316 // We only want to get the SharedPreferences once since it does an FS stat each time we get
317 // it from the context.
318 private SharedPreferences mSharedPrefs;
319
Adam Cohene25af792013-06-06 23:08:25 -0700320 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
321
Winson Chungf0c6ae02012-03-21 16:10:31 -0700322 // Holds the page that we need to animate to, and the icon views that we need to animate up
323 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700324 private ImageView mFolderIconImageView;
325 private Bitmap mFolderIconBitmap;
326 private Canvas mFolderIconCanvas;
327 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700328
Michael Jurkaddd62e92011-02-16 17:49:14 -0800329 private BubbleTextView mWaitingForResume;
330
Michael Jurka22143132012-10-04 17:42:38 +0200331 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
332 = new HideFromAccessibilityHelper();
333
Michael Jurkac1f5d262011-09-30 19:32:27 -0700334 private Runnable mBuildLayersRunnable = new Runnable() {
335 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700336 if (mWorkspace != null) {
337 mWorkspace.buildPageHardwareLayers();
338 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700339 }
340 };
341
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800342 private static ArrayList<PendingAddArguments> sPendingAddList
343 = new ArrayList<PendingAddArguments>();
344
Michael Jurka7267fa52013-09-26 15:29:57 -0700345 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800346
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800347 private static class PendingAddArguments {
348 int requestCode;
349 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700350 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700351 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800352 int cellX;
353 int cellY;
354 }
355
Daniel Sandlerff02d492013-08-05 02:12:05 -0400356 private Stats mStats;
357
Michael Jurka0a457bf2012-11-19 14:05:05 -0800358 private static boolean isPropertyEnabled(String propertyName) {
359 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700360 }
361
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 @Override
363 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700364 if (DEBUG_STRICT_MODE) {
365 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
366 .detectDiskReads()
367 .detectDiskWrites()
368 .detectNetwork() // or .detectAll() for all detectable problems
369 .penaltyLog()
370 .build());
371 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
372 .detectLeakedSqlLiteObjects()
373 .detectLeakedClosableObjects()
374 .penaltyLog()
375 .penaltyDeath()
376 .build());
377 }
378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400380
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400381 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400382 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700383
384 // Determine the dynamic grid properties
385 Point smallestSize = new Point();
386 Point largestSize = new Point();
387 Point realSize = new Point();
388 Display display = getWindowManager().getDefaultDisplay();
389 display.getCurrentSizeRange(smallestSize, largestSize);
390 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700391 DisplayMetrics dm = new DisplayMetrics();
392 display.getMetrics(dm);
Winson Chung5f8afe62013-08-12 16:19:28 -0700393 // Lazy-initialize the dynamic grid
394 DeviceProfile grid = app.initDynamicGrid(this,
395 Math.min(smallestSize.x, smallestSize.y),
396 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700397 realSize.x, realSize.y,
398 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700399
400 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400401 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700402 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800403 mModel = app.setLauncher(this);
404 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700405 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400406 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700408
Daniel Sandlerff02d492013-08-05 02:12:05 -0400409 mStats = new Stats(this);
410
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700411 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700412
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700413 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
414 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700415
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700416 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
417 // this also ensures that any synchronous binding below doesn't re-trigger another
418 // LauncherModel load.
419 mPaused = false;
420
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200422 android.os.Debug.startMethodTracing(
423 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 }
425
Adam Cohen53805212013-10-01 10:39:23 -0700426
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700429
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100431 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800433 registerContentObservers();
434
Joe Onorato7c312c12009-08-13 21:36:53 -0700435 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 mSavedState = savedInstanceState;
438 restoreState(mSavedState);
439
Winson Chunga12a2502010-12-20 14:41:35 -0800440 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700441 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200442 mAppsCustomizeContent.onPackagesUpdated(
443 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700444 }
Winson Chunga12a2502010-12-20 14:41:35 -0800445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 if (PROFILE_STARTUP) {
447 android.os.Debug.stopMethodTracing();
448 }
449
450 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700451 if (sPausedFromUserAction) {
452 // If the user leaves launcher, then we should just load items asynchronously when
453 // they return.
454 mModel.startLoader(true, -1);
455 } else {
456 // We only load the page synchronously if the user rotates (or triggers a
457 // configuration change) while launcher is in the foreground
458 mModel.startLoader(true, mWorkspace.getCurrentPage());
459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
461
462 // For handling default keys
463 mDefaultKeySsb = new SpannableStringBuilder();
464 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800465
466 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
467 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800468
Adam Cohenf9426d52012-06-04 17:26:21 -0700469 updateGlobalIcons();
470
471 // On large interfaces, we want the screen to auto-rotate based on the current orientation
472 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700473
474 showFirstRunCling();
Adam Cohenf9426d52012-06-04 17:26:21 -0700475 }
476
Winson Chung4a2afa32012-07-19 14:53:05 -0700477 protected void onUserLeaveHint() {
478 super.onUserLeaveHint();
479 sPausedFromUserAction = true;
480 }
481
Winson Chung98ca0f72013-07-29 12:58:51 -0700482 /** To be overriden by subclasses to hint to Launcher that we have custom content */
483 protected boolean hasCustomContentToLeft() {
484 return false;
485 }
486
Dave Hawkeya8881582013-09-17 15:55:33 -0600487 /**
488 * To be overridden by subclasses to create the custom content and call
489 * {@link #addToCustomContentPage}. This will only be invoked if
490 * {@link #hasCustomContentToLeft()} is {@code true}.
491 */
492 protected void addCustomContentToLeft() {
493 }
494
495 /**
496 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
497 * ensure the custom content page is added or removed if necessary.
498 */
499 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600500 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600501 // Not bound yet, wait for bindScreens to be called.
502 return;
503 }
504
505 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
506 // Create the custom content page and call the subclass to populate it.
507 mWorkspace.createCustomContentPage();
508 addCustomContentToLeft();
509 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
510 mWorkspace.removeCustomContentPage();
511 }
512 }
513
Adam Cohenf9426d52012-06-04 17:26:21 -0700514 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700515 boolean searchVisible = false;
516 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800517 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700518 int coi = getCurrentOrientationIndexForGlobalIcons();
519 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
520 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700521 if (!DISABLE_MARKET_BUTTON) {
522 updateAppMarketIcon();
523 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700524 searchVisible = updateGlobalSearchIcon();
525 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700526 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700527 if (sGlobalSearchIcon[coi] != null) {
528 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700529 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700530 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700531 if (sVoiceSearchIcon[coi] != null) {
532 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700533 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700534 }
Winson Chungd64a6662013-09-30 11:06:59 -0700535 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700536 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800537 }
Winson Chungadf0c182012-08-23 14:59:07 -0700538 if (mSearchDropTargetBar != null) {
539 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
540 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 }
Romain Guycbb89e42009-06-08 15:52:54 -0700542
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700544 if (sLocaleConfiguration == null) {
545 new AsyncTask<Void, Void, LocaleConfiguration>() {
546 @Override
547 protected LocaleConfiguration doInBackground(Void... unused) {
548 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
549 readConfiguration(Launcher.this, localeConfiguration);
550 return localeConfiguration;
551 }
552
553 @Override
554 protected void onPostExecute(LocaleConfiguration result) {
555 sLocaleConfiguration = result;
556 checkForLocaleChange(); // recursive, but now with a locale configuration
557 }
558 }.execute();
559 return;
560 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700561
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 final Configuration configuration = getResources().getConfiguration();
563
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700564 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565 final String locale = configuration.locale.toString();
566
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700567 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 final int mcc = configuration.mcc;
569
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700570 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 final int mnc = configuration.mnc;
572
Romain Guy84f296c2009-11-04 15:00:44 -0800573 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574
Romain Guy84f296c2009-11-04 15:00:44 -0800575 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700576 sLocaleConfiguration.locale = locale;
577 sLocaleConfiguration.mcc = mcc;
578 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700579
Joe Onorato0589f0f2010-02-08 13:44:00 -0800580 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700581
582 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
583 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700584 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700585 public void run() {
586 writeConfiguration(Launcher.this, localeConfiguration);
587 }
588 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700589 }
590 }
591
592 private static class LocaleConfiguration {
593 public String locale;
594 public int mcc = -1;
595 public int mnc = -1;
596 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700597
Romain Guy98d01652009-06-30 16:21:04 -0700598 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
599 DataInputStream in = null;
600 try {
601 in = new DataInputStream(context.openFileInput(PREFERENCES));
602 configuration.locale = in.readUTF();
603 configuration.mcc = in.readInt();
604 configuration.mnc = in.readInt();
605 } catch (FileNotFoundException e) {
606 // Ignore
607 } catch (IOException e) {
608 // Ignore
609 } finally {
610 if (in != null) {
611 try {
612 in.close();
613 } catch (IOException e) {
614 // Ignore
615 }
616 }
617 }
618 }
619
620 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
621 DataOutputStream out = null;
622 try {
623 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
624 out.writeUTF(configuration.locale);
625 out.writeInt(configuration.mcc);
626 out.writeInt(configuration.mnc);
627 out.flush();
628 } catch (FileNotFoundException e) {
629 // Ignore
630 } catch (IOException e) {
631 //noinspection ResultOfMethodCallIgnored
632 context.getFileStreamPath(PREFERENCES).delete();
633 } finally {
634 if (out != null) {
635 try {
636 out.close();
637 } catch (IOException e) {
638 // Ignore
639 }
640 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 }
642 }
643
Anton Hanssona2f665f2013-09-26 12:18:32 +0100644 public Stats getStats() {
645 return mStats;
646 }
647
Bjorn Bringertc459e522013-06-07 19:36:01 +0100648 public LayoutInflater getInflater() {
649 return mInflater;
650 }
651
Adam Cohen716b51e2011-06-30 12:09:54 -0700652 public DragLayer getDragLayer() {
653 return mDragLayer;
654 }
655
Winson Chung36a62fe2012-05-06 18:04:42 -0700656 boolean isDraggingEnabled() {
657 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
658 // that is subsequently removed from the workspace in startBinding().
659 return !mModel.isLoadingWorkspace();
660 }
661
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 static int getScreen() {
663 synchronized (sLock) {
664 return sScreen;
665 }
666 }
667
668 static void setScreen(int screen) {
669 synchronized (sLock) {
670 sScreen = screen;
671 }
672 }
673
Winson Chung557d6ed2011-07-08 15:34:52 -0700674 /**
675 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
676 * a configuration step, this allows the proper animations to run after other transitions.
677 */
678 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700679 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800680 switch (args.requestCode) {
681 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700682 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700683 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800684 break;
685 case REQUEST_PICK_SHORTCUT:
686 processShortcut(args.intent);
687 break;
688 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700689 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700690 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700691 result = true;
692 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800693 case REQUEST_CREATE_APPWIDGET:
694 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700695 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700696 result = true;
697 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800698 }
Michael Jurka27614d22012-04-02 06:40:22 -0700699 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
700 // if you turned the screen off and then back while in All Apps, Launcher would not
701 // return to the workspace. Clearing mAddInfo.container here fixes this issue
702 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700703 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800704 }
705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700707 protected void onActivityResult(
708 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700709 // Reset the startActivity waiting flag
710 mWaitingForResult = false;
711
Michael Jurka8b805b12012-04-18 14:23:14 -0700712 if (requestCode == REQUEST_BIND_APPWIDGET) {
713 int appWidgetId = data != null ?
714 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
715 if (resultCode == RESULT_CANCELED) {
716 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
717 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700718 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700719 }
720 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200721 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
722 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
723 mWorkspace.exitOverviewMode(false);
724 }
725 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700726 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200727
Winson Chung557d6ed2011-07-08 15:34:52 -0700728 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800729 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
730 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700731
Adam Cohened66b2b2012-01-23 17:28:51 -0800732 // We have special handling for widgets
733 if (isWidgetDrop) {
734 int appWidgetId = data != null ?
735 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700736 if (appWidgetId < 0) {
737 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
738 "widget configuration activity.");
739 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen00074722013-10-11 17:46:09 -0700740 mWorkspace.stripEmptyScreens();
Winson Chung5aaab772012-04-27 15:24:02 -0700741 } else {
742 completeTwoStageWidgetDrop(resultCode, appWidgetId);
743 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 return;
745 }
746
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 // The pattern used here is that a user PICKs a specific application,
748 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700749
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
751 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700752 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800753 final PendingAddArguments args = new PendingAddArguments();
754 args.requestCode = requestCode;
755 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700756 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700757 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700758 args.cellX = mPendingAddInfo.cellX;
759 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800760 if (isWorkspaceLocked()) {
761 sPendingAddList.add(args);
762 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700763 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 }
Adam Cohen00074722013-10-11 17:46:09 -0700765 } else if (resultCode == RESULT_CANCELED) {
766 mWorkspace.stripEmptyScreens();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800768 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700769 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800770 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
771 null);
772 }
773
774 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700775 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700776 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800777 Runnable onCompleteRunnable = null;
778 int animationType = 0;
779
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700780 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800781 if (resultCode == RESULT_OK) {
782 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
783 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700784 mPendingAddWidgetInfo);
785 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800786 onCompleteRunnable = new Runnable() {
787 @Override
788 public void run() {
789 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700790 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800791 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
792 null);
793 }
794 };
795 } else if (resultCode == RESULT_CANCELED) {
796 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
797 onCompleteRunnable = new Runnable() {
798 @Override
799 public void run() {
800 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
801 null);
802 }
803 };
804 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700805 if (mDragLayer.getAnimatedView() != null) {
806 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
807 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
808 animationType, boundWidget, true);
809 } else {
810 // The animated view may be null in the case of a rotation during widget configuration
811 onCompleteRunnable.run();
812 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 }
814
815 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700816 protected void onStop() {
817 super.onStop();
818 FirstFrameAnimatorHelper.setIsVisible(false);
819 }
820
821 @Override
822 protected void onStart() {
823 super.onStart();
824 FirstFrameAnimatorHelper.setIsVisible(true);
825 }
826
827 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200829 long startTime = 0;
830 if (DEBUG_RESUME_TIME) {
831 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400832 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700835
Winson Chung4a2afa32012-07-19 14:53:05 -0700836 // Restore the previous launcher state
837 if (mOnResumeState == State.WORKSPACE) {
838 showWorkspace(false);
839 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung82963d52013-10-09 11:20:57 -0700840 showAllApps(false, AppsCustomizePagedView.ContentType.Applications, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700841 }
842 mOnResumeState = State.NONE;
843
Craig Mautner360310b2012-10-26 15:13:08 -0700844 // Background was set to gradient in onPause(), restore to black if in all apps.
845 setWorkspaceBackground(mState == State.WORKSPACE);
846
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800847 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700848 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700849 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400850 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700851 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400852 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700853 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700855 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200856 // We might have postponed some bind calls until onResume (see waitUntilResume) --
857 // execute them here
858 long startTimeCallbacks = 0;
859 if (DEBUG_RESUME_TIME) {
860 startTimeCallbacks = System.currentTimeMillis();
861 }
862
863 if (mAppsCustomizeContent != null) {
864 mAppsCustomizeContent.setBulkBind(true);
865 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700866 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
867 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200868 }
869 if (mAppsCustomizeContent != null) {
870 mAppsCustomizeContent.setBulkBind(false);
871 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700872 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200873 if (DEBUG_RESUME_TIME) {
874 Log.d(TAG, "Time spent processing callbacks in onResume: " +
875 (System.currentTimeMillis() - startTimeCallbacks));
876 }
Michael Jurka447bf842013-05-15 14:52:15 +0200877 }
Michael Jurka54554252013-08-01 12:52:23 +0200878 if (mOnResumeCallbacks.size() > 0) {
879 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
880 mOnResumeCallbacks.get(i).run();
881 }
882 mOnResumeCallbacks.clear();
883 }
Winson Chunge4e50662012-01-23 14:45:13 -0800884
885 // Reset the pressed state of icons that were locked in the press state while activities
886 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800887 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800888 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800889 mWaitingForResume.setStayPressed(false);
890 }
Winson Chunge4e50662012-01-23 14:45:13 -0800891 if (mAppsCustomizeContent != null) {
892 // Resets the previous all apps icon press state
893 mAppsCustomizeContent.resetDrawableState();
894 }
Winson Chung82963d52013-10-09 11:20:57 -0700895
Adam Cohen06dff352012-06-01 17:17:08 -0700896 // It is possible that widgets can receive updates while launcher is not in the foreground.
897 // Consequently, the widgets will be inflated in the orientation of the foreground activity
898 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
899 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700900 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700901
Winson Chung780fe592013-09-26 14:48:44 -0700902 // Process any items that were added while Launcher was away.
903 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
904
Winson Chung5841efa2013-09-30 18:06:44 -0700905 // Update the voice search button proxy
906 updateVoiceButtonProxyVisible(false);
907
Adam Cohenf9426d52012-06-04 17:26:21 -0700908 // Again, as with the above scenario, it's possible that one or more of the global icons
909 // were updated in the wrong orientation.
910 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200911 if (DEBUG_RESUME_TIME) {
912 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
913 }
Adam Cohen6fecd412013-10-02 17:41:50 -0700914
915 if (mWorkspace.getCustomContentCallbacks() != null) {
916 // If we are resuming and the custom content is the current page, we call onShow().
917 // It is also poassible that onShow will instead be called slightly after first layout
918 // if PagedView#setRestorePage was set to the custom content page in onCreate().
919 if (mWorkspace.isOnOrMovingToCustomContent()) {
920 mWorkspace.getCustomContentCallbacks().onShow();
921 }
922 }
Adam Cohenedaaa302013-10-01 17:33:27 -0700923 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -0700924 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -0700925 }
926
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700928 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700929 // Ensure that items added to Launcher are queued until Launcher returns
930 InstallShortcutReceiver.enableInstallQueue();
931
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700932 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700933 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800934 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700935 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700936
937 // We call onHide() aggressively. The custom content callbacks should be able to
938 // debounce excess onHide calls.
939 if (mWorkspace.getCustomContentCallbacks() != null) {
940 mWorkspace.getCustomContentCallbacks().onHide();
941 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700942 }
Romain Guycbb89e42009-06-08 15:52:54 -0700943
Adam Cohen66a01fd2013-06-11 12:48:00 -0700944 protected void onFinishBindingItems() {
Adam Cohen82398362013-09-29 12:57:34 -0700945 if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600946 addCustomContentToLeft();
947 }
Adam Cohen66a01fd2013-06-11 12:48:00 -0700948 }
949
Adam Cohenbffe7452013-07-22 18:21:45 -0700950 QSBScroller mQsbScroller = new QSBScroller() {
951 int scrollY = 0;
952
953 @Override
954 public void setScrollY(int scroll) {
955 scrollY = scroll;
956
957 if (mWorkspace.isOnOrMovingToCustomContent()) {
958 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +0100959 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -0700960 }
961 }
962 };
963
964 public void resetQSBScroll() {
965 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +0100966 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -0700967 }
968
969 public interface CustomContentCallbacks {
970 // Custom content is completely shown
971 public void onShow();
972
973 // Custom content is completely hidden
974 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -0700975
976 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
977 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -0700978 }
979
Adam Cohen30bacb22013-08-29 14:25:25 -0700980 protected void startSettings() {
981 }
982
Adam Cohenbffe7452013-07-22 18:21:45 -0700983 public interface QSBScroller {
984 public void setScrollY(int scrollY);
985 }
986
Winson Chung98ca0f72013-07-29 12:58:51 -0700987 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -0700988 CustomContentCallbacks callbacks, String description) {
989 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -0700990 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700991 }
992
Adam Cohenedb40762013-07-18 16:45:45 -0700993 // The custom content needs to offset its content to account for the QSB
994 public int getTopOffsetForCustomContent() {
995 return mWorkspace.getPaddingTop();
996 }
997
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700998 @Override
999 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000 // Flag the loader to stop early before switching
1001 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001002 if (mAppsCustomizeContent != null) {
1003 mAppsCustomizeContent.surrender();
1004 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001005 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001006 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001007
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001008 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001009 @Override
1010 public void onWindowFocusChanged(boolean hasFocus) {
1011 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001012 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001013 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 private boolean acceptFilter() {
1016 final InputMethodManager inputManager = (InputMethodManager)
1017 getSystemService(Context.INPUT_METHOD_SERVICE);
1018 return !inputManager.isFullscreenMode();
1019 }
1020
1021 @Override
1022 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001023 final int uniChar = event.getUnicodeChar();
1024 final boolean handled = super.onKeyDown(keyCode, event);
1025 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1026 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1028 keyCode, event);
1029 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001030 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001031 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001032 // showSearchDialog()
1033 // If there are multiple keystrokes before the search dialog takes focus,
1034 // onSearchRequested() will be called for every keystroke,
1035 // but it is idempotent, so it's fine.
1036 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 }
1038 }
1039
Joe Onorato8a9625e2010-01-28 15:55:35 -08001040 // Eat the long press event so the keyboard doesn't come up.
1041 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1042 return true;
1043 }
1044
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 return handled;
1046 }
1047
Karl Rosaen138a0412009-04-23 19:00:21 -07001048 private String getTypedText() {
1049 return mDefaultKeySsb.toString();
1050 }
1051
1052 private void clearTypedText() {
1053 mDefaultKeySsb.clear();
1054 mDefaultKeySsb.clearSpans();
1055 Selection.setSelection(mDefaultKeySsb, 0);
1056 }
1057
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001059 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1060 * State
1061 */
1062 private static State intToState(int stateOrdinal) {
1063 State state = State.WORKSPACE;
1064 final State[] stateValues = State.values();
1065 for (int i = 0; i < stateValues.length; i++) {
1066 if (stateValues[i].ordinal() == stateOrdinal) {
1067 state = stateValues[i];
1068 break;
1069 }
1070 }
1071 return state;
1072 }
1073
1074 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 * Restores the previous state, if it exists.
1076 *
1077 * @param savedState The previous state.
1078 */
1079 private void restoreState(Bundle savedState) {
1080 if (savedState == null) {
1081 return;
1082 }
1083
Michael Jurka883f55b2010-10-21 15:47:14 -07001084 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001085 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001086 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001087 }
1088
Adam Cohen21cd0022013-10-09 18:57:02 -07001089 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1090 PagedView.INVALID_RESTORE_PAGE);
1091 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001092 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 }
1094
Winson Chung3d503fb2011-07-13 17:25:49 -07001095 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001096 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001097
Winson Chung3d503fb2011-07-13 17:25:49 -07001098 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1099 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001100 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001101 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1102 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001103 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1104 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1105 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001106 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 mRestoring = true;
1108 }
1109
1110 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1111 if (renameFolder) {
1112 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001113 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 mRestoring = true;
1115 }
Winson Chunga12a2502010-12-20 14:41:35 -08001116
Winson Chung785d2eb2011-04-14 16:08:02 -07001117 // Restore the AppsCustomize tab
1118 if (mAppsCustomizeTabHost != null) {
1119 String curTab = savedState.getString("apps_customize_currentTab");
1120 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001121 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001122 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001123 mAppsCustomizeContent.loadAssociatedPages(
1124 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001125 }
1126
Winson Chung5afbf7b2011-07-25 11:53:08 -07001127 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1128 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 }
1131
1132 /**
1133 * Finds all the views we need and configure them properly.
1134 */
1135 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001136 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001137
Craig Mautner360310b2012-10-26 15:13:08 -07001138 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001139 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1140 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001141
John Spurlock77e1f472013-09-11 10:09:51 -04001142 mLauncherView.setSystemUiVisibility(
1143 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001144 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145
Winson Chung4c98d922011-05-31 16:50:48 -07001146 // Setup the drag layer
1147 mDragLayer.setup(this, dragController);
1148
Winson Chung3d503fb2011-07-13 17:25:49 -07001149 // Setup the hotseat
1150 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1151 if (mHotseat != null) {
1152 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001153 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001154 }
1155
Adam Cohenf358a4b2013-07-23 16:47:31 -07001156 mOverviewPanel = findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001157 View widgetButton = findViewById(R.id.widget_button);
1158 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001159 @Override
1160 public void onClick(View arg0) {
Winson Chung82963d52013-10-09 11:20:57 -07001161 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001162 }
1163 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001164 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1165
1166 View wallpaperButton = findViewById(R.id.wallpaper_button);
1167 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001168 @Override
1169 public void onClick(View arg0) {
1170 startWallpaper();
1171 }
1172 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001173 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1174
1175 View settingsButton = findViewById(R.id.settings_button);
1176 settingsButton.setOnClickListener(new OnClickListener() {
Adam Cohen30bacb22013-08-29 14:25:25 -07001177 @Override
1178 public void onClick(View arg0) {
1179 startSettings();
1180 }
1181 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001182 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohendbdff6b2013-09-18 19:09:15 -07001183 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001184
Winson Chung4c98d922011-05-31 16:50:48 -07001185 // Setup the workspace
1186 mWorkspace.setHapticFeedbackEnabled(false);
1187 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001188 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001189 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001190
Winson Chungf0ea4d32011-06-06 14:27:16 -07001191 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001192 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001193
Winson Chungf0ea4d32011-06-06 14:27:16 -07001194 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001195 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001196 mAppsCustomizeContent = (AppsCustomizePagedView)
1197 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1198 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001199
Winson Chung3d503fb2011-07-13 17:25:49 -07001200 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001201 dragController.setDragScoller(mWorkspace);
1202 dragController.setScrollView(mDragLayer);
1203 dragController.setMoveTarget(mWorkspace);
1204 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001205 if (mSearchDropTargetBar != null) {
1206 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001207 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001208
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001209 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001210 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001211 mWeightWatcher = new WeightWatcher(this);
1212 mWeightWatcher.setAlpha(0.5f);
1213 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001214 new FrameLayout.LayoutParams(
1215 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001216 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001217 Gravity.BOTTOM)
1218 );
Adam Cohen39a06042013-07-19 14:30:12 -07001219
1220 boolean show = shouldShowWeightWatcher();
1221 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001222 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 }
1224
1225 /**
1226 * Creates a view representing a shortcut.
1227 *
1228 * @param info The data structure describing the shortcut.
1229 *
1230 * @return A View inflated from R.layout.application.
1231 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001232 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001234 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 }
1236
1237 /**
1238 * Creates a view representing a shortcut inflated from the specified resource.
1239 *
1240 * @param layoutResId The id of the XML layout used to create the shortcut.
1241 * @param parent The group the shortcut belongs to.
1242 * @param info The data structure describing the shortcut.
1243 *
1244 * @return A View inflated from layoutResId.
1245 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001246 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001247 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1248 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 return favorite;
1251 }
1252
1253 /**
1254 * Add an application shortcut to the workspace.
1255 *
1256 * @param data The intent describing the application.
1257 * @param cellInfo The position on screen where to create the shortcut.
1258 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001259 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001260 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001261 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001262
Adam Cohenfbba09b2011-07-18 21:43:05 -07001263 // First we check if we already know the exact location where we want to add this item.
1264 if (cellX >= 0 && cellY >= 0) {
1265 cellXY[0] = cellX;
1266 cellXY[1] = cellY;
1267 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001268 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001269 return;
1270 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001272 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001273
Romain Guy73b979d2009-06-09 12:57:21 -07001274 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001275 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001277 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001278 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001279 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001280 } else {
1281 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 }
1283 }
Romain Guycbb89e42009-06-08 15:52:54 -07001284
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 /**
1286 * Add a shortcut to the workspace.
1287 *
1288 * @param data The intent describing the shortcut.
1289 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001291 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001292 int cellY) {
1293 int[] cellXY = mTmpAddItemCellCoordinates;
1294 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001295 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001296
Michael Jurkad3ef3062010-11-23 16:23:58 -08001297 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001298
Adam Cohen558baaf2011-08-15 15:22:57 -07001299 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001300 if (info == null) {
1301 return;
1302 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001303 final View view = createShortcut(info);
1304
Adam Cohenfbba09b2011-07-18 21:43:05 -07001305 // First we check if we already know the exact location where we want to add this item.
1306 if (cellX >= 0 && cellY >= 0) {
1307 cellXY[0] = cellX;
1308 cellXY[1] = cellY;
1309 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001310
1311 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001312 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001313 true, null,null)) {
1314 return;
1315 }
1316 DragObject dragObject = new DragObject();
1317 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001318 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1319 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001320 return;
1321 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001322 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001323 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001324 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001325 foundCellSpan = (result != null);
1326 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001327 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001328 }
1329
1330 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001331 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001332 return;
1333 }
1334
Adam Cohendcd297f2013-06-18 13:13:40 -07001335 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336
1337 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001338 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001339 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 }
1341 }
1342
Adam Cohen2f093b62012-04-30 18:59:53 -07001343 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1344 int minHeight) {
1345 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001346 // We want to account for the extra amount of padding that we are adding to the widget
1347 // to ensure that it gets the full amount of space that it has requested
1348 int requiredWidth = minWidth + padding.left + padding.right;
1349 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001350 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001351 }
1352
Adam Cohen2f093b62012-04-30 18:59:53 -07001353 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1354 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001355 }
1356
Adam Cohen2f093b62012-04-30 18:59:53 -07001357 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1358 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001359 }
1360
Adam Cohen2f093b62012-04-30 18:59:53 -07001361 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1362 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001363 }
1364
Adam Cohen2f093b62012-04-30 18:59:53 -07001365 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1366 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001367 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001368 }
1369
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001371 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001373 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001374 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001376 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001377 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1378 if (appWidgetInfo == null) {
1379 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1380 }
Romain Guycbb89e42009-06-08 15:52:54 -07001381
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001382 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001383 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001384
Adam Cohen2f093b62012-04-30 18:59:53 -07001385 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1386 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001387
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001389 // We have saved the position to which the widget was dragged-- this really only matters
1390 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001391 int[] cellXY = mTmpAddItemCellCoordinates;
1392 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001393 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001394 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001395 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1396 cellXY[0] = mPendingAddInfo.cellX;
1397 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001398 spanXY[0] = mPendingAddInfo.spanX;
1399 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001400 foundCellSpan = true;
1401 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001402 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001403 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001404 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1405 spanXY[1], cellXY, finalSpan);
1406 spanXY[0] = finalSpan[0];
1407 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001408 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001409 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001410 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001411 }
1412
Michael Jurka0280c3b2010-09-17 15:00:07 -07001413 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001414 if (appWidgetId != -1) {
1415 // Deleting an app widget ID is a void call but writes to disk before returning
1416 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001417 new Thread("deleteAppWidgetId") {
1418 public void run() {
1419 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1420 }
1421 }.start();
1422 }
Winson Chung93eef082012-03-23 15:59:27 -07001423 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001424 return;
1425 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001427 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001428 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1429 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001430 launcherInfo.spanX = spanXY[0];
1431 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001432 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1433 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001434
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001436 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437
1438 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001439 if (hostView == null) {
1440 // Perform actual inflation because we're live
1441 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1442 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1443 } else {
1444 // The AppWidgetHostView has already been inflated and instantiated
1445 launcherInfo.hostView = hostView;
1446 }
Romain Guycbb89e42009-06-08 15:52:54 -07001447
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001449 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001450 launcherInfo.notifyWidgetSizeChanged(this);
1451
Adam Cohendcd297f2013-06-18 13:13:40 -07001452 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001453 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001454
1455 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001457 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 }
Romain Guycbb89e42009-06-08 15:52:54 -07001459
Adam Cohended9f8d2010-11-03 13:25:16 -07001460 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1461 @Override
1462 public void onReceive(Context context, Intent intent) {
1463 final String action = intent.getAction();
1464 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1465 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001466 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001467 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001468
Winson Chungc100e8e2011-08-09 16:02:43 -07001469 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001470 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001471 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001472 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001473 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001474 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1475 mUserPresent = true;
1476 updateRunning();
1477 }
1478 }
1479 };
1480
1481 @Override
1482 public void onAttachedToWindow() {
1483 super.onAttachedToWindow();
1484
1485 // Listen for broadcasts related to user-presence
1486 final IntentFilter filter = new IntentFilter();
1487 filter.addAction(Intent.ACTION_SCREEN_OFF);
1488 filter.addAction(Intent.ACTION_USER_PRESENT);
1489 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001490 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001491 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001492 mVisible = true;
1493 }
1494
1495 @Override
1496 public void onDetachedFromWindow() {
1497 super.onDetachedFromWindow();
1498 mVisible = false;
1499
Adam Cohend113e0c2010-11-11 10:48:05 -08001500 if (mAttached) {
1501 unregisterReceiver(mReceiver);
1502 mAttached = false;
1503 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001504 updateRunning();
1505 }
1506
1507 public void onWindowVisibilityChanged(int visibility) {
1508 mVisible = visibility == View.VISIBLE;
1509 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001510 // The following code used to be in onResume, but it turns out onResume is called when
1511 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1512 // is a more appropriate event to handle
1513 if (mVisible) {
1514 mAppsCustomizeTabHost.onWindowVisible();
1515 if (!mWorkspaceLoading) {
1516 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001517 // We want to let Launcher draw itself at least once before we force it to build
1518 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001519 // apps is nice and speedy.
1520 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001521 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001522 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001523 if (mStarted) return;
1524 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001525 // We delay the layer building a bit in order to give
1526 // other message processing a time to run. In particular
1527 // this avoids a delay in hiding the IME if it was
1528 // currently shown, because doing that may involve
1529 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001530 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001531 final ViewTreeObserver.OnDrawListener listener = this;
1532 mWorkspace.post(new Runnable() {
1533 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001534 if (mWorkspace != null &&
1535 mWorkspace.getViewTreeObserver() != null) {
1536 mWorkspace.getViewTreeObserver().
1537 removeOnDrawListener(listener);
1538 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001539 }
1540 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001541 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001542 }
1543 });
1544 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001545 // When Launcher comes back to foreground, a different Activity might be responsible for
1546 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001547 if (!DISABLE_MARKET_BUTTON) {
1548 updateAppMarketIcon();
1549 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001550 clearTypedText();
1551 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001552 }
1553
1554 private void sendAdvanceMessage(long delay) {
1555 mHandler.removeMessages(ADVANCE_MSG);
1556 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1557 mHandler.sendMessageDelayed(msg, delay);
1558 mAutoAdvanceSentTime = System.currentTimeMillis();
1559 }
1560
1561 private void updateRunning() {
1562 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1563 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1564 mAutoAdvanceRunning = autoAdvanceRunning;
1565 if (autoAdvanceRunning) {
1566 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1567 sendAdvanceMessage(delay);
1568 } else {
1569 if (!mWidgetsToAdvance.isEmpty()) {
1570 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1571 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1572 }
1573 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001574 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001575 }
1576 }
1577 }
1578
1579 private final Handler mHandler = new Handler() {
1580 @Override
1581 public void handleMessage(Message msg) {
1582 if (msg.what == ADVANCE_MSG) {
1583 int i = 0;
1584 for (View key: mWidgetsToAdvance.keySet()) {
1585 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1586 final int delay = mAdvanceStagger * i;
1587 if (v instanceof Advanceable) {
1588 postDelayed(new Runnable() {
1589 public void run() {
1590 ((Advanceable) v).advance();
1591 }
1592 }, delay);
1593 }
1594 i++;
1595 }
1596 sendAdvanceMessage(mAdvanceInterval);
1597 }
1598 }
1599 };
1600
1601 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001602 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001603 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1604 if (v instanceof Advanceable) {
1605 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001606 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001607 updateRunning();
1608 }
1609 }
1610
1611 void removeWidgetToAutoAdvance(View hostView) {
1612 if (mWidgetsToAdvance.containsKey(hostView)) {
1613 mWidgetsToAdvance.remove(hostView);
1614 updateRunning();
1615 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001616 }
1617
Joe Onorato9c1289c2009-08-17 11:03:03 -04001618 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001619 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001620 launcherInfo.hostView = null;
1621 }
1622
Winson Chung93eef082012-03-23 15:59:27 -07001623 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1624 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1625 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001626 }
1627
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001628 public LauncherAppWidgetHost getAppWidgetHost() {
1629 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 }
Romain Guycbb89e42009-06-08 15:52:54 -07001631
Winson Chunga9abd0e2010-10-27 17:18:37 -07001632 public LauncherModel getModel() {
1633 return mModel;
1634 }
1635
Bjorn Bringertc459e522013-06-07 19:36:01 +01001636 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001637 getWindow().closeAllPanels();
1638
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001639 // Whatever we were doing is hereby canceled.
1640 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001641 }
1642
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 @Override
1644 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001645 long startTime = 0;
1646 if (DEBUG_RESUME_TIME) {
1647 startTime = System.currentTimeMillis();
1648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 super.onNewIntent(intent);
1650
1651 // Close the menu
1652 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001653 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001654 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001655
Adam Cohened307df2013-10-02 09:37:31 -07001656 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1657 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1658 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001659
Adam Cohen6fecd412013-10-02 17:41:50 -07001660 if (mWorkspace == null) {
1661 // Can be cases where mWorkspace is null, this prevents a NPE
1662 return;
1663 }
1664 Folder openFolder = mWorkspace.getOpenFolder();
1665 // In all these cases, only animate if we're already on home
1666 mWorkspace.exitWidgetResizeMode();
1667 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1668 openFolder == null) {
1669 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001670 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001671
Adam Cohen6fecd412013-10-02 17:41:50 -07001672 closeFolder();
1673 exitSpringLoadedDragMode();
1674
1675 // If we are already on home, then just animate back to the workspace,
1676 // otherwise, just wait until onResume to set the state back to Workspace
1677 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001678 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001679 } else {
1680 mOnResumeState = State.WORKSPACE;
1681 }
1682
1683 final View v = getWindow().peekDecorView();
1684 if (v != null && v.getWindowToken() != null) {
1685 InputMethodManager imm = (InputMethodManager)getSystemService(
1686 INPUT_METHOD_SERVICE);
1687 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1688 }
1689
1690 // Reset the apps customize page
1691 if (mAppsCustomizeTabHost != null) {
1692 mAppsCustomizeTabHost.reset();
1693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 }
Adam Cohened307df2013-10-02 09:37:31 -07001695
Michael Jurka447bf842013-05-15 14:52:15 +02001696 if (DEBUG_RESUME_TIME) {
1697 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1698 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 }
1700
1701 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001702 public void onRestoreInstanceState(Bundle state) {
1703 super.onRestoreInstanceState(state);
1704 for (int page: mSynchronouslyBoundPages) {
1705 mWorkspace.restoreInstanceStateForChild(page);
1706 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 }
1708
1709 @Override
1710 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001711 if (mWorkspace.getChildCount() > 0) {
1712 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getRestorePage());
1713 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001714 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715
Michael Jurka883f55b2010-10-21 15:47:14 -07001716 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001717 // We close any open folder since it will not be re-opened, and we need to make sure
1718 // this state is reflected.
1719 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720
Adam Cohendcd297f2013-06-18 13:13:40 -07001721 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001722 mWaitingForResult) {
1723 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001724 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001725 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1726 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001727 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1728 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1729 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730 }
1731
1732 if (mFolderInfo != null && mWaitingForResult) {
1733 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1734 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1735 }
Michael Jurka946ad472010-07-09 18:05:18 -07001736
Winson Chung785d2eb2011-04-14 16:08:02 -07001737 // Save the current AppsCustomize tab
1738 if (mAppsCustomizeTabHost != null) {
1739 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1740 if (currentTabTag != null) {
1741 outState.putString("apps_customize_currentTab", currentTabTag);
1742 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001743 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1744 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001745 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 }
1747
1748 @Override
1749 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001751
Winson Chunge7a03942011-08-05 15:05:12 -07001752 // Remove all pending runnables
1753 mHandler.removeMessages(ADVANCE_MSG);
1754 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001755 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001756
Winson Chungcd2b0142011-06-08 16:02:26 -07001757 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001758 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001759 mModel.stopLoader();
1760 app.setLauncher(null);
1761
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001763 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001765 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001767 mAppWidgetHost = null;
1768
1769 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770
1771 TextKeyListener.getInstance().release();
1772
Winson Chung81b52252012-08-27 15:34:29 -07001773 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1774 // to prevent leaking Launcher activities on orientation change.
1775 if (mModel != null) {
1776 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1777 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001778
1779 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001780 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001781
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001782 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001783 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1784 mWorkspace.removeAllViews();
1785 mWorkspace = null;
1786 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001787
Michael Jurka2ecf9952012-06-18 12:52:28 -07001788 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 }
1790
Adam Cohena9cf38f2011-05-02 15:36:58 -07001791 public DragController getDragController() {
1792 return mDragController;
1793 }
1794
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 @Override
1796 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001797 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 super.startActivityForResult(intent, requestCode);
1799 }
1800
Winson Chung70d72102011-08-12 11:24:35 -07001801 /**
1802 * Indicates that we want global search for this activity by setting the globalSearch
1803 * argument for {@link #startSearch} to true.
1804 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001806 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001808
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001809 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001810
Karl Rosaen138a0412009-04-23 19:00:21 -07001811 if (initialQuery == null) {
1812 // Use any text typed in the launcher as the initial query
1813 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001814 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 if (appSearchData == null) {
1816 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001817 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 }
Winson Chungadf0c182012-08-23 14:59:07 -07001819 Rect sourceBounds = new Rect();
1820 if (mSearchDropTargetBar != null) {
1821 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1822 }
Romain Guycbb89e42009-06-08 15:52:54 -07001823
Bjorn Bringertc459e522013-06-07 19:36:01 +01001824 startSearch(initialQuery, selectInitialQuery,
1825 appSearchData, sourceBounds);
1826 }
1827
1828 public void startSearch(String initialQuery,
1829 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001830 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001831 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001832 }
1833
1834 /**
1835 * Starts the global search activity. This code is a copied from SearchManager
1836 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001837 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001838 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001839 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001840 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1841 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1842 if (globalSearchActivity == null) {
1843 Log.w(TAG, "No global search activity found.");
1844 return;
1845 }
1846 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1847 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1848 intent.setComponent(globalSearchActivity);
1849 // Make sure that we have a Bundle to put source in
1850 if (appSearchData == null) {
1851 appSearchData = new Bundle();
1852 } else {
1853 appSearchData = new Bundle(appSearchData);
1854 }
1855 // Set source to package name of app that starts global search, if not set already.
1856 if (!appSearchData.containsKey("source")) {
1857 appSearchData.putString("source", getPackageName());
1858 }
1859 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1860 if (!TextUtils.isEmpty(initialQuery)) {
1861 intent.putExtra(SearchManager.QUERY, initialQuery);
1862 }
1863 if (selectInitialQuery) {
1864 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1865 }
1866 intent.setSourceBounds(sourceBounds);
1867 try {
1868 startActivity(intent);
1869 } catch (ActivityNotFoundException ex) {
1870 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1871 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 }
1873
Winson Chung70d72102011-08-12 11:24:35 -07001874 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001875 public boolean onPrepareOptionsMenu(Menu menu) {
1876 super.onPrepareOptionsMenu(menu);
Michael Jurkab94f3f82013-09-11 18:08:54 +02001877 if (!mWorkspace.isInOverviewMode()) {
1878 mWorkspace.enterOverviewMode();
Winson Chung70d72102011-08-12 11:24:35 -07001879 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001880 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001881 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001883 @Override
1884 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001885 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001886 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001887 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001888 }
1889
Joe Onorato9c1289c2009-08-17 11:03:03 -04001890 public boolean isWorkspaceLocked() {
1891 return mWorkspaceLoading || mWaitingForResult;
1892 }
1893
Michael Jurka0280c3b2010-09-17 15:00:07 -07001894 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001895 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001896 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001897 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1898 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001899 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001900 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001901 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001902
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001903 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1904 AppWidgetProviderInfo appWidgetInfo) {
1905 if (appWidgetInfo.configure != null) {
1906 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001907
Bjorn Bringert7984c942009-12-09 15:38:25 +00001908 // Launch over to configure widget, if needed
1909 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001910 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001911 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001912 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001914 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001915 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001916 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001917 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001918 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
1920 }
Romain Guycbb89e42009-06-08 15:52:54 -07001921
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001922 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07001923 // Close any folders that may be open.
1924 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001925 mWorkspace.moveToCustomContentScreen(animate);
1926 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001927 /**
1928 * Process a shortcut drop.
1929 *
1930 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001931 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001932 * @param cell The cell it should be added to, optional
1933 * @param position The location on the screen where it was dropped, optional
1934 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001935 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001936 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001937 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001938 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001939 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001940 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001941
1942 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001943 mPendingAddInfo.cellX = cell[0];
1944 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001945 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001946
1947 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1948 createShortcutIntent.setComponent(componentName);
1949 processShortcut(createShortcutIntent);
1950 }
1951
Adam Cohenfbba09b2011-07-18 21:43:05 -07001952 /**
1953 * Process a widget drop.
1954 *
1955 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001956 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001957 * @param cell The cell it should be added to, optional
1958 * @param position The location on the screen where it was dropped, optional
1959 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001960 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001961 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001962 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001963 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001964 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001965 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001966 mPendingAddInfo.minSpanX = info.minSpanX;
1967 mPendingAddInfo.minSpanY = info.minSpanY;
1968
Adam Cohenfbba09b2011-07-18 21:43:05 -07001969 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001970 mPendingAddInfo.cellX = cell[0];
1971 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001972 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001973 if (span != null) {
1974 mPendingAddInfo.spanX = span[0];
1975 mPendingAddInfo.spanY = span[1];
1976 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001977
Adam Cohened66b2b2012-01-23 17:28:51 -08001978 AppWidgetHostView hostView = info.boundWidget;
1979 int appWidgetId;
1980 if (hostView != null) {
1981 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001982 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001983 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001984 // In this case, we either need to start an activity to get permission to bind
1985 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001986 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001987 Bundle options = info.bindOptions;
1988
1989 boolean success = false;
1990 if (options != null) {
1991 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1992 info.componentName, options);
1993 } else {
1994 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1995 info.componentName);
1996 }
1997 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001998 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001999 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002000 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002001 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2002 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2003 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002004 // TODO: we need to make sure that this accounts for the options bundle.
2005 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002006 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2007 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002008 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002009 }
2010
Joe Onoratodeb98af2010-02-19 14:59:39 -08002011 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002012 // Handle case where user selected "Applications"
2013 String applicationName = getResources().getString(R.string.group_applications);
2014 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002015
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002016 if (applicationName != null && applicationName.equals(shortcutName)) {
2017 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2018 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002019
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002020 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2021 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002022 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002023 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002024 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002025 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 }
2028
Winson Chung24ab2f12010-09-16 14:10:47 -07002029 void processWallpaper(Intent intent) {
2030 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2031 }
2032
Adam Cohendcd297f2013-06-18 13:13:40 -07002033 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002034 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002035 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 folderInfo.title = getText(R.string.folder_name);
2037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002039 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002040 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002041 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042
2043 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002044 FolderIcon newFolder =
2045 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002046 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002047 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002048 // Force measure the new folder icon
2049 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2050 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002051 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 }
Romain Guycbb89e42009-06-08 15:52:54 -07002053
Joe Onorato9c1289c2009-08-17 11:03:03 -04002054 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002055 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002056 }
2057
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002058 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002060 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002061 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 }
2063
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002064 protected ComponentName getWallpaperPickerComponent() {
2065 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2066 }
2067
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002068 /**
2069 * Registers various content observers. The current implementation registers
2070 * only a favorites observer to keep track of the favorites applications.
2071 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002072 private void registerContentObservers() {
2073 ContentResolver resolver = getContentResolver();
2074 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2075 true, mWidgetObserver);
2076 }
2077
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 @Override
2079 public boolean dispatchKeyEvent(KeyEvent event) {
2080 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2081 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002083 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002084 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002085 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002086 dumpState();
2087 return true;
2088 }
2089 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002090 }
2091 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2092 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002093 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 return true;
2095 }
2096 }
2097
2098 return super.dispatchKeyEvent(event);
2099 }
2100
Joe Onorato88ec0992009-11-19 13:16:06 -08002101 @Override
2102 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002103 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002104 if (mAppsCustomizeContent.getContentType() ==
2105 AppsCustomizePagedView.ContentType.Applications) {
2106 showWorkspace(true);
2107 } else {
2108 showOverviewMode(true);
2109 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002110 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002111 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002112 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002113 Folder openFolder = mWorkspace.getOpenFolder();
2114 if (openFolder.isEditingName()) {
2115 openFolder.dismissEditingName();
2116 } else {
2117 closeFolder();
2118 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002119 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002120 mWorkspace.exitWidgetResizeMode();
2121
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002122 // Back button is a no-op here, but give at least some feedback for the button press
2123 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002124 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002125 }
2126
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002127 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002128 * Re-listen when widgets are reset.
2129 */
2130 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002131 if (mAppWidgetHost != null) {
2132 mAppWidgetHost.startListening();
2133 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002134 }
2135
2136 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 * Launches the intent referred by the clicked shortcut.
2138 *
2139 * @param v The view representing the clicked shortcut.
2140 */
2141 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002142 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2143 // view has detached (it's possible for this to happen if the view is removed mid touch).
2144 if (v.getWindowToken() == null) {
2145 return;
2146 }
2147
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002148 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002149 return;
2150 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002151
Adam Cohen1697b792013-09-17 19:08:21 -07002152 if (v instanceof Workspace) {
2153 if (mWorkspace.isInOverviewMode()) {
2154 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002155 }
2156 return;
2157 }
2158
2159 if (v instanceof CellLayout) {
2160 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002161 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002162 }
2163 }
2164
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002165 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002166 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002168 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2169 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002170
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002171 // Check for special shortcuts
2172 if (intent.getComponent() != null) {
2173 final String shortcutClass = intent.getComponent().getClassName();
2174
2175 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002176 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002177 return;
2178 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2179 MemoryDumpActivity.startDump(this);
2180 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002181 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2182 toggleShowWeightWatcher();
2183 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002184 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002185 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002186
2187 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002188 int[] pos = new int[2];
2189 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002190 intent.setSourceBounds(new Rect(pos[0], pos[1],
2191 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002192
2193 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002194
Daniel Sandlerff02d492013-08-05 02:12:05 -04002195 mStats.recordLaunch(intent, shortcut);
2196
Michael Jurkaddd62e92011-02-16 17:49:14 -08002197 if (success && v instanceof BubbleTextView) {
2198 mWaitingForResume = (BubbleTextView) v;
2199 mWaitingForResume.setStayPressed(true);
2200 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002202 if (v instanceof FolderIcon) {
2203 FolderIcon fi = (FolderIcon) v;
2204 handleFolderClick(fi);
2205 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002206 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002207 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002208 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002209 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002210 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 }
2213 }
2214
Michael Jurka0e260592010-06-30 17:07:39 -07002215 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002216 return false;
2217 }
2218
Michael Jurkaaf442092010-06-10 17:01:57 -07002219 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002220 * Event handler for the search button
2221 *
2222 * @param v The view that was clicked.
2223 */
2224 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002225 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2226
Amith Yamasania135ba82011-08-09 17:42:01 -07002227 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002228 }
2229
2230 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002231 * Event handler for the voice button
2232 *
2233 * @param v The view that was clicked.
2234 */
2235 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002236 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002237
Bjorn Bringertc459e522013-06-07 19:36:01 +01002238 startVoice();
2239 }
2240
2241 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002242 try {
2243 final SearchManager searchManager =
2244 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2245 ComponentName activityName = searchManager.getGlobalSearchActivity();
2246 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002247 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002248 if (activityName != null) {
2249 intent.setPackage(activityName.getPackageName());
2250 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002251 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002252 } catch (ActivityNotFoundException e) {
2253 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002254 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002255 startActivitySafely(null, intent, "onClickVoiceButton");
2256 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002257 }
2258
2259 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002260 * Event handler for the "grid" button that appears on the home screen, which
2261 * enters all apps mode.
2262 *
2263 * @param v The view that was clicked.
2264 */
2265 public void onClickAllAppsButton(View v) {
Winson Chung82963d52013-10-09 11:20:57 -07002266 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, true);
Michael Jurka5130e402011-10-13 04:55:35 -07002267 }
2268
2269 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002270 // Provide the same haptic feedback that the system offers for virtual keys.
2271 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002272 }
2273
Adam Cohen61f560d2013-09-30 15:58:20 -07002274 public void performHapticFeedbackOnTouchDown(View v) {
2275 // Provide the same haptic feedback that the system offers for virtual keys.
2276 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2277 }
2278
2279 public View.OnTouchListener getHapticFeedbackTouchListener() {
2280 if (mHapticFeedbackTouchListener == null) {
2281 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2282 @Override
2283 public boolean onTouch(View v, MotionEvent event) {
2284 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2285 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2286 }
2287 return false;
2288 }
2289 };
2290 }
2291 return mHapticFeedbackTouchListener;
2292 }
2293
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002294 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002295 if (!DISABLE_MARKET_BUTTON) {
2296 if (mAppMarketIntent != null) {
2297 startActivitySafely(v, mAppMarketIntent, "app market");
2298 } else {
2299 Log.e(TAG, "Invalid app market intent.");
2300 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002301 }
2302 }
2303
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002304 /**
2305 * Called when the user stops interacting with the launcher.
2306 * This implies that the user is now on the homescreen and is not doing housekeeping.
2307 */
2308 protected void onInteractionEnd() {}
2309
2310 /**
2311 * Called when the user starts interacting with the launcher.
2312 * The possible interactions are:
2313 * - open all apps
2314 * - reorder an app shortcut, or a widget
2315 * - open the overview mode.
2316 * This is a good time to stop doing things that only make sense
2317 * when the user is on the homescreen and not doing housekeeping.
2318 */
2319 protected void onInteractionBegin() {}
2320
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002321 void startApplicationDetailsActivity(ComponentName componentName) {
2322 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002323 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2324 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002325 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002326 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002327 }
2328
Michael Jurka1e2f4652013-07-08 18:03:46 -07002329 // returns true if the activity was started
2330 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002331 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002332 // System applications cannot be installed. For now, show a toast explaining that.
2333 // We may give them the option of disabling apps this way.
2334 int messageId = R.string.uninstall_system_app_text;
2335 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002336 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002337 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002338 String packageName = componentName.getPackageName();
2339 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002340 Intent intent = new Intent(
2341 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002342 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2343 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002344 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002345 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002346 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002347 }
2348
Michael Jurka86a720e2012-05-09 11:23:23 -07002349 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002351
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002352 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002353 // Only launch using the new animation if the shortcut has not opted out (this is a
2354 // private contract between launcher and may be ignored in the future).
2355 boolean useLaunchAnimation = (v != null) &&
2356 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2357 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002358 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2359 v.getMeasuredWidth(), v.getMeasuredHeight());
2360
2361 startActivity(intent, opts.toBundle());
2362 } else {
2363 startActivity(intent);
2364 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002365 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 } catch (SecurityException e) {
2367 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002368 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002369 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002370 "or use the exported attribute for this activity. "
2371 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002372 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002373 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002375
Michael Jurka86a720e2012-05-09 11:23:23 -07002376 boolean startActivitySafely(View v, Intent intent, Object tag) {
2377 boolean success = false;
2378 try {
2379 success = startActivity(v, intent, tag);
2380 } catch (ActivityNotFoundException e) {
2381 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2382 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2383 }
2384 return success;
2385 }
2386
Adam Cohena9cf38f2011-05-02 15:36:58 -07002387 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002388 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002389 Folder openFolder = mWorkspace.getFolderForTag(info);
2390
2391 // If the folder info reports that the associated folder is open, then verify that
2392 // it is actually opened. There have been a few instances where this gets out of sync.
2393 if (info.opened && openFolder == null) {
2394 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002395 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002396 info.opened = false;
2397 }
2398
Adam Cohenfb91f302012-06-11 15:45:18 -07002399 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 // Close any open folder
2401 closeFolder();
2402 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002403 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002404 } else {
2405 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 int folderScreen;
2407 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002408 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 // .. and close it
2410 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002411 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 // Close any folder open on the current screen
2413 closeFolder();
2414 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002415 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 }
2417 }
2418 }
2419 }
2420
Adam Cohen268c4752012-06-06 17:47:33 -07002421 /**
2422 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2423 * in the DragLayer in the exact absolute location of the original FolderIcon.
2424 */
2425 private void copyFolderIconToImage(FolderIcon fi) {
2426 final int width = fi.getMeasuredWidth();
2427 final int height = fi.getMeasuredHeight();
2428
2429 // Lazy load ImageView, Bitmap and Canvas
2430 if (mFolderIconImageView == null) {
2431 mFolderIconImageView = new ImageView(this);
2432 }
2433 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2434 mFolderIconBitmap.getHeight() != height) {
2435 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2436 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2437 }
2438
2439 DragLayer.LayoutParams lp;
2440 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2441 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2442 } else {
2443 lp = new DragLayer.LayoutParams(width, height);
2444 }
2445
Adam Cohen307fe232012-08-16 17:55:58 -07002446 // The layout from which the folder is being opened may be scaled, adjust the starting
2447 // view size by this scale factor.
2448 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002449 lp.customPosition = true;
2450 lp.x = mRectForFolderAnimation.left;
2451 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002452 lp.width = (int) (scale * width);
2453 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002454
2455 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2456 fi.draw(mFolderIconCanvas);
2457 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002458 if (fi.getFolder() != null) {
2459 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2460 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002461 }
Adam Cohen268c4752012-06-06 17:47:33 -07002462 // Just in case this image view is still in the drag layer from a previous animation,
2463 // we remove it and re-add it.
2464 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2465 mDragLayer.removeView(mFolderIconImageView);
2466 }
2467 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002468 if (fi.getFolder() != null) {
2469 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002470 }
Adam Cohen268c4752012-06-06 17:47:33 -07002471 }
2472
Adam Cohen2801caf2011-05-13 20:57:39 -07002473 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002474 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002475 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2476 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2477 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2478
Adam Cohenc51934b2011-07-26 21:07:43 -07002479 FolderInfo info = (FolderInfo) fi.getTag();
2480 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2481 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002482 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2483 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002484 }
2485
Adam Cohen268c4752012-06-06 17:47:33 -07002486 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2487 copyFolderIconToImage(fi);
2488 fi.setVisibility(View.INVISIBLE);
2489
Michael Jurka2ecf9952012-06-18 12:52:28 -07002490 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002491 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002492 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2493 oa.start();
2494 }
2495
Adam Cohen268c4752012-06-06 17:47:33 -07002496 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002497 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002498 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2499 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2500 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2501
Adam Cohen268c4752012-06-06 17:47:33 -07002502 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002503
Adam Cohen268c4752012-06-06 17:47:33 -07002504 // We remove and re-draw the FolderIcon in-case it has changed
2505 mDragLayer.removeView(mFolderIconImageView);
2506 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002507 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002508 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002509 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002510 oa.addListener(new AnimatorListenerAdapter() {
2511 @Override
2512 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002513 if (cl != null) {
2514 cl.clearFolderLeaveBehind();
2515 // Remove the ImageView copy of the FolderIcon and make the original visible.
2516 mDragLayer.removeView(mFolderIconImageView);
2517 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002518 }
2519 }
2520 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002521 oa.start();
2522 }
2523
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002524 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002525 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002526 * is animated relative to the specified View. If the View is null, no animation
2527 * is played.
2528 *
2529 * @param folderInfo The FolderInfo describing the folder to open.
2530 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002531 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002532 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002533 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002534
Adam Cohena9cf38f2011-05-02 15:36:58 -07002535 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002536
Adam Cohen4554ee12011-08-03 16:13:21 -07002537 // Just verify that the folder hasn't already been added to the DragLayer.
2538 // There was a one-off crash where the folder had a parent already.
2539 if (folder.getParent() == null) {
2540 mDragLayer.addView(folder);
2541 mDragController.addDropTarget((DropTarget) folder);
2542 } else {
2543 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2544 folder.getParent() + ").");
2545 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002546 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002547 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002548
2549 // Notify the accessibility manager that this folder "window" has appeared and occluded
2550 // the workspace items
2551 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2552 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002553 }
2554
2555 public void closeFolder() {
2556 Folder folder = mWorkspace.getOpenFolder();
2557 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002558 if (folder.isEditingName()) {
2559 folder.dismissEditingName();
2560 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002561 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002562
2563 // Dismiss the folder cling
2564 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002565 }
2566 }
2567
2568 void closeFolder(Folder folder) {
2569 folder.getInfo().opened = false;
2570
2571 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2572 if (parent != null) {
2573 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2574 shrinkAndFadeInFolderIcon(fi);
2575 }
2576 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002577
2578 // Notify the accessibility manager that this folder "window" has disappeard and no
2579 // longer occludeds the workspace items
2580 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002581 }
2582
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002583 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002584 if (!isDraggingEnabled()) return false;
2585 if (isWorkspaceLocked()) return false;
2586 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587
Adam Cohen1697b792013-09-17 19:08:21 -07002588 if (v instanceof Workspace) {
2589 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002590 if (mWorkspace.enterOverviewMode()) {
2591 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2592 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2593 return true;
2594 } else {
2595 return false;
2596 }
Adam Cohen1697b792013-09-17 19:08:21 -07002597 }
Adam Cohen1697b792013-09-17 19:08:21 -07002598 }
2599
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002600 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002601 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002602 }
2603
Michael Jurka0280c3b2010-09-17 15:00:07 -07002604 resetAddInfo();
2605 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002606 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002607 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002608 return true;
2609 }
2610
Winson Chung3d503fb2011-07-13 17:25:49 -07002611 // The hotseat touch handling does not go through Workspace, and we always allow long press
2612 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002613 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002614 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2615 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002616 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002617 // User long pressed on empty space
2618 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2619 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2620 // Disabling reordering until we sort out some issues.
2621 if (mWorkspace.isInOverviewMode()) {
2622 mWorkspace.startReordering(v);
2623 } else {
2624 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002625 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002626 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002627 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002628 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002629 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002630 }
2631 }
2632 }
2633 return true;
2634 }
2635
Winson Chung3d503fb2011-07-13 17:25:49 -07002636 boolean isHotseatLayout(View layout) {
2637 return mHotseat != null && layout != null &&
2638 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2639 }
2640 Hotseat getHotseat() {
2641 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002642 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002643 View getOverviewPanel() {
2644 return mOverviewPanel;
2645 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002646 SearchDropTargetBar getSearchBar() {
2647 return mSearchDropTargetBar;
2648 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002649
Winson Chung3d503fb2011-07-13 17:25:49 -07002650 /**
2651 * Returns the CellLayout of the specified container at the specified screen.
2652 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002653 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002654 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2655 if (mHotseat != null) {
2656 return mHotseat.getLayout();
2657 } else {
2658 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002659 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002660 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002661 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002662 }
2663 }
2664
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002665 Workspace getWorkspace() {
2666 return mWorkspace;
2667 }
2668
Daniel Sandler843e8602010-06-07 14:59:01 -04002669 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002670 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002671 }
2672
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002673 /**
2674 * Helper method for the cameraZoomIn/cameraZoomOut animations
2675 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002676 * @param scaleFactor The scale factor used for the zoom
2677 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002678 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002679 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002680 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002681 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002682
Craig Mautner360310b2012-10-26 15:13:08 -07002683 private void setWorkspaceBackground(boolean workspace) {
2684 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002685 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002686 }
2687
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002688 void updateWallpaperVisibility(boolean visible) {
2689 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2690 int curflags = getWindow().getAttributes().flags
2691 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2692 if (wpflags != curflags) {
2693 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2694 }
Craig Mautner360310b2012-10-26 15:13:08 -07002695 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002696 }
2697
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002698 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2699 if (v instanceof LauncherTransitionable) {
2700 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2701 }
2702 }
2703
Michael Jurkabed61d22012-02-14 22:51:29 -08002704 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2705 if (v instanceof LauncherTransitionable) {
2706 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2707 }
Winson Chung70442722012-02-10 15:43:22 -08002708
2709 // Update the workspace transition step as well
2710 dispatchOnLauncherTransitionStep(v, 0f);
2711 }
2712
2713 private void dispatchOnLauncherTransitionStep(View v, float t) {
2714 if (v instanceof LauncherTransitionable) {
2715 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2716 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002717 }
2718
2719 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2720 if (v instanceof LauncherTransitionable) {
2721 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2722 }
Winson Chung70442722012-02-10 15:43:22 -08002723
2724 // Update the workspace transition step as well
2725 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002726 }
2727
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002728 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002729 * Things to test when changing the following seven functions.
2730 * - Home from workspace
2731 * - from center screen
2732 * - from other screens
2733 * - Home from all apps
2734 * - from center screen
2735 * - from other screens
2736 * - Back from all apps
2737 * - from center screen
2738 * - from other screens
2739 * - Launch app from workspace and quit
2740 * - with back
2741 * - with home
2742 * - Launch app from all apps and quit
2743 * - with back
2744 * - with home
2745 * - Go to a screen that's not the default, then all
2746 * apps, and launch and app, and go back
2747 * - with back
2748 * -with home
2749 * - On workspace, long press power and go back
2750 * - with back
2751 * - with home
2752 * - On all apps, long press power and go back
2753 * - with back
2754 * - with home
2755 * - On workspace, power off
2756 * - On all apps, power off
2757 * - Launch an app and turn off the screen while in that app
2758 * - Go back with home key
2759 * - Go back with back key TODO: make this not go to workspace
2760 * - From all apps
2761 * - From workspace
2762 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2763 * - From all apps
2764 * - From the center workspace
2765 * - From another workspace
2766 */
2767
2768 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002769 * Zoom the camera out from the workspace to reveal 'toView'.
2770 * Assumes that the view to show is anchored at either the very top or very bottom
2771 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002772 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002773 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002774 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2775 showAppsCustomizeHelper(animated, springLoaded, contentType);
2776 }
2777 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2778 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002779 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002780 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002781 mStateAnimation.cancel();
2782 mStateAnimation = null;
2783 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002784 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002785
Winson Chungf0ea4d32011-06-06 14:27:16 -07002786 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2787 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2788 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002789 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002790 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002791 final int startDelay =
2792 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002793
Michael Jurkab3e22d92011-10-31 15:58:33 -07002794 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002795
Michael Jurkad74c9842011-07-10 12:44:21 -07002796 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002797 Animator workspaceAnim =
2798 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002799 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2800 // Set the content type for the all apps space
2801 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2802 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002803
2804 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002805 toView.setScaleX(scale);
2806 toView.setScaleY(scale);
2807 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2808 scaleAnim.
2809 scaleX(1f).scaleY(1f).
2810 setDuration(duration).
2811 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002812
Winson Chungf0ea4d32011-06-06 14:27:16 -07002813 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002814 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002815 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002816 .ofFloat(toView, "alpha", 0f, 1f)
2817 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002818 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002819 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2820 @Override
2821 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002822 if (animation == null) {
2823 throw new RuntimeException("animation is null");
2824 }
Winson Chung70442722012-02-10 15:43:22 -08002825 float t = (Float) animation.getAnimatedValue();
2826 dispatchOnLauncherTransitionStep(fromView, t);
2827 dispatchOnLauncherTransitionStep(toView, t);
2828 }
2829 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002830
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002831 // toView should appear right at the end of the workspace shrink
2832 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002833 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002834 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002835 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002836
2837 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002838 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002839 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002840 // Prepare the position
2841 toView.setTranslationX(0.0f);
2842 toView.setTranslationY(0.0f);
2843 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002844 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002845 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002846 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002847 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002848 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2849 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002850
Winson Chungc7d2b602012-05-16 17:02:20 -07002851 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002852 if (mSearchDropTargetBar != null) {
2853 mSearchDropTargetBar.hideSearchBar(false);
2854 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002855 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002856 });
2857
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002858 if (workspaceAnim != null) {
2859 mStateAnimation.play(workspaceAnim);
2860 }
Michael Jurka1899a362011-11-03 13:50:45 -07002861
2862 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002863
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002864 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2865 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002866
2867 // If any of the objects being animated haven't been measured/laid out
2868 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002869 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002870 (mWorkspace.getMeasuredWidth() == 0) ||
2871 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002872 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002873 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002874
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002875 final AnimatorSet stateAnimation = mStateAnimation;
2876 final Runnable startAnimRunnable = new Runnable() {
2877 public void run() {
2878 // Check that mStateAnimation hasn't changed while
2879 // we waited for a layout/draw pass
2880 if (mStateAnimation != stateAnimation)
2881 return;
2882 setPivotsForZoom(toView, scale);
2883 dispatchOnLauncherTransitionStart(fromView, animated, false);
2884 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002885 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002886 }
2887 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002888 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002889 final ViewTreeObserver observer = toView.getViewTreeObserver();
2890 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2891 public void onGlobalLayout() {
2892 startAnimRunnable.run();
2893 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2894 }
2895 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002896 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002897 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002898 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002899 } else {
2900 toView.setTranslationX(0.0f);
2901 toView.setTranslationY(0.0f);
2902 toView.setScaleX(1.0f);
2903 toView.setScaleY(1.0f);
2904 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002905 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002906
Daniel Sandlere4f98912013-06-25 15:13:26 -04002907 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002908 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002909 if (mSearchDropTargetBar != null) {
2910 mSearchDropTargetBar.hideSearchBar(false);
2911 }
Michael Jurkaabded662011-03-04 12:06:57 -08002912 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002913 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002914 dispatchOnLauncherTransitionStart(fromView, animated, false);
2915 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002916 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002917 dispatchOnLauncherTransitionStart(toView, animated, false);
2918 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002919 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002920 }
2921
2922 /**
2923 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002924 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002925 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002926 */
Adam Cohened307df2013-10-02 09:37:31 -07002927 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08002928 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002929
Michael Jurkab3e22d92011-10-31 15:58:33 -07002930 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002931 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002932 mStateAnimation.cancel();
2933 mStateAnimation = null;
2934 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002935 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002936
Winson Chungf0ea4d32011-06-06 14:27:16 -07002937 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002938 final int fadeOutDuration =
2939 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002940 final float scaleFactor = (float)
2941 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2942 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002943 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002944 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07002945 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002946 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2947 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07002948 toState, animated, stagger, -1);
2949 } else if (toState == Workspace.State.SPRING_LOADED ||
2950 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002951 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07002952 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002953 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002954
Michael Jurkab3e22d92011-10-31 15:58:33 -07002955 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07002956 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002957 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002958 final LauncherViewPropertyAnimator scaleAnim =
2959 new LauncherViewPropertyAnimator(fromView);
2960 scaleAnim.
2961 scaleX(scaleFactor).scaleY(scaleFactor).
2962 setDuration(duration).
2963 setInterpolator(new Workspace.ZoomInInterpolator());
2964
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002965 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002966 .ofFloat(fromView, "alpha", 1f, 0f)
2967 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002968 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002969 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2970 @Override
2971 public void onAnimationUpdate(ValueAnimator animation) {
2972 float t = 1f - (Float) animation.getAnimatedValue();
2973 dispatchOnLauncherTransitionStep(fromView, t);
2974 dispatchOnLauncherTransitionStep(toView, t);
2975 }
2976 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002977
Michael Jurka2ecf9952012-06-18 12:52:28 -07002978 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002979
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002980 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2981 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002982 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002983
2984 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002985 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002986 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002987 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002988 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2989 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002990 if (onCompleteRunnable != null) {
2991 onCompleteRunnable.run();
2992 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002993 mAppsCustomizeContent.updateCurrentPageScroll();
2994 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002995 }
2996 });
2997
Winson Chungf0ea4d32011-06-06 14:27:16 -07002998 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002999 if (workspaceAnim != null) {
3000 mStateAnimation.play(workspaceAnim);
3001 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003002 dispatchOnLauncherTransitionStart(fromView, animated, true);
3003 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003004 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003005 } else {
3006 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003007 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003008 dispatchOnLauncherTransitionStart(fromView, animated, true);
3009 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003010 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003011 dispatchOnLauncherTransitionStart(toView, animated, true);
3012 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003013 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003014 }
3015
Michael Jurkae326f182011-11-21 14:05:46 -08003016 @Override
3017 public void onTrimMemory(int level) {
3018 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003019 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003020 mAppsCustomizeTabHost.onTrimMemory();
3021 }
3022 }
3023
Adam Cohened307df2013-10-02 09:37:31 -07003024 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003025 showWorkspace(animated, null);
3026 }
3027
Adam Cohened307df2013-10-02 09:37:31 -07003028 protected void showWorkspace() {
3029 showWorkspace(true);
3030 }
3031
Adam Cohened66b2b2012-01-23 17:28:51 -08003032 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003033 if (mWorkspace.isInOverviewMode()) {
3034 mWorkspace.exitOverviewMode(animated);
3035 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003036 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003037 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003038 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003039 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003040
Winson Chungc7d2b602012-05-16 17:02:20 -07003041 // Show the search bar (only animate if we were showing the drop target bar in spring
3042 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003043 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003044 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003045 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003046
Michael Jurkab3e22d92011-10-31 15:58:33 -07003047 // Set focus to the AppsCustomize button
3048 if (mAllAppsButton != null) {
3049 mAllAppsButton.requestFocus();
3050 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003051 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003052
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003053 // Change the state *after* we've called all the transition code
3054 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003055
Winson Chung5fb63472011-02-02 17:03:37 -08003056 // Resume the auto-advance of widgets
3057 mUserPresent = true;
3058 updateRunning();
3059
alanv1d4fde62012-10-17 13:15:47 -07003060 // Send an accessibility event to announce the context change
3061 getWindow().getDecorView()
3062 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003063
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003064 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003065 }
3066
Adam Cohened307df2013-10-02 09:37:31 -07003067 void showOverviewMode(boolean animated) {
3068 mWorkspace.setVisibility(View.VISIBLE);
3069 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3070 mState = State.WORKSPACE;
3071 onWorkspaceShown(animated);
3072 }
3073
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003074 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003075 }
3076
Winson Chung82963d52013-10-09 11:20:57 -07003077 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3078 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003079 if (mState != State.WORKSPACE) return;
3080
Winson Chung82963d52013-10-09 11:20:57 -07003081 if (resetPageToZero) {
3082 mAppsCustomizeTabHost.reset();
3083 }
Winson Chungc58497e2013-09-03 17:48:37 -07003084 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003085 mAppsCustomizeTabHost.requestFocus();
3086
Michael Jurkab3e22d92011-10-31 15:58:33 -07003087 // Change the state *after* we've called all the transition code
3088 mState = State.APPS_CUSTOMIZE;
3089
3090 // Pause the auto-advance of widgets until we are out of AllApps
3091 mUserPresent = false;
3092 updateRunning();
3093 closeFolder();
3094
3095 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003096 getWindow().getDecorView()
3097 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003098 }
3099
Adam Cohen7777d962011-08-18 18:58:38 -07003100 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003101 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003102 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003103 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003104 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003105 }
Adam Cohen7777d962011-08-18 18:58:38 -07003106
Adam Cohened66b2b2012-01-23 17:28:51 -08003107 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3108 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003109 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3110
Winson Chunge7a03942011-08-05 15:05:12 -07003111 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003112 @Override
3113 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003114 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003115 // Before we show workspace, hide all apps again because
3116 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3117 // clean up our state transition functions
3118 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003119 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003120 } else {
3121 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003122 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003123 }
3124 }, (extendedDelay ?
3125 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3126 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3127 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003128
Michael Jurkad3ef3062010-11-23 16:23:58 -08003129 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003130 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003131 final boolean animated = true;
3132 final boolean springLoaded = true;
3133 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003134 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003135 }
3136 // Otherwise, we are not in spring loaded mode, so don't do anything.
3137 }
3138
Michael Jurkab3e22d92011-10-31 15:58:33 -07003139 void lockAllApps() {
3140 // TODO
3141 }
3142
3143 void unlockAllApps() {
3144 // TODO
3145 }
3146
Winson Chungf0ea4d32011-06-06 14:27:16 -07003147 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003148 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003149 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003150 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003151 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003152 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003153 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003154 int duration = 0;
3155 if (mSearchDropTargetBar != null) {
3156 duration = mSearchDropTargetBar.getTransitionInDuration();
3157 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003158 mHotseat.animate().alpha(1f).setDuration(duration);
3159 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003160 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003161 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003162 }
3163 }
3164 }
3165
3166 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003167 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003168 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003169 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003170 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003171 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003172 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003173 int duration = 0;
3174 if (mSearchDropTargetBar != null) {
3175 duration = mSearchDropTargetBar.getTransitionOutDuration();
3176 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003177 mHotseat.animate().alpha(0f).setDuration(duration);
3178 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003179 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003180 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003181 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003182 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003183 }
3184
Patrick Dubroy5f445422011-02-18 14:35:21 -08003185 /**
3186 * Add an item from all apps or customize onto the given workspace screen.
3187 * If layout is null, add to the current screen.
3188 */
3189 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003190 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003191 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003192 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003193 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003194
Winson Chungdff8ebb2011-09-08 17:25:31 -07003195 /** Maps the current orientation to an index for referencing orientation correct global icons */
3196 private int getCurrentOrientationIndexForGlobalIcons() {
3197 // default - 0, landscape - 1
3198 switch (getResources().getConfiguration().orientation) {
3199 case Configuration.ORIENTATION_LANDSCAPE:
3200 return 1;
3201 default:
3202 return 0;
3203 }
3204 }
3205
Michael Jurkaae65ee32012-04-30 11:45:54 -07003206 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003207 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003208 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003209 // Look for the toolbar icon specified in the activity meta-data
3210 Bundle metaData = packageManager.getActivityInfo(
3211 activityName, PackageManager.GET_META_DATA).metaData;
3212 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003213 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003214 if (iconResId != 0) {
3215 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003216 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003217 }
3218 }
3219 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003220 // This can happen if the activity defines an invalid drawable
3221 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3222 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003223 } catch (Resources.NotFoundException nfe) {
3224 // This can happen if the activity defines an invalid drawable
3225 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3226 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003227 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003228 return null;
3229 }
3230
3231 // if successful in getting icon, return it; otherwise, set button to use default drawable
3232 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003233 int buttonId, ComponentName activityName, int fallbackDrawableId,
3234 String toolbarResourceName) {
3235 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003236 Resources r = getResources();
3237 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3238 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003239
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003240 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003241 // If we were unable to find the icon via the meta-data, use a generic one
3242 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003243 toolbarIcon = r.getDrawable(fallbackDrawableId);
3244 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003245 if (button != null) {
3246 button.setCompoundDrawables(toolbarIcon, null, null, null);
3247 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003248 return null;
3249 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003250 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003251 if (button != null) {
3252 button.setCompoundDrawables(toolbarIcon, null, null, null);
3253 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003254 return toolbarIcon.getConstantState();
3255 }
3256 }
3257
3258 // if successful in getting icon, return it; otherwise, set button to use default drawable
3259 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003260 int buttonId, ComponentName activityName, int fallbackDrawableId,
3261 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003262 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003263 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003264
Michael Jurka19e0fc52011-07-22 18:00:21 -07003265 if (button != null) {
3266 // If we were unable to find the icon via the meta-data, use a
3267 // generic one
3268 if (toolbarIcon == null) {
3269 button.setImageResource(fallbackDrawableId);
3270 } else {
3271 button.setImageDrawable(toolbarIcon);
3272 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003273 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003274
3275 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3276
Michael Jurka0423dcf2010-10-05 14:56:18 -07003277 }
3278
Winson Chung1b884092012-06-08 17:13:02 -07003279 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003280 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003281 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003282 }
3283
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003284 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003285 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003286 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003287 }
3288
Winson Chungbb185bd2011-11-21 12:31:42 -08003289 private void invalidatePressedFocusedStates(View container, View button) {
3290 if (container instanceof HolographicLinearLayout) {
3291 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3292 layout.invalidatePressedFocusedStates();
3293 } else if (button instanceof HolographicImageView) {
3294 HolographicImageView view = (HolographicImageView) button;
3295 view.invalidatePressedFocusedStates();
3296 }
3297 }
3298
Cristina Stancu476493b2013-08-07 17:20:14 +01003299 public View getQsbBar() {
3300 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003301 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3302 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003303 }
3304 return mQsbBar;
3305 }
3306
3307 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003308 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003309 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003310 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003311 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003312
Winson Chung1cad91e2011-05-25 17:41:01 -07003313 final SearchManager searchManager =
3314 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3315 ComponentName activityName = searchManager.getGlobalSearchActivity();
3316 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003317 int coi = getCurrentOrientationIndexForGlobalIcons();
3318 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003319 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3320 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3321 if (sGlobalSearchIcon[coi] == null) {
3322 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3323 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3324 TOOLBAR_ICON_METADATA_NAME);
3325 }
3326
Winson Chungc51db6a2011-10-05 11:44:49 -07003327 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3328 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003329 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003330 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003331 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003332 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003333 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3334 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003335 if (searchButton != null) searchButton.setVisibility(View.GONE);
3336 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003337 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003338 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003339 }
3340 }
3341
Cristina Stancu476493b2013-08-07 17:20:14 +01003342 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003343 final View searchButtonContainer = findViewById(R.id.search_button_container);
3344 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003345 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003346 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003347 }
3348
Cristina Stancu476493b2013-08-07 17:20:14 +01003349 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003350 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003351 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003352
Winson Chungc51db6a2011-10-05 11:44:49 -07003353 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003354 final SearchManager searchManager =
3355 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3356 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3357
3358 ComponentName activityName = null;
3359 if (globalSearchActivity != null) {
3360 // Check if the global search activity handles voice search
3361 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3362 intent.setPackage(globalSearchActivity.getPackageName());
3363 activityName = intent.resolveActivity(getPackageManager());
3364 }
3365
3366 if (activityName == null) {
3367 // Fallback: check if an activity other than the global search activity
3368 // resolves this
3369 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3370 activityName = intent.resolveActivity(getPackageManager());
3371 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003372 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003373 int coi = getCurrentOrientationIndexForGlobalIcons();
3374 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003375 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3376 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3377 if (sVoiceSearchIcon[coi] == null) {
3378 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3379 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3380 TOOLBAR_ICON_METADATA_NAME);
3381 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003382 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003383 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003384 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003385 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003386 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003387 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003388 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003389 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003390 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003391 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003392 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003393 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003394
Cristina Stancu476493b2013-08-07 17:20:14 +01003395 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003396 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3397 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003398 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003399 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003400 }
3401
Winson Chung5841efa2013-09-30 18:06:44 -07003402 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003403 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3404 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003405 boolean visible = !forceDisableVoiceButtonProxy &&
3406 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003407 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003408 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003409 }
3410 }
Winson Chung5841efa2013-09-30 18:06:44 -07003411
3412 /**
3413 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3414 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3415 */
3416 public void disableVoiceButtonProxy(boolean disabled) {
3417 updateVoiceButtonProxyVisible(disabled);
3418 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003419 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003420 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003421 */
3422 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003423 if (!DISABLE_MARKET_BUTTON) {
3424 final View marketButton = findViewById(R.id.market_button);
3425 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3426 // Find the app market activity by resolving an intent.
3427 // (If multiple app markets are installed, it will return the ResolverActivity.)
3428 ComponentName activityName = intent.resolveActivity(getPackageManager());
3429 if (activityName != null) {
3430 int coi = getCurrentOrientationIndexForGlobalIcons();
3431 mAppMarketIntent = intent;
3432 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3433 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3434 TOOLBAR_ICON_METADATA_NAME);
3435 marketButton.setVisibility(View.VISIBLE);
3436 } else {
3437 // We should hide and disable the view so that we don't try and restore the visibility
3438 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3439 marketButton.setVisibility(View.GONE);
3440 marketButton.setEnabled(false);
3441 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003442 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003443 }
3444
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003445 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003446 if (!DISABLE_MARKET_BUTTON) {
3447 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3448 Resources r = getResources();
3449 Drawable marketIconDrawable = d.newDrawable(r);
3450 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3451 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3452 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003453
Winson Chungd64a6662013-09-30 11:06:59 -07003454 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3455 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003456 }
3457
Michael Jurkad7c28052012-04-27 15:43:36 -07003458 @Override
3459 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003460 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003461 final List<CharSequence> text = event.getText();
3462 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003463 // Populate event with a fake title based on the current state.
3464 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003465 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003466 } else {
3467 text.add(getString(R.string.all_apps_home_button_label));
3468 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003469 return result;
3470 }
3471
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003472 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003473 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003474 */
3475 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3476 @Override
3477 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003478 closeSystemDialogs();
3479 }
3480 }
3481
3482 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003483 * Receives notifications whenever the appwidgets are reset.
3484 */
3485 private class AppWidgetResetObserver extends ContentObserver {
3486 public AppWidgetResetObserver() {
3487 super(new Handler());
3488 }
3489
3490 @Override
3491 public void onChange(boolean selfChange) {
3492 onAppWidgetReset();
3493 }
3494 }
3495
3496 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003497 * If the activity is currently paused, signal that we need to run the passed Runnable
3498 * in onResume.
3499 *
3500 * This needs to be called from incoming places where resources might have been loaded
3501 * while we are paused. That is becaues the Configuration might be wrong
3502 * when we're not running, and if it comes back to what it was when we
3503 * were paused, we are not restarted.
3504 *
3505 * Implementation of the method from LauncherModel.Callbacks.
3506 *
3507 * @return true if we are currently paused. The caller might be able to
3508 * skip some work in that case since we will come back again.
3509 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003510 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003511 if (mPaused) {
3512 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003513 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003514 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003515 }
3516 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003517 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003518 return true;
3519 } else {
3520 return false;
3521 }
3522 }
3523
Michael Jurkac402cd92013-05-20 15:49:32 +02003524 private boolean waitUntilResume(Runnable run) {
3525 return waitUntilResume(run, false);
3526 }
3527
Michael Jurka1e2f4652013-07-08 18:03:46 -07003528 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003529 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003530 }
3531
Michael Jurka7607c2f2013-04-03 14:33:19 -07003532 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003533 * If the activity is currently paused, signal that we need to re-run the loader
3534 * in onResume.
3535 *
3536 * This needs to be called from incoming places where resources might have been loaded
3537 * while we are paused. That is becaues the Configuration might be wrong
3538 * when we're not running, and if it comes back to what it was when we
3539 * were paused, we are not restarted.
3540 *
3541 * Implementation of the method from LauncherModel.Callbacks.
3542 *
3543 * @return true if we are currently paused. The caller might be able to
3544 * skip some work in that case since we will come back again.
3545 */
3546 public boolean setLoadOnResume() {
3547 if (mPaused) {
3548 Log.i(TAG, "setLoadOnResume");
3549 mOnResumeNeedsLoad = true;
3550 return true;
3551 } else {
3552 return false;
3553 }
3554 }
3555
3556 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003557 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003558 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003559 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003560 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003561 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003562 } else {
3563 return SCREEN_COUNT / 2;
3564 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003565 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003566
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 /**
3568 * Refreshes the shortcuts shown on the workspace.
3569 *
3570 * Implementation of the method from LauncherModel.Callbacks.
3571 */
3572 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003573 // If we're starting binding all over again, clear any bind calls we'd postponed in
3574 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3575 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003576 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003577
Winson Chungd64d1762013-08-20 14:37:16 -07003578 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003579 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003580 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003581
Michael Jurka05bf6442011-11-30 20:28:53 -08003582 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003583 if (mHotseat != null) {
3584 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003585 }
3586 }
3587
Adam Cohendcd297f2013-06-18 13:13:40 -07003588 @Override
3589 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003590 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003591
Adam Cohen5084cba2013-09-03 12:01:16 -07003592 // If there are no screens, we need to have an empty screen
3593 if (orderedScreenIds.size() == 0) {
3594 mWorkspace.addExtraEmptyScreen();
3595 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003596
3597 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003598 // setCurrentPage() so ensure that all pages are added before calling this).
3599 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003600 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3601 mWorkspace.createCustomContentPage();
3602 }
Winson Chung64359a52013-07-08 17:17:08 -07003603 }
3604
3605 @Override
3606 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003607 int count = orderedScreenIds.size();
3608 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003609 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003610 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003611 }
3612
Adam Cohen39a06042013-07-19 14:30:12 -07003613 private boolean shouldShowWeightWatcher() {
3614 String spKey = LauncherAppState.getSharedPreferencesKey();
3615 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003616 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003617
3618 return show;
3619 }
3620
3621 private void toggleShowWeightWatcher() {
3622 String spKey = LauncherAppState.getSharedPreferencesKey();
3623 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3624 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3625
3626 show = !show;
3627
3628 SharedPreferences.Editor editor = sp.edit();
3629 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3630 editor.commit();
3631
3632 if (mWeightWatcher != null) {
3633 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3634 }
3635 }
3636
Winson Chungd64d1762013-08-20 14:37:16 -07003637 public void bindAppsAdded(final ArrayList<Long> newScreens,
3638 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003639 final ArrayList<ItemInfo> addAnimated,
3640 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003641 Runnable r = new Runnable() {
3642 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003643 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003644 }
3645 };
3646 if (waitUntilResume(r)) {
3647 return;
3648 }
3649
Winson Chungd64d1762013-08-20 14:37:16 -07003650 // Add the new screens
3651 bindAddScreens(newScreens);
3652
3653 // We add the items without animation on non-visible pages, and with
3654 // animations on the new page (which we will try and snap to).
3655 if (!addNotAnimated.isEmpty()) {
3656 bindItems(addNotAnimated, 0,
3657 addNotAnimated.size(), false);
3658 }
3659 if (!addAnimated.isEmpty()) {
3660 bindItems(addAnimated, 0,
3661 addAnimated.size(), true);
3662 }
Winson Chungc58497e2013-09-03 17:48:37 -07003663
Winson Chung87412982013-10-03 18:34:14 -07003664 // Remove the extra empty screen
3665 mWorkspace.removeExtraEmptyScreen();
3666
Winson Chungc58497e2013-09-03 17:48:37 -07003667 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3668 addedApps != null && mAppsCustomizeContent != null) {
3669 mAppsCustomizeContent.addApps(addedApps);
3670 }
Winson Chungd64d1762013-08-20 14:37:16 -07003671 }
3672
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003673 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003674 * Bind the items start-end from the list.
3675 *
3676 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003677 */
Winson Chung64359a52013-07-08 17:17:08 -07003678 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3679 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003680 Runnable r = new Runnable() {
3681 public void run() {
3682 bindItems(shortcuts, start, end, forceAnimateIcons);
3683 }
3684 };
3685 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003686 return;
3687 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003688
Winson Chungf0c6ae02012-03-21 16:10:31 -07003689 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003690 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3691 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003692 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003693 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003694 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003695 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003696 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003697
3698 // Short circuit if we are loading dock items for a configuration which has no dock
3699 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3700 mHotseat == null) {
3701 continue;
3702 }
3703
Joe Onorato9c1289c2009-08-17 11:03:03 -04003704 switch (item.itemType) {
3705 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3706 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003707 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003708 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003709
Winson Chung64359a52013-07-08 17:17:08 -07003710 /*
3711 * TODO: FIX collision case
3712 */
Winson Chungce376632013-07-11 16:12:41 -07003713 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3714 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3715 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3716 throw new RuntimeException("OCCUPIED");
3717 }
Winson Chung64359a52013-07-08 17:17:08 -07003718 }
3719
Adam Cohendcd297f2013-06-18 13:13:40 -07003720 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3721 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003722 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003723 // Animate all the applications up now
3724 shortcut.setAlpha(0f);
3725 shortcut.setScaleX(0f);
3726 shortcut.setScaleY(0f);
3727 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003728 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003729 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003731 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003732 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003733 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003734 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003735 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3736 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003737 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003738 default:
3739 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003740 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003741 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003742
Winson Chung997a9232013-07-24 15:33:46 -07003743 if (animateIcons) {
3744 // Animate to the correct page
3745 if (newShortcutsScreenId > -1) {
3746 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003747 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003748 final Runnable startBounceAnimRunnable = new Runnable() {
3749 public void run() {
3750 anim.playTogether(bounceAnims);
3751 anim.start();
3752 }
3753 };
Winson Chung997a9232013-07-24 15:33:46 -07003754 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003755 // We post the animation slightly delayed to prevent slowdowns
3756 // when we are loading right after we return to launcher.
3757 mWorkspace.postDelayed(new Runnable() {
3758 public void run() {
3759 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003760 mWorkspace.postDelayed(startBounceAnimRunnable,
3761 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003762 }
3763 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003764 } else {
3765 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003766 }
3767 }
Winson Chung64359a52013-07-08 17:17:08 -07003768 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003769 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003770 }
3771
Joe Onorato9c1289c2009-08-17 11:03:03 -04003772 /**
3773 * Implementation of the method from LauncherModel.Callbacks.
3774 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003775 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003776 Runnable r = new Runnable() {
3777 public void run() {
3778 bindFolders(folders);
3779 }
3780 };
3781 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003782 return;
3783 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003784 sFolders.clear();
3785 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003786 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003787
3788 /**
3789 * Add the views for a widget to the workspace.
3790 *
3791 * Implementation of the method from LauncherModel.Callbacks.
3792 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003793 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003794 Runnable r = new Runnable() {
3795 public void run() {
3796 bindAppWidget(item);
3797 }
3798 };
3799 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003800 return;
3801 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003802
Daniel Sandler843e8602010-06-07 14:59:01 -04003803 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3804 if (DEBUG_WIDGETS) {
3805 Log.d(TAG, "bindAppWidget: " + item);
3806 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003807 final Workspace workspace = mWorkspace;
3808
3809 final int appWidgetId = item.appWidgetId;
3810 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003811 if (DEBUG_WIDGETS) {
3812 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3813 }
3814
Joe Onorato9c1289c2009-08-17 11:03:03 -04003815 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3816
Joe Onorato9c1289c2009-08-17 11:03:03 -04003817 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003818 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003819
Adam Cohendcd297f2013-06-18 13:13:40 -07003820 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003821 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003822 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3823
Joe Onorato9c1289c2009-08-17 11:03:03 -04003824 workspace.requestLayout();
3825
Daniel Sandler843e8602010-06-07 14:59:01 -04003826 if (DEBUG_WIDGETS) {
3827 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3828 + (SystemClock.uptimeMillis()-start) + "ms");
3829 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003830 }
3831
Adam Cohen1462de32012-07-24 22:34:36 -07003832 public void onPageBoundSynchronously(int page) {
3833 mSynchronouslyBoundPages.add(page);
3834 }
3835
Joe Onorato9c1289c2009-08-17 11:03:03 -04003836 /**
3837 * Callback saying that there aren't any more items to bind.
3838 *
3839 * Implementation of the method from LauncherModel.Callbacks.
3840 */
Adam Cohene25af792013-06-06 23:08:25 -07003841 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003842 Runnable r = new Runnable() {
3843 public void run() {
3844 finishBindingItems(upgradePath);
3845 }
3846 };
3847 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003848 return;
3849 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003850 if (mSavedState != null) {
3851 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003852 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003853 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003854 mSavedState = null;
3855 }
3856
Adam Cohen1462de32012-07-24 22:34:36 -07003857 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003858
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003859 // If we received the result of any pending adds while the loader was running (e.g. the
3860 // widget configuration forced an orientation change), process them now.
3861 for (int i = 0; i < sPendingAddList.size(); i++) {
3862 completeAdd(sPendingAddList.get(i));
3863 }
3864 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003865
Winson Chungc51db6a2011-10-05 11:44:49 -07003866 // Update the market app icon as necessary (the other icons will be managed in response to
3867 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07003868 if (!DISABLE_MARKET_BUTTON) {
3869 updateAppMarketIcon();
3870 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07003871
Winson Chungf0c6ae02012-03-21 16:10:31 -07003872 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003873 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003874 mWorkspace.getUniqueComponents(true, null);
3875 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003876 }
Adam Cohen99894d92013-06-14 11:22:59 -07003877
Adam Cohen66a01fd2013-06-11 12:48:00 -07003878 mWorkspace.post(new Runnable() {
3879 @Override
3880 public void run() {
3881 onFinishBindingItems();
3882 }
3883 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003884 }
3885
Winson Chunga0b7e862013-09-05 16:03:15 -07003886 public boolean isAllAppsButtonRank(int rank) {
3887 if (mHotseat != null) {
3888 return mHotseat.isAllAppsButtonRank(rank);
3889 }
3890 return false;
3891 }
3892
Winson Chunga2413752012-04-03 14:22:34 -07003893 private boolean canRunNewAppsAnimation() {
3894 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3895 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3896 }
3897
Winson Chungc9168342013-06-26 14:54:55 -07003898 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3899 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3900 PropertyValuesHolder.ofFloat("alpha", 1f),
3901 PropertyValuesHolder.ofFloat("scaleX", 1f),
3902 PropertyValuesHolder.ofFloat("scaleY", 1f));
3903 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3904 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3905 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3906 return bounceAnim;
3907 }
3908
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003909 @Override
3910 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003911 boolean searchVisible = updateGlobalSearchIcon();
3912 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003913 if (mSearchDropTargetBar != null) {
3914 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3915 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003916 }
3917
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003918 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003919 * Add the icons for all apps.
3920 *
3921 * Implementation of the method from LauncherModel.Callbacks.
3922 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003923 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003924 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3925 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3926 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3927 getHotseat().addAllAppsFolder(mIconCache, apps,
3928 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3929 }
3930 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003931 }
Winson Chungc58497e2013-09-03 17:48:37 -07003932 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07003933 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07003934 mAppsCustomizeContent.setApps(apps);
3935 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003936 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003937 }
3938
3939 /**
3940 * A package was updated.
3941 *
3942 * Implementation of the method from LauncherModel.Callbacks.
3943 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003944 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003945 Runnable r = new Runnable() {
3946 public void run() {
3947 bindAppsUpdated(apps);
3948 }
3949 };
3950 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003951 return;
3952 }
3953
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003954 if (mWorkspace != null) {
3955 mWorkspace.updateShortcuts(apps);
3956 }
Winson Chungc58497e2013-09-03 17:48:37 -07003957
3958 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3959 mAppsCustomizeContent != null) {
3960 mAppsCustomizeContent.updateApps(apps);
3961 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003962 }
3963
3964 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003965 * A package was uninstalled. We take both the super set of packageNames
3966 * in addition to specific applications to remove, the reason being that
3967 * this can be called when a package is updated as well. In that scenario,
3968 * we only remove specific components from the workspace, where as
3969 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003970 *
3971 * Implementation of the method from LauncherModel.Callbacks.
3972 */
Winson Chung83892cc2013-05-01 16:53:33 -07003973 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07003974 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07003975 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003976 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07003977 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07003978 }
Winson Chungd64d1762013-08-20 14:37:16 -07003979 };
3980 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003981 return;
3982 }
3983
Winson Chungdf95eb12013-10-16 14:57:07 -07003984 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07003985 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07003986 }
3987 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07003988 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003989 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003990
Winson Chunga1820962011-10-03 16:31:06 -07003991 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07003992 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003993
Winson Chungdf95eb12013-10-16 14:57:07 -07003994 // Update AllApps
Winson Chungc58497e2013-09-03 17:48:37 -07003995 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3996 mAppsCustomizeContent != null) {
3997 mAppsCustomizeContent.removeApps(appInfos);
3998 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003999 }
Joe Onoratobe386092009-11-17 17:32:16 -08004000
4001 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004002 * A number of packages were updated.
4003 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004004 private ArrayList<Object> mWidgetsAndShortcuts;
4005 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004006 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004007 bindPackagesUpdated(mWidgetsAndShortcuts);
4008 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004009 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004010 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004011 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4012 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4013 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004014 return;
4015 }
4016
Winson Chung64359a52013-07-08 17:17:08 -07004017 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004018 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4019 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004020 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004021 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004022 }
4023
Winson Chung400438b2011-01-16 17:53:48 -08004024 private int mapConfigurationOriActivityInfoOri(int configOri) {
4025 final Display d = getWindowManager().getDefaultDisplay();
4026 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4027 switch (d.getRotation()) {
4028 case Surface.ROTATION_0:
4029 case Surface.ROTATION_180:
4030 // We are currently in the same basic orientation as the natural orientation
4031 naturalOri = configOri;
4032 break;
4033 case Surface.ROTATION_90:
4034 case Surface.ROTATION_270:
4035 // We are currently in the other basic orientation to the natural orientation
4036 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4037 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4038 break;
4039 }
4040
4041 int[] oriMap = {
4042 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4043 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4044 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4045 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4046 };
4047 // Since the map starts at portrait, we need to offset if this device's natural orientation
4048 // is landscape.
4049 int indexOffset = 0;
4050 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4051 indexOffset = 1;
4052 }
4053 return oriMap[(d.getRotation() + indexOffset) % 4];
4054 }
Adam Cohen446e9402011-09-15 18:21:21 -07004055
Winson Chung4b919f82012-05-01 10:44:08 -07004056 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004057 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004058 getResources().getBoolean(R.bool.allow_rotation);
4059 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004060 }
Winson Chung4b919f82012-05-01 10:44:08 -07004061 public void lockScreenOrientation() {
4062 if (isRotationEnabled()) {
4063 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4064 .getConfiguration().orientation));
4065 }
4066 }
4067 public void unlockScreenOrientation(boolean immediate) {
4068 if (isRotationEnabled()) {
4069 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004070 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004071 } else {
4072 mHandler.postDelayed(new Runnable() {
4073 public void run() {
4074 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4075 }
4076 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004077 }
Winson Chung4b919f82012-05-01 10:44:08 -07004078 }
Winson Chung400438b2011-01-16 17:53:48 -08004079 }
4080
Winson Chung82f55532011-08-09 14:14:23 -07004081 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004082 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004083 if (DISABLE_CLINGS) {
4084 return false;
4085 }
4086
Winson Chung3a6e7f32013-10-09 15:50:52 -07004087 // For now, limit only to phones
4088 LauncherAppState app = LauncherAppState.getInstance();
4089 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
4090 if (grid.isTablet()) {
4091 return false;
4092 }
4093
Brett Chabot2a9e2282011-08-23 15:03:13 -07004094 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004095 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004096
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -07004097 // Disable clings for accessibility when explore by touch is enabled
4098 final AccessibilityManager a11yManager = (AccessibilityManager) getSystemService(
4099 ACCESSIBILITY_SERVICE);
4100 if (a11yManager.isTouchExplorationEnabled()) {
4101 return false;
4102 }
4103
Michael Jurkae233a8b2013-03-19 13:49:20 +01004104 // Restricted secondary users (child mode) will potentially have very few apps
4105 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004106// boolean supportsLimitedUsers =
4107// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4108// Account[] accounts = AccountManager.get(this).getAccounts();
4109// if (supportsLimitedUsers && accounts.length == 0) {
4110// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4111// Bundle restrictions = um.getUserRestrictions();
4112// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4113// return false;
4114// }
4115// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004116 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004117 }
Michael Jurka22143132012-10-04 17:42:38 +02004118
Winson Chungfa545132013-09-26 17:45:32 -07004119 private Cling initCling(int clingId, int scrimId, boolean animate,
Winson Chungaf40f202013-09-18 18:26:31 -07004120 boolean dimNavBarVisibilty) {
Winson Chungfa545132013-09-26 17:45:32 -07004121 Cling cling = (Cling) findViewById(clingId);
4122 View scrim = null;
4123 if (scrimId > 0) {
Sandeep Siddharthaa6125ef2013-09-26 20:30:40 -07004124 scrim = findViewById(R.id.cling_scrim);
Winson Chungfa545132013-09-26 17:45:32 -07004125 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004126 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -07004127 cling.init(this, scrim);
Winson Chungaf40f202013-09-18 18:26:31 -07004128 cling.show(animate, SHOW_CLING_DURATION);
4129
4130 if (dimNavBarVisibilty) {
4131 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
4132 View.SYSTEM_UI_FLAG_LOW_PROFILE);
Winson Chung7d7541e2011-09-16 20:14:36 -07004133 }
4134 }
4135 return cling;
4136 }
Michael Jurka22143132012-10-04 17:42:38 +02004137
Winson Chungaf40f202013-09-18 18:26:31 -07004138 private void dismissCling(final Cling cling, final Runnable postAnimationCb,
4139 final String flag, int duration, boolean restoreNavBarVisibilty) {
Winson Chung7819abd2012-11-29 14:29:38 -08004140 // To catch cases where siblings of top-level views are made invisible, just check whether
4141 // the cling is directly set to GONE before dismissing it.
4142 if (cling != null && cling.getVisibility() != View.GONE) {
Winson Chungaf40f202013-09-18 18:26:31 -07004143 final Runnable cleanUpClingCb = new Runnable() {
4144 public void run() {
Winson Chung82f55532011-08-09 14:14:23 -07004145 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004146 // We should update the shared preferences on a background thread
4147 new Thread("dismissClingThread") {
4148 public void run() {
4149 SharedPreferences.Editor editor = mSharedPrefs.edit();
4150 editor.putBoolean(flag, true);
4151 editor.commit();
4152 }
4153 }.start();
Winson Chungaf40f202013-09-18 18:26:31 -07004154 if (postAnimationCb != null) {
4155 postAnimationCb.run();
4156 }
4157 }
4158 };
4159 if (duration <= 0) {
4160 cleanUpClingCb.run();
4161 } else {
4162 cling.hide(duration, cleanUpClingCb);
4163 }
Michael Jurka22143132012-10-04 17:42:38 +02004164 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chungaf40f202013-09-18 18:26:31 -07004165
4166 if (restoreNavBarVisibilty) {
4167 cling.setSystemUiVisibility(cling.getSystemUiVisibility() &
4168 ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
4169 }
Winson Chung82f55532011-08-09 14:14:23 -07004170 }
4171 }
Michael Jurka22143132012-10-04 17:42:38 +02004172
Winson Chung9d9d74f2011-09-19 11:49:12 -07004173 private void removeCling(int id) {
4174 final View cling = findViewById(id);
4175 if (cling != null) {
4176 final ViewGroup parent = (ViewGroup) cling.getParent();
4177 parent.post(new Runnable() {
4178 @Override
4179 public void run() {
4180 parent.removeView(cling);
4181 }
4182 });
Michael Jurka22143132012-10-04 17:42:38 +02004183 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004184 }
4185 }
Michael Jurka974c3862012-05-22 22:00:31 -07004186
4187 private boolean skipCustomClingIfNoAccounts() {
4188 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4189 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4190 if (customCling) {
4191 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004192 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004193 Account[] accounts = am.getAccountsByType("com.google");
4194 return accounts.length == 0;
4195 }
4196 return false;
4197 }
4198
Winson Chung82e5c982013-10-09 12:04:50 -07004199 public void updateCustomContentHintVisibility() {
4200 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4201 String ccHintStr = getFirstRunCustomContentHint();
4202
4203 if (mWorkspace.hasCustomContent()) {
4204 // Show the custom content hint if ccHintStr is not empty
4205 if (cling != null) {
4206 setCustomContentHintVisibility(cling, ccHintStr, true, true);
4207 }
4208 } else {
4209 // Hide the custom content hint
4210 if (cling != null) {
4211 setCustomContentHintVisibility(cling, ccHintStr, false, true);
4212 }
4213 }
4214 }
4215
4216 private void setCustomContentHintVisibility(Cling cling, String ccHintStr, boolean visible,
4217 boolean animate) {
4218 final TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
4219 if (ccHint != null) {
4220 if (visible && !ccHintStr.isEmpty()) {
4221 ccHint.setText(ccHintStr);
4222 ccHint.setVisibility(View.VISIBLE);
4223 if (animate) {
4224 ccHint.setAlpha(0f);
4225 ccHint.animate().alpha(1f)
4226 .setDuration(SHOW_CLING_DURATION)
4227 .start();
4228 } else {
4229 ccHint.setAlpha(1f);
4230 }
4231 } else {
4232 if (animate) {
4233 ccHint.animate().alpha(0f)
4234 .setDuration(SHOW_CLING_DURATION)
4235 .setListener(new AnimatorListenerAdapter() {
4236 @Override
4237 public void onAnimationEnd(Animator animation) {
4238 ccHint.setVisibility(View.GONE);
4239 }
4240 })
4241 .start();
4242 } else {
4243 ccHint.setAlpha(0f);
4244 ccHint.setVisibility(View.GONE);
4245 }
4246 }
4247 }
4248 }
4249
Winson Chungaf40f202013-09-18 18:26:31 -07004250 public void showFirstRunCling() {
Winson Chung46353de2012-02-16 14:05:10 -08004251 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004252 !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false) &&
Michael Jurka974c3862012-05-22 22:00:31 -07004253 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004254 // If we're not using the default workspace layout, replace workspace cling
4255 // with a custom workspace cling (usually specified in an overlay)
4256 // For now, only do this on tablets
Winson Chunge6eabff2013-09-24 11:01:23 -07004257 if (!DISABLE_CUSTOM_CLINGS) {
4258 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
4259 getResources().getBoolean(R.bool.config_useCustomClings)) {
4260 // Use a custom cling
4261 View cling = findViewById(R.id.workspace_cling);
4262 ViewGroup clingParent = (ViewGroup) cling.getParent();
4263 int clingIndex = clingParent.indexOfChild(cling);
4264 clingParent.removeViewAt(clingIndex);
4265 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4266 clingParent.addView(customCling, clingIndex);
4267 customCling.setId(R.id.workspace_cling);
4268 }
4269 }
4270 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4271 if (cling != null) {
4272 String sbHintStr = getFirstRunClingSearchBarHint();
4273 String ccHintStr = getFirstRunCustomContentHint();
4274 if (!sbHintStr.isEmpty()) {
4275 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
4276 sbHint.setText(sbHintStr);
4277 sbHint.setVisibility(View.VISIBLE);
4278 }
Winson Chung82e5c982013-10-09 12:04:50 -07004279 setCustomContentHintVisibility(cling, ccHintStr, true, false);
Michael Jurka45355c42012-10-08 13:21:35 +02004280 }
Winson Chungfa545132013-09-26 17:45:32 -07004281 initCling(R.id.first_run_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004282 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004283 removeCling(R.id.first_run_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004284 }
4285 }
Winson Chungaf40f202013-09-18 18:26:31 -07004286
Winson Chunge6eabff2013-09-24 11:01:23 -07004287 protected String getFirstRunClingSearchBarHint() {
4288 return "";
4289 }
4290 protected String getFirstRunCustomContentHint() {
4291 return "";
4292 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004293 protected int getFirstRunFocusedHotseatAppDrawableId() {
4294 return -1;
4295 }
4296 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4297 return null;
4298 }
4299 protected int getFirstRunFocusedHotseatAppRank() {
4300 return -1;
4301 }
4302 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4303 return "";
4304 }
4305 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4306 return "";
4307 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004308
Winson Chungaf40f202013-09-18 18:26:31 -07004309 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004310 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004311 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004312 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung3a6e7f32013-10-09 15:50:52 -07004313 Cling c = initCling(R.id.workspace_cling, 0, false, true);
4314
4315 // Set the focused hotseat app if there is one
4316 c.setFocusedHotseatApp(getFirstRunFocusedHotseatAppDrawableId(),
4317 getFirstRunFocusedHotseatAppRank(),
4318 getFirstRunFocusedHotseatAppComponentName(),
4319 getFirstRunFocusedHotseatAppBubbleTitle(),
4320 getFirstRunFocusedHotseatAppBubbleDescription());
Winson Chung9d9d74f2011-09-19 11:49:12 -07004321 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004322 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004323 }
4324 }
4325 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004326 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004327 if (isClingsEnabled() &&
4328 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004329 Cling cling = initCling(R.id.folder_cling, R.id.cling_scrim,
4330 true, true);
Winson Chungaf40f202013-09-18 18:26:31 -07004331 return cling;
Winson Chung9d9d74f2011-09-19 11:49:12 -07004332 } else {
4333 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004334 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004335 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004336 }
Michael Jurka104c4562013-07-08 18:03:46 -07004337 protected SharedPreferences getSharedPrefs() {
4338 return mSharedPrefs;
4339 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004340 public boolean isFolderClingVisible() {
4341 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004342 if (cling != null) {
4343 return cling.getVisibility() == View.VISIBLE;
4344 }
4345 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004346 }
Winson Chungaf40f202013-09-18 18:26:31 -07004347 public void dismissFirstRunCling(View v) {
4348 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4349 Runnable cb = new Runnable() {
4350 public void run() {
4351 // Show the workspace cling next
4352 showFirstRunWorkspaceCling();
4353 }
4354 };
4355 dismissCling(cling, cb, Cling.FIRST_RUN_CLING_DISMISSED_KEY,
4356 DISMISS_CLING_DURATION, false);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004357
4358 // Fade out the search bar for the workspace cling coming up
4359 mSearchDropTargetBar.hideSearchBar(true);
Winson Chungaf40f202013-09-18 18:26:31 -07004360 }
Winson Chung82f55532011-08-09 14:14:23 -07004361 public void dismissWorkspaceCling(View v) {
4362 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004363 Runnable cb = null;
4364 if (v == null) {
4365 cb = new Runnable() {
4366 public void run() {
4367 mWorkspace.enterOverviewMode();
4368 }
4369 };
4370 }
4371 dismissCling(cling, cb, Cling.WORKSPACE_CLING_DISMISSED_KEY,
4372 DISMISS_CLING_DURATION, true);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004373
4374 // Fade in the search bar
4375 mSearchDropTargetBar.showSearchBar(true);
Winson Chung7d7541e2011-09-16 20:14:36 -07004376 }
4377 public void dismissFolderCling(View v) {
4378 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004379 dismissCling(cling, null, Cling.FOLDER_CLING_DISMISSED_KEY,
4380 DISMISS_CLING_DURATION, true);
Winson Chung82f55532011-08-09 14:14:23 -07004381 }
4382
Winson Chung80baf5a2010-08-09 16:03:15 -07004383 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004384 * Prints out out state for debugging.
4385 */
4386 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004387 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004388 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004389 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4390 Log.d(TAG, "mRestoring=" + mRestoring);
4391 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4392 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004393 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004394 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004395
Winson Chung785d2eb2011-04-14 16:08:02 -07004396 if (mAppsCustomizeContent != null) {
4397 mAppsCustomizeContent.dumpState();
4398 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004399 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004400 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004401
4402 @Override
4403 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4404 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004405 synchronized (sDumpLogs) {
4406 writer.println(" ");
4407 writer.println("Debug logs: ");
4408 for (int i = 0; i < sDumpLogs.size(); i++) {
4409 writer.println(" " + sDumpLogs.get(i));
4410 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004411 }
4412 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004413
4414 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004415 if (DEBUG_DUMP_LOG) {
4416 synchronized (sDumpLogs) {
4417 Log.d(TAG, "");
4418 Log.d(TAG, "*********************");
4419 Log.d(TAG, "Launcher debug logs: ");
4420 for (int i = 0; i < sDumpLogs.size(); i++) {
4421 Log.d(TAG, " " + sDumpLogs.get(i));
4422 }
4423 Log.d(TAG, "*********************");
4424 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004425 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004426 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004427 }
4428
4429 public static void addDumpLog(String tag, String log, boolean debugLog) {
4430 if (debugLog) {
4431 Log.d(tag, log);
4432 }
Winson Chungede41292013-09-19 16:27:36 -07004433 if (DEBUG_DUMP_LOG) {
4434 sDateStamp.setTime(System.currentTimeMillis());
4435 synchronized (sDumpLogs) {
4436 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Adam Cohen4caf2982013-08-20 18:54:31 -07004437 }
Winson Chungede41292013-09-19 16:27:36 -07004438 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004439 }
4440
Winson Chungede41292013-09-19 16:27:36 -07004441 public void dumpLogsToLocalData() {
4442 if (DEBUG_DUMP_LOG) {
4443 new Thread("DumpLogsToLocalData") {
4444 @Override
4445 public void run() {
4446 boolean success = false;
4447 sDateStamp.setTime(sRunStart);
4448 String FILENAME = sDateStamp.getMonth() + "-"
4449 + sDateStamp.getDay() + "_"
4450 + sDateStamp.getHours() + "-"
4451 + sDateStamp.getMinutes() + "_"
4452 + sDateStamp.getSeconds() + ".txt";
4453
4454 FileOutputStream fos = null;
4455 File outFile = null;
4456 try {
4457 outFile = new File(getFilesDir(), FILENAME);
4458 outFile.createNewFile();
4459 fos = new FileOutputStream(outFile);
4460 } catch (Exception e) {
4461 e.printStackTrace();
4462 }
4463 if (fos != null) {
4464 PrintWriter writer = new PrintWriter(fos);
4465
4466 writer.println(" ");
4467 writer.println("Debug logs: ");
4468 synchronized (sDumpLogs) {
4469 for (int i = 0; i < sDumpLogs.size(); i++) {
4470 writer.println(" " + sDumpLogs.get(i));
4471 }
4472 }
4473 writer.close();
4474 }
4475 try {
4476 if (fos != null) {
4477 fos.close();
4478 success = true;
4479 }
4480 } catch (IOException e) {
4481 e.printStackTrace();
4482 }
4483 }
4484 }.start();
Adam Cohen4caf2982013-08-20 18:54:31 -07004485 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004486 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004487}
Michael Jurka2763be32011-02-24 11:19:57 -08004488
Michael Jurkaabded662011-03-04 12:06:57 -08004489interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004490 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004491 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004492 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004493 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004494 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004495}